2020-09-23 09:52:30 -04:00
|
|
|
/**
|
|
|
|
@file main_gbmeta.ino
|
|
|
|
|
|
|
|
This is Gamebuino Meta implementation of the game front end, using the
|
2020-10-30 14:13:25 -04:00
|
|
|
official library. Leaving out the library bloat could probably optimize this.
|
|
|
|
|
|
|
|
To compile using Arduin IDE you need to copy this file as well as all
|
|
|
|
necessary .h files into a project folder, then open the project and compile.
|
|
|
|
Do NOT put .c and .cpp files into the folder, stupid Arduino tries to compile
|
|
|
|
them even if they're not needed.
|
|
|
|
|
|
|
|
DON'T FORGET to set compiler flag to -O3 (default is -Os). With Arduino IDE
|
|
|
|
this is done in platform.txt file.
|
2020-09-23 09:52:30 -04:00
|
|
|
|
|
|
|
by Miloslav Ciz (drummyfish), 2019
|
|
|
|
|
|
|
|
Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
|
2020-11-07 12:58:40 -05:00
|
|
|
plus a waiver of all other intellectual property. The goal of this work is to
|
2020-09-23 09:52:30 -04:00
|
|
|
be and remain completely in the public domain forever, available for any use
|
|
|
|
whatsoever.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Gamebuino-Meta.h>
|
|
|
|
|
|
|
|
#define SFG_ARDUINO 1
|
|
|
|
#define SFG_CAN_EXIT 0
|
2020-11-15 15:15:37 -05:00
|
|
|
#define SFG_FPS 22
|
2020-11-20 06:48:09 -05:00
|
|
|
#define SFG_TIME_MULTIPLIER 900 /* Without this the game seems too fast. This
|
2020-11-15 15:15:37 -05:00
|
|
|
also achieves an effective FPS of about
|
|
|
|
17. */
|
2020-10-24 13:21:46 -04:00
|
|
|
#define SFG_SCREEN_RESOLUTION_X 80
|
2020-09-23 09:52:30 -04:00
|
|
|
#define SFG_SCREEN_RESOLUTION_Y 64
|
|
|
|
#define SFG_RESOLUTION_SCALEDOWN 1
|
2020-10-03 12:53:18 -04:00
|
|
|
#define SFG_RAYCASTING_MAX_STEPS 11
|
|
|
|
#define SFG_RAYCASTING_MAX_HITS 3
|
2020-10-30 14:13:25 -04:00
|
|
|
#define SFG_RAYCASTING_SUBSAMPLE 2
|
2020-09-26 14:24:40 -04:00
|
|
|
#define SFG_DIMINISH_SPRITES 0
|
2020-09-23 09:52:30 -04:00
|
|
|
#define SFG_DITHERED_SHADOW 0
|
2020-10-30 14:13:25 -04:00
|
|
|
#define SFG_PLAYER_TURN_SPEED 135
|
2020-09-23 09:52:30 -04:00
|
|
|
|
|
|
|
#include "game.h"
|
|
|
|
|
|
|
|
Gamebuino_Meta::Color palette[256];
|
|
|
|
|
2020-10-03 12:53:18 -04:00
|
|
|
uint8_t blinkFramesLeft;
|
|
|
|
|
|
|
|
void blinkLED(Gamebuino_Meta::Color color)
|
|
|
|
{
|
|
|
|
gb.lights.fill(color);
|
|
|
|
blinkFramesLeft = 5;
|
|
|
|
}
|
|
|
|
|
2020-09-24 11:41:52 -04:00
|
|
|
const Gamebuino_Meta::SaveDefault saveDefault[] =
|
|
|
|
{ { 0, SAVETYPE_BLOB, SFG_SAVE_SIZE, 0 } };
|
|
|
|
|
2020-09-23 09:52:30 -04:00
|
|
|
void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
|
|
|
|
{
|
|
|
|
Gamebuino_Meta::Color c = palette[colorIndex];
|
|
|
|
gb.display.drawPixel(x,y,c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SFG_sleepMs(uint16_t timeMs)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t SFG_keyPressed(uint8_t key)
|
|
|
|
{
|
|
|
|
Gamebuino_Meta::Button button;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case SFG_KEY_UP: button = BUTTON_UP; break;
|
|
|
|
case SFG_KEY_RIGHT: button = BUTTON_RIGHT; break;
|
|
|
|
case SFG_KEY_DOWN: button = BUTTON_DOWN; break;
|
|
|
|
case SFG_KEY_LEFT: button = BUTTON_LEFT; break;
|
|
|
|
case SFG_KEY_A: button = BUTTON_A; break;
|
|
|
|
case SFG_KEY_B: button = BUTTON_B; break;
|
|
|
|
case SFG_KEY_C: button = BUTTON_MENU; break;
|
|
|
|
default: return 0; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gb.buttons.timeHeld(button) > 0;
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:53:18 -04:00
|
|
|
void SFG_processEvent(uint8_t event, uint8_t value)
|
|
|
|
{
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case SFG_EVENT_LEVEL_STARTS: blinkLED(BLUE); break;
|
|
|
|
case SFG_EVENT_PLAYER_HURT: blinkLED(RED); break;
|
|
|
|
case SFG_EVENT_LEVEL_WON: blinkLED(YELLOW); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 09:52:30 -04:00
|
|
|
void SFG_getMouseOffset(int16_t *x, int16_t *y)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:21:46 -04:00
|
|
|
void SFG_setMusic(uint8_t value)
|
2020-09-23 09:52:30 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SFG_save(uint8_t data[SFG_SAVE_SIZE])
|
|
|
|
{
|
2020-09-24 11:41:52 -04:00
|
|
|
gb.save.set(0,data,SFG_SAVE_SIZE);
|
2020-09-23 09:52:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
|
|
|
|
{
|
2020-09-24 11:41:52 -04:00
|
|
|
gb.save.get(0,data,SFG_SAVE_SIZE);
|
|
|
|
return 1;
|
2020-09-23 09:52:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SFG_playSound(uint8_t soundIndex, uint8_t volume)
|
|
|
|
{
|
2020-09-24 11:41:52 -04:00
|
|
|
switch (soundIndex)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
gb.sound.playCancel();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
gb.sound.playOK();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
gb.sound.playTick();
|
|
|
|
break;
|
|
|
|
}
|
2020-09-23 09:52:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SFG_getTimeMs()
|
|
|
|
{
|
|
|
|
return gb.frameStartMicros / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
gb.begin();
|
|
|
|
gb.setFrameRate(SFG_FPS);
|
2020-09-24 11:41:52 -04:00
|
|
|
gb.save.config(saveDefault);
|
2020-09-23 09:52:30 -04:00
|
|
|
|
2020-11-13 09:37:34 -05:00
|
|
|
uint8_t data[SFG_SAVE_SIZE];
|
|
|
|
|
|
|
|
gb.save.get(0,data,SFG_SAVE_SIZE);
|
|
|
|
|
|
|
|
uint8_t allZeros = 1;
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i)
|
|
|
|
if (data[i] != 0)
|
|
|
|
{
|
|
|
|
allZeros = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allZeros) // 1st time save?
|
|
|
|
{
|
|
|
|
SFG_createDefaultSaveData(data);
|
|
|
|
gb.save.set(0,data,SFG_SAVE_SIZE);
|
|
|
|
}
|
|
|
|
|
2020-09-23 09:52:30 -04:00
|
|
|
for (int i = 0; i < 256; ++i)
|
|
|
|
{
|
|
|
|
uint16_t rgb565 = paletteRGB565[i];
|
|
|
|
palette[i] = gb.createColor((rgb565 & 0xf800) >> 8,(rgb565 & 0x07e0) >> 3,(rgb565 & 0x001f) << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
SFG_init();
|
2020-10-03 12:53:18 -04:00
|
|
|
blinkLED(RED);
|
2020-09-23 09:52:30 -04:00
|
|
|
}
|
|
|
|
|
2020-11-13 09:37:34 -05:00
|
|
|
uint8_t stop = 0;
|
|
|
|
|
2020-09-23 09:52:30 -04:00
|
|
|
void loop()
|
|
|
|
{
|
2020-11-13 09:37:34 -05:00
|
|
|
if (stop)
|
|
|
|
return;
|
|
|
|
|
2020-09-23 09:52:30 -04:00
|
|
|
while(!gb.update())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:53:18 -04:00
|
|
|
if (blinkFramesLeft != 0)
|
|
|
|
{
|
|
|
|
if (blinkFramesLeft == 1)
|
|
|
|
gb.lights.clear();
|
|
|
|
|
|
|
|
blinkFramesLeft--;
|
|
|
|
}
|
|
|
|
|
2020-09-23 09:52:30 -04:00
|
|
|
SFG_mainLoopBody();
|
2020-11-13 09:37:34 -05:00
|
|
|
|
|
|
|
if (
|
|
|
|
gb.buttons.timeHeld(BUTTON_LEFT) >= 255 &&
|
|
|
|
gb.buttons.timeHeld(BUTTON_RIGHT) >= 255 &&
|
|
|
|
gb.buttons.timeHeld(BUTTON_B) >= 255)
|
|
|
|
{
|
|
|
|
// holding L+R+B in menu will erase all saved data
|
|
|
|
|
|
|
|
gb.save.del(0);
|
|
|
|
stop = 1;
|
|
|
|
}
|
2020-10-03 12:53:18 -04:00
|
|
|
|
2020-10-03 14:29:15 -04:00
|
|
|
#if 0
|
|
|
|
// debuggin performance
|
2020-10-03 12:53:18 -04:00
|
|
|
gb.display.setCursor(1,1);
|
|
|
|
gb.display.print(gb.getCpuLoad());
|
2020-10-03 14:29:15 -04:00
|
|
|
#endif
|
2020-09-23 09:52:30 -04:00
|
|
|
}
|