/
sdlml.c
475 lines (396 loc) · 11.2 KB
/
sdlml.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* Hooks and glue for using SDL through SML. Portable to win32, linux, osx.
This is incomplete and a little messy. But we try to make it better.
- Tom 23 May 2010
*/
#include <SDL.h>
#ifdef USE_DISPLAY_FORMAT
# if USE_DISPLAY_FORMAT == 0
# error "sdlml.c no longer supports explicitly setting USE_DISPLAY_FORMAT. If you want console mode, use -DCONSOLE_MODE."
# endif
#endif
#ifdef CONSOLE_MODE
// Use the display format for surfaces, unless
// in console mode, where there is no display!
# define USE_DISPLAY_FORMAT 0
#else
# define USE_DISPLAY_FORMAT 1
#endif
/* XXX It seems that byte order doesn't affect the
ordering of the colors within a Uint32. */
#if 0 /* SDL_BYTEORDER == SDL_BIG_ENDIAN */
const Uint32 rmask = 0xff000000;
const Uint32 gmask = 0x00ff0000;
const Uint32 bmask = 0x0000ff00;
const Uint32 amask = 0x000000ff;
const Uint32 rshift = 24;
const Uint32 gshift = 16;
const Uint32 bshift = 8;
const Uint32 ashift = 0;
#else
const Uint32 rmask = 0x000000ff;
const Uint32 gmask = 0x0000ff00;
const Uint32 bmask = 0x00ff0000;
const Uint32 amask = 0xff000000;
const Uint32 ashift = 24;
const Uint32 bshift = 16;
const Uint32 gshift = 8;
const Uint32 rshift = 0;
#endif
int ml_init() {
#ifdef CONSOLE_MODE
if (SDL_Init(SDL_INIT_NOPARACHUTE) < 0) {
printf("Unable to initialize SDL. (%s)\n", SDL_GetError());
return 0;
} else {
return 1;
}
#else
if (SDL_Init(SDL_INIT_VIDEO |
SDL_INIT_TIMER |
SDL_INIT_JOYSTICK |
SDL_INIT_AUDIO |
/* for debugging */
SDL_INIT_NOPARACHUTE |
SDL_INIT_AUDIO) < 0) {
printf("Unable to initialize SDL. (%s)\n", SDL_GetError());
return 0;
} else {
SDL_EnableUNICODE(1);
return 1;
}
#endif
}
int ml_platform() {
#if defined(WIN32)
return 1;
#elif defined(OSX)
return 2;
#elif defined(LINUX)
return 3;
#else
# error "You must define (-D) WIN32, OSX, or LINUX when compiling sdlml.o. This is used to determine the platform."
return -1;
#endif
}
int ml_consolemode() {
#ifdef CONSOLE_MODE
return 1;
#else
return 0;
#endif
}
void slock(SDL_Surface *surf) {
if(SDL_MUSTLOCK(surf))
SDL_LockSurface(surf);
}
void sulock(SDL_Surface *surf) {
if (SDL_MUSTLOCK(surf))
SDL_UnlockSurface(surf);
}
/* out has room for 512 chars */
void ml_joystickname (int i, char * out) {
const char * p = SDL_JoystickName(i);
if (p) {
strncpy(out, p, 512);
}
}
void ml_setjoystate(int i) {
SDL_JoystickEventState(i?SDL_ENABLE:SDL_IGNORE);
}
/* try to make a hardware surface, and, failing that,
make a software surface */
SDL_Surface * ml_makesurface(int w, int h, int alpha) {
/* PERF need to investigate relative performance
of sw/hw surfaces */
SDL_Surface * ss = 0;
#if 0
SDL_CreateRGBSurface(SDL_HWSURFACE |
(alpha?SDL_SRCALPHA:0),
w, h, 32,
rmask, gmask, bmask,
amask);
#endif
if (!ss) ss = SDL_CreateRGBSurface(SDL_SWSURFACE |
(alpha?SDL_SRCALPHA:0),
w, h, 32,
rmask, gmask, bmask,
amask);
if (ss && !alpha) SDL_SetAlpha(ss, 0, 255);
// XXX we probably don't want this if we are using GL.
# if USE_DISPLAY_FORMAT
if (ss) {
SDL_Surface * rr;
if (alpha) rr = SDL_DisplayFormatAlpha(ss);
else rr = SDL_DisplayFormat(ss);
if (rr) {
SDL_FreeSurface(ss);
return rr;
} else return ss;
} else return 0;
# else
return ss;
# endif
}
int ml_setalpha(SDL_Surface *surf, Uint8 srcalpha, Uint8 rleaccel, Uint8 alpha) {
Uint32 flags = 0;
if (srcalpha)
flags |= SDL_SRCALPHA;
if (rleaccel)
flags |= SDL_RLEACCEL;
return SDL_SetAlpha(surf, flags, alpha);
}
SDL_Surface * ml_makescreen(int w, int h) {
/* Can't use HWSURFACE here, because not handling this SDL_BlitSurface
case mentioned in the documentation:
"If either of the surfaces were in video memory, and the blit returns -2,
the video memory was lost, so it should be reloaded with artwork
and re-blitted."
Plus, on Windows, the only time you get HWSURFACE is with FULLSCREEN.
-- Adam
*/
/* SDL_ANYFORMAT
"Normally, if a video surface of the requested bits-per-pixel (bpp)
is not available, SDL will emulate one with a shadow surface.
Passing SDL_ANYFORMAT prevents this and causes SDL to use the
video surface, regardless of its pixel depth."
Probably should not pass this.
-- Adam
*/
/* SDL_DOUBLEBUF only valid with SDL_HWSURFACE! */
SDL_Surface * ret = SDL_SetVideoMode(w, h, 32,
SDL_SWSURFACE |
SDL_RESIZABLE);
return ret;
}
SDL_Surface * ml_makefullscreen(int w, int h) {
/* Can't use HWSURFACE here, because not handling this SDL_BlitSurface
case mentioned in the documentation:
"If either of the surfaces were in video memory, and the blit returns -2,
the video memory was lost, so it should be reloaded with artwork
and re-blitted."
Plus, on Windows, the only time you get HWSURFACE is with FULLSCREEN.
-- Adam
*/
/* SDL_ANYFORMAT
"Normally, if a video surface of the requested bits-per-pixel (bpp)
is not available, SDL will emulate one with a shadow surface.
Passing SDL_ANYFORMAT prevents this and causes SDL to use the
video surface, regardless of its pixel depth."
Probably should not pass this.
-- Adam
*/
/* SDL_DOUBLEBUF only valid with SDL_HWSURFACE! */
SDL_Surface * ret = SDL_SetVideoMode(w, h, 32,
SDL_FULLSCREEN |
SDL_DOUBLEBUF |
SDL_HWSURFACE);
return ret;
}
SDL_Surface * ml_glmakescreen(int w, int h) {
SDL_Surface * ret = SDL_SetVideoMode(w, h, 32,
SDL_SWSURFACE |
SDL_RESIZABLE |
SDL_OPENGL |
SDL_GL_DOUBLEBUFFER);
return ret;
}
void ml_blitall(SDL_Surface * src, SDL_Surface * dst, int x, int y) {
SDL_Rect r;
r.x = x;
r.y = y;
SDL_BlitSurface(src, 0, dst, &r);
}
void ml_blit(SDL_Surface * src, int srcx, int srcy, int srcw, int srch,
SDL_Surface * dst, int dstx, int dsty) {
SDL_Rect sr;
SDL_Rect dr;
sr.x = srcx;
sr.y = srcy;
sr.w = srcw;
sr.h = srch;
dr.x = dstx;
dr.y = dsty;
SDL_BlitSurface(src, &sr, dst, &dr);
}
int ml_surfacewidth(SDL_Surface * src) {
return src->w;
}
int ml_surfaceheight(SDL_Surface * src) {
return src->h;
}
void ml_clearsurface(SDL_Surface * s, Uint32 color) {
SDL_FillRect(s, 0, color);
}
SDL_Event * ml_newevent() {
return (SDL_Event*) malloc (sizeof (SDL_Event));
}
int ml_eventtag(SDL_Event * e) {
return e->type;
}
int ml_event8_2nd(SDL_Event * e) {
return (((SDL_KeyboardEvent*)e)->which);
}
int ml_event8_3rd(SDL_Event * e) {
return (((SDL_KeyboardEvent*)e)->state);
}
int ml_event8_4th(SDL_Event * e) {
return (((SDL_JoyHatEvent*)e)->value);
}
int ml_event_joyaxis_value(SDL_JoyAxisEvent *e) {
return e->value;
}
int ml_event_keyboard_sym(SDL_KeyboardEvent* e) {
return e->keysym.sym;
}
int ml_event_keyboard_mod(SDL_KeyboardEvent* e) {
return e->keysym.mod;
}
int ml_event_keyboard_unicode(SDL_KeyboardEvent* e) {
return e->keysym.unicode;
}
int ml_event_mmotion_x(SDL_MouseMotionEvent* e) {
return e->x;
}
int ml_event_mmotion_y(SDL_MouseMotionEvent* e) {
return e->y;
}
int ml_event_mmotion_xrel(SDL_MouseMotionEvent* e) {
return e->xrel;
}
int ml_event_mmotion_yrel(SDL_MouseMotionEvent* e) {
return e->yrel;
}
int ml_event_mbutton_x(SDL_MouseButtonEvent * e) { return e->x; }
int ml_event_mbutton_y(SDL_MouseButtonEvent * e) { return e->y; }
int ml_event_mbutton_button(SDL_MouseButtonEvent * e) { return e->button; }
/* XXX should lock before calling (for certain modes)... */
void ml_drawpixel(SDL_Surface *surf, int x, int y,
int R, int G, int B) {
// printf("RGB %d %d %d\n", R, G, B);
Uint32 color = SDL_MapRGB(surf->format, R, G, B);
switch (surf->format->BytesPerPixel) {
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 *)surf->pixels + y*surf->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 *)surf->pixels + y*surf->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 *)surf->pixels + y*surf->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 *)surf->pixels + y*surf->pitch/4 + x;
*bufp = color;
}
break;
}
}
void ml_getpixel(SDL_Surface *surf, int x, int y,
unsigned char * R, unsigned char * G, unsigned char * B) {
switch (surf->format->BytesPerPixel) {
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 *)surf->pixels + y*surf->pitch/4 + x;
SDL_GetRGB(*bufp, surf->format, R, G, B);
}
break;
default:
printf("want 32bpp\n");
abort();
}
}
void ml_getpixela(SDL_Surface *surf, int x, int y,
unsigned char * R, unsigned char * G, unsigned char * B, unsigned char * A) {
switch (surf->format->BytesPerPixel) {
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 *)surf->pixels + y*surf->pitch/4 + x;
SDL_GetRGBA(*bufp, surf->format, R, G, B, A);
}
break;
default:
printf("want 32bpp\n");
abort();
}
}
void *ml_getpixels(SDL_Surface *surf) {
return surf->pixels;
}
int ml_get_bytes_per_pixel(SDL_Surface *surf) {
return surf->format->BytesPerPixel;
}
int ml_is_rgb(SDL_Surface *surf) {
return (surf->format->Rmask == rmask);
}
void ml_fillrect(SDL_Surface *dst, int x, int y, int w, int h, int r, int g, int b) {
Uint32 c = SDL_MapRGB(dst->format, r, g, b);
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_FillRect(dst, &rect, c);
}
void ml_fillrecta(SDL_Surface *dst, int x, int y, int w, int h, int r, int g, int b, int a) {
Uint32 c = SDL_MapRGBA(dst->format, r, g, b, a);
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_FillRect(dst, &rect, c);
}
SDL_Surface * ml_alphadim(SDL_Surface * src) {
/* must be 32 bpp */
if (src->format->BytesPerPixel != 4) return 0;
int ww = src->w, hh = src->h;
SDL_Surface * ret = ml_makesurface(ww, hh, 1);
if (!ret) return 0;
slock(ret);
slock(src);
int y, x;
for(y = 0; y < hh; y ++) {
for(x = 0; x < ww; x ++) {
Uint32 color = *((Uint32 *)src->pixels + y*src->pitch/4 + x);
/* divide alpha channel by 2 */
/* XXX this seems to be wrong on powerpc (dims some other channel
instead). but if the masks are right, how could this go wrong? */
color =
(color & (rmask | gmask | bmask)) |
(((color & amask) >> 1) & amask);
*((Uint32 *)ret->pixels + y*ret->pitch/4 + x) = color;
}
}
sulock(src);
sulock(ret);
return ret;
}
int ml_version_packed() {
const SDL_version * v = SDL_Linked_Version();
fprintf(stderr, "Version: %u.%u.%u\n", v->major, v->minor, v->patch);
return (v->major << 16) | (v->minor << 8) | v->patch;
}