-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiosdl.c
407 lines (366 loc) · 9.76 KB
/
iosdl.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* iosdl.c: The SDL user interface.
*
* Copyright (C) 2010 Brian Raiter.
* This program is free software. See README for details.
*/
#include <stdlib.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include "yahtzee.h"
#include "gen.h"
#include "iosdlctl.h"
#include "iosdl.h"
/* True if the point is inside the rectangle.
*/
#define inrect(p, r) ((p).x >= (r).x && (p).x < (r).x + (r).w && \
(p).y >= (r).y && (p).y < (r).y + (r).h)
/* The display.
*/
SDL_Surface *sdl_screen;
/* The scaling unit. Changing this changes the size of everything.
*/
int sdl_scalingunit;
/* The default scaling unit.
*/
static int const defaultscalingunit = 4;
/* The color of the window's background area.
*/
static SDL_Color const bkgnd = { 128, 191, 191, 0 };
static Uint32 bkgndcolor;
/* Size of the display.
*/
static int cxWindow, cyWindow;
/* True if the window needs to be rendered from scratch.
*/
static int redrawall;
/* The array of SDL control info, mirroring the controls array.
*/
static struct sdlcontrol sdlcontrols[ctl_count];
/* Each control's state update functions.
*/
static int (*stateupdatefunctions[ctl_count])(struct sdlcontrol *);
/* A small event queue. Needed because a single SDL event
* can potentially map to two or even three input events.
*/
static int eventqueue[4];
static unsigned int eventqueuesize = 0;
/*
* Display initialization.
*/
/* Put the SDL controls in their initial states.
*/
static void initcontrols(void)
{
int i;
for (i = 0 ; i < ctl_count ; ++i)
sdlcontrols[i].control = &controls[i];
for (i = ctl_dice ; i < ctl_dice_end ; ++i) {
makedie(&sdlcontrols[i], bkgnd);
stateupdatefunctions[i] = updatedie;
}
makebutton(&sdlcontrols[ctl_button]);
stateupdatefunctions[i] = updatebutton;
for (i = ctl_slots ; i < ctl_slots_end ; ++i) {
makeslot(&sdlcontrols[i], i);
stateupdatefunctions[i] = updateslot;
}
}
/* Free all resources associated with the SDL controls.
*/
static void uninitcontrols(void)
{
int i;
for (i = ctl_dice ; i < ctl_dice_end ; ++i)
unmakedie(&sdlcontrols[i]);
unmakebutton(&sdlcontrols[ctl_button]);
for (i = ctl_slots ; i < ctl_slots_end ; ++i)
unmakeslot(&sdlcontrols[i]);
}
/* Determine the display locations of all of the SDL controls.
*/
static void initlayout(void)
{
int cxSpacing, cySpacing, cxDieSpacing;
int i, n, x, y;
for (i = 0 ; i < ctl_count ; ++i) {
sdlcontrols[i].rect.w = sdlcontrols[i].images[0]->w;
sdlcontrols[i].rect.h = sdlcontrols[i].images[0]->h;
}
cxSpacing = sdl_scalingunit * 4;
cySpacing = sdl_scalingunit * 4;
cxDieSpacing = sdl_scalingunit;
x = sdlcontrols[ctl_dice].rect.w * 5 + cxDieSpacing * 4;
cxWindow = sdlcontrols[ctl_slots].rect.w * 2;
if (cxWindow < x)
cxWindow = x;
cxWindow += cxSpacing * 2;
x = (cxWindow - x) / 2;
y = cySpacing;
for (i = ctl_dice ; i < ctl_dice_end ; ++i) {
sdlcontrols[i].rect.x = x;
sdlcontrols[i].rect.y = y;
x += sdlcontrols[ctl_dice].rect.w + cxDieSpacing;
}
y += sdlcontrols[ctl_dice].rect.h + cySpacing;
sdlcontrols[ctl_button].rect.x =
(cxWindow - sdlcontrols[ctl_button].rect.w) / 2;
sdlcontrols[ctl_button].rect.y = y;
x = (cxWindow - sdlcontrols[ctl_slots].rect.w * 2) / 2;
y += sdlcontrols[ctl_button].rect.h + cySpacing;
for (n = 0 ; n < ctl_slots_count / 2 ; ++n) {
i = ctl_slots + n;
sdlcontrols[i].rect.x = x;
sdlcontrols[i].rect.y = y;
i += ctl_slots_count / 2;
sdlcontrols[i].rect.x = x + sdlcontrols[ctl_slots].rect.w;
sdlcontrols[i].rect.y = y;
y += sdlcontrols[ctl_slots].rect.h;
}
cyWindow = y + cySpacing;
}
/* Lay out the display and create an appropriately-sized screen.
*/
static void createscreen(void)
{
initcontrols();
initlayout();
sdl_screen = SDL_SetVideoMode(cxWindow, cyWindow, 0,
SDL_SWSURFACE | SDL_ANYFORMAT);
if (!sdl_screen)
croak("%s\nCannot resize display to %d x %d.",
SDL_GetError(), cxWindow, cyWindow);
bkgndcolor = SDL_MapRGB(sdl_screen->format, bkgnd.r, bkgnd.g, bkgnd.b);
redrawall = 1;
}
/*
* Exported functions.
*/
/* Called when the program is exiting.
*/
static void shutdown(void)
{
uninitcontrols();
if (TTF_WasInit())
TTF_Quit();
if (SDL_WasInit(SDL_INIT_VIDEO))
SDL_Quit();
}
/* Create the SDL display and initialize everything.
*/
int sdl_initializeio(void)
{
if (SDL_Init(SDL_INIT_VIDEO))
return 0;
atexit(shutdown);
if (TTF_Init())
croak("%s\nCannot initialize SDL_ttf.", TTF_GetError());
sdl_scalingunit = defaultscalingunit;
sdl_screen = SDL_SetVideoMode(1, 1, 0, SDL_SWSURFACE | SDL_ANYFORMAT);
if (!sdl_screen)
croak("%s\nCannot initialize display.", SDL_GetError());
SDL_WM_SetCaption("Yahtzee", "Yahtzee");
SDL_EnableUNICODE(1);
createscreen();
return 1;
}
/* Render our controls to the display as required.
*/
static void render(void)
{
SDL_Rect drawrects[ctl_count];
int drawcount, i;
if (redrawall) {
SDL_FillRect(sdl_screen, NULL, bkgndcolor);
for (i = 0 ; i < ctl_count ; ++i) {
stateupdatefunctions[i](&sdlcontrols[i]);
SDL_BlitSurface(sdlcontrols[i].images[sdlcontrols[i].state], NULL,
sdl_screen, &sdlcontrols[i].rect);
}
SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);
redrawall = 0;
} else {
drawcount = 0;
for (i = 0 ; i < ctl_count ; ++i) {
if (!stateupdatefunctions[i](&sdlcontrols[i]))
continue;
SDL_BlitSurface(sdlcontrols[i].images[sdlcontrols[i].state], NULL,
sdl_screen, &sdlcontrols[i].rect);
drawrects[drawcount++] = sdlcontrols[i].rect;
}
if (drawcount)
SDL_UpdateRects(sdl_screen, drawcount, drawrects);
}
}
/*
* Managing the controls.
*/
/* Add an input event to the queue.
*/
static void queueinputevent(int control)
{
if (eventqueuesize < sizeof eventqueue / sizeof *eventqueue)
eventqueue[eventqueuesize++] = control;
}
/* Retrieve the first input event from the queue. Returns false if the
* queue is empty.
*/
static int unqueueinputevent(int *control)
{
if (eventqueuesize == 0)
return 0;
*control = eventqueue[0];
--eventqueuesize;
memmove(eventqueue, eventqueue + 1, eventqueuesize * sizeof *eventqueue);
return 1;
}
/* Track which control the mouse is hovering over.
*/
static void sethovering(int id)
{
int i;
for (i = 0 ; i < ctl_count ; ++i)
sdlcontrols[i].hovering = i == id;
}
/* Simulate a brief mouse click on a control.
*/
static void flashcontrol(int id)
{
struct sdlcontrol saved;
saved = sdlcontrols[id];
sdlcontrols[id].hovering = 1;
sdlcontrols[id].down = 1;
render();
SDL_Delay(100);
sdlcontrols[id] = saved;
render();
}
/* Update the display and run the event loop until a valid input event
* is encountered. Input events include mouse button clicks (up and
* down), and keyboard characters corresponding to a control's hotkey.
*/
int sdl_runio(int *control)
{
static int mousetrap = -1;
SDL_Event event;
int ch, i;
for (;;) {
if (unqueueinputevent(control))
return 1;
render();
if (SDL_WaitEvent(&event) < 0)
exit(1);
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
if (event.button.button != SDL_BUTTON_LEFT)
break;
if (mousetrap >= 0) {
sdlcontrols[mousetrap].down = 0;
mousetrap = -1;
}
i = ctl_count;
while (i-- > 0 && !inrect(event.button, sdlcontrols[i].rect)) ;
sethovering(i);
if (i == ctl_button) {
mousetrap = i;
sdlcontrols[i].down = 1;
} else {
if (i >= 0)
queueinputevent(i);
}
break;
case SDL_MOUSEMOTION:
if (mousetrap >= 0) {
if (inrect(event.button, sdlcontrols[mousetrap].rect))
sethovering(mousetrap);
else
sethovering(-1);
} else {
i = ctl_count;
while (i-- && !inrect(event.button, sdlcontrols[i].rect)) ;
sethovering(i);
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button != SDL_BUTTON_LEFT)
break;
if (mousetrap < 0)
break;
sdlcontrols[mousetrap].down = 0;
if (inrect(event.button, sdlcontrols[mousetrap].rect))
queueinputevent(mousetrap);
else
sethovering(-1);
mousetrap = -1;
break;
case SDL_KEYDOWN:
ch = event.key.keysym.unicode;
if (ch) {
for (i = 0 ; i < ctl_count ; ++i) {
if (sdlcontrols[i].control->key == ch) {
queueinputevent(i);
break;
}
}
if (i < ctl_count)
break;
}
if (event.key.keysym.unicode == '\r') {
flashcontrol(ctl_button);
queueinputevent(ctl_button);
break;
} else if (event.key.keysym.unicode == '?' ||
event.key.keysym.sym == SDLK_F1) {
if (!runhelp())
return 0;
redrawall = 1;
break;
} else if (event.key.keysym.unicode == '\013') {
if (!showkeyhelp(sdlcontrols))
return 0;
redrawall = 1;
break;
} else if (event.key.keysym.unicode == '\026') {
if (!showlicense())
return 0;
redrawall = 1;
break;
} else if ((event.key.keysym.mod & KMOD_CTRL) &&
(event.key.keysym.sym == SDLK_PLUS ||
event.key.keysym.sym == SDLK_EQUALS)) {
uninitcontrols();
++sdl_scalingunit;
createscreen();
break;
} else if ((event.key.keysym.mod & KMOD_CTRL) &&
event.key.keysym.sym == SDLK_MINUS) {
if (sdl_scalingunit > 1) {
uninitcontrols();
--sdl_scalingunit;
createscreen();
}
break;
} else if ((event.key.keysym.mod & KMOD_CTRL) &&
event.key.keysym.sym == SDLK_0) {
if (sdl_scalingunit != defaultscalingunit) {
uninitcontrols();
sdl_scalingunit = defaultscalingunit;
createscreen();
}
break;
}
if (event.key.keysym.unicode == '\030')
return 0;
if ((event.key.keysym.mod & KMOD_ALT) &&
event.key.keysym.sym == SDLK_F4)
return 0;
break;
case SDL_VIDEOEXPOSE:
redrawall = 1;
break;
case SDL_QUIT:
return 0;
default:
break;
}
}
}