added licence (GPLv3, and all previous versions except one uploaded to Abandonia...
[awish.git] / src / mainloop.c
blobe5f5b0700b524a063a98e243da315a4462b7c9c6
1 /*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include "mainloop.h"
21 #include "video.h"
22 #include "vm.h"
25 ////////////////////////////////////////////////////////////////////////////////
26 FrameCB frameCB = NULL;
27 KeyCB keyCB = NULL;
28 int vmPaused = 0;
31 ////////////////////////////////////////////////////////////////////////////////
32 static Uint32 cbFrameTimer (Uint32 interval, void *param) {
33 SDL_Event evt;
35 evt.type = SDL_USEREVENT;
37 //evt.user.type = SDL_USEREVENT;
38 evt.user.code = 0;
39 evt.user.data1 = NULL;
40 evt.user.data2 = NULL;
42 SDL_PushEvent(&evt);
43 //fprintf(stderr, "!\n");
44 return interval;
48 // <0: quit; >0: new frame
49 static int processEvents (void) {
50 SDL_Event event;
52 while (SDL_WaitEvent(&event)) {
53 switch (event.type) {
54 case SDL_QUIT: // the user want to quit
55 return -1;
56 case SDL_KEYDOWN:
57 case SDL_KEYUP:
58 if (event.key.keysym.sym == SDLK_F12) return -1; // quit
59 if (keyCB) keyCB(&event.key);
60 break;
61 case SDL_USEREVENT:
62 //while (SDL_PollEvent(NULL)) processEvents();
63 return 1;
64 default: ;
67 if (!vmIsThreadAlive(0)) return -1; // main thread is dead
68 return 0;
72 static void buildFrame (void) {
73 SurfaceLock lock;
75 lockSurface(&lock, screen);
76 if (frameCB) frameCB(screen);
77 unlockSurface(&lock);
78 SDL_Flip(screen);
82 void mainLoop (void) {
83 SDL_TimerID frameTimer = SDL_AddTimer(25, cbFrameTimer, NULL);
84 int eres = 1;
86 while (eres >= 0) {
87 if (eres > 0) {
88 if (!vmPaused) vmExecuteAll();
89 buildFrame();
91 eres = processEvents();
93 SDL_RemoveTimer(frameTimer);