-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDL_Maze_Renderer.h
140 lines (125 loc) · 4.21 KB
/
SDL_Maze_Renderer.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef MAZE_SDL_MAZE_RENDERER_H
#define MAZE_SDL_MAZE_RENDERER_H
#include <SDL2/SDL.h>
#include "Maze.h"
void render_maze_to_sdl(SDL_Renderer *renderer, const Maze *maze, int cell_size);
int render_maze_with_refresh(
const Maze *maze,
int cell_size,
void (*maze_generator)(const Maze *)
) {
if (maze == NULL || cell_size < 1 || maze_generator == NULL) {
fprintf(stderr, "Invalid arguments for rendering");
return 1;
}
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Failed to init video: %s\n", SDL_GetError());
return 1;
}
const int window_width = maze->width * cell_size;
const int window_height = maze->height * cell_size;
SDL_Window *window = SDL_CreateWindow(
"Maze",
0, 0,
window_width, window_height,
SDL_WINDOW_SHOWN
);
if (window == NULL) {
fprintf(stderr, "Unable to create window: %s\n", SDL_GetError());
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_SOFTWARE
);
if (renderer == NULL) {
fprintf(stderr, "Unable to create renderer: %s\n", SDL_GetError());
return 1;
}
maze_generator(maze);
render_maze_to_sdl(renderer, maze, cell_size);
bool done = false;
SDL_Event event;
while (!done && SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
done = true;
break;
case SDL_WINDOWEVENT: {
switch (event.window.event) {
case SDL_WINDOWEVENT_MOVED:
case SDL_WINDOWEVENT_MAXIMIZED:
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_SIZE_CHANGED:
render_maze_to_sdl(renderer, maze, cell_size);
break;
default:
break;
}
}
break;
case SDL_KEYUP: {
SDL_KeyCode code = event.key.keysym.sym;
if (code == SDLK_ESCAPE) {
done = true;
} else {
maze_generator(maze);
render_maze_to_sdl(renderer, maze, cell_size);
}
break;
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return 0;
}
void render_maze_to_sdl(SDL_Renderer *renderer, const Maze *maze, int cell_size) {
if (renderer == NULL || maze == NULL) {
return;
}
const int width = maze->width;
const int height = maze->height;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Cell *cell = cell_at(maze, x, y);
if (cell == NULL) continue;
Directions dirs = get_blocked_directions(cell);
if (dirs.north) {
SDL_RenderDrawLine(
renderer,
x * cell_size, y * cell_size,
(x * cell_size) + cell_size, y * cell_size
);
}
if (dirs.south) {
SDL_RenderDrawLine(
renderer,
x * cell_size, (y * cell_size) + cell_size,
(x * cell_size) + cell_size, (y * cell_size) + cell_size
);
}
if (dirs.west) {
SDL_RenderDrawLine(
renderer,
x * cell_size, y * cell_size,
x * cell_size, (y * cell_size) + cell_size
);
}
if (dirs.east) {
SDL_RenderDrawLine(
renderer,
(x * cell_size) + cell_size, y * cell_size,
(x * cell_size) + cell_size, (y * cell_size) + cell_size
);
}
}
}
SDL_RenderPresent(renderer);
}
#endif //MAZE_SDL_MAZE_RENDERER_H