Skip to content

Commit 1d31b3f

Browse files
authored
minor cleanup of game renderer code
1 parent 7e46c9f commit 1d31b3f

File tree

6 files changed

+85
-95
lines changed

6 files changed

+85
-95
lines changed

Marlin/src/lcd/e3v2/common/dwin_api.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,51 +188,48 @@ void dwinFrameClear(const uint16_t color) {
188188
const uint16_t map_columns,
189189
const uint16_t map_rows,
190190
const uint8_t *map_data) {
191-
// how many bytes can we write to the send buffer?
192-
// one byte is used for F_HONE, so we can write up to len(dwinSendBuf) - 1 bytes.
193-
constexpr size_t send_buffer_size = (COUNT(dwinSendBuf) - 1);
194-
// at how many bytes should we flush the send buffer?
195-
// one byte is used (hidden) for F_HONE, and we need 4 bytes when appending a point.
196-
// so we should flush the send buffer when we have less than 5 bytes left.
191+
// At how many bytes should we flush the send buffer?
192+
// One byte is used (hidden) for F_HONE, and we need 4 bytes when appending a point.
193+
// So we should flush the send buffer when we have less than 5 bytes left.
197194
constexpr size_t flush_send_buffer_at = (COUNT(dwinSendBuf) - 1 - 4);
198195

199-
// how long is the header of each draw command?
196+
// How long is the header of each draw command?
200197
// 1B CMD, 2B COLOR, 1B WIDTH, 1B HEIGHT
201198
constexpr size_t command_header_size = 5;
202199

203-
// draw the point map
200+
// Draw the point map
204201
size_t i = 0;
205202
for (uint16_t row = 0; row < map_rows; row++) {
206203
for (uint16_t col = 0; col < map_columns; col++) {
207204
const uint8_t map_byte = map_data[(row * map_columns) + col];
208205
for (uint8_t bit = 0; bit < 8; bit++) {
209-
// draw the bit of the byte if it's set
206+
// Draw the bit of the byte if it's set
210207
if (TEST(map_byte, bit)) {
211-
// flush the send buffer and prepare next draw if either
212-
// a) the buffer reached the 'should flush' state, or
213-
// b) this is the first point to draw
208+
// Flush the send buffer and prepare next draw if either
209+
// a) The buffer reached the 'should flush' state, or
210+
// b) This is the first point to draw
214211
if (i >= flush_send_buffer_at || i == 0)
215212
{
216-
// dispatch the current draw command
213+
// Dispatch the current draw command
217214
if (i > command_header_size) dwinSend(i);
218215

219-
// prepare the next draw command
216+
// Prepare the next draw command
220217
i = 0;
221218
dwinByte(i, 0x02); // cmd: draw point(s)
222219
dwinWord(i, color);
223220
dwinByte(i, point_width);
224221
dwinByte(i, point_height);
225222
}
226223

227-
// append point coordinates to draw command
224+
// Append point coordinates to draw command
228225
dwinWord(i, x + (point_width * ((8 * col) + (7 - bit)))); // x
229226
dwinWord(i, y + (point_height * (row))); // y
230227
}
231228
}
232229
}
233230
}
234231

235-
// dispatch final draw command if the buffer contains any points
232+
// Dispatch final draw command if the buffer contains any points
236233
if (i > command_header_size) dwinSend(i);
237234
}
238235
#endif

Marlin/src/lcd/e3v2/marlinui/game.cpp

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,68 @@
22

33
#if IS_DWIN_MARLINUI && HAS_GAMES
44

5-
#define PERFORMANCE_COUNTERS 1
5+
// Show performance counters on the screen (frame timing and draw call count)
6+
#define PERFORMANCE_COUNTERS 0
7+
8+
// Compound calls are calls that are made up of multiple subcalls (e.g. draw_hline, which is made up of a draw_box call)
9+
#define INCLUDE_COMPOUNT_CALLS 0
610

711
#include "../../menu/game/types.h" // includes e3v2/marlinui/game.h
812
#include "../../lcdprint.h"
913
#include "lcdprint_dwin.h"
1014
#include "marlinui_dwin.h"
1115

12-
#if PERFORMANCE_COUNTERS == 1
13-
static uint32_t draw_call_cnt = 0; // total number of draw calls in the current frame
14-
static millis_t frame_draw_millis = 0, // time spent drawing the frame
15-
frame_wait_millis = 0; // time spent waiting for the next frame
16+
#if ENABLED(PERFORMANCE_COUNTERS)
17+
static uint32_t draw_call_cnt = 0; // Total number of draw calls in the current frame
18+
static millis_t frame_draw_millis = 0, // Time spent drawing the frame
19+
frame_wait_millis = 0; // Time spent waiting for the next frame
1620

