gba-trycatch

diff src/main_sdl.c @ 9:b0ed38f13261

working on the rasterizer
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Jun 2014 05:16:10 +0300
parents fb0a0d6a8b52
children 2070a81127f2
line diff
     1.1 --- a/src/main_sdl.c	Thu Jun 19 05:53:46 2014 +0300
     1.2 +++ b/src/main_sdl.c	Sun Jun 22 05:16:10 2014 +0300
     1.3 @@ -5,6 +5,14 @@
     1.4  #include "gbasys.h"
     1.5  #include "game.h"
     1.6  
     1.7 +#ifdef PALMODE
     1.8 +#define BPP		8
     1.9 +#define PIXSZ	1
    1.10 +#else
    1.11 +#define BPP		16
    1.12 +#define PIXSZ	2
    1.13 +#endif
    1.14 +
    1.15  static int proc_events(SDL_Event *ev);
    1.16  static void handle_keyboard(int key, int state);
    1.17  
    1.18 @@ -29,7 +37,7 @@
    1.19  
    1.20  	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
    1.21  
    1.22 -	if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, 16, SDL_SWSURFACE))) {
    1.23 +	if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, BPP, SDL_SWSURFACE))) {
    1.24  		fprintf(stderr, "failed to initialize graphics\n");
    1.25  		return 1;
    1.26  	}
    1.27 @@ -37,8 +45,8 @@
    1.28  
    1.29  	bbuf.x = WIDTH;
    1.30  	bbuf.y = HEIGHT;
    1.31 -	bbuf.bpp = 16;
    1.32 -	if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * 2))) {
    1.33 +	bbuf.bpp = BPP;
    1.34 +	if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * PIXSZ))) {
    1.35  		fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT);
    1.36  		SDL_Quit();
    1.37  		return 1;
    1.38 @@ -53,7 +61,11 @@
    1.39  
    1.40  	for(;;) {
    1.41  		SDL_Event ev;
    1.42 +#ifdef PALMODE
    1.43 +		uint8_t *dest, *src;
    1.44 +#else
    1.45  		uint16_t *dest, *src;
    1.46 +#endif
    1.47  
    1.48  		while(SDL_PollEvent(&ev)) {
    1.49  			if(proc_events(&ev) == -1) {
    1.50 @@ -102,14 +114,32 @@
    1.51  
    1.52  void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
    1.53  {
    1.54 -	int i;
    1.55 +	int i, sz = pbuf->x * pbuf->y;
    1.56  	unsigned short *pixels = pbuf->pixels;
    1.57  
    1.58 -	for(i=0; i<pbuf->x * pbuf->y; i++) {
    1.59 +#ifdef PALMODE
    1.60 +	color |= color << 8;
    1.61 +	sz /= 2;
    1.62 +#endif
    1.63 +
    1.64 +	for(i=0; i<sz; i++) {
    1.65  		*pixels++ = color;
    1.66  	}
    1.67  }
    1.68  
    1.69 +void set_palette(int idx, int r, int g, int b)
    1.70 +{
    1.71 +	SDL_Color col;
    1.72 +	col.r = r;
    1.73 +	col.g = g;
    1.74 +	col.b = b;
    1.75 +
    1.76 +	if(SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
    1.77 +		fprintf(stderr, "set_palette failed to set the required color\n");
    1.78 +	}
    1.79 +}
    1.80 +
    1.81 +
    1.82  int get_key_state(int key)
    1.83  {
    1.84  	return keystate & key;
    1.85 @@ -125,7 +155,7 @@
    1.86  	switch(ev->type) {
    1.87  	case SDL_KEYDOWN:
    1.88  	case SDL_KEYUP:
    1.89 -		handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_KEYDOWN);
    1.90 +		handle_keyboard(ev->key.keysym.sym, ev->key.state);
    1.91  		break;
    1.92  
    1.93  	case SDL_QUIT:
    1.94 @@ -173,6 +203,7 @@
    1.95  		break;
    1.96  
    1.97  	case '\n':
    1.98 +	case '\r':
    1.99  		gba_key = KEY_A;
   1.100  		break;
   1.101  	case '\b':