summaryrefslogtreecommitdiff
path: root/Gfx.cpp
blob: 49b8a9a277f0f2d4ff96153d34863601c0c8b6d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* *************************************
 * 	Includes
 * *************************************/

#include "Gfx.h"

/* *************************************
 * 	Defines
 * *************************************/

/* *************************************
 * 	Structs and enums
 * *************************************/

/* *************************************
 * 	Local variables
 * *************************************/

static bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h);

void GfxInit(void)
{
	gb.display.persistence = false;	// Clears screen automatically
	gb.display.setFont(font3x5);
}

void GfxDrawSprite(TYPE_SPRITE * ptrSprite)
{
	if (GfxIsSpriteInsideScreenArea(ptrSprite) == true)
	{
		int8_t orig_color = gb.display.getColor();

		gb.display.setColor(ptrSprite->color, GFX_WHITE);
		gb.display.drawBitmap(	ptrSprite->x,
								ptrSprite->y,
								ptrSprite->Data,
								ptrSprite->rotation,
								ptrSprite->flip		);

		gb.display.setColor(orig_color);
	}
}

bool GfxRefreshNeeded(void)
{
	return gb.update();
}

void GfxShowKeyboard(char* str, uint8_t length)
{
	gb.keyboard(str, length);
}

void GfxClearScreen(void)
{
	//gb.display.fillScreen(GFX_WHITE);
	gb.display.clear();
}

bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h)
{
	/*char strBuffer[16];

	snprintf(strBuffer, 16, "%d", (int)(x + w));
	GfxPrintText(strBuffer,48,8);

	snprintf(strBuffer, 16, "x = %d", (int)(x));
	GfxPrintText(strBuffer,48,16);

	snprintf(strBuffer, 16, "w = %d", (int)(w));
	GfxPrintText(strBuffer,48,24);*/

	if ( ( (x + w) >= 0)
			&&
		(x < X_SCREEN_RESOLUTION)
			&&
		( (y + h) >= 0)
			&&
		(y < Y_SCREEN_RESOLUTION)	)
	{
		return true;
	}

	return false;
}

bool GfxIsSpriteInsideScreenArea(TYPE_SPRITE * spr)
{
	return GfxIsInsideScreenArea(spr->x, spr->y, spr->w, spr->h);
}

uint8_t GfxGetWidthFromSpriteData(const uint8_t* sprData)
{
	// On Gamebuino bitmaps, width is always stored on first byte.
	return pgm_read_byte_near(&sprData[0]);
}

uint8_t GfxGetHeightFromSpriteData(const uint8_t* sprData)
{
	// On Gamebuino bitmaps, height is always stored on second byte.
	return pgm_read_byte_near(&sprData[1]);
}

void GfxPrintText_Flash(const __FlashStringHelper * str)
{
	gb.popup(str, 20 * 3 /* 3 seconds */);
}

void GfxPrintTextFont(const char* str, const uint8_t* font, uint8_t x, uint8_t y)
{
	uint8_t* orig_font = gb.display.getFont();

	gb.display.cursorX = x;
	gb.display.cursorY = y;

	gb.display.setFont(font);

	gb.display.setColor(GFX_BLACK, GFX_WHITE);

	gb.display.print(str);

	if (orig_font != NULL)
	{
		gb.display.setFont(orig_font);
	}
}

void GfxRenderTiles(TYPE_CAMERA* ptrCamera)
{
	gb.display.setColor(GFX_GRAY);

	if (ptrCamera == NULL)
	{
		return;
	}

	for (int i = 0; i < Y_SCREEN_RESOLUTION; i+=8)
	{
		for (int j = 0; j < X_SCREEN_RESOLUTION; j++)
		{
			//if (j & 1)
			//{
				int x = j + ptrCamera->X_Offset;
				int y = i + ptrCamera->Y_Offset;

				if ((x >= 0) && (y >= 0))
				{
					gb.display.drawPixel(j + ptrCamera->X_Offset, i + ptrCamera->Y_Offset);
				}
			//}
		}
	}

	for (int i = 0; i < X_SCREEN_RESOLUTION; i+=8)
	{
		for (int j = 0; j < Y_SCREEN_RESOLUTION; j++)
		{
			//if (j & 1)
			//{
				int x = j + ptrCamera->X_Offset;
				int y = i + ptrCamera->Y_Offset;

				if ((x >= 0) && (y >= 0))
				{
					gb.display.drawPixel(i + ptrCamera->X_Offset, j + ptrCamera->Y_Offset);
				}
			//}
		}
	}
}

void GfxPrintText(const char* str, uint8_t x, uint8_t y)
{
	GfxPrintTextFont(str, font3x5, x, y);
}

void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color)
{
	if (GfxIsInsideScreenArea(x, y, radius, radius) == true)
	{
		int8_t orig_color = gb.display.getColor();
		gb.display.setColor(color);
		gb.display.drawCircle(x, y, radius);
		gb.display.setColor(orig_color);
	}
}

void GfxDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color)
{
	if (GfxIsInsideScreenArea(x, y, w, h) == true)
	{
		int8_t orig_color = gb.display.getColor();

		gb.display.setColor(color);
		gb.display.drawRect(x, y, w, h);
		gb.display.setColor(orig_color);
	}
}

void GfxDrawLine(uint8_t x0, uint8_t x1, uint8_t y0, uint8_t y1, uint8_t color)
{
    gb.display.setColor(color);
    gb.display.drawLine(x0, y0, x1, y1);
}

void GfxFillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color)
{
	if (GfxIsInsideScreenArea(x, y, w, h) == true)
	{
		int8_t orig_color = gb.display.getColor();

		gb.display.setColor(color);
		gb.display.fillRect(x, y, w, h);
		gb.display.setColor(orig_color);
	}
}

void GfxShowResources(TYPE_RESOURCES* ptrResources)
{
	char str[8];

	gb.display.setColor(GFX_WHITE);
	gb.display.fillRect(0, 0, X_SCREEN_RESOLUTION, 5);

	snprintf(str, 8, "W=%d", ptrResources->Wood);

	GfxPrintTextFont(str, font3x3, 4, 1);

	snprintf(str, 8, "G=%d", ptrResources->Gold);

	GfxPrintTextFont(str, font3x3, 24, 1);

	snprintf(str, 8, "F=%d", ptrResources->Food);

	GfxPrintTextFont(str, font3x3, 48, 1);
}