sgl

changeset 20:0697fbd075b6

added fallback SDL module
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 26 Jun 2011 07:56:13 +0300
parents 12ce0cef7ebf
children 5d38efd33da0
files Makefile.in src/wsys_cocoa.m src/wsys_glut.c src/wsys_sdl.c
diffstat 4 files changed, 284 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/Makefile.in	Sun Jun 26 02:30:37 2011 +0300
     1.2 +++ b/Makefile.in	Sun Jun 26 07:56:13 2011 +0300
     1.3 @@ -23,7 +23,7 @@
     1.4  
     1.5  AR = ar
     1.6  CC = gcc
     1.7 -CFLAGS = -pedantic -Wall -g -fPIC -Iinclude -Isrc
     1.8 +CFLAGS = -pedantic -Wall -g -fPIC -Iinclude -Isrc $(CFLAGS_extra)
     1.9  LDFLAGS = $(wsys_libs)
    1.10  
    1.11  .PHONY: all
    1.12 @@ -35,6 +35,9 @@
    1.13  $(lib_so): $(obj)
    1.14  	$(CC) $(sharedopt_$(sys)) -o $@ $(obj) $(LDFLAGS)
    1.15  
    1.16 +%.o: %.m
    1.17 +	$(CC) $(CFLAGS) -c -o $@ $<
    1.18 +
    1.19  -include $(dep)
    1.20  
    1.21  %.d: %.c
     2.1 --- a/src/wsys_cocoa.m	Sun Jun 26 02:30:37 2011 +0300
     2.2 +++ b/src/wsys_cocoa.m	Sun Jun 26 07:56:13 2011 +0300
     2.3 @@ -1,6 +1,5 @@
     2.4  /* SimplyGL window system module for Cocoa */
     2.5  /* mac-framework: -framework Cocoa */
     2.6 -/* link-with: `gnustep-config --gui-libs` */
     2.7  
     2.8  #include "config.h"
     2.9  
    2.10 @@ -449,4 +448,6 @@
    2.11  	attr[i++] = 0;
    2.12  }
    2.13  
    2.14 +#else
    2.15 +int sgl_wsys_cocoa_silence_the_fucking_empty_file_warnings;
    2.16  #endif	/* USE_WSYS_MODULE_COCOA */
     3.1 --- a/src/wsys_glut.c	Sun Jun 26 02:30:37 2011 +0300
     3.2 +++ b/src/wsys_glut.c	Sun Jun 26 07:56:13 2011 +0300
     3.3 @@ -63,7 +63,7 @@
     3.4  
     3.5  
     3.6  static struct wsys_module ws = {
     3.7 -	"glut", 1,
     3.8 +	"glut", 5,
     3.9  	init,
    3.10  	shutdown,
    3.11  	set_vidmode,
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/wsys_sdl.c	Sun Jun 26 07:56:13 2011 +0300
     4.3 @@ -0,0 +1,277 @@
     4.4 +/* SimplyGL window system module for SDL */
     4.5 +/* link-with: `sdl-config --libs` */
     4.6 +
     4.7 +#include "config.h"
     4.8 +
     4.9 +#ifdef USE_WSYS_MODULE_SDL
    4.10 +
    4.11 +#include <SDL/SDL.h>
    4.12 +#include "sgl.h"
    4.13 +#include "wsys.h"
    4.14 +
    4.15 +
    4.16 +
    4.17 +static int init(void);
    4.18 +static void shutdown(void);
    4.19 +
    4.20 +/* video mode switching */
    4.21 +static int set_vidmode(int xsz, int ysz);
    4.22 +static int get_vidmode(int *xsz, int *ysz);
    4.23 +
    4.24 +/* create/destroy windows */
    4.25 +static int create_window(int xsz, int ysz, unsigned int flags);
    4.26 +static void close_window(int wid);
    4.27 +
    4.28 +/* window management */
    4.29 +static int set_active(int wid);
    4.30 +static int set_title(const char *str);
    4.31 +static void redisplay(void);
    4.32 +static void swap_buffers(void);
    4.33 +
    4.34 +static int get_modifiers(void);
    4.35 +
    4.36 +/* event handling and friends */
    4.37 +static void set_event(int idx, int enable);
    4.38 +static int process_events(void);
    4.39 +static int handle_event(SDL_Event *ev);
    4.40 +
    4.41 +
    4.42 +static struct wsys_module ws = {
    4.43 +	"sdl", 6,
    4.44 +	init,
    4.45 +	shutdown,
    4.46 +	set_vidmode,
    4.47 +	get_vidmode,
    4.48 +	create_window,
    4.49 +	close_window,
    4.50 +	set_active,
    4.51 +	set_title,
    4.52 +	redisplay,
    4.53 +	swap_buffers,
    4.54 +	get_modifiers,
    4.55 +	set_event,
    4.56 +	process_events,
    4.57 +	0
    4.58 +};
    4.59 +
    4.60 +static int must_redisplay = 1;
    4.61 +static int win_width, win_height;
    4.62 +static unsigned int sdl_flags;
    4.63 +
    4.64 +
    4.65 +void sgl_register_sdl(void)
    4.66 +{
    4.67 +	sgl_register_module(&ws);
    4.68 +}
    4.69 +
    4.70 +
    4.71 +static int init(void)
    4.72 +{
    4.73 +	return SDL_Init(SDL_INIT_VIDEO);
    4.74 +}
    4.75 +
    4.76 +static void shutdown(void)
    4.77 +{
    4.78 +	SDL_Quit();
    4.79 +}
    4.80 +
    4.81 +/* video mode switching */
    4.82 +static int set_vidmode(int xsz, int ysz)
    4.83 +{
    4.84 +	return 0;	/* TODO */
    4.85 +}
    4.86 +
    4.87 +static int get_vidmode(int *xsz, int *ysz)
    4.88 +{
    4.89 +	return 0;	/* TODO */
    4.90 +}
    4.91 +
    4.92 +/* create/destroy windows */
    4.93 +static int create_window(int xsz, int ysz, unsigned int flags)
    4.94 +{
    4.95 +	void (*func)();
    4.96 +
    4.97 +	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    4.98 +	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    4.99 +	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
   4.100 +
   4.101 +	if(flags & SGL_DOUBLE) {
   4.102 +		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   4.103 +	}
   4.104 +	if(flags & SGL_DEPTH) {
   4.105 +		SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
   4.106 +	}
   4.107 +	if(flags & SGL_STENCIL) {
   4.108 +		SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
   4.109 +	}
   4.110 +	if(flags & SGL_STEREO) {
   4.111 +		SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
   4.112 +	}
   4.113 +	if(flags & SGL_MULTISAMPLE) {
   4.114 +		SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
   4.115 +		SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
   4.116 +	}
   4.117 +
   4.118 +	sdl_flags = SDL_OPENGL | SDL_RESIZABLE;
   4.119 +
   4.120 +	if(!SDL_SetVideoMode(xsz, ysz, 32, sdl_flags)) {
   4.121 +		return -1;
   4.122 +	}
   4.123 +	win_width = xsz;
   4.124 +	win_height = ysz;
   4.125 +	set_title("OpenGL/SDL");
   4.126 +
   4.127 +	if((func = sgl_get_callback(SGL_CREATE))) {
   4.128 +		func(0);
   4.129 +	}
   4.130 +	if((func = sgl_get_callback(SGL_RESHAPE))) {
   4.131 +		func(xsz, ysz);
   4.132 +	}
   4.133 +
   4.134 +	return 0;
   4.135 +}
   4.136 +
   4.137 +static void close_window(int wid)
   4.138 +{
   4.139 +	SDL_Quit();
   4.140 +}
   4.141 +
   4.142 +/* window management */
   4.143 +static int set_active(int wid)
   4.144 +{
   4.145 +	return 0;	/* only one window is supported in SDL */
   4.146 +}
   4.147 +
   4.148 +static int set_title(const char *str)
   4.149 +{
   4.150 +	SDL_WM_SetCaption(str, str);
   4.151 +	return 0;
   4.152 +}
   4.153 +
   4.154 +static void redisplay(void)
   4.155 +{
   4.156 +	must_redisplay = 1;
   4.157 +}
   4.158 +
   4.159 +static void swap_buffers(void)
   4.160 +{
   4.161 +	SDL_GL_SwapBuffers();
   4.162 +}
   4.163 +
   4.164 +static int get_modifiers(void)
   4.165 +{
   4.166 +	int mod = 0;
   4.167 +	SDLMod sdlmod = SDL_GetModState();
   4.168 +
   4.169 +	if(sdlmod & (KMOD_LSHIFT | KMOD_RSHIFT)) {
   4.170 +		mod |= SGL_MOD_SHIFT;
   4.171 +	}
   4.172 +	if(sdlmod & (KMOD_LCTRL | KMOD_RCTRL)) {
   4.173 +		mod |= SGL_MOD_CONTROL;
   4.174 +	}
   4.175 +	if(sdlmod & (KMOD_LALT | KMOD_RALT)) {
   4.176 +		mod |= SGL_MOD_ALT;
   4.177 +	}
   4.178 +	return mod;
   4.179 +}
   4.180 +
   4.181 +/* event handling and friends */
   4.182 +static void set_event(int idx, int enable)
   4.183 +{
   4.184 +	/* no need to do anything for SDL */
   4.185 +}
   4.186 +
   4.187 +static int process_events(void)
   4.188 +{
   4.189 +	int got_event;
   4.190 +	SDL_Event ev;
   4.191 +	sgl_idle_callback_t idle = sgl_get_callback(SGL_IDLE);
   4.192 +	sgl_display_callback_t disp = sgl_get_callback(SGL_DISPLAY);
   4.193 +
   4.194 +	if(must_redisplay && disp) {
   4.195 +		disp();
   4.196 +		must_redisplay = 0;
   4.197 +	}
   4.198 +
   4.199 +	if(idle) {
   4.200 +		got_event = SDL_PollEvent(&ev);
   4.201 +	} else {
   4.202 +		if(!SDL_WaitEvent(&ev)) {
   4.203 +			return -1;
   4.204 +		}
   4.205 +		got_event = 1;
   4.206 +	}
   4.207 +
   4.208 +	if(got_event) {
   4.209 +		do {
   4.210 +			if(handle_event(&ev) == -1) {
   4.211 +				return -1;
   4.212 +			}
   4.213 +		} while(SDL_PollEvent(&ev));
   4.214 +	}
   4.215 +
   4.216 +	if(idle) {
   4.217 +		idle();
   4.218 +	}
   4.219 +
   4.220 +	return 0;
   4.221 +}
   4.222 +
   4.223 +static int handle_event(SDL_Event *ev)
   4.224 +{
   4.225 +	void (*func)();
   4.226 +
   4.227 +	switch(ev->type) {
   4.228 +	case SDL_VIDEORESIZE:
   4.229 +		/* XXX this'll fuck up big time on windows, will lose the gl state! */
   4.230 +		SDL_SetVideoMode(ev->resize.w, ev->resize.h, 32, sdl_flags);
   4.231 +
   4.232 +		if((func = sgl_get_callback(SGL_RESHAPE)) &&
   4.233 +				(ev->resize.w != win_width || ev->resize.h != win_height)) {
   4.234 +			win_width = ev->resize.w;
   4.235 +			win_height = ev->resize.h;
   4.236 +
   4.237 +			func(win_width, win_height);
   4.238 +		}
   4.239 +		break;
   4.240 +
   4.241 +	case SDL_VIDEOEXPOSE:
   4.242 +		must_redisplay = 1;
   4.243 +		break;
   4.244 +
   4.245 +	case SDL_KEYDOWN:
   4.246 +	case SDL_KEYUP:
   4.247 +		if((func = sgl_get_callback(SGL_KEYBOARD))) {
   4.248 +			func(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
   4.249 +		}
   4.250 +		break;
   4.251 +
   4.252 +	case SDL_MOUSEBUTTONDOWN:
   4.253 +	case SDL_MOUSEBUTTONUP:
   4.254 +		if((func = sgl_get_callback(SGL_MOUSE))) {
   4.255 +			func(ev->button.which - 1, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
   4.256 +		}
   4.257 +		break;
   4.258 +
   4.259 +	case SDL_MOUSEMOTION:
   4.260 +		if(ev->motion.state) {
   4.261 +			func = sgl_get_callback(SGL_MOTION);
   4.262 +		} else {
   4.263 +			func = sgl_get_callback(SGL_PASSIVE);
   4.264 +		}
   4.265 +
   4.266 +		if(func) {
   4.267 +			func(ev->motion.x, ev->motion.y);
   4.268 +		}
   4.269 +		break;
   4.270 +
   4.271 +	case SDL_QUIT:
   4.272 +		return -1;
   4.273 +	}
   4.274 +
   4.275 +	return 0;
   4.276 +}
   4.277 +
   4.278 +#else
   4.279 +int sgl_wsys_sdl_silence_the_fucking_empty_file_warnings;
   4.280 +#endif	/* USE_WSYS_MODULE_SDL */