17-
#define DRAW_CALL_INC(subcall_cnt) draw_call_cnt++
21+
#define COUNT_DRAW_CALL(compound_call_count) TERN(INCLUDE_COMPOUNT_CALLS, draw_call_cnt++, draw_call_cnt = draw_call_cnt + 1 - compound_call_count)
1822
#else
19-
#define DRAW_CALL_INC(subcall_cnt)
23+
#define COUNT_DRAW_CALL(compound_call_count)
2024
#endif
2125

2226
void MarlinGame::frame_start() {
23-
// clear the screen before each frame
27+
// Clear the screen before each frame
2428
//dwinFrameClear(CLEAR_COLOR);
2529

26-
// filling the play area should be faster than clearing the whole screen
30+
// Filling the play area is faster than clearing the whole screen
2731
const uint16_t fg = dwin_font.fg;
2832
dwin_font.fg = COLOR_BG_BLACK;
2933
draw_box(0, 0, GAME_WIDTH, GAME_HEIGHT);
3034
dwin_font.fg = fg;
3135

36+
// Ensure the correct font is selected
3237
dwin_font.index = DWIN_FONT_MENU;
3338

34-
// reset performance counters
35-
#if PERFORMANCE_COUNTERS == 1
39+
// Reset the performance counters
40+
#if ENABLED(PERFORMANCE_COUNTERS)
3641
draw_call_cnt = 0;
3742
frame_draw_millis = millis();
38-
frame_wait_millis = frame_draw_millis - frame_wait_millis;
43+
frame_wait_millis = frame_draw_millis - frame_wait_millis;
3944
#endif
4045
}
4146

4247
void MarlinGame::frame_end() {
43-
#if PERFORMANCE_COUNTERS == 1
48+
#if ENABLED(PERFORMANCE_COUNTERS)
4449
const millis_t frame_wait = frame_wait_millis;
4550
frame_wait_millis = millis();
4651
frame_draw_millis = frame_wait_millis - frame_draw_millis;
4752

48-
// calculate frames per deci-seconds (100 milliseconds)
49-
const uint32_t fpds = 100 / (frame_draw_millis + frame_wait);
50-
51-
// format the performance counters as a string
53+
// Format the performance counters as a string
5254
char perf_str[64];
5355
sprintf_P(
5456
perf_str,
55-
PSTR("d%04lu w%04lu c%04lu f%02lu "),
57+
PSTR("d%04lu w%04lu c%04lu "),
5658
frame_draw_millis,
5759
frame_wait,
58-
draw_call_cnt,
59-
fpds
60+
draw_call_cnt
6061
);
6162

62-
// draw the performance counters at the (physical) origin of the screen
63+
// Draw the performance counters at the (physical) origin of the screen
6364
const uint16_t fg = dwin_font.fg;
6465
const bool solid = dwin_font.solid;
65-
dwin_font.fg = RGB(0x1F, 0x3F, 0x00);
66+
set_color(color::YELLOW);
6667
dwin_font.solid = true;
6768

6869
lcd_moveto_xy(0, 0);
@@ -107,17 +108,17 @@ void MarlinGame::set_color(const color color) {
107108
}
108109

109110
void MarlinGame::draw_hline(const game_dim_t x, const game_dim_t y, const game_dim_t w) {
110-
// draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
111+
// Draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
111112
draw_box(x, y, w, 1);
112113

113-
DRAW_CALL_INC(1);
114+
COUNT_DRAW_CALL(1);
114115
}
115116

116117
void MarlinGame::draw_vline(const game_dim_t x, const game_dim_t y, const game_dim_t h) {
117-
// draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
118+
// Draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
118119
draw_box(x, y, 1, h);
119120

120-
DRAW_CALL_INC(1);
121+
COUNT_DRAW_CALL(1);
121122
}
122123

123124
void MarlinGame::draw_frame(const game_dim_t x, const game_dim_t y, const game_dim_t w, const game_dim_t h) {
@@ -130,7 +131,7 @@ void MarlinGame::draw_frame(const game_dim_t x, const game_dim_t y, const game_d
130131
dwin_game::game_to_screen(h)
131132
);
132133

133-
DRAW_CALL_INC(0);
134+
COUNT_DRAW_CALL(0);
134135
}
135136

136137
void MarlinGame::draw_box(const game_dim_t x, const game_dim_t y, const game_dim_t w, const game_dim_t h) {
@@ -143,17 +144,17 @@ void MarlinGame::draw_box(const game_dim_t x, const game_dim_t y, const game_dim
143144
dwin_game::game_to_screen(h)
144145
);
145146

146-
DRAW_CALL_INC(0);
147+
COUNT_DRAW_CALL(0);
147148
}
148149

149150
void MarlinGame::draw_pixel(const game_dim_t x, const game_dim_t y) {
150-
// draw pixels using boxes.
151-
// while DWIN protocol supports drawing points with different sizes, the
152-
// 0x02 'draw point' command is way slower per pixel than 0x05 'fill rectangle'
151+
// Draw pixels using boxes.
152+
// While DWIN protocol supports drawing points with different sizes, the
153+
// 0x02 'draw point' command is slower per pixel than 0x05 'fill rectangle'
153154
// (0.4 us vs 0.14 us per pixel)
154155
draw_box(x, y, 1, 1);
155156

156-
DRAW_CALL_INC(1);
157+
COUNT_DRAW_CALL(1);
157158
}
158159

159160
void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_dim_t bytes_per_row, const game_dim_t rows, const pgm_bitmap_t bitmap) {
@@ -163,20 +164,19 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
163164

164165
#if DISABLED(TJC_DISPLAY)
165166
// DWIN T5UI actually supports drawing multiple points in one go using the 0x02 'draw point' command, ever since kernel 1.2.
166-
// So we use that to draw the bitmap as a series of points, which should be faster than drawing rectangles using draw_pixel.
167-
// This will be somewhat slow, but way faster than drawing rectangles one by one.
167+
// So we use that to draw the bitmap as a series of points, which is faster than drawing rectangles using draw_pixel.
168168
dwinDrawPointMap(
169-
dwin_font.fg, // color
170-
dwin_game::game_to_screen(1), // point size
169+
dwin_font.fg,
170+
dwin_game::game_to_screen(1),
171171
dwin_game::game_to_screen(1),
172-
dwin_game::game_to_screen(x) + dwin_game::x_offset, // x / y
172+
dwin_game::game_to_screen(x) + dwin_game::x_offset,
173173
dwin_game::game_to_screen(y) + dwin_game::y_offset,
174-
bytes_per_row, // bitmap dimensions
174+
bytes_per_row,
175175
rows,
176-
bitmap // U8G bitmap format is compatible to DrawPointMap format
176+
bitmap
177177
);
178178

179-
DRAW_CALL_INC(0);
179+
COUNT_DRAW_CALL(0);
180180
#else
181181
// TJC displays don't seem to support the 0x02 'draw point' command, so instead we have to draw the bitmap
182182
// as a series of rectangles using draw_pixel.
@@ -185,10 +185,10 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
185185
for (game_dim_t col = 0; col < bytes_per_row; col++) {
186186
const uint8_t byte = bitmap[(row * bytes_per_row) + col];
187187
for (uint8_t bit = 0; bit < 8; bit++) {
188-
// assume that the screen area is cleared before drawing
188+
// Assuming that the drawing area was cleared before drawing
189189
if (byte & (1 << bit)) {
190190
draw_pixel(x + (col * 8) + (7 - bit + 1), y + row);
191-
DRAW_CALL_INC(1);
191+
COUNT_DRAW_CALL(1);
192192
}
193193
}
194194
}
@@ -197,7 +197,7 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
197197
}
198198

