summaryrefslogtreecommitdiff
path: root/Gfx.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-03-07 20:57:09 +0100
committerXaviDCR92 <xavi.dcr@gmail.com>2017-03-07 20:57:09 +0100
commit8ec41b4410aba535008daf991ea59a8740951d44 (patch)
tree01ee0846f579d9d139ee46a6a43f67ba522c7196 /Gfx.cpp
downloadpocketempires-8ec41b4410aba535008daf991ea59a8740951d44.tar.gz
+ Initial commit. Added source, sprites and final executable.
Diffstat (limited to 'Gfx.cpp')
-rw-r--r--Gfx.cpp191
1 files changed, 191 insertions, 0 deletions
diff --git a/Gfx.cpp b/Gfx.cpp
new file mode 100644
index 0000000..469ddd7
--- /dev/null
+++ b/Gfx.cpp
@@ -0,0 +1,191 @@
+/* *************************************
+ * 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)
+ {
+ gb.display.setColor(ptrSprite->color, GFX_WHITE);
+ gb.display.drawBitmap( ptrSprite->x,
+ ptrSprite->y,
+ ptrSprite->Data,
+ ptrSprite->rotation,
+ ptrSprite->flip );
+ }
+}
+
+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]) << 1;
+}
+
+uint8_t GfxGetHeightFromSpriteData(const uint8_t * sprData)
+{
+ // On Gamebuino bitmaps, height is always stored on second byte.
+ return pgm_read_byte_near(sprData[1]) << 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 GfxShowResources(TYPE_RESOURCES * ptrResources)
+{
+ char str[8];
+
+ gb.display.setColor(GFX_WHITE);
+ gb.display.fillRect(0, 0, X_SCREEN_RESOLUTION, 8);
+
+ snprintf(str, 8, "W=%d", ptrResources->Wood);
+
+ GfxPrintTextFont(str, font3x3, 4, 4);
+
+ snprintf(str, 8, "G=%d", ptrResources->Gold);
+
+ GfxPrintTextFont(str, font3x3, 24, 4);
+
+ snprintf(str, 8, "F=%d", ptrResources->Food);
+
+ GfxPrintTextFont(str, font3x3, 48, 4);
+}