main_nibble.ino 3.0 KB

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