eobish

changeset 2:cdbcae5b3b98

added fblib
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 18 Jan 2015 03:16:37 +0200
parents bc0996863c79
children e32bdd5fb622
files .clang_complete Makefile src/fblib.c src/fblib.h src/fblibimp.h src/fblibsdl.c src/main.cc
diffstat 7 files changed, 334 insertions(+), 57 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.clang_complete	Sun Jan 18 03:16:37 2015 +0200
     1.3 @@ -0,0 +1,1 @@
     1.4 +-DFBLIB_SDL
     2.1 --- a/Makefile	Sat Jan 17 18:40:49 2015 +0200
     2.2 +++ b/Makefile	Sun Jan 18 03:16:37 2015 +0200
     2.3 @@ -3,7 +3,12 @@
     2.4  obj = $(csrc:.c=.o) $(ccsrc:.cc=.o)
     2.5  bin = eobish
     2.6  
     2.7 -CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl`
     2.8 +warn = -pedantic -Wall
     2.9 +opt = -O0
    2.10 +dbg = -g
    2.11 +def = -DFBLIB_SDL
    2.12 +
    2.13 +CFLAGS = $(warn) $(opt) $(dbg) $(def) `pkg-config --cflags sdl`
    2.14  CXXFLAGS = $(CFLAGS)
    2.15  LDFLAGS = `pkg-config --libs sdl`
    2.16  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/fblib.c	Sun Jan 18 03:16:37 2015 +0200
     3.3 @@ -0,0 +1,48 @@
     3.4 +#include "fblib.h"
     3.5 +#include "fblibimp.h"
     3.6 +
     3.7 +void fb_set_palette(int *colors)
     3.8 +{
     3.9 +	fb_set_palette_range(0, 256, colors);
    3.10 +}
    3.11 +
    3.12 +void fb_set_palette_entry(int idx, int r, int g, int b)
    3.13 +{
    3.14 +	int col[3];
    3.15 +	col[0] = r;
    3.16 +	col[1] = g;
    3.17 +	col[2] = b;
    3.18 +
    3.19 +	fb_set_palette_range(idx, 1, col);
    3.20 +}
    3.21 +
    3.22 +int fb_key_state(int key)
    3.23 +{
    3.24 +	if(key < 256) {
    3.25 +		return fb_inp.key[key];
    3.26 +	}
    3.27 +	return 0;
    3.28 +}
    3.29 +
    3.30 +int fb_mouse_state(int bn, int *x, int *y)
    3.31 +{
    3.32 +	return 0;
    3.33 +}
    3.34 +
    3.35 +void fb_keyboard_callback(void (*func)(int, int, void*), void *cls)
    3.36 +{
    3.37 +	fb_cb.keyb = func;
    3.38 +	fb_cb.keyb_data = cls;
    3.39 +}
    3.40 +
    3.41 +void fb_mouse_button_callback(void (*func)(int, int, int, int, void*), void *cls)
    3.42 +{
    3.43 +	fb_cb.button = func;
    3.44 +	fb_cb.button_data = cls;
    3.45 +}
    3.46 +
    3.47 +void fb_mouse_motion_callback(void (*func)(int, int, void*), void *cls)
    3.48 +{
    3.49 +	fb_cb.motion = func;
    3.50 +	fb_cb.motion_data = cls;
    3.51 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/fblib.h	Sun Jan 18 03:16:37 2015 +0200
     4.3 @@ -0,0 +1,47 @@
     4.4 +#ifndef FBLIB_H_
     4.5 +#define FBLIB_H_
     4.6 +
     4.7 +#ifdef __cplusplus
     4.8 +extern "C" {
     4.9 +#endif
    4.10 +
    4.11 +/* pixel formats for various bpp values
    4.12 + * 32: [R8][G8][B8][X8] or [X|B|G|R] (little endian)
    4.13 + * 24: [R8][G8][B8]
    4.14 + * 16: [R5|G6|B5]
    4.15 + * 15: [R5|G5|B5]
    4.16 + *  8: [I8] (8 bit color index)
    4.17 + */
    4.18 +int fb_init(int width, int height, int bpp);
    4.19 +void fb_shutdown(void);
    4.20 +
    4.21 +int fb_get_width(void);
    4.22 +int fb_get_height(void);
    4.23 +int fb_get_bpp(void);
    4.24 +
    4.25 +void *fb_begin_frame(void);
    4.26 +void fb_end_frame(void);
    4.27 +
    4.28 +void fb_set_palette(int *colors);
    4.29 +void fb_set_palette_range(int start, int count, int *colors);
    4.30 +void fb_set_palette_entry(int idx, int r, int g, int b);
    4.31 +
    4.32 +/* event handling */
    4.33 +int fb_key_state(int key);
    4.34 +int fb_mouse_state(int bn, int *x, int *y);
    4.35 +
    4.36 +void fb_keyboard_callback(void (*func)(int, int, void*), void *cls);
    4.37 +void fb_mouse_button_callback(void (*func)(int, int, int, int, void*), void *cls);
    4.38 +void fb_mouse_motion_callback(void (*func)(int, int, void*), void *cls);
    4.39 +
    4.40 +int fb_process_events(void);
    4.41 +
    4.42 +/* misc */
    4.43 +unsigned long fb_get_time(void);
    4.44 +
    4.45 +#ifdef __cplusplus
    4.46 +}
    4.47 +#endif
    4.48 +
    4.49 +
    4.50 +#endif	/* FBLIB_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/fblibimp.h	Sun Jan 18 03:16:37 2015 +0200
     5.3 @@ -0,0 +1,22 @@
     5.4 +#ifndef FBLIBIMP_H_
     5.5 +#define FBLIBIMP_H_
     5.6 +
     5.7 +#include "fblib.h"
     5.8 +
     5.9 +struct callbacks {
    5.10 +	void (*keyb)(int, int, void*);
    5.11 +	void (*button)(int, int, int, int, void*);
    5.12 +	void (*motion)(int, int, void*);
    5.13 +
    5.14 +	void *keyb_data, *button_data, *motion_data;
    5.15 +} fb_cb;
    5.16 +
    5.17 +struct input_state {
    5.18 +	unsigned char key[256];
    5.19 +	unsigned char mbutton[8];
    5.20 +	int mx, my;
    5.21 +} fb_inp;
    5.22 +
    5.23 +int fb_width, fb_height, fb_bpp;
    5.24 +
    5.25 +#endif	/* FBLIBIMP_H_ */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/fblibsdl.c	Sun Jan 18 03:16:37 2015 +0200
     6.3 @@ -0,0 +1,199 @@
     6.4 +#ifdef FBLIB_SDL
     6.5 +#include <stdio.h>
     6.6 +#include <stdlib.h>
     6.7 +#include <string.h>
     6.8 +#include <SDL/SDL.h>
     6.9 +#include "fblib.h"
    6.10 +#include "fblibimp.h"
    6.11 +
    6.12 +static int scale;
    6.13 +static SDL_Surface *surf;
    6.14 +static unsigned char *scalebuf;	/* only if scale != 1 */
    6.15 +static int pixbytes;	/* pixel size in bytes */
    6.16 +
    6.17 +int fb_init(int width, int height, int bpp)
    6.18 +{
    6.19 +	static int sdlinit_done;
    6.20 +	char *env, title[64];
    6.21 +
    6.22 +	if((env = getenv("FBLIB_SCALE"))) {
    6.23 +		scale = atoi(env);
    6.24 +	}
    6.25 +	if(!scale) scale = 1;
    6.26 +
    6.27 +	fb_width = width;
    6.28 +	fb_height = height;
    6.29 +	fb_bpp = bpp;
    6.30 +
    6.31 +	pixbytes = (bpp + 7) / 8;
    6.32 +
    6.33 +	if(!sdlinit_done) {
    6.34 +		if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
    6.35 +			fprintf(stderr, "failed to initialize SDL!\n");
    6.36 +			return -1;
    6.37 +		}
    6.38 +		sdlinit_done = 1;
    6.39 +	}
    6.40 +
    6.41 +	if(!(surf = SDL_SetVideoMode(width * scale, height * scale, bpp, SDL_SWSURFACE))) {
    6.42 +		fprintf(stderr, "failed to set video mode\n");
    6.43 +		return -1;
    6.44 +	}
    6.45 +	sprintf(title, "fblib window (%dx)", scale);
    6.46 +	SDL_WM_SetCaption(title, 0);
    6.47 +
    6.48 +	if(scale != 1) {
    6.49 +		if(!(scalebuf = malloc(width * height * pixbytes))) {
    6.50 +			fprintf(stderr, "failed to allocate back buffer\n");
    6.51 +			SDL_Quit();
    6.52 +			return -1;
    6.53 +		}
    6.54 +	} else {
    6.55 +		scalebuf = 0;
    6.56 +	}
    6.57 +
    6.58 +	return 0;
    6.59 +}
    6.60 +
    6.61 +void fb_shutdown(void)
    6.62 +{
    6.63 +	free(scalebuf);
    6.64 +	SDL_Quit();
    6.65 +}
    6.66 +
    6.67 +int fb_get_width(void)
    6.68 +{
    6.69 +	return fb_width;
    6.70 +}
    6.71 +
    6.72 +int fb_get_height(void)
    6.73 +{
    6.74 +	return fb_height;
    6.75 +}
    6.76 +
    6.77 +int fb_get_bpp(void)
    6.78 +{
    6.79 +	return fb_bpp;
    6.80 +}
    6.81 +
    6.82 +void *fb_begin_frame(void)
    6.83 +{
    6.84 +	if(!surf) return 0;
    6.85 +
    6.86 +	if(scalebuf) {
    6.87 +		return scalebuf;
    6.88 +	}
    6.89 +
    6.90 +	if(SDL_MUSTLOCK(surf)) {
    6.91 +		SDL_LockSurface(surf);
    6.92 +	}
    6.93 +	return surf->pixels;
    6.94 +}
    6.95 +
    6.96 +void fb_end_frame(void)
    6.97 +{
    6.98 +	if(scalebuf) {
    6.99 +		int i, j, k;
   6.100 +		unsigned char *dest;
   6.101 +
   6.102 +		if(SDL_MUSTLOCK(surf)) {
   6.103 +			SDL_LockSurface(surf);
   6.104 +		}
   6.105 +		dest = surf->pixels;
   6.106 +
   6.107 +		for(i=0; i<surf->h; i++) {
   6.108 +			int y = i / scale;
   6.109 +			unsigned char *scan = scalebuf + y * fb_width * pixbytes;
   6.110 +
   6.111 +			for(j=0; j<surf->w; j++) {
   6.112 +				int x = j / scale;
   6.113 +
   6.114 +				for(k=0; k<pixbytes; k++) {
   6.115 +					*dest++ = scan[x * pixbytes + k];
   6.116 +				}
   6.117 +			}
   6.118 +		}
   6.119 +	}
   6.120 +
   6.121 +	if(SDL_MUSTLOCK(surf)) {
   6.122 +		SDL_UnlockSurface(surf);
   6.123 +	}
   6.124 +
   6.125 +	SDL_Flip(surf);
   6.126 +}
   6.127 +
   6.128 +void fb_set_palette_range(int start, int count, int *colors)
   6.129 +{
   6.130 +	int i, *col = colors;
   6.131 +	SDL_Colour sdlcol[256];
   6.132 +
   6.133 +	for(i=0; i<count; i++) {
   6.134 +		sdlcol[i].r = *col++;
   6.135 +		sdlcol[i].g = *col++;
   6.136 +		sdlcol[i].b = *col++;
   6.137 +	}
   6.138 +
   6.139 +	SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, sdlcol, start, count);
   6.140 +}
   6.141 +
   6.142 +int fb_process_events(void)
   6.143 +{
   6.144 +	SDL_Event ev;
   6.145 +
   6.146 +	while(SDL_PollEvent(&ev)) {
   6.147 +		switch(ev.type) {
   6.148 +		case SDL_QUIT:
   6.149 +			return -1;
   6.150 +
   6.151 +		case SDL_KEYDOWN:
   6.152 +		case SDL_KEYUP:
   6.153 +			{
   6.154 +				int key = ev.key.keysym.sym;
   6.155 +				int state = ev.key.state == SDL_PRESSED;
   6.156 +
   6.157 +				if(key < 256) {
   6.158 +					fb_inp.key[key] = state;
   6.159 +				}
   6.160 +				if(fb_cb.keyb) {
   6.161 +					fb_cb.keyb(key, state, fb_cb.keyb_data);
   6.162 +				} else {
   6.163 +					if(key == SDLK_ESCAPE) {
   6.164 +						return -1;
   6.165 +					}
   6.166 +				}
   6.167 +			}
   6.168 +			break;
   6.169 +
   6.170 +		case SDL_MOUSEBUTTONDOWN:
   6.171 +		case SDL_MOUSEBUTTONUP:
   6.172 +			{
   6.173 +				int state = ev.button.state == SDL_PRESSED;
   6.174 +				fb_inp.mx = ev.motion.x / scale;
   6.175 +				fb_inp.my = ev.motion.y / scale;
   6.176 +				fb_inp.mbutton[ev.button.which] = state;
   6.177 +
   6.178 +				if(fb_cb.button) {
   6.179 +					fb_cb.button(ev.button.which, state, fb_inp.mx, fb_inp.my, fb_cb.button_data);
   6.180 +				}
   6.181 +			}
   6.182 +			break;
   6.183 +
   6.184 +		case SDL_MOUSEMOTION:
   6.185 +			fb_inp.mx = ev.motion.x / scale;
   6.186 +			fb_inp.my = ev.motion.y / scale;
   6.187 +			if(fb_cb.motion) {
   6.188 +				fb_cb.motion(fb_inp.mx, fb_inp.my, fb_cb.motion_data);
   6.189 +			}
   6.190 +			break;
   6.191 +		}
   6.192 +	}
   6.193 +
   6.194 +	return 0;
   6.195 +}
   6.196 +
   6.197 +unsigned long fb_get_time(void)
   6.198 +{
   6.199 +	return SDL_GetTicks();
   6.200 +}
   6.201 +
   6.202 +#endif	/* FBLIB_SDL */
     7.1 --- a/src/main.cc	Sat Jan 17 18:40:49 2015 +0200
     7.2 +++ b/src/main.cc	Sun Jan 18 03:16:37 2015 +0200
     7.3 @@ -1,81 +1,36 @@
     7.4  #include <stdio.h>
     7.5 -#include <SDL/SDL.h>
     7.6 +#include "fblib.h"
     7.7  
     7.8  static void display();
     7.9 -static bool proc_event(SDL_Event *ev);
    7.10 -static void set_pal_entry(int idx, int r, int g, int b);
    7.11 -
    7.12 -static SDL_Surface *fbsurf;
    7.13  
    7.14  int main(int argc, char **argv)
    7.15  {
    7.16 -	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    7.17 -
    7.18 -	if(!(fbsurf = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE))) {
    7.19 -		fprintf(stderr, "failed to set video mode\n");
    7.20 +	if(fb_init(320, 240, 8) == -1) {
    7.21  		return 1;
    7.22  	}
    7.23 -	set_pal_entry(1, 255, 0, 0);
    7.24 +	fb_set_palette_entry(1, 255, 0, 0);
    7.25  
    7.26  	for(;;) {
    7.27 -		SDL_Event ev;
    7.28 -		while(SDL_PollEvent(&ev)) {
    7.29 -			if(!proc_event(&ev)) {
    7.30 -				goto done;
    7.31 -			}
    7.32 +		if(fb_process_events() == -1) {
    7.33 +			break;
    7.34  		}
    7.35 -
    7.36  		display();
    7.37  	}
    7.38  
    7.39  done:
    7.40 -	SDL_Quit();
    7.41 +	fb_shutdown();
    7.42  	return 0;
    7.43  }
    7.44  
    7.45  void display()
    7.46  {
    7.47 -	if(SDL_MUSTLOCK(fbsurf)) {
    7.48 -		SDL_LockSurface(fbsurf);
    7.49 -	}
    7.50 +	int width = fb_get_width();
    7.51 +	int height = fb_get_height();
    7.52 +	unsigned char *pixels = (unsigned char*)fb_begin_frame();
    7.53  
    7.54 -	unsigned char *pixels = (unsigned char*)fbsurf->pixels;
    7.55 -	for(int i=0; i<fbsurf->w * fbsurf->h; i++) {
    7.56 +	for(int i=0; i<width * height; i++) {
    7.57  		*pixels++ = 1;
    7.58  	}
    7.59  
    7.60 -	if(SDL_MUSTLOCK(fbsurf)) {
    7.61 -		SDL_UnlockSurface(fbsurf);
    7.62 -	}
    7.63 -
    7.64 -	SDL_Flip(fbsurf);
    7.65 +	fb_end_frame();
    7.66  }
    7.67 -
    7.68 -static bool proc_event(SDL_Event *ev)
    7.69 -{
    7.70 -	switch(ev->type) {
    7.71 -	case SDL_KEYDOWN:
    7.72 -		if(ev->key.keysym.sym == SDLK_ESCAPE) {
    7.73 -			return false;
    7.74 -		}
    7.75 -		break;
    7.76 -
    7.77 -	default:
    7.78 -		break;
    7.79 -	}
    7.80 -	return true;
    7.81 -}
    7.82 -
    7.83 -static void set_pal_entry(int idx, int r, int g, int b)
    7.84 -{
    7.85 -	SDL_Color col;
    7.86 -	col.r = r;
    7.87 -	col.g = g;
    7.88 -	col.b = b;
    7.89 -
    7.90 -	SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1);
    7.91 -
    7.92 -	/*palette[idx].r = r;
    7.93 -	palette[idx].g = g;
    7.94 -	palette[idx].b = b;*/
    7.95 -}