fbee

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sys_sdl.c	Tue Feb 05 13:40:36 2013 +0200
     1.3 @@ -0,0 +1,134 @@
     1.4 +#include <SDL/SDL.h>
     1.5 +#include "fbee.h"
     1.6 +#include "fbeeimpl.h"
     1.7 +
     1.8 +static int quit, dirty;
     1.9 +static SDL_Surface *fbsurf;
    1.10 +
    1.11 +int fbee_sys_init(void)
    1.12 +{
    1.13 +	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    1.14 +	quit = 0;
    1.15 +	dirty = 1;
    1.16 +	return 0;
    1.17 +}
    1.18 +
    1.19 +int fbee_sys_set_video(int width, int height, int bpp, unsigned int flags)
    1.20 +{
    1.21 +	unsigned int sdl_flags = SDL_HWSURFACE;
    1.22 +	if(!(fbsurf = SDL_SetVideoMode(width, height, bpp, sdl_flags))) {
    1.23 +		fprintf(stderr, "failed to set video mode\n");
    1.24 +		return -1;
    1.25 +	}
    1.26 +	SDL_WM_SetCaption("fbee", "fbee");
    1.27 +	return 0;
    1.28 +}
    1.29 +
    1.30 +int fbee_sys_get_video(int *width, int *height, int *bpp)
    1.31 +{
    1.32 +	if(fbsurf) {
    1.33 +		*width = fbsurf->w;
    1.34 +		*height = fbsurf->h;
    1.35 +		*bpp = fbsurf->format->BitsPerPixel;
    1.36 +		return 0;
    1.37 +	}
    1.38 +	return -1;
    1.39 +}
    1.40 +
    1.41 +void fbee_sys_destroy(void)
    1.42 +{
    1.43 +	SDL_Quit();
    1.44 +}
    1.45 +
    1.46 +int fbee_sys_process_events(void)
    1.47 +{
    1.48 +	SDL_Event ev;
    1.49 +
    1.50 +	if(dirty) {
    1.51 +		struct closure *cb = fbee_get_callback(FBEE_EV_DRAW);
    1.52 +		if(cb) {
    1.53 +			cb->func(cb->cls);
    1.54 +			dirty = 0;
    1.55 +		}
    1.56 +	}
    1.57 +
    1.58 +	if(!fbee_get_callback(FBEE_EV_IDLE)) {
    1.59 +		if(SDL_WaitEvent(&ev)) {
    1.60 +			SDL_PushEvent(&ev);
    1.61 +		}
    1.62 +	}
    1.63 +
    1.64 +	while(SDL_PollEvent(&ev)) {
    1.65 +		struct closure *cb;
    1.66 +
    1.67 +		switch(ev.type) {
    1.68 +		case SDL_KEYDOWN:
    1.69 +		case SDL_KEYUP:
    1.70 +			if((cb = fbee_get_callback(FBEE_EV_KEY))) {
    1.71 +				int pressed = ev.key.state == SDL_PRESSED ? 1 : 0;
    1.72 +				cb->func(ev.key.keysym.sym, pressed, cb->cls);
    1.73 +			}
    1.74 +			break;
    1.75 +
    1.76 +		case SDL_MOUSEBUTTONDOWN:
    1.77 +		case SDL_MOUSEBUTTONUP:
    1.78 +			if((cb = fbee_get_callback(FBEE_EV_BUTTON))) {
    1.79 +				int pressed = ev.button.state == SDL_PRESSED ? 1 : 0;
    1.80 +				int idx = ev.button.button - SDL_BUTTON_LEFT;
    1.81 +				cb->func(idx, pressed, cb->cls);
    1.82 +			}
    1.83 +			break;
    1.84 +
    1.85 +		case SDL_MOUSEMOTION:
    1.86 +			if((cb = fbee_get_callback(FBEE_EV_MOTION))) {
    1.87 +				cb->func(ev.motion.x, ev.motion.y, cb->cls);
    1.88 +			}
    1.89 +			break;
    1.90 +
    1.91 +		case SDL_QUIT:
    1.92 +			quit = 1;
    1.93 +			return 0;
    1.94 +
    1.95 +		default:
    1.96 +			break;
    1.97 +		}
    1.98 +	}
    1.99 +	return 1;
   1.100 +}
   1.101 +
   1.102 +void fbee_sys_evloop(void)
   1.103 +{
   1.104 +	while(!quit && fbee_sys_process_events()) {
   1.105 +		struct closure *cb = fbee_get_callback(FBEE_EV_IDLE);
   1.106 +		if(cb) {
   1.107 +			cb->func(cb->cls);
   1.108 +		}
   1.109 +	}
   1.110 +}
   1.111 +
   1.112 +void fbee_redisplay(void)
   1.113 +{
   1.114 +	dirty = 1;
   1.115 +}
   1.116 +
   1.117 +void *fbee_sys_framebuffer(void)
   1.118 +{
   1.119 +	return fbsurf->pixels;
   1.120 +}
   1.121 +
   1.122 +void fbee_sys_update(void *img)
   1.123 +{
   1.124 +	if(img && img != fbsurf->pixels) {
   1.125 +		if(SDL_MUSTLOCK(fbsurf)) {
   1.126 +			SDL_LockSurface(fbsurf);
   1.127 +		}
   1.128 +
   1.129 +		memcpy(fbsurf->pixels, img, fbsurf->pitch * fbsurf->h);
   1.130 +
   1.131 +		if(SDL_MUSTLOCK(fbsurf)) {
   1.132 +			SDL_UnlockSurface(fbsurf);
   1.133 +		}
   1.134 +	}
   1.135 +
   1.136 +	SDL_UpdateRect(fbsurf, 0, 0, 0, 0);
   1.137 +}