changeset: 6:87a34b9b4a6f
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Tue Apr 20 10:19:39 2010 +0200
files: Makefile app1.c app2.c draw.c draw.h gi_sdl.c swk.c swk.h test.c
description:
revert last commit
refactorize some code and remove draw.{c|h}
handle mouse events in sdl backend
extended example program
diff -r fcdc9b71b256 -r 87a34b9b4a6f Makefile
--- a/Makefile Tue Apr 20 00:29:19 2010 +0100
+++ b/Makefile Tue Apr 20 10:19:39 2010 +0200
@@ -8,7 +8,7 @@
GI?=sdl
GI_LIBS=-lSDL
-GI_OBJS=gi_${GI}.o draw.o
+GI_OBJS=gi_${GI}.o
all: static test
diff -r fcdc9b71b256 -r 87a34b9b4a6f app1.c
--- a/app1.c Tue Apr 20 00:29:19 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-void (*swkhandler)(SwkWidget *, SwkEvent *);
-
-void onclick(SwkWidget *w);
-
-SwkTextField t = {
- SwkTypeTextField,
- "enter some text",
- NULL
-};
-SwkButton b = {
- SwkTypeButton,
- "OK",
- NULL
-};
-SwkWin w = {
- SwkTypeWin,
- { &t, &b, NULL }
-};
-
-void onclick(SwkWidget *w) {
- static int clicks = 0;
- SwkButton *b = (SwkButton *)w;
- fprintf(stdout, "onclick: %s\n", t.data);
- if(++clicks == 10)
- swk_exit();
-}
-
-static void myhandler(SwkWidget *w, SwkEvent *e) {
- if(e->type == SWK_CLICK && w->type == SwkTypeButton)
- onclick(w);
- else /* fallback */
- swkhandler(w, e);
-}
-
-int main() {
- swk_init(&w);
- swkhandler = swk_set_handler(myhandler);
- swk_loop(NULL); // loops until swk_exit() is called
- return 0;
-}
diff -r fcdc9b71b256 -r 87a34b9b4a6f app2.c
--- a/app2.c Tue Apr 20 00:29:19 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-void (*swkhandler)(SwkWidget *, SwkEvent *);
-
-void onclick(SwkWidget *w);
-
-SwkTextField t = {
- SwkTypeTextField,
- "enter some text",
- NULL
-};
-SwkButton b = {
- SwkTypeButton,
- "OK",
- NULL
-};
-SwkWin w = {
- SwkTypeWin,
- { &t, &b, NULL }
-};
-
-void onclick(SwkWidget *w) {
- static int clicks = 0;
- SwkButton *b = (SwkButton *)w;
- fprintf(stdout, "onclick: %s\n", t.data);
- if(++clicks == 10)
- swk_exit();
-}
-
-static void myhandler(SwkWidget *w, SwkEvent *e) {
- if(e->type == SWK_CLICK && w->type == SwkTypeButton)
- onclick(w);
- else /* fallback */
- swkhandler(w, e);
-}
-
-int main() {
- swk_init(&w);
- swkhandler = swk_set_handler(myhandler);
- swk_loop(NULL); // loops until swk_exit() is called
- return 0;
-}
diff -r fcdc9b71b256 -r 87a34b9b4a6f draw.c
--- a/draw.c Tue Apr 20 00:29:19 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-#include <stdlib.h>
-#include "draw.h"
-
-static DrawBuffer buf;
-
-int
-draw_init(int w, int h) {
- buf.pixels = malloc(sizeof(int)*w*h);
- buf.w = w;
- buf.h = h;
- return swk_gi_init(w, h);
-}
-
-void
-swk_exit() {
- free (buf.pixels);
- swk_gi_exit();
-}
-
-/* TODO: move to gi_sdl.c */
-void
-draw_line(int x, int y, int w, int h) {
-}
-
-void
-draw_box(int x, int y, int w, int h) {
-}
-
-void
-draw_rect(int x, int y, int w, int h) {
-}
-
-void
-draw_text(int x, int y, const char *text) {
-}
diff -r fcdc9b71b256 -r 87a34b9b4a6f draw.h
--- a/draw.h Tue Apr 20 00:29:19 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-
-typedef struct {
- int *pixels;
- int w;
- int h;
-} DrawBuffer;
-
-int swk_gi_init();
-void swk_gi_exit();
-void swk_gi_flip();
-
-int draw_init(int w, int h);
-void draw_exit();
-
-void draw_line(int x, int y, int w, int h);
-void draw_box(int x, int y, int w, int h);
-void draw_rect(int x, int y, int w, int h);
-void draw_text(int x, int y, const char *text);
-//void draw_pixmap();
diff -r fcdc9b71b256 -r 87a34b9b4a6f gi_sdl.c
--- a/gi_sdl.c Tue Apr 20 00:29:19 2010 +0100
+++ b/gi_sdl.c Tue Apr 20 10:19:39 2010 +0200
@@ -1,18 +1,30 @@
#include <SDL/SDL.h>
#include "swk.h"
+static SDL_Surface *screen = NULL;
+
int
-swk_gi_init(int w, int h) {
+swk_gi_init(SwkWindow *w) {
if (SDL_Init(SDL_INIT_VIDEO)) {
fprintf(stderr, "Cannot initialize SDL\n");
return 0;
}
- SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE);
+ SDL_SetVideoMode(w->r.w, w->r.h, 32, SDL_DOUBLEBUF|SDL_RESIZABLE);
+ screen = SDL_GetVideoSurface();
SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
return 1;
}
+int
+swk_gi_update(SwkWindow *w) {
+ /* nothing to do here */
+ SDL_SetVideoMode(screen->w, screen->h, 32, SDL_DOUBLEBUF|SDL_RESIZABLE);
+ screen = SDL_GetVideoSurface();
+ fprintf(stderr, "NEW(%d,%d)\n", screen->w, screen->h);
+ return 1;
+}
+
void
swk_gi_exit() {
SDL_Quit();
@@ -21,38 +33,88 @@
static char sdlkeys[256] = { 0 }; // TODO
SwkEvent *
-swk_event() {
+swk_gi_event(int dowait) {
+ int evret;
SDL_Event event;
static SwkEvent ev;
SwkEvent *ret = NULL;
- if(SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_KEYDOWN:
- ret = &ev;
- ev.type = EKey;
- ev.data.key.keycode = sdlkeys[event.key.keysym.sym];
- ev.data.key.modmask = 0;
- if(event.key.keysym.mod & KMOD_LCTRL ||
- event.key.keysym.mod & KMOD_RCTRL)
- ev.data.key.modmask |= Ctrl;
- if(event.key.keysym.mod & KMOD_LSHIFT||
- event.key.keysym.mod & KMOD_RSHIFT)
- ev.data.key.modmask |= Shift;
- if(event.key.keysym.mod & KMOD_LMETA ||
- event.key.keysym.mod & KMOD_RMETA)
- ev.data.key.modmask |= Meta;
- break;
- case SDL_QUIT:
- ev.type = ev.type = EQuit;
- ret = &ev;
- break;
- }
+ if (dowait) evret = SDL_WaitEvent(&event);
+ else evret = SDL_PollEvent(&event);
+
+ if (evret)
+ switch (event.type) {
+ case SDL_VIDEORESIZE:
+ fprintf(stderr, "resize %d %d\n", event.resize.w, event.resize.h);
+ SDL_SetVideoMode(event.resize.w, event.resize.h,
+ 32, SDL_DOUBLEBUF|SDL_RESIZABLE);
+ case SDL_VIDEOEXPOSE:
+ ret = &ev;
+ ev.type = EExpose;
+ ev.data.expose.x = ev.data.expose.y = \
+ ev.data.expose.w = ev.data.expose.h = 0;
+ break;
+ case SDL_MOUSEMOTION:
+ ret = &ev;
+ ev.type = EMotion;
+ ev.data.motion.x = event.motion.x;
+ ev.data.motion.y = event.motion.y;
+ fprintf(stderr, "event: motion %d %d\n",
+ event.motion.x, event.motion.y);
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ ret = &ev;
+ ev.type = EClick;
+ ev.data.click.button = event.button.button;
+ ev.data.click.point.x = event.button.x;
+ ev.data.click.point.y = event.button.y;
+ fprintf(stderr, "event: click %d\n", event.button.button);
+ break;
+ case SDL_KEYDOWN:
+ ret = &ev;
+ ev.type = EKey;
+ ev.data.key.keycode = sdlkeys[event.key.keysym.sym];
+ ev.data.key.modmask = 0;
+ if(event.key.keysym.mod & KMOD_LCTRL ||
+ event.key.keysym.mod & KMOD_RCTRL)
+ ev.data.key.modmask |= Ctrl;
+ if(event.key.keysym.mod & KMOD_LSHIFT||
+ event.key.keysym.mod & KMOD_RSHIFT)
+ ev.data.key.modmask |= Shift;
+ if(event.key.keysym.mod & KMOD_LMETA ||
+ event.key.keysym.mod & KMOD_RMETA)
+ ev.data.key.modmask |= Meta;
+ fprintf(stderr, "event: key %d %d\n",
+ ev.data.key.modmask, ev.data.key.keycode);
+ break;
+ case SDL_QUIT:
+ fprintf(stderr, "event: quit\n");
+ ev.type = ev.type = EQuit;
+ ret = &ev;
+ break;
}
return ret;
}
void
swk_gi_flip() {
- /* TODO */
+ fprintf(stderr, "flip\n");
+ SDL_Flip(screen);
}
+
+/* -- drawing primitives -- */
+void
+swk_gi_line(int x, int y, int w, int h) {
+}
+
+void
+swk_gi_box(int x, int y, int w, int h) {
+}
+
+void
+swk_gi_rect(int x, int y, int w, int h) {
+}
+
+void
+swk_gi_text(int x, int y, const char *text) {
+}
diff -r fcdc9b71b256 -r 87a34b9b4a6f swk.c
--- a/swk.c Tue Apr 20 00:29:19 2010 +0100
+++ b/swk.c Tue Apr 20 10:19:39 2010 +0200
@@ -2,16 +2,41 @@
#include <stdio.h>
#include "swk.h"
+static int running = 0;
static SwkWindow *w = NULL;
int
swk_init(SwkWindow* window) {
w = window;
+ w->box = w->boxes;
if (w->r.w == 0 || w->r.h == 0) {
w->r.w = 640;
w->r.h = 480;
}
- return draw_init(w->r.w, w->r.h);
+ if (swk_gi_init(w))
+ running = 1;
+ return running;
+}
+
+void
+swk_update() {
+ if (!swk_gi_update(w))
+ running = 0;
+ else swk_gi_flip();
+}
+
+void
+swk_exit() {
+ running = 0;
+}
+
+void
+swk_loop() {
+ SwkEvent *e;
+ do {
+ if ((e = swk_event(1)))
+ swk_event_handle(e);
+ } while (!e || e->type != EQuit);
}
void
@@ -21,6 +46,15 @@
printf("Handler: %p text: \"%s\"\n", b->cb, b->text);
}
+SwkEvent *
+swk_event(int dowait) {
+ static SwkEvent ev;
+ if (running)
+ return swk_gi_event();
+ ev.type = EQuit;
+ return &ev;
+}
+
void
swk_event_handle(SwkEvent *e) {
switch(e->type) {
@@ -31,10 +65,10 @@
break;
case EExpose:
swk_fit();
- //swk_gi_flip();
+ swk_update();
break;
case EQuit:
- swk_exit();
+ swk_gi_exit();
break;
default:
break;
diff -r fcdc9b71b256 -r 87a34b9b4a6f swk.h
--- a/swk.h Tue Apr 20 00:29:19 2010 +0100
+++ b/swk.h Tue Apr 20 10:19:39 2010 +0200
@@ -1,11 +1,8 @@
/* See LICENSE file for copyright and license details. */
-#include "draw.h"
-
#define SWK_NEWLINE .h=-1, .w=-1
typedef enum { EVoid, EClick, EMotion, EKey, EExpose, EQuit, ELast } SwkEventType;
typedef enum { Ctrl=1, Meta=2, Shift=4 } SwkKeyMod;
-typedef enum { Left=1, Middle=2, Right=3, WheelUp=4, WheelDown=5 } SwkClickButton;
typedef struct SwkBox SwkBox;
@@ -22,14 +19,14 @@
} Rect;
typedef struct {
- SwkClickButton button;
- long modmask;
+ int button;
+ int modmask;
Point point;
} Click;
typedef struct {
int keycode;
- long modmask;
+ int modmask;
} Key;
typedef struct {
@@ -61,6 +58,7 @@
} SwkWindow;
int swk_init(SwkWindow *w);
+int swk_gi_update(SwkWindow *w);
void swk_exit();
void swk_fit();
SwkEvent * swk_event();
@@ -73,3 +71,16 @@
void swk_label(SwkEvent *e);
void swk_entry(SwkEvent *e);
void swk_filler(SwkEvent *e);
+
+/* graphic backend */
+
+int swk_gi_init(SwkWindow *w);
+void swk_gi_exit();
+SwkEvent * swk_gi_event();
+
+void swk_gi_flip();
+
+void swk_gi_line(int x, int y, int w, int h);
+void swk_gi_box(int x, int y, int w, int h);
+void swk_gi_rect(int x, int y, int w, int h);
+void swk_gi_text(int x, int y, const char *text);
diff -r fcdc9b71b256 -r 87a34b9b4a6f test.c
--- a/test.c Tue Apr 20 00:29:19 2010 +0100
+++ b/test.c Tue Apr 20 10:19:39 2010 +0200
@@ -1,17 +1,27 @@
#include <stdio.h>
#include "swk.h"
+static int count = 3;
+
+static int mybutton(SwkEvent *e) {
+ if (e->type == EClick) {
+ fprintf(stderr, "Button clicked %d\n", count);
+ if (count-- == 0)
+ swk_exit();
+ }
+ return swk_button(e);
+}
+
static SwkBox helloworld[] = {
{ .cb=swk_label, .text="Press this button", },
{ SWK_NEWLINE },
{ .cb=swk_filler, },
- { .cb=swk_button, .text="clickme" },
+ { .cb=mybutton, .text="clickme" },
{ .cb=NULL }
};
int
main() {
- SwkEvent *e;
SwkWindow w = {
.title="Hello World",
.boxes=helloworld
@@ -19,10 +29,6 @@
if (!swk_init(&w))
return 1;
- do {
- if ((e = swk_event()))
- swk_event_handle(e);
- } while (!e || e->type != EQuit);
- swk_exit();
+ swk_loop();
return 0;
}
Received on Tue Apr 20 2010 - 08:26:12 UTC
This archive was generated by hypermail 2.2.0 : Tue Apr 20 2010 - 08:36:04 UTC