The main loop now uses SDL_WaitEvent instead of SDL_PollEvent, and the
indentation level needs to change. For clarity, this commit contains the
code changes, while the next one updates the indentation.
| ... | ... |
@@ -32,7 +32,6 @@ WITH REGARD TO THIS SOFTWARE. |
| 32 | 32 |
#define WIDTH 64 * 8 |
| 33 | 33 |
#define HEIGHT 40 * 8 |
| 34 | 34 |
#define PAD 4 |
| 35 |
-#define BENCH 0 |
|
| 36 | 35 |
|
| 37 | 36 |
static SDL_Window *gWindow; |
| 38 | 37 |
static SDL_Texture *gTexture; |
| ... | ... |
@@ -44,7 +43,7 @@ static SDL_Rect gRect; |
| 44 | 43 |
|
| 45 | 44 |
static Device *devscreen, *devmouse, *devctrl, *devaudio0, *devfile0; |
| 46 | 45 |
static Uint8 zoom = 1; |
| 47 |
-static Uint32 stdin_event, audio0_event; |
|
| 46 |
+static Uint32 stdin_event, audio0_event, redraw_event; |
|
| 48 | 47 |
|
| 49 | 48 |
static int |
| 50 | 49 |
error(char *msg, const char *err) |
| ... | ... |
@@ -87,6 +86,20 @@ stdin_handler(void *p) |
| 87 | 86 |
(void)p; |
| 88 | 87 |
} |
| 89 | 88 |
|
| 89 |
+static int |
|
| 90 |
+redraw_handler(void *p) |
|
| 91 |
+{
|
|
| 92 |
+ SDL_Event event; |
|
| 93 |
+ event.type = redraw_event; |
|
| 94 |
+ for(;;) {
|
|
| 95 |
+ SDL_Delay(16); |
|
| 96 |
+ if(SDL_HasEvent(redraw_event) == SDL_FALSE) |
|
| 97 |
+ SDL_PushEvent(&event); |
|
| 98 |
+ } |
|
| 99 |
+ return 0; |
|
| 100 |
+ (void)p; |
|
| 101 |
+} |
|
| 102 |
+ |
|
| 90 | 103 |
static void |
| 91 | 104 |
set_window_size(SDL_Window *window, int w, int h) |
| 92 | 105 |
{
|
| ... | ... |
@@ -155,7 +168,9 @@ init(void) |
| 155 | 168 |
error("sdl_joystick", SDL_GetError());
|
| 156 | 169 |
stdin_event = SDL_RegisterEvents(1); |
| 157 | 170 |
audio0_event = SDL_RegisterEvents(POLYPHONY); |
| 171 |
+ redraw_event = SDL_RegisterEvents(1); |
|
| 158 | 172 |
SDL_CreateThread(stdin_handler, "stdin", NULL); |
| 173 |
+ SDL_CreateThread(redraw_handler, "redraw", NULL); |
|
| 159 | 174 |
SDL_StartTextInput(); |
| 160 | 175 |
SDL_ShowCursor(SDL_DISABLE); |
| 161 | 176 |
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); |
| ... | ... |
@@ -374,14 +389,13 @@ console_input(Uxn *u, char c) |
| 374 | 389 |
static int |
| 375 | 390 |
run(Uxn *u) |
| 376 | 391 |
{
|
| 392 |
+ SDL_Event event; |
|
| 377 | 393 |
Device *devsys = &u->dev[0]; |
| 378 | 394 |
redraw(); |
| 379 |
- while(!devsys->dat[0xf]) {
|
|
| 380 |
- SDL_Event event; |
|
| 381 |
- double elapsed, begin; |
|
| 382 |
- if(!BENCH) |
|
| 383 |
- begin = SDL_GetPerformanceCounter(); |
|
| 384 |
- while(SDL_PollEvent(&event) != 0) {
|
|
| 395 |
+ while(SDL_WaitEvent(&event)) {
|
|
| 396 |
+ /* .System/halt */ |
|
| 397 |
+ if(devsys->dat[0xf]) |
|
| 398 |
+ return error("Run", "Ended.");
|
|
| 385 | 399 |
/* Window */ |
| 386 | 400 |
if(event.type == SDL_QUIT) |
| 387 | 401 |
return error("Run", "Quit.");
|
| ... | ... |
@@ -420,8 +434,14 @@ run(Uxn *u) |
| 420 | 434 |
else |
| 421 | 435 |
do_shortcut(u, &event); |
| 422 | 436 |
ksym = event.key.keysym.sym; |
| 423 |
- if(SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, SDL_KEYUP, SDL_KEYUP) == 1 && ksym == event.key.keysym.sym) |
|
| 424 |
- break; |
|
| 437 |
+ while(SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, redraw_event, redraw_event) == 0) {
|
|
| 438 |
+ SDL_Delay(4); |
|
| 439 |
+ SDL_PumpEvents(); |
|
| 440 |
+ } |
|
| 441 |
+ if(SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, SDL_KEYUP, SDL_KEYUP) == 1 && ksym == event.key.keysym.sym) {
|
|
| 442 |
+ SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYUP, SDL_KEYUP); |
|
| 443 |
+ SDL_PushEvent(&event); |
|
| 444 |
+ } |
|
| 425 | 445 |
} else if(event.type == SDL_KEYUP) |
| 426 | 446 |
controller_up(devctrl, get_button(&event)); |
| 427 | 447 |
else if(event.type == SDL_JOYAXISMOTION) {
|
| ... | ... |
@@ -437,16 +457,14 @@ run(Uxn *u) |
| 437 | 457 |
/* Console */ |
| 438 | 458 |
else if(event.type == stdin_event) |
| 439 | 459 |
console_input(u, event.cbutton.button); |
| 440 |
- } |
|
| 441 |
- uxn_eval(u, GETVECTOR(devscreen)); |
|
| 442 |
- if(uxn_screen.fg.changed || uxn_screen.bg.changed) |
|
| 443 |
- redraw(); |
|
| 444 |
- if(!BENCH) {
|
|
| 445 |
- elapsed = (SDL_GetPerformanceCounter() - begin) / (double)SDL_GetPerformanceFrequency() * 1000.0f; |
|
| 446 |
- SDL_Delay(clamp(16.666f - elapsed, 0, 1000)); |
|
| 447 |
- } |
|
| 460 |
+ /* .Screen/vector and redraw */ |
|
| 461 |
+ else if(event.type == redraw_event) {
|
|
| 462 |
+ uxn_eval(u, GETVECTOR(devscreen)); |
|
| 463 |
+ if(uxn_screen.fg.changed || uxn_screen.bg.changed) |
|
| 464 |
+ redraw(); |
|
| 465 |
+ } |
|
| 448 | 466 |
} |
| 449 |
- return error("Run", "Ended.");
|
|
| 467 |
+ return error("SDL_WaitEvent", SDL_GetError());
|
|
| 450 | 468 |
} |
| 451 | 469 |
|
| 452 | 470 |
int |