implement setting coloured shapes

This commit is contained in:
2025-01-20 00:27:01 +01:00
parent 2933f5bdd8
commit 34fd24b01f
5 changed files with 86 additions and 51 deletions

View File

@@ -26,16 +26,6 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
return 0;
}
static inline int set_block(SDL_Renderer* renderer, const Row row, const uint8_t block, const uint8_t filter, const uint8_t x, const uint8_t y) {
uint8_t colour = block & filter;
return SDL_SetRenderDrawColor(renderer,
0xFF * (0 != (colour & (RED | RED << 3))),
0xFF * (0 != (colour & (GREEN | GREEN << 3))),
0xFF * (0 != (colour & (BLUE | BLUE << 3))),
0xFF) |
SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
}
void renderer_update(const RenderData* render_data) {
SDL_Renderer* renderer = render_data->renderer;
GameData* data = render_data->game_data;
@@ -43,20 +33,21 @@ void renderer_update(const RenderData* render_data) {
int success = 0; // if an error occurs, this value is <0
// clear render
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x50);
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
success |= SDL_RenderClear(renderer);
for (uint8_t y = 0; y < ROWS; y++) {
Row row = data->row[y];
for (uint8_t x = 0; x < COLUMNS; x += 2) {
uint8_t block = row >> (x * 3); // get the two blocks stored at this column
if ((block & 0x0F) != 0)
success |= set_block(renderer, row, block, 0x0F, x + 1, y);
if ((block & 0xF0) != 0)
success |= set_block(renderer, row, block, 0xF0, x, y);
for (uint8_t x = 0; x < COLUMNS; x++) {
if (row.columns[x].block != 0) {
success |= SDL_SetRenderDrawColor(renderer,
0xFF * !!(row.columns[x].block & RED),
0xFF * !!(row.columns[x].block & GREEN),
0xFF * !!(row.columns[x].block & BLUE),
0xFF);
success |= SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
}
}
}

View File

@@ -6,7 +6,7 @@
#define SCREEN_WIDTH 256
#define SCREEN_HEIGHT (SCREEN_WIDTH * 2)
#define BLOCK_WIDTH (SCREEN_WIDTH / COLUMNS)
#define BLOCK_HEIGHT (SCREEN_HEIGHT / (BLOCK_WIDTH * 2))
#define BLOCK_HEIGHT (SCREEN_HEIGHT / ROWS)
// fits colours in a 3 bit length
// 4th bit is unused