Update
[anarch.git] / main_nibble.ino
blob36adf0861d3839286609ec5bb45df845f1f6d358
1  /**
2   @file main_nibble.ino
4   This is Nibble (CircuitMess) implementation of the game front end.
6   by Miloslav Ciz (drummyfish), 2021
8   Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
9   plus a waiver of all other intellectual property. The goal of this work is to
10   be and remain completely in the public domain forever, available for any use
11   whatsoever.
14 #include <Arduino.h>
15 #include <EEPROM.h>
16 #include <CircuitOS.h>
17 #include <Nibble.h>
19 #define PLUS_BRIGHTNESS 2 // this can be changed (max: 8)
21 #define SFG_AVR 1
23 #define SFG_SCREEN_RESOLUTION_X 128
24 #define SFG_SCREEN_RESOLUTION_Y 128
25 #define SFG_FPS 35
27 #define SFG_RAYCASTING_MAX_STEPS 20
28 #define SFG_RAYCASTING_SUBSAMPLE 2
29 #define SFG_DIMINISH_SPRITES 1
30 #define SFG_DITHERED_SHADOW 1
32 #define SFG_CAN_EXIT 0 /* If the game is compiled into loeader, this can be set
33                           to 1 which will show the "exit" option in the menu. */
34 #include "game.h"
36 Display* display;
37 Sprite* sprite;
38 uint8_t buttons[7];
39 uint16_t paletteRAM[256];
41 void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
43   sprite->drawPixel(x,y,paletteRAM[colorIndex]);
46 uint32_t SFG_getTimeMs()
48   return millis();
51 void SFG_sleepMs(uint16_t timeMs)
55 int8_t SFG_keyPressed(uint8_t key)
57   return key < 7 ? buttons[key] : 0;
60 void SFG_getMouseOffset(int16_t *x, int16_t *y)
64 void SFG_setMusic(uint8_t value)
68 void SFG_save(uint8_t data[SFG_SAVE_SIZE])
72 void SFG_processEvent(uint8_t event, uint8_t data)
76 uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
78   return 0;
81 void SFG_playSound(uint8_t soundIndex, uint8_t volume)
83   switch (soundIndex)
84   {
85     case 0: Piezo.tone(120, 45); break; // shot
86     case 1: Piezo.tone(200, 30); break; // door
87     case 2: Piezo.tone(80, 60);  break; // explosion
88     case 3: Piezo.tone(220, 50); break; // click
89     case 4: Piezo.tone(180, 60); break; // plasma
90     case 5: Piezo.tone(300, 10); break; // monster
91     default: break;
92   }
95 // create button callbacks:
97 #define cbf(b,n) void b ## _down() { buttons[n] = 255; } void b ## _up() { buttons[n] = 0; }
98 cbf(BTN_UP,0)
99 cbf(BTN_RIGHT,1)
100 cbf(BTN_DOWN,2)
101 cbf(BTN_LEFT,3)
102 cbf(BTN_A,4)
103 cbf(BTN_B,5)
104 cbf(BTN_C,6)
105 #undef cbf
107 void setup()
108 {  
109   Nibble.begin();
110   display = Nibble.getDisplay();
111   sprite = display->getBaseSprite();
113   SFG_init();
115   for (uint8_t i = 0; i < 7; ++i)
116     buttons[i] = 0;
118   // move palette to RAM plus increase brightness of the colors:
120   for (int i = 0; i < 256; ++i)
121   { 
122     int helper = i % 8;
123     helper = (helper < 8 - PLUS_BRIGHTNESS) ? PLUS_BRIGHTNESS : (7 - helper);
124     paletteRAM[i] = pgm_read_word(paletteRGB565 + i + helper);
125   }
127   // register button callbacks:
129   #define cb(b) \
130     Input::getInstance()->setBtnPressCallback(b,b ## _down); \
131     Input::getInstance()->setBtnReleaseCallback(b,b ## _up);
133   cb(BTN_UP)
134   cb(BTN_DOWN)
135   cb(BTN_LEFT)
136   cb(BTN_RIGHT)
137   cb(BTN_A)
138   cb(BTN_B)
139   cb(BTN_C)
141   #undef cb
144 void loop()
146   Input::getInstance()->loop(0);
147   SFG_mainLoopBody();
148   display->commit();