-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.c
executable file
·354 lines (307 loc) · 8.51 KB
/
App.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "SDL.h"
#include "SDL_image.h"
#include "SlidePuzzle.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <stdbool.h>
static SPUZ_Board p;
static Uint32 SolvedTicksEnd;
static bool ShowSolved = false;
static bool Running = true;
static bool NewGame = true;
static Uint16 PressedX = 0;
static Uint16 PressedY = 0;
static bool SpaceHeld = false;
static SDL_Window* Window = NULL;
static SDL_Renderer* Renderer = NULL;
static SDL_Texture* Picture = NULL;
static SDL_Texture* Arrows = NULL;
void Init();
void MainLoop();
void ProcessEvents();
void Update();
void RenderPuzzle();
void RenderSolved(bool solved);
void Quit(const char* const format, ...);
#define TILE_SIZE 256
#define BOARD_SIZE (256 * 4)
#define SOLVED_DELAY 5000
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
static EM_BOOL EmscriptenResizeCallback(int eventType, const EmscriptenUiEvent* uiEvent, void* userData) {
if (
eventType != EMSCRIPTEN_EVENT_RESIZE ||
!Window
) {
return false;
}
SDL_SetWindowSize(Window, uiEvent->windowInnerWidth, uiEvent->windowInnerHeight);
return true;
}
int main(int argc, char** argv) {
Init();
if (emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, false, EmscriptenResizeCallback) < 0 ) {
Quit("Failed to set window resizing callback.\n");
}
else {
int width = -1, height = -1;
width = EM_ASM_INT({ return window.innerWidth; });
height = EM_ASM_INT({ return window.innerHeight; });
if (width > 0 && height > 0) {
SDL_SetWindowSize(Window, width, height);
}
}
emscripten_set_main_loop(MainLoop, 60, 1);
Quit(NULL);
return 0;
}
#else
int main(int argc, char** argv) {
Init();
while (Running) {
MainLoop();
}
Quit(NULL);
return 0;
}
#endif
void MainLoop() {
if (NewGame) {
/* In the odd event permuting the panels results in a solved puzzle,
* try again until it isn't solved. The permuting function doesn't
* guarantee the puzzle is not in the solved state. */
do {
SPUZ_Permute(&p, (unsigned)time(NULL));
} while (SPUZ_Solved(&p));
NewGame = false;
}
ProcessEvents();
Update();
const bool solved = SPUZ_Solved(&p);
if (solved && !ShowSolved) {
ShowSolved = true;
SolvedTicksEnd = SDL_GetTicks() + SOLVED_DELAY;
}
if (ShowSolved || SpaceHeld) {
RenderSolved(solved);
if (ShowSolved && SDL_TICKS_PASSED(SDL_GetTicks(), SolvedTicksEnd)) {
ShowSolved = false;
NewGame = true;
}
}
else {
RenderPuzzle();
}
}
void Init(void) {
if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Controls", "Using the mouse, click squares next to the empty square to move.\nHold space bar to preview the solved puzzle.", NULL) < 0) {
Quit("Unable to show controls message:\n%s", SDL_GetError());
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
Quit("Unable to initialize SDL:\n%s", SDL_GetError());
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
SDL_DisplayMode mode;
if (SDL_GetDesktopDisplayMode(0, &mode) < 0) {
Quit("Unable to get desktop display mode:\n%s", SDL_GetError());
}
const int windowSize = ((mode.w > mode.h ? mode.h : mode.w) * 2) / 3;
if ((Window = SDL_CreateWindow("Slide Puzzle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowSize, windowSize, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE)) == NULL) {
Quit("Unable to create SDL window:\n%s", SDL_GetError());
}
if ((Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)) == NULL) {
Quit("Unable to create SDL renderer:\n%s", SDL_GetError());
}
if (SDL_RenderSetLogicalSize(Renderer, BOARD_SIZE, BOARD_SIZE) < 0) {
Quit("Unable to set SDL renderer logical size:\n%s", SDL_GetError());
}
#define LOADIMG(image, type) \
if ((image = IMG_LoadTexture(Renderer, #image "." type)) == NULL) { \
Quit("Unable to load \"" #image "." type "\" image:\n%s", SDL_GetError()); \
} \
SDL_SetTextureBlendMode(image, SDL_BLENDMODE_BLEND);
LOADIMG(Picture, "jpg");
LOADIMG(Arrows, "png");
#undef LOADIMG
SDL_ShowWindow(Window);
SPUZ_Init(&p);
}
void ProcessEvents(void) {
SDL_Event e;
SDL_PumpEvents();
while (SDL_PeepEvents(&e, 1, SDL_GETEVENT, 0, SDL_LASTEVENT) == 1) {
switch (e.type) {
case SDL_MOUSEBUTTONDOWN:
PressedX = e.button.x;
PressedY = e.button.y;
break;
case SDL_MOUSEBUTTONUP:
PressedX = -1;
PressedY = -1;
break;
case SDL_KEYDOWN:
if (e.key.keysym.sym == SDLK_SPACE) {
SpaceHeld = true;
}
break;
case SDL_KEYUP:
if (e.key.keysym.sym == SDLK_SPACE) {
SpaceHeld = false;
}
break;
case SDL_QUIT:
Running = false;
return;
default:
break;
}
}
}
void Update() {
const Sint16 x = p.emptyX * TILE_SIZE;
const Sint16 y = p.emtpyY * TILE_SIZE;
SPUZ_Direction dir = SPUZ_INVALIDDIRECTION;
/*
* SDL/board coordinates:
* 012345...
* 1
* 2
* 3
* 4
* 5
* .
* .
* .
*/
if (PressedX >= 0 && PressedX < BOARD_SIZE && PressedY >= 0 && PressedY < BOARD_SIZE) {
// Either case of if/else below could be inside empty panel
// In that case, dir remains set to SPUZ_INVALIDDIRECTION
// Could be above or below empty panel
if (PressedX > x && PressedX < x + TILE_SIZE) {
// Below empty panel
if (PressedY > y + TILE_SIZE && PressedY <= y + TILE_SIZE * 2) {
dir = SPUZ_UP;
}
// Above empty panel
else if (PressedY < y && PressedY >= y - TILE_SIZE) {
dir = SPUZ_DOWN;
}
}
// Could be left or right of empty panel
else if (PressedY > y && PressedY < y + TILE_SIZE) {
// Right of empty panel
if (PressedX > x + TILE_SIZE && PressedX <= x + TILE_SIZE * 2) {
dir = SPUZ_LEFT;
}
else if (PressedX < x && PressedX >= x - TILE_SIZE) {
dir = SPUZ_RIGHT;
}
}
}
SPUZ_Move(&p, dir);
}
void RenderPuzzle() {
/* Clear the screen. */
const SDL_Color black = {0, 0, 0};
SDL_SetRenderDrawColor(Renderer, black.r, black.g, black.b, SDL_ALPHA_OPAQUE);
if (SDL_RenderClear(Renderer) < 0) {
Quit("Unable to clear the screen:\n%s", SDL_GetError());
}
/* Draw the board. */
SDL_Rect dst, src;
src.w = TILE_SIZE;
src.h = TILE_SIZE;
dst.w = TILE_SIZE;
dst.h = TILE_SIZE;
dst.y = 0;
for (int j = 0; j < SPUZ_BOARD_H; j++, dst.y += TILE_SIZE) {
dst.x = 0;
for (int i = 0; i < SPUZ_BOARD_W; i++, dst.x += TILE_SIZE) {
const int panel = p.panels[j][i] - 1;
if (panel >= 0) {
src.x = (panel % SPUZ_BOARD_W) * TILE_SIZE;
src.y = (panel / SPUZ_BOARD_W) * TILE_SIZE;
if (SDL_RenderCopy(Renderer, Picture, &src, &dst) < 0) {
Quit("Unable to render copy a puzzle panel to the screen:\n%s", SDL_GetError());
}
}
else {
src.x = TILE_SIZE;
src.y = TILE_SIZE;
if (p.emptyX == 0) {
src.x = 0;
}
else if (p.emptyX == SPUZ_BOARD_W - 1) {
src.x = TILE_SIZE * 2;
}
if (p.emtpyY == 0) {
src.y = 0;
}
else if (p.emtpyY == SPUZ_BOARD_H - 1) {
src.y = TILE_SIZE * 2;
}
if (SDL_RenderCopy(Renderer, Arrows, &src, &dst) < 0) {
Quit("Unable to render copy the empty puzzle panel to the screen:\n%s", SDL_GetError());
}
}
}
}
SDL_RenderPresent(Renderer);
}
void RenderSolved(bool solved) {
/* Clear the screen. */
const SDL_Color black = {0, 0, 0};
if (SDL_SetRenderDrawColor(Renderer, black.r, black.g, black.b, SDL_ALPHA_OPAQUE) < 0) {
Quit("Failed to set render draw color:\n%s", SDL_GetError());
}
if (SDL_RenderClear(Renderer) < 0) {
Quit("Unable to clear the screen:\n%s", SDL_GetError());
}
/* Draw the picture. */
if (SDL_RenderCopy(Renderer, Picture, NULL, NULL) < 0) {
Quit("Unable to render copy the picture to the screen:\n%s", SDL_GetError());
}
if (!solved) {
SDL_Rect dst;
dst.x = TILE_SIZE * 3;
dst.y = TILE_SIZE * 3;
dst.w = TILE_SIZE;
dst.h = TILE_SIZE;
if (SDL_RenderFillRect(Renderer, &dst) < 0) {
Quit("Unable to render fill a rectangle:\n%s", SDL_GetError());
}
}
SDL_RenderPresent(Renderer);
}
void Quit(const char* const format, ...) {
if (Arrows) SDL_DestroyTexture(Arrows);
Arrows = NULL;
if (Picture) SDL_DestroyTexture(Picture);
Picture = NULL;
if (Renderer) SDL_DestroyRenderer(Renderer);
Renderer = NULL;
if (Window) SDL_DestroyWindow(Window);
Window = NULL;
if (SDL_WasInit(0)) SDL_Quit();
if (format) {
va_list args;
va_start(args, format);
const int size = vsnprintf(NULL, 0u, format, args) + 1;
va_end(args);
char* message = malloc(size);
if (message != NULL) {
va_start(args, format);
vsnprintf(message, size, format, args);
va_end(args);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", message, NULL);
free(message);
}
exit(EXIT_FAILURE);
}
else {
exit(EXIT_SUCCESS);
}
}