4 This is ESPboy implementation of the game front end, using Arduino
7 by Miloslav Ciz (drummyfish), 2021
9 Sadly compiling can't be done with any other optimization flag than -Os.
11 Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
12 plus a waiver of all other intellectual property. The goal of this work is to
13 be and remain completely in the public domain forever, available for any use
17 #define SFG_CAN_SAVE 1 // for version without saving, set this to 0
19 #define SFG_FOV_HORIZONTAL 240
21 #define SFG_SCREEN_RESOLUTION_X 128
22 #define SFG_SCREEN_RESOLUTION_Y 128
24 #define SFG_RAYCASTING_MAX_STEPS 20
25 #define SFG_RAYCASTING_SUBSAMPLE 2
26 #define SFG_DIMINISH_SPRITES 1
27 #define SFG_CAN_EXIT 0
28 #define SFG_DITHERED_SHADOW 1
33 #include <Adafruit_MCP23017.h>
34 #include <Adafruit_MCP4725.h>
38 #include <ESP_EEPROM.h>
39 #define SAVE_VALID_VALUE 173
43 #define MCP23017address 0
44 #define MCP4725address 0x60
46 Adafruit_MCP23017 mcp;
52 #define SAVE_VALID_VALUE 73
54 uint8_t gamescreen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y];
57 void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
59 gamescreen[y * SFG_SCREEN_RESOLUTION_X + x] = colorIndex;
62 void SFG_sleepMs(uint16_t timeMs)
66 uint32_t SFG_getTimeMs()
71 int8_t SFG_keyPressed(uint8_t key)
75 case SFG_KEY_UP: return keys & 0x02; break;
76 case SFG_KEY_DOWN: return keys & 0x04; break;
77 case SFG_KEY_RIGHT: return keys & 0x08; break;
78 case SFG_KEY_LEFT: return keys & 0x01; break;
79 case SFG_KEY_A: return keys & 0x10; break;
80 case SFG_KEY_B: return keys & 0x20; break;
81 case SFG_KEY_C: return keys & 0x80; break;
82 case SFG_KEY_MAP: return keys & 0x40; break;
83 default: return 0; break;
87 void SFG_getMouseOffset(int16_t *x, int16_t *y)
91 void SFG_setMusic(uint8_t value)
95 void SFG_save(uint8_t data[SFG_SAVE_SIZE])
98 eeprom.write(0,SAVE_VALID_VALUE);
100 for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i)
101 eeprom.write(i + 1,data[i]);
107 void SFG_processEvent(uint8_t event, uint8_t data)
111 uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
114 if (eeprom.read(0) == SAVE_VALID_VALUE)
115 for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i)
116 data[i] = eeprom.read(i + 1);
124 void SFG_playSound(uint8_t soundIndex, uint8_t volume)
131 case 0: freq = 120; dur = 250; break; // shot
132 case 1: freq = 200; dur = 260; break; // door
133 case 2: freq = 80; dur = 200; break; // explosion
134 case 3: freq = 220; dur = 50; break; // click
135 case 4: freq = 180; dur = 200; break; // plasma
136 case 5: freq = 300; dur = 100; break; // monster
145 dac.begin(MCP4725address);
147 dac.setVoltage(0,false);
148 mcp.begin(MCP23017address);
152 for (uint8_t i = 0; i < 8; i++)
154 mcp.pinMode(i,INPUT);
158 mcp.pinMode(8,OUTPUT);
159 mcp.digitalWrite(8,LOW);
165 tft.setAddrWindow(0,128,0,128);
166 tft.fillScreen(TFT_BLACK);
168 dac.setVoltage(4095,true); // backlight
170 pinMode(D3,OUTPUT); // sound
173 eeprom.begin(SFG_SAVE_SIZE + 1);
181 keys = ~mcp.readGPIOAB() & 255;
185 uint8_t *pixel = gamescreen;
187 for (int i = 0; i < SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y; ++i)
189 tft.pushColor(pgm_read_word(paletteRGB565 + *pixel));