fbee

view src/sys_sdl.c @ 0:88a2049be27b

fbee initial
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 05 Feb 2013 13:40:36 +0200
parents
children
line source
1 #include <SDL/SDL.h>
2 #include "fbee.h"
3 #include "fbeeimpl.h"
5 static int quit, dirty;
6 static SDL_Surface *fbsurf;
8 int fbee_sys_init(void)
9 {
10 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
11 quit = 0;
12 dirty = 1;
13 return 0;
14 }
16 int fbee_sys_set_video(int width, int height, int bpp, unsigned int flags)
17 {
18 unsigned int sdl_flags = SDL_HWSURFACE;
19 if(!(fbsurf = SDL_SetVideoMode(width, height, bpp, sdl_flags))) {
20 fprintf(stderr, "failed to set video mode\n");
21 return -1;
22 }
23 SDL_WM_SetCaption("fbee", "fbee");
24 return 0;
25 }
27 int fbee_sys_get_video(int *width, int *height, int *bpp)
28 {
29 if(fbsurf) {
30 *width = fbsurf->w;
31 *height = fbsurf->h;
32 *bpp = fbsurf->format->BitsPerPixel;
33 return 0;
34 }
35 return -1;
36 }
38 void fbee_sys_destroy(void)
39 {
40 SDL_Quit();
41 }
43 int fbee_sys_process_events(void)
44 {
45 SDL_Event ev;
47 if(dirty) {
48 struct closure *cb = fbee_get_callback(FBEE_EV_DRAW);
49 if(cb) {
50 cb->func(cb->cls);
51 dirty = 0;
52 }
53 }
55 if(!fbee_get_callback(FBEE_EV_IDLE)) {
56 if(SDL_WaitEvent(&ev)) {
57 SDL_PushEvent(&ev);
58 }
59 }
61 while(SDL_PollEvent(&ev)) {
62 struct closure *cb;
64 switch(ev.type) {
65 case SDL_KEYDOWN:
66 case SDL_KEYUP:
67 if((cb = fbee_get_callback(FBEE_EV_KEY))) {
68 int pressed = ev.key.state == SDL_PRESSED ? 1 : 0;
69 cb->func(ev.key.keysym.sym, pressed, cb->cls);
70 }
71 break;
73 case SDL_MOUSEBUTTONDOWN:
74 case SDL_MOUSEBUTTONUP:
75 if((cb = fbee_get_callback(FBEE_EV_BUTTON))) {
76 int pressed = ev.button.state == SDL_PRESSED ? 1 : 0;
77 int idx = ev.button.button - SDL_BUTTON_LEFT;
78 cb->func(idx, pressed, cb->cls);
79 }
80 break;
82 case SDL_MOUSEMOTION:
83 if((cb = fbee_get_callback(FBEE_EV_MOTION))) {
84 cb->func(ev.motion.x, ev.motion.y, cb->cls);
85 }
86 break;
88 case SDL_QUIT:
89 quit = 1;
90 return 0;
92 default:
93 break;
94 }
95 }
96 return 1;
97 }
99 void fbee_sys_evloop(void)
100 {
101 while(!quit && fbee_sys_process_events()) {
102 struct closure *cb = fbee_get_callback(FBEE_EV_IDLE);
103 if(cb) {
104 cb->func(cb->cls);
105 }
106 }
107 }
109 void fbee_redisplay(void)
110 {
111 dirty = 1;
112 }
114 void *fbee_sys_framebuffer(void)
115 {
116 return fbsurf->pixels;
117 }
119 void fbee_sys_update(void *img)
120 {
121 if(img && img != fbsurf->pixels) {
122 if(SDL_MUSTLOCK(fbsurf)) {
123 SDL_LockSurface(fbsurf);
124 }
126 memcpy(fbsurf->pixels, img, fbsurf->pitch * fbsurf->h);
128 if(SDL_MUSTLOCK(fbsurf)) {
129 SDL_UnlockSurface(fbsurf);
130 }
131 }
133 SDL_UpdateRect(fbsurf, 0, 0, 0, 0);
134 }