gba-trycatch

changeset 12:ecc022a21279

more tuff
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 06:44:04 +0300
parents c47e54796b82
children 2070a81127f2
files src/game.c src/ggen.c src/ggen.h src/mesh.c src/mesh.h src/x3d.c src/x3d.h
diffstat 7 files changed, 160 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/src/game.c	Sun Jun 22 07:54:29 2014 +0300
     1.2 +++ b/src/game.c	Mon Jun 23 06:44:04 2014 +0300
     1.3 @@ -9,11 +9,12 @@
     1.4  static void draw_rect(int x, int y, int w, int h, uint16_t color);
     1.5  
     1.6  #define X16INT(x)	((x) << 16)
     1.7 +#define X16FLT(x)	((int32_t)((x) * 65536.0))
     1.8  
     1.9  static const int32_t poly[] = {
    1.10 -	X16INT(120), X16INT(20), 0,
    1.11 -	X16INT(200), X16INT(130), 0,
    1.12 -	X16INT(40), X16INT(100), 0
    1.13 +	X16FLT(0), X16FLT(1), 0,
    1.14 +	X16FLT(1), X16FLT(-0.8), 0,
    1.15 +	X16FLT(-1), X16FLT(-0.5), 0
    1.16  };
    1.17  static const short vcount = sizeof poly / sizeof *poly / 3;
    1.18  
    1.19 @@ -24,6 +25,8 @@
    1.20  	palman_init();
    1.21  #endif
    1.22  
    1.23 +	x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500);
    1.24 +
    1.25  	return 0;
    1.26  }
    1.27  
    1.28 @@ -37,13 +40,20 @@
    1.29  	clear_buffer(back_buffer, 0);
    1.30  
    1.31  	x3d_load_identity();
    1.32 -	x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
    1.33 +	/*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
    1.34  	if(autorot) {
    1.35  		x3d_rotate((msec / 64) << 16, 0, 0, 65536);
    1.36  	} else {
    1.37  		x3d_rotate(keyrot << 16, 0, 0, 65536);
    1.38  	}
    1.39 -	x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);
    1.40 +	x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
    1.41 +
    1.42 +	if(autorot) {
    1.43 +		x3d_rotate((msec / 64) << 16, 0, 65536, 0);
    1.44 +	} else {
    1.45 +		x3d_rotate(keyrot << 16, 0, 65536, 0);
    1.46 +	}
    1.47 +	x3d_translate(0, 0, X16INT(2));
    1.48  
    1.49  	x3d_vertex_array(vcount, poly);
    1.50  
    1.51 @@ -52,14 +62,14 @@
    1.52  #else
    1.53  	x3d_color(65536, 65536, 65536);
    1.54  #endif
    1.55 -	x3d_draw_arrays(X3D_TRIANGLES, vcount);
    1.56 +	x3d_draw(X3D_TRIANGLES, vcount);
    1.57  
    1.58  #ifdef PALMODE
    1.59  	x3d_color_index(RGBPAL(0, 255, 0));
    1.60  #else
    1.61  	x3d_color(0, 65536, 0);
    1.62  #endif
    1.63 -	x3d_draw_arrays(X3D_POINTS, vcount);
    1.64 +	x3d_draw(X3D_POINTS, vcount);
    1.65  	x3d_vertex_array(0, 0);
    1.66  
    1.67  	draw_rect(0, 0, WIDTH, HEIGHT, RGBPAL(255, 0, 0));
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/ggen.c	Mon Jun 23 06:44:04 2014 +0300
     2.3 @@ -0,0 +1,63 @@
     2.4 +#include <stdlib.h>
     2.5 +#include "ggen.h"
     2.6 +#include "mesh.h"
     2.7 +
     2.8 +#define X1	65536
     2.9 +#define VERTEX(x, y, z)	\
    2.10 +	do { \
    2.11 +		*vptr++ = x; \
    2.12 +		*vptr++ = y; \
    2.13 +		*vptr++ = z; \
    2.14 +	} while(0)
    2.15 +
    2.16 +int gen_box(struct mesh *m)
    2.17 +{
    2.18 +	int num_faces = 8;
    2.19 +	int num_verts = num_faces * 4;
    2.20 +	int32_t v[8][3] = {
    2.21 +		{-X1, -X1, -X1}, {X1, -X1, -X1}, {X1, -X1, X1}, {-X1, -X1, X1},
    2.22 +		{-X1, X1, -X1}, {X1, X1, -X1}, {X1, X1, X1}, {-X1, X1, X1}
    2.23 +	};
    2.24 +	int32_t *vptr;
    2.25 +
    2.26 +
    2.27 +	m->prim = X3D_QUADS;
    2.28 +	m->nverts = num_verts;
    2.29 +	if(!(m->verts = malloc(num_verts * sizeof *m->verts))) {
    2.30 +		return -1;
    2.31 +	}
    2.32 +	vptr = m->verts;
    2.33 +
    2.34 +	/* -Y */
    2.35 +	VERTEX(v[0][0], v[0][1], v[0][2]);
    2.36 +	VERTEX(v[1][0], v[1][1], v[1][2]);
    2.37 +	VERTEX(v[2][0], v[2][1], v[2][2]);
    2.38 +	VERTEX(v[3][0], v[3][1], v[3][2]);
    2.39 +	/* +Y */
    2.40 +	VERTEX(v[4][0], v[4][1], v[4][2]);
    2.41 +	VERTEX(v[5][0], v[5][1], v[5][2]);
    2.42 +	VERTEX(v[6][0], v[6][1], v[6][2]);
    2.43 +	VERTEX(v[7][0], v[7][1], v[7][2]);
    2.44 +	/* -Z */
    2.45 +	VERTEX(v[0][0], v[0][1], v[0][2]);
    2.46 +	VERTEX(v[4][0], v[4][1], v[4][2]);
    2.47 +	VERTEX(v[5][0], v[5][1], v[5][2]);
    2.48 +	VERTEX(v[1][0], v[1][1], v[1][2]);
    2.49 +	/* +Z */
    2.50 +	VERTEX(v[2][0], v[2][1], v[2][2]);
    2.51 +	VERTEX(v[6][0], v[6][1], v[6][2]);
    2.52 +	VERTEX(v[7][0], v[7][1], v[7][2]);
    2.53 +	VERTEX(v[3][0], v[3][1], v[3][2]);
    2.54 +	/* +X */
    2.55 +	VERTEX(v[1][0], v[1][1], v[1][2]);
    2.56 +	VERTEX(v[5][0], v[5][1], v[5][2]);
    2.57 +	VERTEX(v[6][0], v[6][1], v[6][2]);
    2.58 +	VERTEX(v[2][0], v[2][1], v[2][2]);
    2.59 +	/* -X */
    2.60 +	VERTEX(v[3][0], v[3][1], v[3][2]);
    2.61 +	VERTEX(v[7][0], v[7][1], v[7][2]);
    2.62 +	VERTEX(v[4][0], v[4][1], v[4][2]);
    2.63 +	VERTEX(v[0][0], v[0][1], v[0][2]);
    2.64 +
    2.65 +	return 0;
    2.66 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/ggen.h	Mon Jun 23 06:44:04 2014 +0300
     3.3 @@ -0,0 +1,8 @@
     3.4 +#ifndef GGEN_H_
     3.5 +#define GGEN_H_
     3.6 +
     3.7 +#include "mesh.h"
     3.8 +
     3.9 +int gen_box(struct mesh *m);
    3.10 +
    3.11 +#endif	/* GGEN_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/mesh.c	Mon Jun 23 06:44:04 2014 +0300
     4.3 @@ -0,0 +1,22 @@
     4.4 +#include <stdlib.h>
     4.5 +#include "mesh.h"
     4.6 +#include "x3d.h"
     4.7 +
     4.8 +void init_mesh(struct mesh *m)
     4.9 +{
    4.10 +	m->prim = X3D_TRIANGLES;
    4.11 +	m->verts = 0;
    4.12 +	m->nverts = 0;
    4.13 +}
    4.14 +
    4.15 +void destroy_mesh(struct mesh *m)
    4.16 +{
    4.17 +	free(m->verts);
    4.18 +}
    4.19 +
    4.20 +void draw_mesh(struct mesh *m)
    4.21 +{
    4.22 +	x3d_vertex_array(m->nverts, m->verts);
    4.23 +	x3d_draw(m->prim, m->nverts);
    4.24 +	x3d_vertex_array(0, 0);
    4.25 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/mesh.h	Mon Jun 23 06:44:04 2014 +0300
     5.3 @@ -0,0 +1,19 @@
     5.4 +#ifndef MESH_H_
     5.5 +#define MESH_H_
     5.6 +
     5.7 +#include <stdint.h>
     5.8 +#include "x3d.h"
     5.9 +
    5.10 +struct mesh {
    5.11 +	int prim;
    5.12 +
    5.13 +	int32_t *verts;
    5.14 +	int nverts;
    5.15 +};
    5.16 +
    5.17 +void init_mesh(struct mesh *m);
    5.18 +void destroy_mesh(struct mesh *m);
    5.19 +
    5.20 +void draw_mesh(struct mesh *m);
    5.21 +
    5.22 +#endif	/* MESH_H_ */
     6.1 --- a/src/x3d.c	Sun Jun 22 07:54:29 2014 +0300
     6.2 +++ b/src/x3d.c	Mon Jun 23 06:44:04 2014 +0300
     6.3 @@ -1,5 +1,6 @@
     6.4  #include "config.h"
     6.5  #include <string.h>
     6.6 +#include <math.h>
     6.7  #include "x3d.h"
     6.8  #include "fixed.h"
     6.9  #include "sincos.h"
    6.10 @@ -20,6 +21,7 @@
    6.11  static int32_t proj_aspect = 65536;
    6.12  static int32_t proj_near = ftox16(0.5);
    6.13  static int32_t proj_far = 500 << 16;
    6.14 +static int32_t tan_half_xfov, tan_half_yfov;
    6.15  
    6.16  #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0}
    6.17  
    6.18 @@ -36,12 +38,15 @@
    6.19  static int32_t im_color[3];
    6.20  static uint8_t im_color_index;
    6.21  
    6.22 -void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz)
    6.23 +void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz)
    6.24  {
    6.25 -	proj_fov = fov;
    6.26 +	proj_fov = (M_PI_X16 * fov) / 180;
    6.27  	proj_aspect = aspect;
    6.28  	proj_near = nearz;
    6.29  	proj_far = farz;
    6.30 +
    6.31 +	tan_half_yfov = (int32_t)(tan(0.5 * proj_fov / 65536.0) * 65536.0);
    6.32 +	tan_half_xfov = x16mul(tan_half_yfov, aspect);
    6.33  }
    6.34  
    6.35  int x3d_push_matrix(void)
    6.36 @@ -152,7 +157,7 @@
    6.37  	color_count = count;
    6.38  }
    6.39  
    6.40 -int x3d_draw_arrays(int prim, int vnum)
    6.41 +int x3d_draw(int prim, int vnum)
    6.42  {
    6.43  	int i, j, pverts = prim;
    6.44  	const int32_t *vptr = vertex_array;
    6.45 @@ -182,6 +187,11 @@
    6.46  
    6.47  		for(j=0; j<pverts; j++) {
    6.48  			proc_vertex(vptr, cptr, vpos + j, col + j);
    6.49 +
    6.50 +			if(vpos[j].z <= proj_near) {
    6.51 +				goto skip_prim;
    6.52 +			}
    6.53 +
    6.54  			vptr += 3;
    6.55  			if(cptr) cptr += 3;
    6.56  		}
    6.57 @@ -200,6 +210,19 @@
    6.58  		color = RGB(cr, cg, cb);
    6.59  #endif
    6.60  
    6.61 +		/* project & viewport */
    6.62 +		for(j=0; j<pverts; j++) {
    6.63 +			int32_t x, y;
    6.64 +
    6.65 +			x = x16mul(vpos[j].x, tan_half_xfov);
    6.66 +			x = x16div(x, vpos[j].z);
    6.67 +			vpos[j].x = (x + 65536) * (WIDTH / 2);
    6.68 +
    6.69 +			y = x16mul(vpos[j].y, tan_half_yfov);
    6.70 +			y = x16div(y, vpos[j].z);
    6.71 +			vpos[j].y = (65536 - y) * (HEIGHT / 2);
    6.72 +		}
    6.73 +
    6.74  		switch(pverts) {
    6.75  		case X3D_POINTS:
    6.76  			draw_point(vpos, color);
    6.77 @@ -213,6 +236,7 @@
    6.78  			draw_poly(pverts, vpos, color);
    6.79  			break;
    6.80  		}
    6.81 +skip_prim: ;
    6.82  	}
    6.83  	return 0;
    6.84  }
     7.1 --- a/src/x3d.h	Sun Jun 22 07:54:29 2014 +0300
     7.2 +++ b/src/x3d.h	Mon Jun 23 06:44:04 2014 +0300
     7.3 @@ -10,7 +10,7 @@
     7.4  	X3D_QUADS = 4
     7.5  };
     7.6  
     7.7 -void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz);
     7.8 +void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz);
     7.9  
    7.10  int x3d_push_matrix(void);
    7.11  int x3d_pop_matrix(void);
    7.12 @@ -23,7 +23,9 @@
    7.13  
    7.14  void x3d_vertex_array(int count, const int32_t *ptr);
    7.15  void x3d_color_array(int count, const int32_t *ptr);
    7.16 -int x3d_draw_arrays(int prim, int vnum);
    7.17 +
    7.18 +int x3d_draw(int prim, int vnum);
    7.19 +int x3d_draw_indexed(int prim, int count, uint16_t *ptr);
    7.20  
    7.21  void x3d_color_index(int cidx);
    7.22  void x3d_color(int32_t r, int32_t g, int32_t b);