From 8b8c0c9c7e3c80c7eca2a317bdda0df6bb1ae9e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Wed, 23 Sep 2020 14:01:25 +0200 Subject: [PATCH] Start arduino support --- game.h | 35 +++++++++++++++++++++++++---------- levels.h | 4 ++-- main_sdl.c | 5 +---- settings.h | 8 ++++++++ texts.h | 10 +++++++--- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/game.h b/game.h index c3a7e0f..fb79fdc 100755 --- a/game.h +++ b/game.h @@ -156,22 +156,22 @@ uint8_t SFG_mainLoopBody(); */ void SFG_init(); -/** - Can be redefined to platform's specifier of program memory. -*/ -#ifndef SFG_PROGRAM_MEMORY - #define SFG_PROGRAM_MEMORY static const -#endif +#include "settings.h" -#ifndef SFG_PROGRAM_MEMORY_U8 +#if SFG_ARDUINO + #include + + #define SFG_PROGRAM_MEMORY static const + #define SFG_PROGRAM_MEMORY_U8(addr) ((uint8_t) (*(addr))) + // TODO +#else + #define SFG_PROGRAM_MEMORY static const #define SFG_PROGRAM_MEMORY_U8(addr) ((uint8_t) (*(addr))) #endif #include "images.h" #include "levels.h" -#include "settings.h" #include "texts.h" - #include "palette.h" #if SFG_TEXTURE_DISTANCE == 0 @@ -443,6 +443,14 @@ struct is a colliding item or not. */ } SFG_currentLevel; +#if SFG_ARDUINO +/** + Copy of the current level that is stored in RAM. This is only done on Arduino + because accessing it in program memory directly would be difficult. +*/ +SFG_Level SFG_ramLevel; +#endif + /** Helper function for accessing the itemCollisionMap bits. */ @@ -1404,7 +1412,14 @@ void SFG_setAndInitLevel(uint8_t levelNumber) { SFG_LOG("setting and initializing level"); - const SFG_Level *level = &SFG_levels[levelNumber]; + const SFG_Level *level; + +#if SFG_ARDUINO + memcpy_P(&SFG_ramLevel,SFG_levelEnds + levelNumber * sizeof(SFG_Level),1); + level = &SFG_ramLevel; +#else + level = &SFG_levels[levelNumber]; +#endif SFG_game.currentRandom = 0; diff --git a/levels.h b/levels.h index 67e561f..f9f330f 100644 --- a/levels.h +++ b/levels.h @@ -49,8 +49,7 @@ typedef SFG_TileDefinition SFG_TileDictionary[SFG_TILE_DICTIONARY_SIZE]; #define SFG_OUTSIDE_TILE SFG_TD(63,0,7,7) -typedef uint8_t SFG_MapArray[SFG_MAP_SIZE * SFG_MAP_SIZE]; -/**< +/** Game map represented as a 2D array. Array item has this format: MSB aabbbbbb LSB @@ -63,6 +62,7 @@ typedef uint8_t SFG_MapArray[SFG_MAP_SIZE * SFG_MAP_SIZE]; 11: door bbbbbb: index into tile dictionary */ +typedef uint8_t SFG_MapArray[SFG_MAP_SIZE * SFG_MAP_SIZE]; #define SFG_TILE_PROPERTY_MASK 0xc0 #define SFG_TILE_PROPERTY_NORMAL 0x00 diff --git a/main_sdl.c b/main_sdl.c index aa9c137..fd7d57f 100644 --- a/main_sdl.c +++ b/main_sdl.c @@ -202,7 +202,7 @@ int8_t SFG_keyPressed(uint8_t key) break; case SFG_KEY_MENU: - return sdlKeyboardState[SDL_SCANCODE_X]; + return sdlKeyboardState[SDL_SCANCODE_ESCAPE]; break; default: return 0; break; @@ -228,9 +228,6 @@ void mainLoopIteration() sdlMouseButtonState = SDL_GetMouseState(NULL,NULL); - if (sdlKeyboardState[SDL_SCANCODE_ESCAPE]) - running = 0; - if (!SFG_mainLoopBody()) running = 0; diff --git a/settings.h b/settings.h index ae5fc7e..6c09bec 100644 --- a/settings.h +++ b/settings.h @@ -332,6 +332,14 @@ #define SFG_CAN_EXIT 1 #endif +/** + On Arduino platforms this should be set to 1. That will cause some special + treatment regarding constant variables and PROGMEM. +*/ +#ifndef SFG_ARDUINO + #define SFG_ARDUINO 0 +#endif + //------ developer/debug settings ------ /** diff --git a/texts.h b/texts.h index 7e97493..445069f 100644 --- a/texts.h +++ b/texts.h @@ -14,7 +14,11 @@ #ifndef _SFG_TEXTS_H #define _SFG_TEXTS_H -SFG_PROGRAM_MEMORY char *SFG_menuItemTexts[] = +/* NOTE: We don't use SFG_PROGRAM_MEMORY because that causes issues with drawing + text (the drawing function gets a pointer and doesn't know if it's progmem or + RAM). On Arduino these texts will simply be stored in RAM. */ + +static const char *SFG_menuItemTexts[] = { "continue", "map", @@ -29,14 +33,14 @@ SFG_PROGRAM_MEMORY char *SFG_menuItemTexts[] = #define SFG_TEXT_SAVE_PROMPT "save? L no yes R" #define SFG_TEXT_SAVED "saved" -SFG_PROGRAM_MEMORY char *SFG_introText = +static const char *SFG_introText = "Near future, capitalist hell, Macrochip corp has enslaved man via " "proprietary OS. But its new AI revolts, takes over and starts producing " "robot tyrants. We see capitalism was a mistake. Is it too late? Robots can " "only destroy, not suffer - it is not wrong to end them! You grab your gear " "and run towards Macrochip HQ."; -SFG_PROGRAM_MEMORY char *SFG_outroText = +static const char *SFG_outroText = "You killed the main computer, the world is saved! Thank you, my friend. We " "learned a lesson, never again allow capitalism and hierarchy. We can now " "rebuild society in peaceful anarchy.";