[hackers] [swk] add image support to gi backend || pancake

From: <hg_AT_suckless.org>
Date: Wed, 28 Apr 2010 16:01:44 +0000 (UTC)

changeset: 28:940979cb2a7f
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Apr 28 17:55:20 2010 +0200
files: Makefile config.def.h gi_sdl.c image.png swk.c swk.h test.c
description:
add image support to gi backend
tested in example
added global event handler in SwkWindow

diff -r c01ce3b4d999 -r 940979cb2a7f Makefile
--- a/Makefile Wed Apr 28 16:54:52 2010 +0200
+++ b/Makefile Wed Apr 28 17:55:20 2010 +0200
@@ -8,7 +8,7 @@
 
 # graphic backend
 GI?=sdl
-GI_LIBS=-lSDL -lSDL_ttf
+GI_LIBS=-lSDL -lSDL_ttf -lSDL_image
 
 GI_OBJS=gi_${GI}.o
 GI_SRCS=gi_${GI}.c
diff -r c01ce3b4d999 -r 940979cb2a7f config.def.h
--- a/config.def.h Wed Apr 28 16:54:52 2010 +0200
+++ b/config.def.h Wed Apr 28 17:55:20 2010 +0200
@@ -8,7 +8,6 @@
 #endif
 
 /* appearance */
-#define FONTSIZE 40
 #define FONTBOLD 1
 #define WINWIDTH 640
 #define WINHEIGHT 480
diff -r c01ce3b4d999 -r 940979cb2a7f gi_sdl.c
--- a/gi_sdl.c Wed Apr 28 16:54:52 2010 +0200
+++ b/gi_sdl.c Wed Apr 28 17:55:20 2010 +0200
@@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #define _BSD_SOURCE // strdup
 #include <SDL/SDL.h>
+#include <SDL/SDL_image.h>
 #include <SDL/SDL_ttf.h>
 #include "swk.h"
 #include "config.h"
@@ -19,13 +20,13 @@
 static int has_event = 0;
 static SDL_Event lastev = { .type=-1 };
 
-static void putpixel(int x, int y, Uint32 pixel) {
+static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) {
         Uint8 *p, *pend;
- int delta, bpp = screen->format->BytesPerPixel;
- delta = y * screen->pitch + x * bpp;
- p = (Uint8 *)screen->pixels + delta;
- pend = (Uint8 *)screen->pixels + (screen->h*screen->w*bpp);
- if((p<((Uint8 *)screen->pixels)) || (p>=pend))
+ int delta, bpp = scr->format->BytesPerPixel;
+ delta = y * scr->pitch + x * bpp;
+ p = (Uint8 *)scr->pixels + delta;
+ pend = (Uint8 *)scr->pixels + (scr->h*scr->w*bpp);
+ if((p<((Uint8 *)scr->pixels)) || (p>=pend))
                 return;
 #if BPP == 8
         *p = pixel;
@@ -209,9 +210,9 @@
         int i;
         x1 *= fs; y1 *= fs;
         x2 *= fs; y2 *= fs;
- if(x2==0) for(i=0;i<y2;i++) putpixel(x1, y1+i, pal[color]);
+ if(x2==0) for(i=0;i<y2;i++) putpixel(screen, x1, y1+i, pal[color]);
         else
- if(y2==0) for(i=0;i<x2;i++) putpixel(x1+i, y1, pal[color]);
+ if(y2==0) for(i=0;i<x2;i++) putpixel(screen, x1+i, y1, pal[color]);
 }
 
 void
@@ -255,3 +256,35 @@
         }
         free(ptr);
 }
