-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
73 lines (65 loc) · 1.77 KB
/
render.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <SDL2/SDL.h>
#include "./game.h"
#include "./render.h"
#include "./logic.h"
void render_board(SDL_Renderer* renderer, board_t* board,
int neighbors[COL_NUM][ROW_NUM])
{
switch(board->game_state) {
case RUNNING_STATE:
render_running_state(renderer, board);
count_neighbors(board, neighbors);
evolve(board, neighbors);
break;
case PAUSE_STATE:
render_pause_state(renderer, board);
break;
default: {}
}
}
void render_running_state(SDL_Renderer *renderer, board_t *board)
{
int pos_x = 0;
int pos_y = 0;
for (int i = 0; i < COL_NUM; i++) {
for (int j = 0; j < ROW_NUM; j++) {
pos_x = i * board->CELL_WIDTH;
pos_y = j * board->CELL_HEIGHT;
if (board->cell_state[i][j] == ALIVE)
render_square(renderer, pos_x, pos_y, board);
}
}
}
void render_pause_state(SDL_Renderer *renderer, board_t *board)
{
int pos_x = 0;
int pos_y = 0;
for (int i = 0; i < COL_NUM; i++) {
for (int j = 0; j < ROW_NUM; j++) {
pos_x = i * board->CELL_WIDTH;
pos_y = j * board->CELL_HEIGHT;
if (board->cell_state[i][j] == ALIVE)
pause_square(renderer, pos_x, pos_y, board);
}
}
}
void render_square(SDL_Renderer *renderer, int pos_x, int pos_y, board_t* board)
{
SDL_Rect cell;
cell.x = pos_x;
cell.y = pos_y;
cell.w = board->CELL_WIDTH - 1;
cell.h = board->CELL_HEIGHT - 1;
SDL_SetRenderDrawColor(renderer, 142, 192, 124, 1);
SDL_RenderFillRect(renderer, &cell);
}
void pause_square(SDL_Renderer *renderer, int pos_x, int pos_y, board_t* board)
{
SDL_Rect cell;
cell.x = pos_x;
cell.y = pos_y;
cell.w = board->CELL_WIDTH - 1;
cell.h = board->CELL_HEIGHT - 1;
SDL_SetRenderDrawColor(renderer, 146, 131, 116, 1);
SDL_RenderFillRect(renderer, &cell);
}