[hackers] [swk] Added getscrpoint helper in gi_sdl || pancake

From: <hg_AT_suckless.org>
Date: Thu, 6 May 2010 09:46:05 +0000 (UTC)

changeset: 33:9d527f82d3fe
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Thu May 06 10:18:56 2010 +0200
files: gi_sdl.c swk.c
description:
Added getscrpoint helper in gi_sdl
Scrollbox is never full window unless specified
Draw lines with rects (faster than per-pixel)
Implement swk_gi_img_set in sdl to paint pixels

diff -r 0858a9652eee -r 9d527f82d3fe gi_sdl.c
--- a/gi_sdl.c Wed May 05 11:12:02 2010 +0200
+++ b/gi_sdl.c Thu May 06 10:18:56 2010 +0200
@@ -20,11 +20,17 @@
 static int has_event = 0;
 static SDL_Event lastev = { .type=-1 };
 
-static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) {
+static inline Uint8 *
+getscrpoint(SDL_Surface *scr, int x, int y) {
         Uint8 *p = (Uint8 *)scr->pixels + (y*scr->pitch+x*(BPP/8));
         Uint8 *pend = (Uint8 *)scr->pixels + (scr->h*scr->w*(BPP/8));
         if((p<((Uint8 *)scr->pixels)) || (p>=pend))
- return;
+ return NULL;
+ return p;
+}
+
+static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) {
+ Uint8 *p = getscrpoint(scr, x, y);
 #if BPP == 8
         *p = pixel;
 #elif BPP == 16
@@ -216,12 +222,10 @@
 /* -- drawing primitives -- */
 void
 swk_gi_line(int x1, int y1, int x2, int y2, int color) {
- int i;
- x1 *= fs; y1 *= fs;
- x2 *= fs; y2 *= fs;
- 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(screen, x1+i, y1, pal[color]);
+ Rect r = { x1, y1, x2, y2 };
+ if(!x2 || !y2)
+ swk_gi_fill(r, color, 0);
+ // TODO: add support for diagonal lines?
 }
 
 void
@@ -234,6 +238,8 @@
                 area.w -= (s*2);
                 area.h -= (s*2);
         }
+ if (!area.w) area.w = 1;
+ if (!area.h) area.h = 1;
         SDL_FillRect(screen, &area, pal[color]);
 }
 
@@ -286,13 +292,11 @@
 
 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);
+ if(img) putpixel((SDL_Surface*)img, x, y, color);
 }
 
 int
 swk_gi_img_get(void *img, int x, int y) {
- /* TODO */
- return 0;
+ Uint8 *p = getscrpoint(img, x, y);
+ return p?*p:0;
 }
-
diff -r 0858a9652eee -r 9d527f82d3fe swk.c
--- a/swk.c Wed May 05 11:12:02 2010 +0200
+++ b/swk.c Thu May 06 10:18:56 2010 +0200
@@ -82,27 +82,27 @@
 }
 
 
-static SwkBox *
-getscrollbox(SwkWindow *w) {
+static void
+setscrollbox(SwkWindow *w, int delta) {
         SwkBox *r = NULL;
         SwkBox *b = w->boxes;
         for(; b->cb; b++) {
                 if(b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0)
                         r = b;
- if(w->box==b)
- return r?r:w->boxes;
+ if(w->box==b && r)
+ break;
         }
- return w->boxes;
+ if(r) r->scroll += delta;
 }
 
 void
 swk_scroll_up(SwkWindow *w) {
- getscrollbox(w)->scroll++;
+ setscrollbox(w, 1);
 }
 
 void
 swk_scroll_down(SwkWindow *w) {
- getscrollbox(w)->scroll--;
+ setscrollbox(w, -1);
 }
 
 static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) {
Received on Thu May 06 2010 - 09:46:05 UTC

This archive was generated by hypermail 2.2.0 : Thu May 06 2010 - 09:48:03 UTC