added 'videolib' snapshot
[k8-jellyphysics.git] / src / videolib / videolib_mainloop.c
blob9b69587364b47fb2f07a46cdc97607aabf52991f
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://sam.zoy.org/wtfpl/COPYING for more details.
9 */
10 #include "videolib.h"
11 #ifdef VIDEOLIB_ENABLE_MAIN_LOOP
14 ////////////////////////////////////////////////////////////////////////////////
15 // main loop
16 int vl_fps = 60;
19 ////////////////////////////////////////////////////////////////////////////////
20 void (*vl_update) (int elapsed_ms) = NULL;
21 void (*vl_rebuild) (void) = NULL;
22 int (*vl_preprocess_event) (SDL_Event *ev) = NULL;
23 void (*vl_postprocess_event) (SDL_Event *ev) = NULL;
24 void (*vl_process_keydown) (SDL_KeyboardEvent *ev) = NULL;
25 void (*vl_process_keyup) (SDL_KeyboardEvent *ev) = NULL;
26 void (*vl_process_mousedown) (SDL_MouseButtonEvent *ev) = NULL;
27 void (*vl_process_mouseup) (SDL_MouseButtonEvent *ev) = NULL;
28 void (*vl_process_mousemotion) (SDL_MouseMotionEvent *ev) = NULL;
29 void (*vl_process_mousewheel) (SDL_MouseWheelEvent *ev) = NULL;
32 ////////////////////////////////////////////////////////////////////////////////
33 static SDL_TimerID sdl_tidframe = 0;
34 static Uint64 last_update_call = 0;
37 ////////////////////////////////////////////////////////////////////////////////
38 static Uint32 timer_cb (Uint32 interval, void *param) {
39 SDL_Event event;
40 event.type = SDL_USEREVENT;
41 event.user.code = VL_EVT_SHOW_FRAME;
42 event.user.data1 = NULL;
43 event.user.data2 = NULL;
44 SDL_PushEvent(&event);
45 return interval;
49 ////////////////////////////////////////////////////////////////////////////////
50 /* return !0 to stop event loop; <0: from event preprocessor; >0: from SDL_QUIT */
51 int vl_process_event (SDL_Event *ev) {
52 Uint64 tm;
53 if (ev == NULL) return 0;
54 if (vl_preprocess_event != NULL) {
55 int res = vl_preprocess_event(ev);
56 if (res < 0) return -1;
57 if (res > 0) return 0;
59 if (ev->type == SDL_QUIT) return 1;
60 switch (ev->type) {
61 case SDL_KEYDOWN: if (vl_process_keydown != NULL) vl_process_keydown(&ev->key); break;
62 case SDL_KEYUP: if (vl_process_keyup != NULL) vl_process_keyup(&ev->key); break;
63 case SDL_MOUSEBUTTONDOWN: if (vl_process_mousedown != NULL) vl_process_mousedown(&ev->button); break;
64 case SDL_MOUSEBUTTONUP: if (vl_process_mouseup != NULL) vl_process_mouseup(&ev->button); break;
65 case SDL_MOUSEMOTION: if (vl_process_mousemotion != NULL) vl_process_mousemotion(&ev->motion); break;
66 case SDL_MOUSEWHEEL: if (vl_process_mousewheel != NULL) vl_process_mousewheel(&ev->wheel); break;
67 case SDL_USEREVENT:
68 switch (ev->user.code) {
69 case VL_EVT_SHOW_FRAME:
70 tm = vl_clock_milli();
71 if (vl_update != NULL) vl_update(tm-last_update_call);
72 last_update_call = tm;
73 if (sdl_frame_changed) {
74 if (vl_rebuild != NULL) vl_rebuild();
75 paint_screen();
77 break;
79 break;
80 case SDL_WINDOWEVENT:
81 switch (ev->window.event) {
82 case SDL_WINDOWEVENT_EXPOSED:
83 paint_screen();
84 break;
87 return 0;
91 void vl_start_fps_timer (void) {
92 int fps = vl_fps;
93 if (sdl_tidframe != 0) SDL_RemoveTimer(sdl_tidframe);
94 if (fps < 1) fps = 1; else if (fps > 1000) fps = 1000;
95 vl_fps = fps;
96 sdl_tidframe = SDL_AddTimer(1000/fps, timer_cb, NULL);
100 void vl_stop_fps_timer (void) {
101 if (sdl_tidframe != 0) SDL_RemoveTimer(sdl_tidframe);
102 sdl_tidframe = 0;
106 void vl_main_loop (void) {
107 SDL_Event ev;
108 if (vl_update != NULL) vl_update(0);
109 if (vl_rebuild != NULL) vl_rebuild();
110 paint_screen();
111 last_update_call = vl_clock_milli();
112 vl_start_fps_timer();
113 while (SDL_WaitEvent(&ev)) {
114 int res = vl_process_event(&ev);
115 if (res) break;
117 vl_stop_fps_timer();
119 #endif