This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, master has been updated via 94a7d269628a94cf585ed33951bac25b370f20cc (commit) from 48a02d9f6947b24c9b1ea70effaeee5191bf454a (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/94a7d269628a94cf585ed33951bac25b370f2...
commit 94a7d269628a94cf585ed33951bac25b370f20cc Author: BlueBear jiri.bluebear.dluhos@gmail.com Date: Sun May 22 02:47:54 2011 +0200
First try on backend event API.
diff --git a/backends/GP_Backend_SDL.c b/backends/GP_Backend_SDL.c index 17a3d20..af746c1 100644 --- a/backends/GP_Backend_SDL.c +++ b/backends/GP_Backend_SDL.c @@ -32,6 +32,18 @@ #include <SDL/SDL.h>
struct GP_Backend GP_SDL_backend; +static SDL_Surface *GP_SDL_display = NULL; +static GP_Context GP_SDL_context; + +/* Functions from the SDL library, dynamically loaded in GP_SDL_InitFn(). */ +static void (*dyn_SDL_Quit)(void); +static int (*dyn_SDL_Init)(int); +static SDL_Surface *(*dyn_SDL_SetVideoMode)(int, int, int, uint32_t); +static void (*dyn_SDL_UpdateRect)(SDL_Surface *, int, int, int, int); +static int (*dyn_SDL_WaitEvent)(SDL_Event *); + +/* User callbacks. */ +static void (*GP_SDL_update_video_callback)(void) = NULL;
/* * Checks whether pixel color component masks in the given surface are equal @@ -142,14 +154,6 @@ inline GP_RetCode GP_SDL_ContextFromSurface( return GP_ESUCCESS; }
-static SDL_Surface *GP_SDL_display = NULL; -static GP_Context GP_SDL_context; - -static void (*dyn_SDL_Quit)(void); -static int (*dyn_SDL_Init)(int); -static SDL_Surface *(*dyn_SDL_SetVideoMode)(int, int, int, uint32_t); -static void (*dyn_SDL_UpdateRect)(SDL_Surface *, int, int, int, int); - static void GP_SDL_ShutdownFn(void) { dyn_SDL_Quit(); @@ -165,6 +169,7 @@ static struct GP_Backend *GP_SDL_InitFn(void) dyn_SDL_Quit = dlsym(library, "SDL_Quit"); dyn_SDL_UpdateRect = dlsym(library, "SDL_UpdateRect"); dyn_SDL_SetVideoMode = dlsym(library, "SDL_SetVideoMode"); + dyn_SDL_WaitEvent = dlsym(library, "SDL_WaitEvent");
if (dyn_SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) return NULL; @@ -199,13 +204,34 @@ static void GP_SDL_UpdateVideoFn(void) GP_SDL_display->w, GP_SDL_display->h); }
+static int GP_SDL_GetEventFn(struct GP_BackendEvent *event) +{ + SDL_Event sdl_event; + + if (dyn_SDL_WaitEvent(&sdl_event) == 0) + return 0; + + switch (sdl_event.type) { + case SDL_VIDEOEXPOSE: + event->type = GP_BACKEND_EVENT_UPDATE_VIDEO; + return 1; + case SDL_QUIT: + event->type = GP_BACKEND_EVENT_QUIT_REQUEST; + return 1; + } + + /* for the time being, unknown events are simply ignored */ + return 0; +} + struct GP_Backend GP_SDL_backend = { - "SDL", - GP_SDL_InitFn, - GP_SDL_ShutdownFn, - GP_SDL_OpenVideoFn, - GP_SDL_VideoContextFn, - GP_SDL_UpdateVideoFn + .name = "SDL", + .init_fn = GP_SDL_InitFn, + .shutdown_fn = GP_SDL_ShutdownFn, + .open_video_fn = GP_SDL_OpenVideoFn, + .video_context_fn = GP_SDL_VideoContextFn, + .update_video_fn = GP_SDL_UpdateVideoFn, + .get_event_fn = GP_SDL_GetEventFn, };
#endif /* GP_HAVE_SDL */ diff --git a/core/GP_Backend.c b/core/GP_Backend.c index f1cbce5..12d2e5d 100644 --- a/core/GP_Backend.c +++ b/core/GP_Backend.c @@ -28,7 +28,9 @@
#include <string.h>
-/* The current backend. */ +/* + * The currently active backend (NULL if none). + */ static struct GP_Backend *current_backend = NULL;
#ifdef GP_HAVE_SDL @@ -76,3 +78,9 @@ void GP_UpdateBackendVideo(void) GP_CHECK(current_backend, "no current backend"); return current_backend->update_video_fn(); } + +int GP_GetBackendEvent(struct GP_BackendEvent *event) +{ + GP_CHECK(current_backend, "no current backend"); + return current_backend->get_event_fn(event); +} diff --git a/core/GP_Backend.h b/core/GP_Backend.h index afe7f67..d1dff51 100644 --- a/core/GP_Backend.h +++ b/core/GP_Backend.h @@ -28,6 +28,23 @@
#include "GP_Context.h"
+/* + * Types of events provided by the backend. + */ +enum GP_BackendEventType { + GP_BACKEND_EVENT_NONE = 0, + GP_BACKEND_EVENT_UPDATE_VIDEO = 1, /* video redraw is needed */ + GP_BACKEND_EVENT_QUIT_REQUEST = 2, /* user requests quitting */ +}; + +/* + * Structure describing an event reported by a backend. + */ +struct GP_BackendEvent { + enum GP_BackendEventType type; +}; + +/* Describes a backend and holds all its API functions. */ struct GP_Backend { const char *name; struct GP_Backend *(*init_fn)(void); @@ -35,13 +52,14 @@ struct GP_Backend { GP_Context *(*open_video_fn)(int w, int h, int flags); GP_Context *(*video_context_fn)(void); void (*update_video_fn)(void); + int (*get_event_fn)(struct GP_BackendEvent *event); };
/* * Attempts to initialize a backend. * * If name is specified, only that backend is tried; if name is NULL, - * all known backends are tried and the first usable is picked. + * all known backends are tried and the first usable one is picked. * * Returns the backend structure, or NULL on failure. */ @@ -65,4 +83,10 @@ GP_Context *GP_GetBackendVideoContext(void); */ void GP_UpdateBackendVideo(void);
+/* + * Reads the first pending backend event. + * Returns 0 if no events were pending, 1 on success. + */ +int GP_GetBackendEvent(struct GP_BackendEvent *event); + #endif diff --git a/targets/sdl/tests/sierpinsky.c b/targets/sdl/tests/sierpinsky.c index 99fc99a..1136a45 100644 --- a/targets/sdl/tests/sierpinsky.c +++ b/targets/sdl/tests/sierpinsky.c @@ -41,7 +41,7 @@ #define sgn(x) ((x)>0 ? 1 : -1)
SDL_Surface *display; -GP_Context context; +GP_Context *context;
SDL_TimerID timer;
@@ -51,15 +51,15 @@ GP_Pixel black, blue, gray, red;
int draw_edge = 1;
-static void sierpinsky(SDL_Surface *surf, double x1, double y1, double x4, double y4, int iter) +static void sierpinsky(double x1, double y1, double x4, double y4, int iter) { double x2, y2, x3, y3, x5, y5; GP_Pixel pixel; - GP_RGBToPixel(&context, 0, 0, 255-16*iter, &pixel); + GP_RGBToPixel(context, 0, 0, 255-16*iter, &pixel); if (iter <= 0) { if (draw_edge) - GP_Line(&context, x1, y1, x4, y4, black); + GP_Line(context, x1, y1, x4, y4, black); return; }
@@ -72,23 +72,23 @@ static void sierpinsky(SDL_Surface *surf, double x1, double y1, double x4, doubl x5 = (x1+x4)/2 + (y2 - y3)*sqrt(3.00/4); y5 = (y1+y4)/2 + (x3 - x2)*sqrt(3.00/4);
- GP_FillTriangle(&context, x2, y2, x3, y3, x5, y5, pixel); + GP_FillTriangle(context, x2, y2, x3, y3, x5, y5, pixel);
- GP_PutPixel(&context, x2, y2, red); - GP_PutPixel(&context, x3, y3, red); - GP_PutPixel(&context, x5, y5, red); + GP_PutPixel(context, x2, y2, red); + GP_PutPixel(context, x3, y3, red); + GP_PutPixel(context, x5, y5, red);
- sierpinsky(surf, x1, y1, x2, y2, iter - 1); - sierpinsky(surf, x2, y2, x5, y5, iter - 1); - sierpinsky(surf, x5, y5, x3, y3, iter - 1); - sierpinsky(surf, x3, y3, x4, y4, iter - 1); + sierpinsky(x1, y1, x2, y2, iter - 1); + sierpinsky(x2, y2, x5, y5, iter - 1); + sierpinsky(x5, y5, x3, y3, iter - 1); + sierpinsky(x3, y3, x4, y4, iter - 1); }
-static void draw(SDL_Surface *surf, int x, int y, int l, int iter) +static void draw(int x, int y, int l, int iter) { double x1, y1, x2, y2, x3, y3; - int w = surf->w; - int h = surf->h; + int w = context->w; + int h = context->h;
l = ((w < h ? w : h) - 20)/(5 - 1.00*iter/120); @@ -101,15 +101,15 @@ static void draw(SDL_Surface *surf, int x, int y, int l, int iter) x3 = sin(1.00 * (iter+240)/57) * l + x; y3 = cos(1.00 * (iter+240)/57) * l + y;
- GP_Fill(&context, gray); + GP_Fill(context, gray);
- GP_FillTriangle(&context, x1, y1, x2, y2, x3, y3, blue); + GP_FillTriangle(context, x1, y1, x2, y2, x3, y3, blue);
- sierpinsky(surf, x1, y1, x2, y2, iter/60%6); - sierpinsky(surf, x2, y2, x3, y3, iter/60%6); - sierpinsky(surf, x3, y3, x1, y1, iter/60%6); - - SDL_UpdateRect(surf, 0, 0, surf->w, surf->h); + sierpinsky(x1, y1, x2, y2, iter/60%6); + sierpinsky(x2, y2, x3, y3, iter/60%6); + sierpinsky(x3, y3, x1, y1, iter/60%6); + + GP_UpdateBackendVideo(); }
int paused = 0; @@ -127,7 +127,7 @@ Uint32 timer_callback(Uint32 interval __attribute__ ((unused)), void *ptr __attr if (iter < 2 * way) way *= 1; - draw(display, display->w/2, display->h/2, l, iter); + draw(display->w/2, display->h/2, l, iter);
return TIMER_TICK; } @@ -136,6 +136,16 @@ int main(void) { SDL_Event ev;
+ if (!GP_InitBackend("SDL")) { + fprintf(stderr, "error: could not initialize backendn"); + return 2; + } + if (!GP_OpenBackendVideo(DISPLAY_W, DISPLAY_H, 0)) { + fprintf(stderr, "error: could not open videon"); + return 2; + } + +/* if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) return -1;
@@ -147,14 +157,18 @@ int main(void) }
GP_SDL_ContextFromSurface(&context, display); +*/ + + display = SDL_GetVideoSurface(); + context = GP_GetBackendVideoContext();
- GP_ColorNameToPixel(&context, GP_COL_BLACK, &black); - GP_ColorNameToPixel(&context, GP_COL_BLUE, &blue); - GP_ColorNameToPixel(&context, GP_COL_GRAY_LIGHT, &gray); - GP_ColorNameToPixel(&context, GP_COL_RED, &red); + GP_ColorNameToPixel(context, GP_COL_BLACK, &black); + GP_ColorNameToPixel(context, GP_COL_BLUE, &blue); + GP_ColorNameToPixel(context, GP_COL_GRAY_LIGHT, &gray); + GP_ColorNameToPixel(context, GP_COL_RED, &red);
iter = 0; - draw(display, display->w/2, display->h/2, l, iter); + draw(display->w/2, display->h/2, l, iter);
timer = SDL_AddTimer(0, timer_callback, NULL);
-----------------------------------------------------------------------
Summary of changes: backends/GP_Backend_SDL.c | 54 +++++++++++++++++++++++-------- core/GP_Backend.c | 10 +++++- core/GP_Backend.h | 26 ++++++++++++++- targets/sdl/tests/sierpinsky.c | 70 ++++++++++++++++++++++++---------------- 4 files changed, 116 insertions(+), 44 deletions(-)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.