remove umbrella sprite on level start
[awish.git] / src / mainloop.c
blobd918f3b4f85343ceec45de5ea5dc844acbc5ac81
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "mainloop.h"
7 #include "video.h"
8 #include "vm.h"
11 ////////////////////////////////////////////////////////////////////////////////
12 FrameCB frameCB = NULL;
13 KeyCB keyCB = NULL;
14 int vmPaused = 0;
17 ////////////////////////////////////////////////////////////////////////////////
18 static Uint32 cbFrameTimer (Uint32 interval, void *param) {
19 SDL_Event evt;
21 evt.type = SDL_USEREVENT;
23 //evt.user.type = SDL_USEREVENT;
24 evt.user.code = 0;
25 evt.user.data1 = NULL;
26 evt.user.data2 = NULL;
28 SDL_PushEvent(&evt);
29 //fprintf(stderr, "!\n");
30 return interval;
34 // <0: quit; >0: new frame
35 static int processEvents (void) {
36 SDL_Event event;
38 while (SDL_WaitEvent(&event)) {
39 switch (event.type) {
40 case SDL_QUIT: // the user want to quit
41 return -1;
42 case SDL_KEYDOWN:
43 case SDL_KEYUP:
44 if (event.key.keysym.sym == SDLK_F12) return -1; // quit
45 if (keyCB) keyCB(&event.key);
46 break;
47 case SDL_USEREVENT:
48 //while (SDL_PollEvent(NULL)) processEvents();
49 return 1;
50 default: ;
53 if (!vmIsThreadAlive(0)) return -1; // main thread is dead
54 return 0;
58 static void buildFrame (void) {
59 SurfaceLock lock;
61 lockSurface(&lock, screen);
62 if (frameCB) frameCB(screen);
63 unlockSurface(&lock);
64 SDL_Flip(screen);
68 void mainLoop (void) {
69 SDL_TimerID frameTimer = SDL_AddTimer(25, cbFrameTimer, NULL);
70 int eres = 1;
72 while (eres >= 0) {
73 if (eres > 0) {
74 if (!vmPaused) vmExecuteAll();
75 buildFrame();
77 eres = processEvents();
79 SDL_RemoveTimer(frameTimer);