Parcourir la source

Use shared memory space for game data (#14727)

Marcio Teixeira il y a 4 ans
Parent
révision
fec52e61ea

+ 46
- 50
Marlin/src/lcd/menu/game/brickout.cpp Voir le fichier

@@ -28,8 +28,6 @@
28 28
 
29 29
 #define BRICK_H      5
30 30
 #define BRICK_TOP    MENU_FONT_ASCENT
31
-#define BRICK_ROWS   4
32
-#define BRICK_COLS  16
33 31
 
34 32
 #define PADDLE_H     2
35 33
 #define PADDLE_VEL   3
@@ -42,51 +40,47 @@
42 40
 #define BRICK_COL(X) ((X) / (BRICK_W))
43 41
 #define BRICK_ROW(Y) ((Y - (BRICK_TOP)) / (BRICK_H))
44 42
 
45
-uint8_t balls_left, brick_count;
46
-uint16_t bricks[BRICK_ROWS];
43
+brickout_data_t &bdat = marlin_game_data.brickout;
47 44
 
48 45
 inline void reset_bricks(const uint16_t v) {
49
-  brick_count = (BRICK_COLS) * (BRICK_ROWS);
50
-  LOOP_L_N(i, BRICK_ROWS) bricks[i] = v;
46
+  bdat.brick_count = (BRICK_COLS) * (BRICK_ROWS);
47
+  LOOP_L_N(i, BRICK_ROWS) bdat.bricks[i] = v;
51 48
 }
52 49
 
53
-int8_t paddle_x, hit_dir;
54
-fixed_t ballx, bally, ballh, ballv;
55
-
56 50
 void reset_ball() {
57 51
   constexpr uint8_t ball_dist = 24;
58
-  bally = BTOF(PADDLE_Y - ball_dist);
59
-  ballv = FTOP(1.3f);
60
-  ballh = -FTOP(1.25f);
61
-  uint8_t bx = paddle_x + (PADDLE_W) / 2 + ball_dist;
62
-  if (bx >= LCD_PIXEL_WIDTH - 10) { bx -= ball_dist * 2; ballh = -ballh; }
63
-  ballx = BTOF(bx);
64
-  hit_dir = -1;
52
+  bdat.bally = BTOF(PADDLE_Y - ball_dist);
53
+  bdat.ballv = FTOP(1.3f);
54
+  bdat.ballh = -FTOP(1.25f);
55
+  uint8_t bx = bdat.paddle_x + (PADDLE_W) / 2 + ball_dist;
56
+  if (bx >= LCD_PIXEL_WIDTH - 10) { bx -= ball_dist * 2; bdat.ballh = -bdat.ballh; }
57
+  bdat.ballx = BTOF(bx);
58
+  bdat.hit_dir = -1;
65 59
 }
66 60
 
67 61
 void BrickoutGame::game_screen() {
68 62
   if (game_frame()) {     // Run logic twice for finer resolution
69 63
     // Update Paddle Position
70
-    paddle_x = constrain(int8_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL));
71
-    ui.encoderPosition = paddle_x;
72
-    paddle_x *= (PADDLE_VEL);
64
+    bdat.paddle_x = constrain(int8_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL));
65
+    ui.encoderPosition = bdat.paddle_x;
66
+    bdat.paddle_x *= (PADDLE_VEL);
73 67
 
74 68
     // Run the ball logic
75 69
     if (game_state) do {
76 70
 
77 71
       // Provisionally update the ball position
78
-      const fixed_t newx = ballx + ballh, newy = bally + ballv;  // current next position
72
+      const fixed_t newx = bdat.ballx + bdat.ballh, newy = bdat.bally + bdat.ballv;  // current next position
79 73
       if (!WITHIN(newx, 0, BTOF(LCD_PIXEL_WIDTH - 1))) {    // out in x?
80
-        ballh = -ballh; _BUZZ(5, 220);                      // bounce x
74
+        bdat.ballh = -bdat.ballh; _BUZZ(5, 220);            // bounce x
81 75
       }
82 76
       if (newy < 0) {                                       // out in y?
83
-        ballv = -ballv; _BUZZ(5, 280);                      // bounce v
84
-        hit_dir = 1;
77
+        bdat.ballv = -bdat.ballv; _BUZZ(5, 280);            // bounce v
78
+        bdat.hit_dir = 1;
85 79
       }
86 80
       // Did the ball go below the bottom?
87 81
       else if (newy > BTOF(LCD_PIXEL_HEIGHT)) {
88 82
         BUZZ(500, 75);
89
-        if (--balls_left) reset_ball(); else game_state = 0;
83
+        if (--bdat.balls_left) reset_ball(); else game_state = 0;
90 84
         break; // done
91 85
       }
92 86
 
@@ -94,42 +88,42 @@ void BrickoutGame::game_screen() {
94 88
       if (WITHIN(newy, BTOF(BRICK_TOP), BTOF(BRICK_BOT))) {
95 89
         const int8_t bit = BRICK_COL(FTOB(newx)), row = BRICK_ROW(FTOB(newy));
96 90
         const uint16_t mask = _BV(bit);
97
-        if (bricks[row] & mask) {
91
+        if (bdat.bricks[row] & mask) {
98 92
           // Yes. Remove it!
99
-          bricks[row] &= ~mask;
93
+          bdat.bricks[row] &= ~mask;
100 94
           // Score!
101 95
           score += BRICK_ROWS - row;
102 96
           // If bricks are gone, go to reset state
103
-          if (!--brick_count) game_state = 2;
97
+          if (!--bdat.brick_count) game_state = 2;
104 98
           // Bounce the ball cleverly
105
-          if ((ballv < 0) == (hit_dir < 0)) { ballv = -ballv; ballh += fixed_t(random(-16, 16)); _BUZZ(5, 880); }
106
-                                       else { ballh = -ballh; ballv += fixed_t(random(-16, 16)); _BUZZ(5, 640); }
99
+          if ((bdat.ballv < 0) == (bdat.hit_dir < 0)) { bdat.ballv = -bdat.ballv; bdat.ballh += fixed_t(random(-16, 16)); _BUZZ(5, 880); }
100
+                                       else { bdat.ballh = -bdat.ballh; bdat.ballv += fixed_t(random(-16, 16)); _BUZZ(5, 640); }
107 101
         }
108 102
       }
109 103
       // Is the ball moving down and in paddle range?
110
-      else if (ballv > 0 && WITHIN(newy, BTOF(PADDLE_Y), BTOF(PADDLE_Y + PADDLE_H))) {
104
+      else if (bdat.ballv > 0 && WITHIN(newy, BTOF(PADDLE_Y), BTOF(PADDLE_Y + PADDLE_H))) {
111 105
         // Ball actually hitting paddle
112
-        const int8_t diff = FTOB(newx) - paddle_x;
106
+        const int8_t diff = FTOB(newx) - bdat.paddle_x;
113 107
         if (WITHIN(diff, 0, PADDLE_W - 1)) {
114 108
 
115 109
           // Reverse Y direction
116
-          ballv = -ballv; _BUZZ(3, 880);
117
-          hit_dir = -1;
110
+          bdat.ballv = -bdat.ballv; _BUZZ(3, 880);
111
+          bdat.hit_dir = -1;
118 112
 
119 113
           // Near edges affects X velocity
120 114
           const bool is_left_edge = (diff <= 1);
121 115
           if (is_left_edge || diff >= PADDLE_W-1 - 1) {
122
-            if ((ballh > 0) == is_left_edge) ballh = -ballh;
116
+            if ((bdat.ballh > 0) == is_left_edge) bdat.ballh = -bdat.ballh;
123 117
           }
124 118
           else if (diff <= 3) {
125
-            ballh += fixed_t(random(-64, 0));
126
-            NOLESS(ballh, BTOF(-2));
127
-            NOMORE(ballh, BTOF(2));
119
+            bdat.ballh += fixed_t(random(-64, 0));
120
+            NOLESS(bdat.ballh, BTOF(-2));
121
+            NOMORE(bdat.ballh, BTOF(2));
128 122
           }
129 123
           else if (diff >= PADDLE_W-1 - 3) {
130
-            ballh += fixed_t(random( 0, 64));
131
-            NOLESS(ballh, BTOF(-2));
132
-            NOMORE(ballh, BTOF(2));
124
+            bdat.ballh += fixed_t(random( 0, 64));
125
+            NOLESS(bdat.ballh, BTOF(-2));
126
+            NOMORE(bdat.ballh, BTOF(2));
133 127
           }
134 128
 
135 129
           // Paddle hit after clearing the board? Reset the board.
@@ -137,7 +131,7 @@ void BrickoutGame::game_screen() {
137 131
         }
138 132
       }
139 133
 
140
-      ballx += ballh; bally += ballv;   // update with new velocity
134
+      bdat.ballx += bdat.ballh; bdat.bally += bdat.ballv; // update with new velocity
141 135
 
142 136
     } while (false);
143 137
   }
@@ -150,7 +144,7 @@ void BrickoutGame::game_screen() {
150 144
       const uint8_t yy = y * BRICK_H + BRICK_TOP;
151 145
       if (PAGE_CONTAINS(yy, yy + BRICK_H - 1)) {
152 146
         for (uint8_t x = 0; x < BRICK_COLS; ++x) {
153
-          if (TEST(bricks[y], x)) {
147
+          if (TEST(bdat.bricks[y], x)) {
154 148
             const uint8_t xx = x * BRICK_W;
155 149
             for (uint8_t v = 0; v < BRICK_H - 1; ++v)
156 150
               if (PAGE_CONTAINS(yy + v, yy + v))
@@ -163,20 +157,20 @@ void BrickoutGame::game_screen() {
163 157
 
164 158
   // Draw paddle
165 159
   if (PAGE_CONTAINS(PADDLE_Y-1, PADDLE_Y)) {
166
-    u8g.drawHLine(paddle_x, PADDLE_Y, PADDLE_W);
160
+    u8g.drawHLine(bdat.paddle_x, PADDLE_Y, PADDLE_W);
167 161
     #if PADDLE_H > 1
168
-      u8g.drawHLine(paddle_x, PADDLE_Y-1, PADDLE_W);
162
+      u8g.drawHLine(bdat.paddle_x, PADDLE_Y-1, PADDLE_W);
169 163
       #if PADDLE_H > 2
170
-        u8g.drawHLine(paddle_x, PADDLE_Y-2, PADDLE_W);
164
+        u8g.drawHLine(bdat.paddle_x, PADDLE_Y-2, PADDLE_W);
171 165
       #endif
172 166
     #endif
173 167
   }
174 168
 
175 169
   // Draw ball while game is running
176 170
   if (game_state) {
177
-    const uint8_t by = FTOB(bally);
171
+    const uint8_t by = FTOB(bdat.bally);
178 172
     if (PAGE_CONTAINS(by, by+1))
179
-      u8g.drawFrame(FTOB(ballx), by, 2, 2);
173
+      u8g.drawFrame(FTOB(bdat.ballx), by, 2, 2);
180 174
   }
181 175
   // Or draw GAME OVER
182 176
   else
@@ -192,18 +186,20 @@ void BrickoutGame::game_screen() {
192 186
     // Balls Left
193 187
     lcd_moveto(LCD_PIXEL_WIDTH - MENU_FONT_WIDTH * 3, MENU_FONT_ASCENT - 1);
194 188
     PGM_P const ohs = PSTR("ooo\0\0");
195
-    lcd_put_u8str_P(ohs + 3 - balls_left);
189
+    lcd_put_u8str_P(ohs + 3 - bdat.balls_left);
196 190
   }
197 191
 
198 192
   // A click always exits this game
199 193
   if (ui.use_click()) exit_game();
200 194
 }
201 195
 
196
+#define SCREEN_M ((LCD_PIXEL_WIDTH) / 2)
197
+
202 198
 void BrickoutGame::enter_game() {
203 199
   init_game(2, game_screen); // 2 = reset bricks on paddle hit
204 200
   constexpr uint8_t paddle_start = SCREEN_M - (PADDLE_W) / 2;
205
-  paddle_x = paddle_start;
206
-  balls_left = 3;
201
+  bdat.paddle_x = paddle_start;
202
+  bdat.balls_left = 3;
207 203
   reset_bricks(0x0000);
208 204
   reset_ball();
209 205
   ui.encoderPosition = paddle_start / (PADDLE_VEL);

+ 38
- 0
Marlin/src/lcd/menu/game/brickout.h Voir le fichier

@@ -0,0 +1,38 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include "types.h"
25
+
26
+#define BRICK_ROWS   4
27
+#define BRICK_COLS  16
28
+
29
+typedef struct {
30
+  uint8_t balls_left, brick_count;
31
+  uint16_t bricks[BRICK_ROWS];
32
+  int8_t paddle_x, hit_dir;
33
+  fixed_t ballx, bally, ballh, ballv;
34
+} brickout_data_t;
35
+
36
+class BrickoutGame : MarlinGame { public: static void enter_game(), game_screen(); };
37
+
38
+extern BrickoutGame brickout;

+ 2
- 0
Marlin/src/lcd/menu/game/game.cpp Voir le fichier

@@ -30,6 +30,8 @@ int MarlinGame::score;
30 30
 uint8_t MarlinGame::game_state;
31 31
 millis_t MarlinGame::next_frame;
32 32
 
33
+MarlinGameData marlin_game_data;
34
+
33 35
 bool MarlinGame::game_frame() {
34 36
   static int8_t slew;
35 37
   if (ui.first_page) slew = 2;

+ 24
- 32
Marlin/src/lcd/menu/game/game.h Voir le fichier

@@ -34,45 +34,37 @@
34 34
   #define _BUZZ(D,F) BUZZ(D,F)
35 35
 #endif
36 36
 
37
-// Simple 8:8 fixed-point
38
-typedef int16_t fixed_t;
39
-#define FTOP(F) fixed_t((F)*256.0f)
40
-#define PTOF(P) (float(P)*(1.0f/256.0f))
41
-#define BTOF(X) (fixed_t(X)<<8)
42
-#define FTOB(X) int8_t(fixed_t(X)>>8)
43
-
44
-#define SCREEN_M ((LCD_PIXEL_WIDTH) / 2)
45
-
46 37
 #if HAS_GAME_MENU
47 38
   void menu_game();
48 39
 #endif
49 40
 
50
-class MarlinGame {
51
-protected:
52
-  static int score;
53
-  static uint8_t game_state;
54
-  static millis_t next_frame;
55
-
56
-  static bool game_frame();
57
-  static void draw_game_over();
58
-  static void exit_game();
59
-public:
60
-  static void init_game(const uint8_t init_state, const screenFunc_t screen);
61
-};
62
-
63 41
 #if ENABLED(MARLIN_BRICKOUT)
64
-  class BrickoutGame : MarlinGame { public: static void enter_game(); static void game_screen(); };
65
-  extern BrickoutGame brickout;
42
+  #include "brickout.h"
66 43
 #endif
67 44
 #if ENABLED(MARLIN_INVADERS)
68
-  class InvadersGame : MarlinGame { public: static void enter_game(); static void game_screen(); };
69
-  extern InvadersGame invaders;
70
-#endif
71
-#if ENABLED(MARLIN_SNAKE)
72
-  class SnakeGame : MarlinGame { public: static void enter_game(); static void game_screen(); };
73
-  extern SnakeGame snake;
45
+  #include "invaders.h"
74 46
 #endif
75 47
 #if ENABLED(MARLIN_MAZE)
76
-  class MazeGame : MarlinGame { public: static void enter_game(); static void game_screen(); };
77
-  extern MazeGame maze;
48
+  #include "maze.h"
49
+#endif
50
+#if ENABLED(MARLIN_SNAKE)
51
+  #include "snake.h"
78 52
 #endif
53
+
54
+// Pool game data to save SRAM
55
+union MarlinGameData {
56
+  #if ENABLED(MARLIN_BRICKOUT)
57
+    brickout_data_t brickout;
58
+  #endif
59
+  #if ENABLED(MARLIN_INVADERS)
60
+    invaders_data_t invaders;
61
+  #endif
62
+  #if ENABLED(MARLIN_SNAKE)
63
+    snake_data_t snake;
64
+  #endif
65
+  #if ENABLED(MARLIN_MAZE)
66
+    maze_data_t maze;
67
+  #endif
68
+};
69
+
70
+extern MarlinGameData marlin_game_data;

+ 115
- 143
Marlin/src/lcd/menu/game/invaders.cpp Voir le fichier

@@ -26,6 +26,28 @@
26 26
 
27 27
 #include "game.h"
28 28
 
29
+#define CANNON_W      11
30
+#define CANNON_H       8
31
+#define CANNON_VEL     4
32
+#define CANNON_Y      (LCD_PIXEL_HEIGHT - 1 - CANNON_H)
33
+
34
+#define INVADER_VEL    3
35
+
36
+#define INVADER_TOP   MENU_FONT_ASCENT
37
+#define INVADERS_WIDE ((INVADER_COL_W) * (INVADER_COLS))
38
+#define INVADERS_HIGH ((INVADER_ROW_H) * (INVADER_ROWS))
39
+
40
+#define UFO_H          5
41
+#define UFO_W         13
42
+
43
+#define LASER_H        4
44
+#define SHOT_H         3
45
+#define EXPL_W        11
46
+#define LIFE_W         8
47
+#define LIFE_H         5
48
+
49
+#define INVADER_RIGHT ((INVADER_COLS) * (INVADER_COL_W))
50
+
29 51
 // 11x8
30 52
 const unsigned char invader[3][2][16] PROGMEM = {
31 53
   { { B00000110,B00000000,
@@ -120,20 +142,6 @@ const unsigned char ufo[] PROGMEM = {
120 142
   B01111111,B11110000
121 143
 };
122 144
 
123
-#define INVASION_SIZE 3
124
-
125
-#if INVASION_SIZE == 3
126
-  #define INVADER_COLS   5
127
-#elif INVASION_SIZE == 4
128
-  #define INVADER_COLS   6
129
-#else
130
-  #define INVADER_COLS   8
131
-  #undef INVASION_SIZE
132
-  #define INVASION_SIZE  5
133
-#endif
134
-
135
-#define INVADER_ROWS INVASION_SIZE
136
-
137 145
 constexpr uint8_t inv_type[] = {
138 146
   #if INVADER_ROWS == 5
139 147
     0, 1, 1, 2, 2
@@ -146,107 +154,74 @@ constexpr uint8_t inv_type[] = {
146 154
   #endif
147 155
 };
148 156
 
149
-#define INVADER_RIGHT ((INVADER_COLS) * (COL_W))
157
+invaders_data_t &idat = marlin_game_data.invaders;
150 158
 
151
-#define CANNON_W      11
152
-#define CANNON_H       8
153
-#define CANNON_VEL     4
154
-#define CANNON_Y      (LCD_PIXEL_HEIGHT - 1 - CANNON_H)
155
-
156
-#define COL_W         14
157
-#define INVADER_H      8
158
-#define ROW_H         (INVADER_H + 2)
159
-#define INVADER_VEL    3
160
-
161
-#define INVADER_TOP   MENU_FONT_ASCENT
162
-#define INVADERS_WIDE ((COL_W) * (INVADER_COLS))
163
-#define INVADERS_HIGH ((ROW_H) * (INVADER_ROWS))
164
-
165
-#define UFO_H          5
166
-#define UFO_W         13
167
-
168
-#define LASER_H        4
169
-#define SHOT_H         3
170
-#define EXPL_W        11
171
-#define LIFE_W         8
172
-#define LIFE_H         5
173
-
174
-#define INVADER_COL(X) ((X - invaders_x) / (COL_W))
175
-#define INVADER_ROW(Y) ((Y - invaders_y + 2) / (ROW_H))
176
-
177
-#define INV_X_LEFT(C,T) (invaders_x + (C) * (COL_W) + inv_off[T])
159
+#define INV_X_LEFT(C,T) (idat.pos.x + (C) * (INVADER_COL_W) + inv_off[T])
178 160
 #define INV_X_CTR(C,T)  (INV_X_LEFT(C,T) + inv_wide[T] / 2)
179
-#define INV_Y_BOT(R)    (invaders_y + (R + 1) * (ROW_H) - 2)
180
-
181
-typedef struct { int8_t x, y, v; } laser_t;
161
+#define INV_Y_BOT(R)    (idat.pos.y + (R + 1) * (INVADER_ROW_H) - 2)
182 162
 
183
-uint8_t cannons_left;
184
-int8_t cannon_x;
185
-laser_t explod, laser, bullet[10];
186 163
 constexpr uint8_t inv_off[] = { 2, 1, 0 }, inv_wide[] = { 8, 11, 12 };
187
-int8_t invaders_x, invaders_y, invaders_dir, leftmost, rightmost, botmost;
188
-uint8_t invader_count, quit_count, bugs[INVADER_ROWS], shooters[(INVADER_ROWS) * (INVADER_COLS)];
189 164
 
190 165
 inline void update_invader_data() {
191 166
   uint8_t inv_mask = 0;
192 167
   // Get a list of all active invaders
193 168
   uint8_t sc = 0;
194 169
   LOOP_L_N(y, INVADER_ROWS) {
195
-    uint8_t m = bugs[y];
196
-    if (m) botmost = y + 1;
170
+    uint8_t m = idat.bugs[y];
171
+    if (m) idat.botmost = y + 1;
197 172
     inv_mask |= m;
198 173
     for (uint8_t x = 0; x < INVADER_COLS; ++x)
199
-      if (TEST(m, x)) shooters[sc++] = (y << 4) | x;
174
+      if (TEST(m, x)) idat.shooters[sc++] = (y << 4) | x;
200 175
   }
201
-  leftmost = 0;
202
-  LOOP_L_N(i, INVADER_COLS)            { if (TEST(inv_mask, i)) break; leftmost -= COL_W; }
203
-  rightmost = LCD_PIXEL_WIDTH - (INVADERS_WIDE);
204
-  for (uint8_t i = INVADER_COLS; i--;) { if (TEST(inv_mask, i)) break; rightmost += COL_W; }
205
-  if (invader_count == 2) invaders_dir = invaders_dir > 0 ? INVADER_VEL + 1 : -(INVADER_VEL + 1);
176
+  idat.leftmost = 0;
177
+  LOOP_L_N(i, INVADER_COLS)            { if (TEST(inv_mask, i)) break; idat.leftmost -= INVADER_COL_W; }
178
+  idat.rightmost = LCD_PIXEL_WIDTH - (INVADERS_WIDE);
179
+  for (uint8_t i = INVADER_COLS; i--;) { if (TEST(inv_mask, i)) break; idat.rightmost += INVADER_COL_W; }
180
+  if (idat.count == 2) idat.dir = idat.dir > 0 ? INVADER_VEL + 1 : -(INVADER_VEL + 1);
206 181
 }
207 182
 
208 183
 inline void reset_bullets() {
209
-  LOOP_L_N(i, COUNT(bullet)) bullet[i].v = 0;
184
+  LOOP_L_N(i, COUNT(idat.bullet)) idat.bullet[i].v = 0;
210 185
 }
211 186
 
212 187
 inline void reset_invaders() {
213
-  invaders_x = 0; invaders_y = INVADER_TOP;
214
-  invaders_dir = INVADER_VEL;
215
-  invader_count = (INVADER_COLS) * (INVADER_ROWS);
216
-  LOOP_L_N(i, INVADER_ROWS) bugs[i] = _BV(INVADER_COLS) - 1;
188
+  idat.pos.x = 0; idat.pos.y = INVADER_TOP;
189
+  idat.dir = INVADER_VEL;
190
+  idat.count = (INVADER_COLS) * (INVADER_ROWS);
191
+  LOOP_L_N(i, INVADER_ROWS) idat.bugs[i] = _BV(INVADER_COLS) - 1;
217 192
   update_invader_data();
218 193
   reset_bullets();
219 194
 }
220 195
 
221
-int8_t ufox, ufov;
196
+
222 197
 inline void spawn_ufo() {
223
-  ufov = random(0, 2) ? 1 : -1;
224
-  ufox = ufov > 0 ? -(UFO_W) : LCD_PIXEL_WIDTH - 1;
198
+  idat.ufov = random(0, 2) ? 1 : -1;
199
+  idat.ufox = idat.ufov > 0 ? -(UFO_W) : LCD_PIXEL_WIDTH - 1;
225 200
 }
226 201
 
227 202
 inline void reset_player() {
228
-  cannon_x = 0;
203
+  idat.cannon_x = 0;
229 204
   ui.encoderPosition = 0;
230 205
 }
231 206
 
232 207
 inline void fire_cannon() {
233
-  laser.x = cannon_x + CANNON_W / 2;
234
-  laser.y = LCD_PIXEL_HEIGHT - CANNON_H - (LASER_H);
235
-  laser.v = -(LASER_H);
208
+  idat.laser.x = idat.cannon_x + CANNON_W / 2;
209
+  idat.laser.y = LCD_PIXEL_HEIGHT - CANNON_H - (LASER_H);
210
+  idat.laser.v = -(LASER_H);
236 211
 }
237 212
 
238 213
 inline void explode(const int8_t x, const int8_t y, const int8_t v=4) {
239
-  explod.x = x - (EXPL_W) / 2;
240
-  explod.y = y;
241
-  explod.v = v;
214
+  idat.explod.x = x - (EXPL_W) / 2;
215
+  idat.explod.y = y;
216
+  idat.explod.v = v;
242 217
 }
243 218
 
244 219
 inline void kill_cannon(uint8_t &game_state, const uint8_t st) {
245 220
   reset_bullets();
246
-  explode(cannon_x + (CANNON_W) / 2, CANNON_Y, 6);
221
+  explode(idat.cannon_x + (CANNON_W) / 2, CANNON_Y, 6);
247 222
   _BUZZ(1000, 10);
248
-  if (--cannons_left) {
249
-    laser.v = 0;
223
+  if (--idat.cannons_left) {
224
+    idat.laser.v = 0;
250 225
     game_state = st;
251 226
     reset_player();
252 227
   }
@@ -255,8 +230,6 @@ inline void kill_cannon(uint8_t &game_state, const uint8_t st) {
255 230
 }
256 231
 
257 232
 void InvadersGame::game_screen() {
258
-  static bool game_blink;
259
-
260 233
   ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Call as often as possible
261 234
 
262 235
   // Run game logic once per full screen
@@ -267,46 +240,45 @@ void InvadersGame::game_screen() {
267 240
     ui.encoderPosition = ep;
268 241
 
269 242
     ep *= (CANNON_VEL);
270
-    if (ep > cannon_x) { cannon_x += CANNON_VEL - 1; if (ep - cannon_x < 2) cannon_x = ep; }
271
-    if (ep < cannon_x) { cannon_x -= CANNON_VEL - 1; if (cannon_x - ep < 2) cannon_x = ep; }
243
+    if (ep > idat.cannon_x) { idat.cannon_x += CANNON_VEL - 1; if (ep - idat.cannon_x < 2) idat.cannon_x = ep; }
244
+    if (ep < idat.cannon_x) { idat.cannon_x -= CANNON_VEL - 1; if (idat.cannon_x - ep < 2) idat.cannon_x = ep; }
272 245
 
273 246
     // Run the game logic
274 247
     if (game_state) do {
275 248
 
276 249
       // Move the UFO, if any
277
-      if (ufov) { ufox += ufov; if (!WITHIN(ufox, -(UFO_W), LCD_PIXEL_WIDTH - 1)) ufov = 0; }
250
+      if (idat.ufov) { idat.ufox += idat.ufov; if (!WITHIN(idat.ufox, -(UFO_W), LCD_PIXEL_WIDTH - 1)) idat.ufov = 0; }
278 251
 
279 252
       if (game_state > 1) { if (--game_state == 2) { reset_invaders(); } else if (game_state == 100) { game_state = 1; } break; }
280 253
 
281
-      static uint8_t blink_count;
282
-      const bool did_blink = (++blink_count > invader_count >> 1);
254
+      const bool did_blink = (++idat.blink_count > idat.count >> 1);
283 255
       if (did_blink) {
284
-        game_blink = !game_blink;
285
-        blink_count = 0;
256
+        idat.game_blink = !idat.game_blink;
257
+        idat.blink_count = 0;
286 258
       }
287 259
 
288
-      if (invader_count && did_blink) {
289
-        const int8_t newx = invaders_x + invaders_dir;
290
-        if (!WITHIN(newx, leftmost, rightmost)) {             // Invaders reached the edge?
291
-          invaders_dir *= -1;                                 // Invaders change direction
292
-          invaders_y += (ROW_H) / 2;                          // Invaders move down
293
-          invaders_x -= invaders_dir;                         // ...and only move down this time.
294
-          if (invaders_y + botmost * (ROW_H) - 2 >= CANNON_Y) // Invaders reached the bottom?
295
-            kill_cannon(game_state, 20);                      // Kill the cannon. Reset invaders.
260
+      if (idat.count && did_blink) {
261
+        const int8_t newx = idat.pos.x + idat.dir;
262
+        if (!WITHIN(newx, idat.leftmost, idat.rightmost)) { // Invaders reached the edge?
263
+          idat.dir *= -1;                                   // Invaders change direction
264
+          idat.pos.y += (INVADER_ROW_H) / 2;                        // Invaders move down
265
+          idat.pos.x -= idat.dir;                           // ...and only move down this time.
266
+          if (idat.pos.y + idat.botmost * (INVADER_ROW_H) - 2 >= CANNON_Y) // Invaders reached the bottom?
267
+            kill_cannon(game_state, 20);                    // Kill the cannon. Reset invaders.
296 268
         }
297 269
 
298
-        invaders_x += invaders_dir;               // Invaders take one step left/right
270
+        idat.pos.x += idat.dir; // Invaders take one step left/right
299 271
 
300 272
         // Randomly shoot if invaders are listed
301
-        if (invader_count && !random(0, 20)) {
273
+        if (idat.count && !random(0, 20)) {
302 274
 
303 275
           // Find a free bullet
304 276
           laser_t *b = nullptr;
305
-          LOOP_L_N(i, COUNT(bullet)) if (!bullet[i].v) { b = &bullet[i]; break; }
277
+          LOOP_L_N(i, COUNT(idat.bullet)) if (!idat.bullet[i].v) { b = &idat.bullet[i]; break; }
306 278
           if (b) {
307 279
             // Pick a random shooter and update the bullet
308 280
             //SERIAL_ECHOLNPGM("free bullet found");
309
-            const uint8_t inv = shooters[random(0, invader_count + 1)], col = inv & 0x0F, row = inv >> 4, type = inv_type[row];
281
+            const uint8_t inv = idat.shooters[random(0, idat.count + 1)], col = inv & 0x0F, row = inv >> 4, type = inv_type[row];
310 282
             b->x = INV_X_CTR(col, type);
311 283
             b->y = INV_Y_BOT(row);
312 284
             b->v = 2 + random(0, 2);
@@ -315,34 +287,34 @@ void InvadersGame::game_screen() {
315 287
       }
316 288
 
317 289
       // Update the laser position
318
-      if (laser.v) {
319
-        laser.y += laser.v;
320
-        if (laser.y < 0) laser.v = 0;
290
+      if (idat.laser.v) {
291
+        idat.laser.y += idat.laser.v;
292
+        if (idat.laser.y < 0) idat.laser.v = 0;
321 293
       }
322 294
 
323 295
       // Did the laser collide with an invader?
324
-      if (laser.v && WITHIN(laser.y, invaders_y, invaders_y + INVADERS_HIGH - 1)) {
325
-        const int8_t col = INVADER_COL(laser.x);
296
+      if (idat.laser.v && WITHIN(idat.laser.y, idat.pos.y, idat.pos.y + INVADERS_HIGH - 1)) {
297
+        const int8_t col = idat.laser_col();
326 298
         if (WITHIN(col, 0, INVADER_COLS - 1)) {
327
-          const int8_t row = INVADER_ROW(laser.y);
299
+          const int8_t row = idat.laser_row();
328 300
           if (WITHIN(row, 0, INVADER_ROWS - 1)) {
329 301
             const uint8_t mask = _BV(col);
330
-            if (bugs[row] & mask) {
302
+            if (idat.bugs[row] & mask) {
331 303
               const uint8_t type = inv_type[row];
332 304
               const int8_t invx = INV_X_LEFT(col, type);
333
-              if (WITHIN(laser.x, invx, invx + inv_wide[type] - 1)) {
305
+              if (WITHIN(idat.laser.x, invx, invx + inv_wide[type] - 1)) {
334 306
                 // Turn off laser
335
-                laser.v = 0;
307
+                idat.laser.v = 0;
336 308
                 // Remove the invader!
337
-                bugs[row] &= ~mask;
309
+                idat.bugs[row] &= ~mask;
338 310
                 // Score!
339 311
                 score += INVADER_ROWS - row;
340 312
                 // Explode sound!
341 313
                 _BUZZ(40, 10);
342 314
                 // Explosion bitmap!
343
-                explode(invx + inv_wide[type] / 2, invaders_y + row * (ROW_H));
315
+                explode(invx + inv_wide[type] / 2, idat.pos.y + row * (INVADER_ROW_H));
344 316
                 // If invaders are gone, go to reset invaders state
345
-                if (--invader_count) update_invader_data(); else { game_state = 20; reset_bullets(); }
317
+                if (--idat.count) update_invader_data(); else { game_state = 20; reset_bullets(); }
346 318
               } // laser x hit
347 319
             } // invader exists
348 320
           } // good row
@@ -350,31 +322,31 @@ void InvadersGame::game_screen() {
350 322
       } // laser in invader zone
351 323
 
352 324
       // Handle alien bullets
353
-      LOOP_L_N(s, COUNT(bullet)) {
354
-        laser_t *b = &bullet[s];
325
+      LOOP_L_N(s, COUNT(idat.bullet)) {
326
+        laser_t *b = &idat.bullet[s];
355 327
         if (b->v) {
356 328
           // Update alien bullet position
357 329
           b->y += b->v;
358 330
           if (b->y >= LCD_PIXEL_HEIGHT)
359 331
             b->v = 0; // Offscreen
360
-          else if (b->y >= CANNON_Y && WITHIN(b->x, cannon_x, cannon_x + CANNON_W - 1))
332
+          else if (b->y >= CANNON_Y && WITHIN(b->x, idat.cannon_x, idat.cannon_x + CANNON_W - 1))
361 333
             kill_cannon(game_state, 120); // Hit the cannon
362 334
         }
363 335
       }
364 336
 
365 337
       // Randomly spawn a UFO
366
-      if (!ufov && !random(0,500)) spawn_ufo();
338
+      if (!idat.ufov && !random(0,500)) spawn_ufo();
367 339
 
368 340
       // Did the laser hit a ufo?
369
-      if (laser.v && ufov && laser.y < UFO_H + 2 && WITHIN(laser.x, ufox, ufox + UFO_W - 1)) {
341
+      if (idat.laser.v && idat.ufov && idat.laser.y < UFO_H + 2 && WITHIN(idat.laser.x, idat.ufox, idat.ufox + UFO_W - 1)) {
370 342
         // Turn off laser and UFO
371
-        laser.v = ufov = 0;
343
+        idat.laser.v = idat.ufov = 0;
372 344
         // Score!
373 345
         score += 10;
374 346
         // Explode!
375 347
         _BUZZ(40, 10);
376 348
         // Explosion bitmap
377
-        explode(ufox + (UFO_W) / 2, 1);
349
+        explode(idat.ufox + (UFO_W) / 2, 1);
378 350
       }
379 351
 
380 352
     } while (false);
@@ -382,59 +354,59 @@ void InvadersGame::game_screen() {
382 354
   }
383 355
 
384 356
   // Click-and-hold to abort
385
-  if (ui.button_pressed()) --quit_count; else quit_count = 10;
357
+  if (ui.button_pressed()) --idat.quit_count; else idat.quit_count = 10;
386 358
 
387 359
   // Click to fire or exit
388 360
   if (ui.use_click()) {
389 361
     if (!game_state)
390
-      quit_count = 0;
391
-    else if (game_state == 1 && !laser.v)
362
+      idat.quit_count = 0;
363
+    else if (game_state == 1 && !idat.laser.v)
392 364
       fire_cannon();
393 365
   }
394 366
 
395
-  if (!quit_count) exit_game();
367
+  if (!idat.quit_count) exit_game();
396 368
 
397 369
   u8g.setColorIndex(1);
398 370
 
399 371
   // Draw invaders
400
-  if (PAGE_CONTAINS(invaders_y, invaders_y + botmost * (ROW_H) - 2 - 1)) {
401
-    int8_t yy = invaders_y;
372
+  if (PAGE_CONTAINS(idat.pos.y, idat.pos.y + idat.botmost * (INVADER_ROW_H) - 2 - 1)) {
373
+    int8_t yy = idat.pos.y;
402 374
     for (uint8_t y = 0; y < INVADER_ROWS; ++y) {
403 375
       const uint8_t type = inv_type[y];
404 376
       if (PAGE_CONTAINS(yy, yy + INVADER_H - 1)) {
405
-        int8_t xx = invaders_x;
377
+        int8_t xx = idat.pos.x;
406 378
         for (uint8_t x = 0; x < INVADER_COLS; ++x) {
407
-          if (TEST(bugs[y], x))
408
-            u8g.drawBitmapP(xx, yy, 2, INVADER_H, invader[type][game_blink]);
409
-          xx += COL_W;
379
+          if (TEST(idat.bugs[y], x))
380
+            u8g.drawBitmapP(xx, yy, 2, INVADER_H, invader[type][idat.game_blink]);
381
+          xx += INVADER_COL_W;
410 382
         }
411 383
       }
412
-      yy += ROW_H;
384
+      yy += INVADER_ROW_H;
413 385
     }
414 386
   }
415 387
 
416 388
   // Draw UFO
417
-  if (ufov && PAGE_UNDER(UFO_H + 2))
418
-    u8g.drawBitmapP(ufox, 2, 2, UFO_H, ufo);
389
+  if (idat.ufov && PAGE_UNDER(UFO_H + 2))
390
+    u8g.drawBitmapP(idat.ufox, 2, 2, UFO_H, ufo);
419 391
 
420 392
   // Draw cannon
421 393
   if (game_state && PAGE_CONTAINS(CANNON_Y, CANNON_Y + CANNON_H - 1) && (game_state < 2 || (game_state & 0x02)))
422
-    u8g.drawBitmapP(cannon_x, CANNON_Y, 2, CANNON_H, cannon);
394
+    u8g.drawBitmapP(idat.cannon_x, CANNON_Y, 2, CANNON_H, cannon);
423 395
 
424 396
   // Draw laser
425
-  if (laser.v && PAGE_CONTAINS(laser.y, laser.y + LASER_H - 1))
426
-    u8g.drawVLine(laser.x, laser.y, LASER_H);
397
+  if (idat.laser.v && PAGE_CONTAINS(idat.laser.y, idat.laser.y + LASER_H - 1))
398
+    u8g.drawVLine(idat.laser.x, idat.laser.y, LASER_H);
427 399
 
428 400
   // Draw invader bullets
429
-  LOOP_L_N (i, COUNT(bullet)) {
430
-    if (bullet[i].v && PAGE_CONTAINS(bullet[i].y - (SHOT_H - 1), bullet[i].y))
431
-      u8g.drawVLine(bullet[i].x, bullet[i].y - (SHOT_H - 1), SHOT_H);
401
+  LOOP_L_N (i, COUNT(idat.bullet)) {
402
+    if (idat.bullet[i].v && PAGE_CONTAINS(idat.bullet[i].y - (SHOT_H - 1), idat.bullet[i].y))
403
+      u8g.drawVLine(idat.bullet[i].x, idat.bullet[i].y - (SHOT_H - 1), SHOT_H);
432 404
   }
433 405
 
434 406
   // Draw explosion
435
-  if (explod.v && PAGE_CONTAINS(explod.y, explod.y + 7 - 1)) {
436
-    u8g.drawBitmapP(explod.x, explod.y, 2, 7, explosion);
437
-    --explod.v;
407
+  if (idat.explod.v && PAGE_CONTAINS(idat.explod.y, idat.explod.y + 7 - 1)) {
408
+    u8g.drawBitmapP(idat.explod.x, idat.explod.y, 2, 7, explosion);
409
+    --idat.explod.v;
438 410
   }
439 411
 
440 412
   // Blink GAME OVER when game is over
@@ -448,8 +420,8 @@ void InvadersGame::game_screen() {
448 420
     lcd_put_int(score);
449 421
 
450 422
     // Draw lives
451
-    if (cannons_left)
452
-      for (uint8_t i = 1; i <= cannons_left; ++i)
423
+    if (idat.cannons_left)
424
+      for (uint8_t i = 1; i <= idat.cannons_left; ++i)
453 425
         u8g.drawBitmapP(LCD_PIXEL_WIDTH - i * (LIFE_W), 6 - (LIFE_H), 1, LIFE_H, life);
454 426
   }
455 427
 
@@ -457,9 +429,9 @@ void InvadersGame::game_screen() {
457 429
 
458 430
 void InvadersGame::enter_game() {
459 431
   init_game(20, game_screen); // countdown to reset invaders
460
-  cannons_left = 3;
461
-  quit_count = 10;
462
-  laser.v = 0;
432
+  idat.cannons_left = 3;
433
+  idat.quit_count = 10;
434
+  idat.laser.v = 0;
463 435
   reset_invaders();
464 436
   reset_player();
465 437
 }

+ 62
- 0
Marlin/src/lcd/menu/game/invaders.h Voir le fichier

@@ -0,0 +1,62 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include "types.h"
25
+
26
+#define INVASION_SIZE 3
27
+
28
+#if INVASION_SIZE == 3
29
+  #define INVADER_COLS   5
30
+#elif INVASION_SIZE == 4
31
+  #define INVADER_COLS   6
32
+#else
33
+  #define INVADER_COLS   8
34
+  #undef INVASION_SIZE
35
+  #define INVASION_SIZE  5
36
+#endif
37
+
38
+#define INVADER_ROWS INVASION_SIZE
39
+
40
+#define INVADER_COL_W   14
41
+#define INVADER_H        8
42
+#define INVADER_ROW_H   (INVADER_H + 2)
43
+
44
+typedef struct { int8_t x, y, v; } laser_t;
45
+
46
+typedef struct {
47
+  pos_t pos;
48
+  uint8_t cannons_left;
49
+  int8_t cannon_x;
50
+  laser_t bullet[10], laser, explod;
51
+  int8_t dir, leftmost, rightmost, botmost;
52
+  uint8_t count, quit_count, blink_count;
53
+  uint8_t bugs[INVADER_ROWS], shooters[(INVADER_ROWS) * (INVADER_COLS)];
54
+  int8_t ufox, ufov;
55
+  bool game_blink;
56
+  int8_t laser_col() { return ((laser.x - pos.x) / (INVADER_COL_W)); };
57
+  int8_t laser_row() { return ((laser.y - pos.y + 2) / (INVADER_ROW_H)); };
58
+} invaders_data_t;
59
+
60
+class InvadersGame : MarlinGame { public: static void enter_game(), game_screen(); };
61
+
62
+extern InvadersGame invaders;

+ 30
- 0
Marlin/src/lcd/menu/game/maze.h Voir le fichier

@@ -0,0 +1,30 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include "types.h"
25
+
26
+typedef struct { pos_t pos; } maze_data_t;
27
+
28
+class MazeGame : MarlinGame { public: static void enter_game(), game_screen(); };
29
+
30
+extern MazeGame maze;

+ 52
- 60
Marlin/src/lcd/menu/game/snake.cpp Voir le fichier

@@ -65,20 +65,12 @@
65 65
 
66 66
 constexpr fixed_t snakev = FTOP(0.20);
67 67
 
68
-int8_t snake_dir, // NESW
69
-       foodx, foody, food_cnt,
70
-       old_encoder;
71
-fixed_t snakex, snakey;
72
-
73
-// Up to 50 lines, then you win!
74
-typedef struct { int8_t x, y; } pos_t;
75
-uint8_t head_ind;
76
-pos_t snake_tail[50];
68
+snake_data_t &sdat = marlin_game_data.snake;
77 69
 
78 70
 // Remove the first pixel from the tail.
79 71
 // If needed, shift out the first segment.
80 72
 void shorten_tail() {
81
-  pos_t &p = snake_tail[0], &q = snake_tail[1];
73
+  pos_t &p = sdat.snake_tail[0], &q = sdat.snake_tail[1];
82 74
   bool shift = false;
83 75
   if (p.x == q.x) {
84 76
     // Vertical line
@@ -91,21 +83,21 @@ void shorten_tail() {
91 83
     shift = p.x == q.x;
92 84
   }
93 85
   if (shift) {
94
-    head_ind--;
95
-    for (uint8_t i = 0; i <= head_ind; ++i)
96
-      snake_tail[i] = snake_tail[i + 1];
86
+    sdat.head_ind--;
87
+    for (uint8_t i = 0; i <= sdat.head_ind; ++i)
88
+      sdat.snake_tail[i] = sdat.snake_tail[i + 1];
97 89
   }
98 90
 }
99 91
 
100 92
 // The food is on a line
101 93
 inline bool food_on_line() {
102
-  for (uint8_t n = 0; n < head_ind; ++n) {
103
-    pos_t &p = snake_tail[n], &q = snake_tail[n + 1];
94
+  for (uint8_t n = 0; n < sdat.head_ind; ++n) {
95
+    pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
104 96
     if (p.x == q.x) {
105
-      if ((foodx == p.x - 1 || foodx == p.x) && WITHIN(foody, _MIN(p.y, q.y), _MAX(p.y, q.y)))
97
+      if ((sdat.foodx == p.x - 1 || sdat.foodx == p.x) && WITHIN(sdat.foody, _MIN(p.y, q.y), _MAX(p.y, q.y)))
106 98
         return true;
107 99
     }
108
-    else if ((foody == p.y - 1 || foody == p.y) && WITHIN(foodx, _MIN(p.x, q.x), _MAX(p.x, q.x)))
100
+    else if ((sdat.foody == p.y - 1 || sdat.foody == p.y) && WITHIN(sdat.foodx, _MIN(p.x, q.x), _MAX(p.x, q.x)))
109 101
       return true;
110 102
   }
111 103
   return false;
@@ -114,54 +106,54 @@ inline bool food_on_line() {
114 106
 // Add a new food blob
115 107
 void food_reset() {
116 108
   do {
117
-    foodx = random(0, GAME_W);
118
-    foody = random(0, GAME_H);
109
+    sdat.foodx = random(0, GAME_W);
110
+    sdat.foody = random(0, GAME_H);
119 111
   } while (food_on_line());
120 112
 }
121 113
 
122 114
 // Turn the snake cw or ccw
123 115
 inline void turn_snake(const bool cw) {
124
-  snake_dir += cw ? 1 : -1;
125
-  snake_dir &= 0x03;
126
-  head_ind++;
127
-  snake_tail[head_ind].x = FTOB(snakex);
128
-  snake_tail[head_ind].y = FTOB(snakey);
116
+  sdat.snake_dir += cw ? 1 : -1;
117
+  sdat.snake_dir &= 0x03;
118
+  sdat.head_ind++;
119
+  sdat.snake_tail[sdat.head_ind].x = FTOB(sdat.snakex);
120
+  sdat.snake_tail[sdat.head_ind].y = FTOB(sdat.snakey);
129 121
 }
130 122
 
131 123
 // Reset the snake for a new game
132 124
 void snake_reset() {
133 125
   // Init the head and velocity
134
-  snakex = BTOF(1);
135
-  snakey = BTOF(GAME_H / 2);
126
+  sdat.snakex = BTOF(1);
127
+  sdat.snakey = BTOF(GAME_H / 2);
136 128
   //snakev = FTOP(0.25);
137 129
 
138 130
   // Init the tail with a cw turn
139
-  snake_dir = 0;
140
-  head_ind = 0;
141
-  snake_tail[0].x = 0;
142
-  snake_tail[0].y = GAME_H / 2;
131
+  sdat.snake_dir = 0;
132
+  sdat.head_ind = 0;
133
+  sdat.snake_tail[0].x = 0;
134
+  sdat.snake_tail[0].y = GAME_H / 2;
143 135
   turn_snake(true);
144 136
 
145 137
   // Clear food flag
146
-  food_cnt = 5;
138
+  sdat.food_cnt = 5;
147 139
 
148 140
   // Clear the controls
149 141
   ui.encoderPosition = 0;
150
-  old_encoder = 0;
142
+  sdat.old_encoder = 0;
151 143
 }
152 144
 
153 145
 // Check if head segment overlaps another
154 146
 bool snake_overlap() {
155 147
   // 4 lines must exist before a collision is possible
156
-  if (head_ind < 4) return false;
148
+  if (sdat.head_ind < 4) return false;
157 149
   // Is the last segment crossing any others?
158
-  const pos_t &h1 = snake_tail[head_ind - 1], &h2 = snake_tail[head_ind];
150
+  const pos_t &h1 = sdat.snake_tail[sdat.head_ind - 1], &h2 = sdat.snake_tail[sdat.head_ind];
159 151
   // VERTICAL head segment?
160 152
   if (h1.x == h2.x) {
161 153
     // Loop from oldest to segment two away from head
162
-    for (uint8_t n = 0; n < head_ind - 2; ++n) {
154
+    for (uint8_t n = 0; n < sdat.head_ind - 2; ++n) {
163 155
       // Segment p to q
164
-      const pos_t &p = snake_tail[n], &q = snake_tail[n + 1];
156
+      const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
165 157
       if (p.x != q.x) {
166 158
         // Crossing horizontal segment
167 159
         if (WITHIN(h1.x, _MIN(p.x, q.x), _MAX(p.x, q.x)) && (h1.y <= p.y) == (h2.y >= p.y)) return true;
@@ -171,9 +163,9 @@ bool snake_overlap() {
171 163
   }
172 164
   else {
173 165
     // Loop from oldest to segment two away from head
174
-    for (uint8_t n = 0; n < head_ind - 2; ++n) {
166
+    for (uint8_t n = 0; n < sdat.head_ind - 2; ++n) {
175 167
       // Segment p to q
176
-      const pos_t &p = snake_tail[n], &q = snake_tail[n + 1];
168
+      const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
177 169
       if (p.y != q.y) {
178 170
         // Crossing vertical segment
179 171
         if (WITHIN(h1.y, _MIN(p.y, q.y), _MAX(p.y, q.y)) && (h1.x <= p.x) == (h2.x >= p.x)) return true;
@@ -189,14 +181,14 @@ void SnakeGame::game_screen() {
189 181
   if (game_frame()) do {    // Run logic twice for finer resolution
190 182
 
191 183
     // Move the snake's head one unit in the current direction
192
-    const int8_t oldx = FTOB(snakex), oldy = FTOB(snakey);
193
-    switch (snake_dir) {
194
-      case 0: snakey -= snakev; break;
195
-      case 1: snakex += snakev; break;
196
-      case 2: snakey += snakev; break;
197
-      case 3: snakex -= snakev; break;
184
+    const int8_t oldx = FTOB(sdat.snakex), oldy = FTOB(sdat.snakey);
185
+    switch (sdat.snake_dir) {
186
+      case 0: sdat.snakey -= snakev; break;
187
+      case 1: sdat.snakex += snakev; break;
188
+      case 2: sdat.snakey += snakev; break;
189
+      case 3: sdat.snakex -= snakev; break;
198 190
     }
199
-    const int8_t x = FTOB(snakex), y = FTOB(snakey);
191
+    const int8_t x = FTOB(sdat.snakex), y = FTOB(sdat.snakey);
200 192
 
201 193
     // If movement took place...
202 194
     if (oldx != x || oldy != y) {
@@ -207,17 +199,17 @@ void SnakeGame::game_screen() {
207 199
         break;          // ...out of do-while
208 200
       }
209 201
 
210
-      snake_tail[head_ind].x = x;
211
-      snake_tail[head_ind].y = y;
202
+      sdat.snake_tail[sdat.head_ind].x = x;
203
+      sdat.snake_tail[sdat.head_ind].y = y;
212 204
 
213 205
       // Change snake direction if set
214
-      const int8_t enc = int8_t(ui.encoderPosition), diff = enc - old_encoder;
206
+      const int8_t enc = int8_t(ui.encoderPosition), diff = enc - sdat.old_encoder;
215 207
       if (diff) {
216
-        old_encoder = enc;
208
+        sdat.old_encoder = enc;
217 209
         turn_snake(diff > 0);
218 210
       }
219 211
 
220
-      if (food_cnt) --food_cnt; else shorten_tail();
212
+      if (sdat.food_cnt) --sdat.food_cnt; else shorten_tail();
221 213
 
222 214
       // Did the snake collide with itself or go out of bounds?
223 215
       if (snake_overlap()) {
@@ -225,11 +217,11 @@ void SnakeGame::game_screen() {
225 217
         _BUZZ(400, 40); // Bzzzt!
226 218
       }
227 219
       // Is the snake at the food?
228
-      else if (x == foodx && y == foody) {
220
+      else if (x == sdat.foodx && y == sdat.foody) {
229 221
         _BUZZ(5, 220);
230 222
         _BUZZ(5, 280);
231 223
         score++;
232
-        food_cnt = 2;
224
+        sdat.food_cnt = 2;
233 225
         food_reset();
234 226
       }
235 227
     }
@@ -251,8 +243,8 @@ void SnakeGame::game_screen() {
251 243
   #if SNAKE_WH < 2
252 244
 
253 245
     // At this scale just draw a line
254
-    for (uint8_t n = 0; n < head_ind; ++n) {
255
-      const pos_t &p = snake_tail[n], &q = snake_tail[n + 1];
246
+    for (uint8_t n = 0; n < sdat.head_ind; ++n) {
247
+      const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
256 248
       if (p.x == q.x) {
257 249
         const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
258 250
         if (PAGE_CONTAINS(y1, y2))
@@ -267,8 +259,8 @@ void SnakeGame::game_screen() {
267 259
   #elif SNAKE_WH == 2
268 260
 
269 261
     // At this scale draw two lines
270
-    for (uint8_t n = 0; n < head_ind; ++n) {
271
-      const pos_t &p = snake_tail[n], &q = snake_tail[n + 1];
262
+    for (uint8_t n = 0; n < sdat.head_ind; ++n) {
263
+      const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
272 264
       if (p.x == q.x) {
273 265
         const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
274 266
         if (PAGE_CONTAINS(y1, y2 + 1))
@@ -286,8 +278,8 @@ void SnakeGame::game_screen() {
286 278
   #else
287 279
 
288 280
     // Draw a series of boxes
289
-    for (uint8_t n = 0; n < head_ind; ++n) {
290
-      const pos_t &p = snake_tail[n], &q = snake_tail[n + 1];
281
+    for (uint8_t n = 0; n < sdat.head_ind; ++n) {
282
+      const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
291 283
       if (p.x == q.x) {
292 284
         const int8_t y1 = _MIN(p.y, q.y), y2 = _MAX(p.y, q.y);
293 285
         if (PAGE_CONTAINS(GAMEY(y1), GAMEY(y2) + SNAKE_SIZ - 1)) {
@@ -311,9 +303,9 @@ void SnakeGame::game_screen() {
311 303
   #endif
312 304
 
313 305
   // Draw food
314
-  const int8_t fy = GAMEY(foody);
306
+  const int8_t fy = GAMEY(sdat.foody);
315 307
   if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) {
316
-    const int8_t fx = GAMEX(foodx);
308
+    const int8_t fx = GAMEX(sdat.foodx);
317 309
     u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH);
318 310
     if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2);
319 311
   }

+ 38
- 0
Marlin/src/lcd/menu/game/snake.h Voir le fichier

@@ -0,0 +1,38 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include "types.h"
25
+
26
+typedef struct {
27
+  int8_t snake_dir,     // NESW
28
+         foodx, foody,
29
+         food_cnt,
30
+         old_encoder;
31
+  pos_t snake_tail[50];
32
+  fixed_t snakex, snakey;
33
+  uint8_t head_ind;
34
+} snake_data_t;
35
+
36
+class SnakeGame : MarlinGame { public: static void enter_game(), game_screen(); };
37
+
38
+extern SnakeGame snake;

+ 46
- 0
Marlin/src/lcd/menu/game/types.h Voir le fichier

@@ -0,0 +1,46 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include <stdint.h>
25
+
26
+typedef struct { int8_t x, y; } pos_t;
27
+
28
+// Simple 8:8 fixed-point
29
+typedef int16_t fixed_t;
30
+#define FTOP(F) fixed_t((F)*256.0f)
31
+#define PTOF(P) (float(P)*(1.0f/256.0f))
32
+#define BTOF(X) (fixed_t(X)<<8)
33
+#define FTOB(X) int8_t(fixed_t(X)>>8)
34
+
35
+class MarlinGame {
36
+protected:
37
+  static int score;
38
+  static uint8_t game_state;
39
+  static millis_t next_frame;
40
+
41
+  static bool game_frame();
42
+  static void draw_game_over();
43
+  static void exit_game();
44
+public:
45
+  static void init_game(const uint8_t init_state, const screenFunc_t screen);
46
+};

+ 5
- 2
buildroot/share/tests/megaatmega2560-tests Voir le fichier

@@ -319,8 +319,11 @@ opt_set Z_DRIVER_TYPE TMC2208
319 319
 opt_set E0_DRIVER_TYPE TMC2130
320 320
 opt_set X_MIN_ENDSTOP_INVERTING true
321 321
 opt_set Y_MIN_ENDSTOP_INVERTING true
322
-opt_enable REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER MONITOR_DRIVER_STATUS STEALTHCHOP_XY STEALTHCHOP_Z STEALTHCHOP_E HYBRID_THRESHOLD USE_ZMIN_PLUG SENSORLESS_HOMING TMC_DEBUG
323
-exec_test $1 $2 "Mixed TMC configuration"
322
+opt_enable REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER \
323
+           MARLIN_BRICKOUT MARLIN_INVADERS MARLIN_SNAKE \
324
+           MONITOR_DRIVER_STATUS STEALTHCHOP_XY STEALTHCHOP_Z STEALTHCHOP_E HYBRID_THRESHOLD \
325
+           USE_ZMIN_PLUG SENSORLESS_HOMING TMC_DEBUG
326
+exec_test $1 $2 "Mixed TMC configuration, with games!"
324 327
 
325 328
 #
326 329
 # tvrrug Config need to check board type for sanguino atmega644p

Chargement…
Annuler
Enregistrer