summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-07-19 08:59:48 -0500
committerKamil Kaminski <kamilkss@gmail.com>2011-07-19 08:59:48 -0500
commit09d12523ad298f7d58bf2948af5275ddd6caa976 (patch)
treebd9822c055a427613c81ec6249f696172bc9e470
parentc36922caef3098f9a1f62712d57012822ee2145a (diff)
downloadGLPyramid-09d12523ad298f7d58bf2948af5275ddd6caa976.tar.gz
GLPyramid-09d12523ad298f7d58bf2948af5275ddd6caa976.tar.bz2
GLPyramid-09d12523ad298f7d58bf2948af5275ddd6caa976.zip
sdl: introduce a vtbl for event handler
-rw-r--r--sdl/event.c26
-rw-r--r--sdl/event.h8
-rw-r--r--sdl/platform.c12
3 files changed, 43 insertions, 3 deletions
diff --git a/sdl/event.c b/sdl/event.c
index e6c4c00..c3120af 100644
--- a/sdl/event.c
+++ b/sdl/event.c
@@ -2,7 +2,7 @@
*
* Event Handler
*
- * notes: I would be useful to create a following function
+ * notes: It would be useful to create a following function
* register_signal(SDL_HOLDDOWN, EVENT_ACTION_INCR, float *p, float val)
*
* struct platform is still passed globally
@@ -20,6 +20,23 @@
extern struct platform p;
extern int program_running;
+/* our function table */
+static struct event_vtbl event_tbl;
+
+int event_handler_init(struct event_vtbl *tbl)
+{
+ if (!tbl)
+ return -1;
+
+ event_tbl = *tbl;
+
+ /* predefined handlers that are automatically provided */
+ if (!event_tbl.windowresize)
+ event_tbl.windowresize = window_resize;
+
+ return 0;
+}
+
/* process SDL events */
void sdl_process_events(void)
{
@@ -46,12 +63,14 @@ void sdl_process_events(void)
case SDL_KEYDOWN:
{
keys_held[sym] = 1;
- event_keydown(&event.key.keysym, keys_held, flag);
+ if (event_tbl.keydown)
+ event_tbl.keydown(&event.key.keysym, keys_held, flag);
break;
}
case SDL_VIDEORESIZE:
{
- window_resize(event.resize.w, event.resize.h);
+ if (event_tbl.windowresize)
+ event_tbl.windowresize(event.resize.w, event.resize.h);
break;
}
case SDL_QUIT:
@@ -76,6 +95,7 @@ void sdl_process_events(void)
flag = !flag;
}
+/* here are the event handler functions */
void event_keydown(SDL_keysym *keysym, const unsigned int *keys_held, const int flag)
{
if (!flag)
diff --git a/sdl/event.h b/sdl/event.h
index 8b4d81b..468087b 100644
--- a/sdl/event.h
+++ b/sdl/event.h
@@ -1,7 +1,15 @@
#ifndef _EVENT_H_
#define _EVENT_H_
+/* a table holding function pointers to function that handle events */
+struct event_vtbl
+{
+ void (*keydown)(SDL_keysym *, const unsigned int *, const int);
+ void (*windowresize)(int, int);
+};
+
/* function prototypes */
+int event_handler_init(struct event_vtbl *);
void sdl_process_events(void);
void event_keydown(SDL_keysym *, const unsigned int *, const int);
diff --git a/sdl/platform.c b/sdl/platform.c
index ea9dce6..138596a 100644
--- a/sdl/platform.c
+++ b/sdl/platform.c
@@ -12,11 +12,13 @@
#include <SDL/SDL_image.h>
#include <GL/glew.h>
#include <string.h>
+#include "event.h"
#include "gldraw.h"
#include "gltools.h"
#include "luatools.h"
#include "platform.h"
#include "sdltools.h"
+#include "window.h"
/* few light arrays */
const GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 1.0f };
@@ -40,6 +42,8 @@ extern unsigned int maxfps_w;
void platform_init(struct platform *p)
{
+ int ret;
+
/* Lua */
/* create new lua state */
p->L = luaL_newstate();
@@ -63,6 +67,14 @@ void platform_init(struct platform *p)
/* set the camera to <0,0,0> */
glframe_reset(&p->camera);
+ /* setup and init the event handler */
+ struct event_vtbl event_tbl;
+ memset(&event_tbl, 0, sizeof(event_tbl));
+ event_tbl.keydown = event_keydown;
+ ret = event_handler_init(&event_tbl);
+ if (ret == -1)
+ fprintf(stderr, "platform: warning, failed to init event handler\n");
+
/* init client */
p->client_init(p->c);
}