-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.cpp
46 lines (38 loc) · 1.51 KB
/
graphics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef NO_SDL
#include "graphics.h"
const unsigned COLOURS[5] = BLUE;
void init_draw(SDL_Renderer **renderer_ptr, SDL_Window **window_ptr, unsigned win_w, unsigned win_h, unsigned cell_size) {
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(win_w * cell_size, win_h * cell_size, 0, window_ptr, renderer_ptr);
SDL_SetRenderDrawColor(*renderer_ptr,
COLOURS[0] >> 16 & 0xFF,
COLOURS[0] >> 4 & 0xFF,
COLOURS[0] & 0xFF, 255);
SDL_RenderClear(*renderer_ptr);
SDL_RenderPresent(*renderer_ptr);
}
void draw_matrix(SDL_Renderer *renderer, unsigned *field, unsigned H, unsigned W, unsigned cell_size) {
SDL_Rect rect = {.x = 0, .y = 0, .w = (int)cell_size, .h = (int)cell_size};
for (unsigned i = 0; i < H; i++) {
for (unsigned j = 0; j < W; j++) {
unsigned hex;
if (field[i * W + j] <= ALIVE)
hex = COLOURS[field[i * W + j]];
SDL_SetRenderDrawColor(renderer,
hex >> 16 & 0xFF,
hex >> 8 & 0xFF,
hex & 0xFF,
255);
rect.x = j * cell_size;
rect.y = i * cell_size;
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_RenderPresent(renderer);
}
void exit_draw(SDL_Renderer *renderer, SDL_Window *window) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
#endif