199199
int MarlinGame::draw_string(const game_dim_t x, const game_dim_t y, const char* str) {
200-
DRAW_CALL_INC(0);
200+
COUNT_DRAW_CALL(0);
201201

202202
lcd_moveto_xy(
203203
dwin_game::game_to_screen(x) + dwin_game::x_offset,

Marlin/src/lcd/e3v2/marlinui/game.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ namespace dwin_game {
1515

1616
constexpr int calculate_scale()
1717
{
18-
// use whichever is smaller: the width or height scaling factor
18+
// Use whichever is smaller: the width or height scaling factor
1919
float scaling_factor = _MIN(
2020
static_cast<float>(DWIN_WIDTH) / static_cast<float>(TARGET_WIDTH),
2121
static_cast<float>(DWIN_HEIGHT) / static_cast<float>(TARGET_HEIGHT)
2222
);
2323

24-
// round DOWN to closest integer
24+
// Round DOWN to closest integer
2525
return static_cast<int>(scaling_factor);
2626
}
2727

@@ -31,14 +31,14 @@ namespace dwin_game {
3131
constexpr int scale = calculate_scale();
3232

3333
/**
34-
* @brief scale a game dimension to screen dimensions
34+
* @brief Scale a game dimension to screen dimensions
3535
*/
3636
constexpr game_dim_t screen_to_game(const screen_dim_t x) {
3737
return x / scale;
3838
}
3939

4040
/**
41-
* @brief scale a screen dimension to game dimensions
41+
* @brief Scale a screen dimension to game dimensions
4242
*/
4343
constexpr screen_dim_t game_to_screen(const game_dim_t x) {
4444
return x * scale;
@@ -59,7 +59,7 @@ constexpr game_dim_t GAME_HEIGHT = dwin_game::screen_to_game(DWIN_HEIGHT - (dwin
5959
constexpr game_dim_t GAME_FONT_WIDTH = dwin_game::screen_to_game(MENU_FONT_WIDTH);
6060
constexpr game_dim_t GAME_FONT_ASCENT = dwin_game::screen_to_game(MENU_FONT_ASCENT);
6161

62-
// not needed on DWIN
62+
// DWIN screens don't page, so these macros are always true
6363
#define PAGE_OVER(ya) true
6464
#define PAGE_UNDER(yb) true
6565
#define PAGE_CONTAINS(ya, yb) true

Marlin/src/lcd/menu/game/brickout.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,25 +144,21 @@ void BrickoutGame::game_screen() {
144144
const uint8_t yy = y * BRICK_H + BRICK_TOP;
145145
if (PAGE_CONTAINS(yy, yy + BRICK_H - 1)) {
146146
for (uint8_t x = 0; x < BRICK_COLS; ++x) {
147-
// cycle through colors, even if the brick is gone
148-
// otherwise, bricks would change color if their neighbor is hit
147+
// Cycle through colors, even if the brick is gone.
148+
// Otherwise, bricks would change color if their neighbor is hit
149149
set_color(brick_colors[color_index++ % COUNT(brick_colors)]);
150150

151-
// draw brick if it's still there
151+
// Draw brick if it's still there
152152
if (TEST(bdat.bricks[y], x)) {
153153
const uint8_t xx = x * BRICK_W;
154154
draw_box(xx, yy, BRICK_W - 1, BRICK_H - 1);
155-
156-
//for (uint8_t v = 0; v < BRICK_H - 1; ++v)
157-
// if (PAGE_CONTAINS(yy + v, yy + v))
158-
// draw_hline(xx, yy + v, BRICK_W - 1);
159155
}
160156
}
161157
}
162158
}
163159
}
164160

165-
// everything else is white
161+
// Everything else is white
166162
set_color(color::WHITE);
167163

168164
// Draw paddle

Marlin/src/lcd/menu/game/invaders.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151

5252
#define INVADER_COLOR { MarlinGame::color::GREEN, MarlinGame::color::CYAN, MarlinGame::color::YELLOW }
5353
#define CANNON_COLOR MarlinGame::color::WHITE
54-
#define LASER_COLOR MarlinGame::color::WHITE // shot by player
55-
#define BULLET_COLOR LASER_COLOR // shot by invader
54+
#define LASER_COLOR MarlinGame::color::WHITE // Shot by player
55+
#define BULLET_COLOR LASER_COLOR // Shot by invader
5656
#define LIFE_COLOR CANNON_COLOR
5757
#define UFO_COLOR MarlinGame::color::MAGENTA
5858
#define EXPLOSION_COLOR MarlinGame::color::RED
@@ -386,8 +386,8 @@ void InvadersGame::game_screen() {
386386
int8_t xx = idat.pos.x;
387387
for (uint8_t x = 0; x < INVADER_COLS; ++x) {
388388
if (TEST(idat.bugs[y], x)) {
389-
constexpr color type_color[] = INVADER_COLOR;
390-
set_color(type_color[type]);
389+
constexpr color invader_color[] = INVADER_COLOR;
390+
set_color(invader_color[type]);
391391
draw_bitmap(xx, yy, 2, INVADER_H, invader[type][idat.game_blink]);
392392
}
393393
xx += INVADER_COL_W;
@@ -433,22 +433,20 @@ void InvadersGame::game_screen() {
433433
set_color(color::WHITE);
434434

435435
// Blink GAME OVER when game is over
436-
if (!game_state) {
437-
draw_game_over();
438-
}
436+
if (!game_state) draw_game_over();
439437

440438
if (PAGE_UNDER(GAME_FONT_ASCENT - 1)) {
441-
// Draw Score
442-
//const uint8_t sx = (GAME_WIDTH - (score >= 10 ? score >= 100 ? score >= 1000 ? 4 : 3 : 2 : 1) * GAME_FONT_WIDTH) / 2;
443-
constexpr uint8_t sx = 0;
444-
draw_int(sx, GAME_FONT_ASCENT - 1, score);
445-
446439
// Draw lives
447440
if (idat.cannons_left)
448441
for (uint8_t i = 1; i <= idat.cannons_left; ++i) {
449442
set_color(LIFE_COLOR);
450443
draw_bitmap(GAME_WIDTH - i * (LIFE_W), 6 - (LIFE_H), 1, LIFE_H, life);
451444
}
445+
446+
// Draw Score
447+
//const uint8_t sx = (GAME_WIDTH - (score >= 10 ? score >= 100 ? score >= 1000 ? 4 : 3 : 2 : 1) * GAME_FONT_WIDTH) / 2;
448+
constexpr uint8_t sx = 0;
449+
draw_int(sx, GAME_FONT_ASCENT - 1, score);
452450
}
453451

454452
frame_end();

0 commit comments

Comments
 (0)