-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.c
140 lines (120 loc) · 3.64 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
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
#include <SDL.h>
#include <stdlib.h>
#include "maze.h"
SDL_Window * window;
SDL_Renderer * renderer;
SDL_GLContext maincontext; /* Our opengl context handle */
SDL_Event e;
void initRenderer() {
if (SDL_Init(SDL_INIT_VIDEO) != 0){
fprintf(stderr, "ERROR Initializing SDL2\n");
}
window = SDL_CreateWindow("Mazes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_SIZE, WINDOW_SIZE, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "ERROR making window\n");
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL) {
fprintf(stderr, "ERROR making renderer\n");
}
}
void closeRenderer() {
SDL_DestroyWindow(window);
SDL_Quit();
}
Event blockInput() {
// block on input for now. I don't think we have to do this forever.
while (1) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
return QUIT;
} else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_r:
return REDO;
}
}
}
}
return NOTHING;
}
void renderTile(Tile * tile, int x, int y) {
int x1 = x * TILE_SIZE;
int y1 = y * TILE_SIZE;
int x2 = (x+1) * TILE_SIZE;
int y2 = (y+1) * TILE_SIZE;
debugRenderCursor(x, y, 255, 255, 255 - ((tile->distance) % 255));
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
if ((tile->connections & DOWN) == 0 || (tile->blocks & DOWN) != 0) {
SDL_RenderDrawLine(renderer, x1, y2, x2, y2);
}
if ((tile->connections & RIGHT) == 0 || (tile->blocks & RIGHT) != 0) {
SDL_RenderDrawLine(renderer, x2, y1, x2, y2);
}
if (tile->unreachable == 1) {
debugRenderCursor(x, y, 0, 0, 0);
}
}
void renderPuzzle(Map * map) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
for (int x = 0; x < map->width; x++) {
for (int y = 0; y < map->height; y++) {
renderTile(&map->tiles[(y * map->width) + x], x, y);
}
}
SDL_RenderPresent(renderer);
}
void startRender() {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
}
void endRender() {
SDL_RenderPresent(renderer);
}
void renderMap(Map * map) {
for (int x = 0; x < map->width; x++) {
for (int y = 0; y < map->height; y++) {
renderTile(&map->tiles[(y * map->width) + x], x, y);
}
}
}
void debugRenderCursor(int cursorX, int cursorY, int r, int b, int g) {
SDL_Rect sdlRect;
SDL_SetRenderDrawColor(renderer, r,g,b, 0xFF);
sdlRect.x = ((cursorX * TILE_SIZE) + 1);
sdlRect.y = ((cursorY * TILE_SIZE) + 1);
sdlRect.w = TILE_SIZE - 1;
sdlRect.h = TILE_SIZE - 1;
SDL_RenderFillRect(renderer, &sdlRect);
}
void delay(int ms) {
SDL_Delay(ms);
}
void debugRenderPuzzle(Map * map, int * visited, int cursorX, int cursorY) {
SDL_Rect sdlRect;
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
if (cursorX != -1 || cursorY != -1) {
SDL_SetRenderDrawColor(renderer, 160, 160, 160, 0xFF);
sdlRect.x = (cursorX * TILE_SIZE);
sdlRect.y = (cursorY * TILE_SIZE);
sdlRect.w = TILE_SIZE;
sdlRect.h = TILE_SIZE;
SDL_RenderFillRect(renderer, &sdlRect);
}
for (int x = 0; x < map->width; x++) {
for (int y = 0; y < map->height; y++) {
if (visited[(y * map->width) + x] == 0) {
SDL_SetRenderDrawColor(renderer, 224, 224, 224, 0xFF);
sdlRect.x = (x * TILE_SIZE) + 1;
sdlRect.y = (y * TILE_SIZE) + 1;
sdlRect.w = TILE_SIZE - 1;
sdlRect.h = TILE_SIZE - 1;
SDL_RenderFillRect(renderer, &sdlRect);
}
renderTile(&map->tiles[(y * map->width) + x], x, y);
}
}
SDL_RenderPresent(renderer);
}