-
Notifications
You must be signed in to change notification settings - Fork 5
/
sdlwrap.cc
465 lines (414 loc) · 13 KB
/
sdlwrap.cc
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
/* Synaesthesia - program to display sound graphically
Copyright (C) 1997 Paul Francis Harrison
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
675 Mass Ave, Cambridge, MA 02139, USA.
The author may be contacted at:
or
27 Bond St., Mt. Waverley, 3149, Melbourne, Australia
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "syna.h"
#include <SDL.h>
#if defined(EMSCRIPTEN) && !SDL_VERSION_ATLEAST(2,0,0)
#include <emscripten.h>
#endif
#ifdef HAVE_ICON
extern unsigned char syn_icon_rgb[];
#endif
static SDL_Surface *surface;
static SDL_Color sdlPalette[256];
#if SDL_VERSION_ATLEAST(2,0,0)
static SDL_Window *window;
static SDL_Palette sdl2Palette = { 256, sdlPalette };
#endif
static bool fullscreen;
static int scaling; //currently only supports 1, 2
static int depth; // bits per pixel
uint32_t *colorlookup; // pallette for 32bpp
void sdlError(const char *str) {
fprintf(stderr, "synaesthesia: Error %s\n", str);
fprintf(stderr,"(reason for error: %s)\n", SDL_GetError());
exit(1);
}
#if SDL_VERSION_ATLEAST(2,0,0)
static void createSurface() {
surface = SDL_GetWindowSurface(window);
if (surface == NULL) sdlError("at SDL_GetWindowSurface");
scaling = 1;
}
#else
static void createSurface() {
Uint32 videoflags = SDL_HWSURFACE | SDL_DOUBLEBUF |
#if defined(EMSCRIPTEN) && !SDL_VERSION_ATLEAST(2,0,0)
/* This enables more optimized 8 bpp */
((depth == 8) ? SDL_HWPALETTE : 0) |
#endif
SDL_RESIZABLE | (fullscreen?SDL_FULLSCREEN:0);
/* Get available fullscreen modes */
SDL_Rect **modes = SDL_ListModes(0, videoflags);
bool any = false;
if(modes == (SDL_Rect **)-1){
/* All resolutions ok */
any = true;
} else {
/* Is there a resolution that will fit the display nicely */
for(int i=0;modes[i];i++)
if (modes[i]->w < outWidth*2 || modes[i]->h < outHeight*2)
any = true;
}
scaling = (any?1:2);
surface = SDL_SetVideoMode(outWidth*scaling, outHeight*scaling,
depth, videoflags);
if (!surface)
sdlError("setting video mode");
if (depth == 8)
SDL_SetColors(surface, sdlPalette, 0, 256);
}
#endif
void SdlScreen::setPalette(unsigned char *palette) {
if (depth == 32) {
colorlookup = (uint32_t*)palette;
return;
}
for(int i=0;i<256;i++) {
sdlPalette[i].r = palette[i*3+0];
sdlPalette[i].g = palette[i*3+1];
sdlPalette[i].b = palette[i*3+2];
}
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_SetSurfacePalette(surface, &sdl2Palette);
#else
SDL_SetColors(surface, sdlPalette, 0, 256);
#endif
}
bool SdlScreen::init(int xHint,int yHint,int width,int height,bool fullscreen,
int bpp)
{
#if !SDL_VERSION_ATLEAST(2,0,0)
outWidth = width;
outHeight = height;
#endif
::fullscreen = fullscreen;
depth = bpp;
if (depth != 8 && depth != 32)
return false;
/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
sdlError("initializing SDL");
#ifdef HAVE_ICON
SDL_Surface *iconsurface =
SDL_CreateRGBSurfaceFrom(syn_icon_rgb, 64, 64, 24, 64*3,
#ifdef LITTLEENDIAN
0x0000ff, 0x00ff00, 0xff0000, 0
#else
0xff0000, 0x00ff00, 0x0000ff, 0
#endif
);
#endif /* HAVE_ICON */
#if SDL_VERSION_ATLEAST(2,0,0)
window = SDL_CreateWindow("Synaesthesia",
fullscreen ? SDL_WINDOWPOS_UNDEFINED : xHint,
fullscreen ? SDL_WINDOWPOS_UNDEFINED : yHint,
width, height,
(fullscreen ? SDL_WINDOW_FULLSCREEN :
SDL_WINDOW_RESIZABLE) | SDL_WINDOW_SHOWN);
if (window == NULL) sdlError("at SDL_CreateWindow");
/* Raspberry Pi console will set window to size of full screen,
* and not give a resize event. */
SDL_GetWindowSize(window, &outWidth, &outHeight);
#else
#ifdef EMSCRIPTEN
/* Optimize SDL 1 performance */
EM_ASM(
SDL.defaults.copyOnLock = false;
SDL.defaults.discardOnLock = true;
SDL.defaults.opaqueFrontBuffer = false;
);
#endif
#endif
#if !SDL_VERSION_ATLEAST(2,0,0)
SDL_WM_SetCaption("Synaesthesia", "Synaesthesia");
#endif
#ifdef HAVE_ICON
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_SetWindowIcon(window, iconsurface);
#else
/* Must be called before SDL_SetVideoMode() */
SDL_WM_SetIcon(iconsurface, NULL);
#endif
SDL_FreeSurface(iconsurface);
#endif /* HAVE_ICON */
createSurface();
#if !SDL_VERSION_ATLEAST(2,0,0)
SDL_EnableUNICODE(1);
#endif
SDL_ShowCursor(0);
return true;
}
void SdlScreen::end(void) {
SDL_Quit();
}
void SdlScreen::toggleFullScreen(void) {
fullscreen = !fullscreen;
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_SetWindowFullscreen(window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
#else
createSurface();
#endif
}
void SdlScreen::inputUpdate(int &mouseX,int &mouseY,int &mouseButtons,char &keyHit) {
SDL_Event event;
#if SDL_VERSION_ATLEAST(2,0,0)
const char *keyname;
#endif
keyHit = 0;
while ( SDL_PollEvent(&event) > 0 ) {
switch (event.type) {
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
if ( event.button.state == SDL_PRESSED ) {
#if SDL_VERSION_ATLEAST(2,0,2)
if (event.button.clicks == 2) {
mouseButtons |= SYN_DBL_CLICK;
}
#else
// Earlier SDL versions don't report double clicks
static Uint32 dblclicktm = 0;
Uint32 clicktime = SDL_GetTicks();
// Like !SDL_TICKS_PASSED(), which may not be available
if ((Sint32)(dblclicktm - clicktime) > 0) {
mouseButtons |= SYN_DBL_CLICK;
}
dblclicktm = clicktime + 200;
#endif // !SDL_VERSION_ATLEAST(2,0,2)
mouseButtons |= 1 << event.button.button;
} else {
mouseButtons &= ~( 1 << event.button.button );
mouseButtons &= ~SYN_DBL_CLICK;
}
mouseX = event.button.x / scaling;
mouseY = event.button.y / scaling;
break;
case SDL_MOUSEMOTION :
mouseX = event.motion.x / scaling;
mouseY = event.motion.y / scaling;
break;
#if SDL_VERSION_ATLEAST(2,0,0)
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_LEAVE:
/* Lost focus, hide mouse */
mouseX = outWidth;
mouseY = outHeight;
break;
case SDL_WINDOWEVENT_RESIZED:
allocOutput(event.window.data1, event.window.data2);
createSurface();
break;
}
break;
case SDL_KEYDOWN:
keyname = SDL_GetKeyName(event.key.keysym.sym);
if (keyname[0] == 0 ||
(event.key.keysym.mod &
(KMOD_CTRL | KMOD_ALT | KMOD_GUI | KMOD_MODE)) != 0) {
break;
} else if (keyname[1] == 0) {
keyHit = keyname[0];
} else if (strncmp(keyname, "Keypad ", 7) &&
keyname[7] != 0 && keyname[8] == 0) {
keyHit = keyname[7];
} else {
break;
}
if (event.key.keysym.mod & KMOD_CAPS ?
event.key.keysym.mod & KMOD_SHIFT :
(event.key.keysym.mod & KMOD_SHIFT) == 0) {
keyHit = tolower(keyHit);
}
break;
#else // !SDL_VERSION_ATLEAST(2,0,0)
case SDL_ACTIVEEVENT :
/* Lost focus, hide mouse */
if (!event.active.gain) {
mouseX = outWidth;
mouseY = outHeight;
}
case SDL_KEYDOWN:
///* Ignore key releases */
//if ( event.key.state == SDL_RELEASED ) {
// break;
//}
/* Ignore ALT-TAB for windows */
if ( (event.key.keysym.sym == SDLK_LALT) ||
(event.key.keysym.sym == SDLK_TAB) ) {
break;
}
if (event.key.keysym.unicode > 255)
break;
keyHit = event.key.keysym.unicode;
return;
case SDL_VIDEORESIZE:
event.resize.w &= ~3;
allocOutput(event.resize.w,event.resize.h);
createSurface();
break;
#endif
case SDL_QUIT:
keyHit = 'q';
return;
default:
break;
}
}
}
void SdlScreen::show(void) {
attempt(SDL_LockSurface(surface),"locking screen for output.");
if (depth == 32) {
if (scaling == 1) {
uint16_t *in = (uint16_t*)output;
uint8_t *line = (uint8_t*)(surface->pixels);
for (int y = 0; y < outHeight; y++) {
uint32_t *out = (uint32_t*)line;
for (int x = 0; x < outWidth; x++) {
*(out++) = colorlookup[*(in++)];
}
line += surface->pitch;
}
} else {
// scaling 2
uint16_t *in = (uint16_t*)output;
uint8_t *line = (uint8_t*)(surface->pixels);
for(int y = 0; y < outHeight; y++) {
uint32_t *lp1 = (uint32_t*)line;
line += surface->pitch;
uint32_t *lp2 = (uint32_t*)line;
line += surface->pitch;
for(int x = 0; x < outWidth; x++) {
uint32_t v = colorlookup[*(in++)];
*(lp1++) = v; *(lp1++) = v;
*(lp2++) = v; *(lp2++) = v;
} // for each pixel in line
} // for each pair of lines
} // scaling 2
} else
if (scaling == 1) {
uint32_t *ptr2 = (uint32_t*)output;
int lines, linelen;
if (surface->pitch == outWidth) {
// Do everything at once
lines = 1;
linelen = outWidth*outHeight/sizeof(*ptr2);
} else {
// Do one line at once, adjusting for pitch between lines
lines = outHeight;
linelen = outWidth/sizeof(*ptr2);
}
for (int y = 0; y < lines; y++) {
uint32_t *ptr1 = (uint32_t*)( (uint8_t*)surface->pixels +
surface->pitch * y);
int i = linelen;
do {
// Asger Alstrup Nielsen's ([email protected])
// optimized 32 bit screen loop
unsigned int const r1 = *(ptr2++);
unsigned int const r2 = *(ptr2++);
#ifdef LITTLEENDIAN
unsigned int const v =
((r1 & 0x000000f0ul) >> 4)
| ((r1 & 0x0000f000ul) >> 8)
| ((r1 & 0x00f00000ul) >> 12)
| ((r1 & 0xf0000000ul) >> 16);
*(ptr1++) = v |
( ((r2 & 0x000000f0ul) << 12)
| ((r2 & 0x0000f000ul) << 8)
| ((r2 & 0x00f00000ul) << 4)
| ((r2 & 0xf0000000ul)));
#else
unsigned int const v =
((r2 & 0x000000f0ul) >> 4)
| ((r2 & 0x0000f000ul) >> 8)
| ((r2 & 0x00f00000ul) >> 12)
| ((r2 & 0xf0000000ul) >> 16);
*(ptr1++) = v |
( ((r1 & 0x000000f0ul) << 12)
| ((r1 & 0x0000f000ul) << 8)
| ((r1 & 0x00f00000ul) << 4)
| ((r1 & 0xf0000000ul)));
#endif
} while (--i);
}
} else {
// SDL has no standard image scaling routine (!)
uint8_t *pixels = (uint8_t*)(surface->pixels);
for(int y=0;y<outHeight;y++) {
uint32_t *p1 = (uint32_t*)(output+y*outWidth*2);
uint32_t *p2 = (uint32_t*)(pixels+y*2*surface->pitch);
uint32_t *p3 = (uint32_t*)(pixels+(y*2+1)*surface->pitch);
for(int x=0;x<outWidth;x+=2) {
uint32_t v = *(p1++);
v = ((v&0x000000f0) >> 4)
| ((v&0x0000f000) >> 8)
| ((v&0x00f00000) >> 4)
| ((v&0xf0000000) >> 8);
v |= v<<8;
*(p2++) = v;
*(p3++) = v;
}
}
}
SDL_UnlockSurface(surface);
//SDL_UpdateRect(surface, 0, 0, 0, 0);
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_UpdateWindowSurface(window);
#else
SDL_Flip(surface);
#endif
}
int SdlScreen::getDepth() {
return depth;
}
/* Calculate right shift needed to align colour component MSB with bit 7.
* This assumes a 32 bpp or 24 bpp mode, where a left shift shouldn't
* ever be necessary.
*
* SDL does provide Rshift, Bshift and Gshift, but those are documented as
* "(internal use)" and Emscripten's SDL doesn't set them.
*/
static int getPixelShift(Uint32 mask) {
int shift = 0;
while ((mask & ~0xFF) != 0) {
shift++;
mask >>= 1;
}
return shift;
}
void SdlScreen::getPixelFormat(int *rshift, unsigned long *rmask,
int *gshift, unsigned long *gmask,
int *bshift, unsigned long *bmask,
int *ashift, unsigned long *amask) {
SDL_PixelFormat *fmt = surface->format;
*rshift = getPixelShift(fmt->Rmask);
*rmask = fmt->Rmask;
*gshift = getPixelShift(fmt->Gmask);
*gmask = fmt->Gmask;
*bshift = getPixelShift(fmt->Bmask);
*bmask = fmt->Bmask;
*ashift = getPixelShift(fmt->Amask);
*amask = fmt->Amask;
}