gba-x3dtest

changeset 6:73b5f2e5d18a

first triangle on screen
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 18 Jun 2014 04:13:02 +0300
parents 850be43b3135
children 158d23956801
files src/config.h src/game.c src/main_sdl.c src/polyfill.c src/sdlsys/gbasys.h
diffstat 5 files changed, 46 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/src/config.h	Mon Jun 16 22:01:45 2014 +0300
     1.2 +++ b/src/config.h	Wed Jun 18 04:13:02 2014 +0300
     1.3 @@ -5,6 +5,4 @@
     1.4  #define WIDTH		160
     1.5  #define HEIGHT		128
     1.6  
     1.7 -#define SDLSCALE	2
     1.8 -
     1.9  #endif	/* CONFIG_H_ */
     2.1 --- a/src/game.c	Mon Jun 16 22:01:45 2014 +0300
     2.2 +++ b/src/game.c	Wed Jun 18 04:13:02 2014 +0300
     2.3 @@ -1,3 +1,4 @@
     2.4 +#include "config.h"
     2.5  #include "game.h"
     2.6  #include "gbasys.h"
     2.7  #include "polyfill.h"
     2.8 @@ -7,15 +8,23 @@
     2.9  static const pvec3 poly[] = {
    2.10  	{X16INT(80), X16INT(10), 0},
    2.11  	{X16INT(140), X16INT(100), 0},
    2.12 -	{X16INT(40), X16INT(800), 0}
    2.13 +	{X16INT(40), X16INT(80), 0}
    2.14  };
    2.15  
    2.16  void game_draw(void)
    2.17  {
    2.18 +	int i;
    2.19  	clear_buffer(back_buffer, 0);
    2.20  
    2.21  	draw_poly(3, poly, 0xffff);
    2.22  
    2.23 +	for(i=0; i<sizeof poly / sizeof *poly; i++) {
    2.24 +		int x = poly[i].x >> 16;
    2.25 +		int y = poly[i].y >> 16;
    2.26 +
    2.27 +		((uint16_t*)back_buffer->pixels)[y * WIDTH + x] = RGB(0, 255, 0);
    2.28 +	}
    2.29 +
    2.30  	flip();
    2.31  }
    2.32  
     3.1 --- a/src/main_sdl.c	Mon Jun 16 22:01:45 2014 +0300
     3.2 +++ b/src/main_sdl.c	Wed Jun 18 04:13:02 2014 +0300
     3.3 @@ -13,14 +13,23 @@
     3.4  static SDL_Surface *surf;
     3.5  static struct pixel_buffer bbuf;
     3.6  static unsigned int keystate;
     3.7 +static int sdlscale = 2;
     3.8  
     3.9  int main(void)
    3.10  {
    3.11 -	int i, j;
    3.12 +	int i, j, k, l;
    3.13 +	char *env;
    3.14 +
    3.15 +	if((env = getenv("SDLSCALE"))) {
    3.16 +		if(!(sdlscale = atoi(env))) {
    3.17 +			fprintf(stderr, "invalid SDLSCALE envvar value (%s)\n", env);
    3.18 +			sdlscale = 2;
    3.19 +		}
    3.20 +	}
    3.21  
    3.22  	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
    3.23  
    3.24 -	if(!(surf = SDL_SetVideoMode(WIDTH * SDLSCALE, HEIGHT * SDLSCALE, 16, SDL_SWSURFACE))) {
    3.25 +	if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, 16, SDL_SWSURFACE))) {
    3.26  		fprintf(stderr, "failed to initialize graphics\n");
    3.27  		return 1;
    3.28  	}
    3.29 @@ -58,10 +67,16 @@
    3.30  
    3.31  		for(i=0; i<HEIGHT; i++) {
    3.32  			for(j=0; j<WIDTH; j++) {
    3.33 -				dest[0] = dest[1] = dest[WIDTH] = dest[WIDTH + 1] = *src++;
    3.34 -				dest += 2;
    3.35 +				uint16_t pixel = *src++;
    3.36 +
    3.37 +				for(k=0; k<sdlscale; k++) {
    3.38 +					for(l=0; l<sdlscale; l++) {
    3.39 +						dest[k * sdlscale * WIDTH + l] = pixel;
    3.40 +					}
    3.41 +				}
    3.42 +				dest += sdlscale;
    3.43  			}
    3.44 -			dest += WIDTH;
    3.45 +			dest += WIDTH * sdlscale * (sdlscale - 1);
    3.46  		}
    3.47  
    3.48  		if(SDL_MUSTLOCK(surf)) {
     4.1 --- a/src/polyfill.c	Mon Jun 16 22:01:45 2014 +0300
     4.2 +++ b/src/polyfill.c	Wed Jun 18 04:13:02 2014 +0300
     4.3 @@ -1,5 +1,6 @@
     4.4  #include "config.h"
     4.5  #include <string.h>
     4.6 +#include <assert.h>
     4.7  #include "polyfill.h"
     4.8  #include "fixed.h"
     4.9  #include "gbasys.h"
    4.10 @@ -32,29 +33,34 @@
    4.11  	lidx[1] = topidx ? topidx - 1 : num - 1;
    4.12  	ridx[1] = (topidx + 1) % num;
    4.13  
    4.14 -	if(ridx[1] < lidx[1]) {
    4.15 +	if(verts[ridx[1]].x < verts[lidx[1]].x) {
    4.16  		return;	/* backface (CCW) */
    4.17  	}
    4.18  
    4.19  	lx = rx = verts[lidx[0]].x;
    4.20  
    4.21 +	/* TODO handle ldy == 0 or rdy == 0 */
    4.22  	ldy = verts[lidx[1]].y - verts[lidx[0]].y;
    4.23  	ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
    4.24  
    4.25 -	rdy = verts[ridx[1]].y - verts[ridx[1]].y;
    4.26 +	rdy = verts[ridx[1]].y - verts[ridx[0]].y;
    4.27  	rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
    4.28  
    4.29  	start = topy >> 16;
    4.30  	end = boty >> 16;
    4.31  
    4.32 +	if(end >= HEIGHT) end = HEIGHT - 1;
    4.33 +
    4.34  	y = topy;
    4.35  	for(i=start; i<end; i++) {
    4.36  		unsigned short x0, x1;
    4.37  
    4.38  		x0 = lx < 0 ? 0 : (lx >> 16);
    4.39 -		x1 = rx >= WIDTH ? WIDTH - 1 : (rx >> 16);
    4.40 +		x1 = (rx >> 16) >= WIDTH ? WIDTH - 1 : (rx >> 16);
    4.41  
    4.42 -		fill_scanline(i, x0, x1, color);
    4.43 +		if(i >= 0 && x1 > x0) {
    4.44 +			fill_scanline(i, x0, x1, color);
    4.45 +		}
    4.46  
    4.47  		if(y >= verts[lidx[1]].y) {
    4.48  			lidx[0] = lidx[1];
    4.49 @@ -77,6 +83,7 @@
    4.50  
    4.51  		lx += ldxdy;
    4.52  		rx += rdxdy;
    4.53 +		y += 65536;
    4.54  	}
    4.55  }
    4.56  
     5.1 --- a/src/sdlsys/gbasys.h	Mon Jun 16 22:01:45 2014 +0300
     5.2 +++ b/src/sdlsys/gbasys.h	Wed Jun 18 04:13:02 2014 +0300
     5.3 @@ -8,15 +8,15 @@
     5.4  	void *pixels;
     5.5  };
     5.6  
     5.7 -extern struct pixel_buffer *back_buffer;
     5.8 +extern struct pixel_buffer *back_buffer, *front_buffer;
     5.9  
    5.10  #define RGB(r, g, b)\
    5.11 -	((((b) >> 3) & 0x1f) << 10) |\
    5.12 -	((((g) >> 3) & 0x1f) << 5) |\
    5.13 +	((((b) >> 3) & 0x1f) << 11) |\
    5.14 +	((((g) >> 2) & 0x3f) << 5) |\
    5.15  	(((r) >> 3) & 0x1f)
    5.16  
    5.17 -#define GET_R(c)	((((c) >> 10) & 0x1f) << 3)
    5.18 -#define GET_G(c)	((((c) >> 5) & 0x1f) << 3)
    5.19 +#define GET_R(c)	((((c) >> 11) & 0x1f) << 3)
    5.20 +#define GET_G(c)	((((c) >> 5) & 0x3f) << 2)
    5.21  #define GET_B(c)	(((c) & 0x1f) << 3)
    5.22  
    5.23  /* defined in main_sdl.c */