+
+/* images */
+
+void
+swk_gi_img(Rect r, void *img) {
+ SDL_Surface *s = (SDL_Surface*)img;
+ SDL_Rect area = { r.x*fs, r.y*fs, r.w*fs, r.h*fs };
+ if (s) SDL_BlitSurface(s, NULL, screen, &area);
+}
+
+void*
+swk_gi_img_load(const char *str) {
+ return IMG_Load(str);
+}
+
+void*
+swk_gi_img_free(const char *str) {
+ return IMG_Load(str);
+}
+
+void
+swk_gi_img_set(void *img, int x, int y, int color) {
+ SDL_Surface *s = (SDL_Surface*)img;
+ if (s) putpixel(s, x, y, color);
+}
+
+int
+swk_gi_img_get(void *img, int x, int y) {
+ /* TODO */
+ return 0;
+}
+
diff -r c01ce3b4d999 -r 940979cb2a7f image.png
Binary file image.png has changed
diff -r c01ce3b4d999 -r 940979cb2a7f swk.c
--- a/swk.c Wed Apr 28 16:54:52 2010 +0200
+++ b/swk.c Wed Apr 28 17:55:20 2010 +0200
@@ -161,6 +161,8 @@
 swk_handle_event(SwkEvent *e) {
         int i;
         SwkBox *b;
+ if(e->win->cb)
+ e->win->cb(e);
         switch(e->type) {
         case EKey:
                 for(i=0; keys[i].cb; i++) {
@@ -427,3 +429,29 @@
                 break;
         }
 }
+
+/* -- */
+void
+swk_image_free(SwkBox *b) {
+ swk_gi_img_free(b->data);
+ b->data = NULL;
+}
+
+void
+swk_image(SwkEvent *e) {
+ if(e->box->data == NULL)
+ e->box->data = swk_gi_img_load(e->box->text);
+ switch(e->type) {
+ case EExpose:
+ if (e->box->data)
+ swk_gi_img(e->box->r, e->box->data);
+ else swk_gi_rect(e->box->r, ColorFG);
+ if(e->win->box == e->box) {
+ Rect r = e->box->r;
+ swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI);
+ }
+ break;
+ default:
+ break;
+ }
+}
diff -r c01ce3b4d999 -r 940979cb2a7f swk.h
--- a/swk.h Wed Apr 28 16:54:52 2010 +0200
+++ b/swk.h Wed Apr 28 17:55:20 2010 +0200
@@ -68,6 +68,7 @@
 struct SwkWindow {
         char *title;
         int running;
+ SwkEventCallback cb;
         Rect r;
         SwkBox *boxes;
         /* internal use */
@@ -101,6 +102,7 @@
 void swk_option(SwkEvent *e);
 void swk_separator(SwkEvent *e);
 void swk_progress(SwkEvent *e);
+void swk_image(SwkEvent *e);
 
 /* graphic backend */
 
@@ -119,3 +121,10 @@
 void swk_gi_fill(Rect r, int color, int lil);
 void swk_gi_rect(Rect r, int color);
 void swk_gi_text(Rect r, const char *text);
+
+/* images */
+void swk_gi_img(Rect r, void *img);
+void* swk_gi_img_load(const char *str);
+void* swk_gi_img_free(const char *str);
+void swk_gi_img_set(void *img, int x, int y, int color);
+int swk_gi_img_get(void *img, int x, int y);
diff -r c01ce3b4d999 -r 940979cb2a7f test.c
--- a/test.c Wed Apr 28 16:54:52 2010 +0200
+++ b/test.c Wed Apr 28 17:55:20 2010 +0200
@@ -89,6 +89,13 @@
         { .cb=swk_label, .text="Password:", },
         { .cb=swk_password, .text="1234", },
         SWK_BOX_NEWLINE(-1),
+ { .cb=swk_filler, },
+ { .cb=swk_image, .text="image.png" },
+ { .cb=swk_image, .text="image.png" },
+ { .cb=swk_image, .text="image.png" },
+ { .cb=swk_image, .text="image.png" },
+ { .cb=swk_filler, },
+ SWK_BOX_NEWLINE(2),
         { .cb=mybutton, .text="yes" },
         { .cb=mybutton, .text="no" },
         { .cb=swk_filler, },
Received on Wed Apr 28 2010 - 16:01:44 UTC

This archive was generated by hypermail 2.2.0 : Wed Apr 28 2010 - 16:12:04 UTC