gba-x3dtest

diff src/game.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 ecc022a21279
line diff
     1.1 --- a/src/game.c	Thu Jun 19 05:53:46 2014 +0300
     1.2 +++ b/src/game.c	Sun Jun 22 05:16:10 2014 +0300
     1.3 @@ -4,23 +4,32 @@
     1.4  #include "x3d.h"
     1.5  #include "sincos.h"
     1.6  #include "fixed.h"
     1.7 +#include "palman.h"
     1.8 +
     1.9 +static void draw_rect(int x, int y, int w, int h, uint16_t color);
    1.10  
    1.11  #define X16INT(x)	((x) << 16)
    1.12  
    1.13  static const int32_t poly[] = {
    1.14 -	X16INT(80), X16INT(10), 0,
    1.15 -	X16INT(140), X16INT(100), 0,
    1.16 -	X16INT(40), X16INT(80), 0
    1.17 +	X16INT(120), X16INT(20), 0,
    1.18 +	X16INT(200), X16INT(130), 0,
    1.19 +	X16INT(40), X16INT(100), 0
    1.20  };
    1.21  static const short vcount = sizeof poly / sizeof *poly / 3;
    1.22  
    1.23  int game_init(void)
    1.24  {
    1.25  	sincos_init();
    1.26 +#ifdef PALMODE
    1.27 +	palman_init();
    1.28 +#endif
    1.29  
    1.30  	return 0;
    1.31  }
    1.32  
    1.33 +static short keyrot;
    1.34 +static int autorot = 1;
    1.35 +
    1.36  void game_draw(void)
    1.37  {
    1.38  	unsigned long msec = get_millisec();
    1.39 @@ -29,22 +38,119 @@
    1.40  
    1.41  	x3d_load_identity();
    1.42  	x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
    1.43 -	x3d_rotate((msec / 64) << 16, 0, 0, 65536);
    1.44 +	if(autorot) {
    1.45 +		x3d_rotate((msec / 64) << 16, 0, 0, 65536);
    1.46 +	} else {
    1.47 +		x3d_rotate(keyrot << 16, 0, 0, 65536);
    1.48 +	}
    1.49  	x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);
    1.50  
    1.51  	x3d_vertex_array(vcount, poly);
    1.52  
    1.53 +#ifdef PALMODE
    1.54 +	x3d_color_index(255);
    1.55 +#else
    1.56  	x3d_color(65536, 65536, 65536);
    1.57 +#endif
    1.58  	x3d_draw_arrays(X3D_TRIANGLES, vcount);
    1.59  
    1.60 +#ifdef PALMODE
    1.61 +	x3d_color_index(RGBPAL(0, 255, 0));
    1.62 +#else
    1.63  	x3d_color(0, 65536, 0);
    1.64 +#endif
    1.65  	x3d_draw_arrays(X3D_POINTS, vcount);
    1.66 +	x3d_vertex_array(0, 0);
    1.67  
    1.68 -	x3d_vertex_array(0, 0);
    1.69 +	draw_rect(0, 0, WIDTH, HEIGHT, RGBPAL(255, 0, 0));
    1.70 +	draw_rect(1, 1, WIDTH - 2, HEIGHT - 2, RGBPAL(0, 255, 0));
    1.71 +	draw_rect(2, 2, WIDTH - 4, HEIGHT - 4, RGBPAL(32, 64, 255));
    1.72  
    1.73  	flip();
    1.74  }
    1.75  
    1.76  void game_keyb(int key, int pressed)
    1.77  {
    1.78 +	if(!pressed) return;
    1.79 +
    1.80 +	switch(key) {
    1.81 +	case KEY_LEFT:
    1.82 +		keyrot--;
    1.83 +		break;
    1.84 +
    1.85 +	case KEY_RIGHT:
    1.86 +		keyrot++;
    1.87 +		break;
    1.88 +
    1.89 +	case KEY_A:
    1.90 +		autorot = !autorot;
    1.91 +		break;
    1.92 +
    1.93 +	default:
    1.94 +		break;
    1.95 +	}
    1.96  }
    1.97 +
    1.98 +#ifdef PALMODE
    1.99 +#define ROWADV	(WIDTH / 2)
   1.100 +#else
   1.101 +#define ROWADV	WIDTH
   1.102 +#endif
   1.103 +
   1.104 +static void draw_rect(int x, int y, int w, int h, uint16_t color)
   1.105 +{
   1.106 +	int i, xsz = w, ysz = h;
   1.107 +	uint16_t *pixels = back_buffer->pixels;
   1.108 +	uint16_t *topleft, *topright, *botleft;
   1.109 +
   1.110 +#ifdef PALMODE
   1.111 +	pixels += (y * WIDTH + x) / 2;
   1.112 +	topleft = pixels;
   1.113 +	topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2;
   1.114 +
   1.115 +	color |= color << 8;
   1.116 +	xsz /= 2;
   1.117 +#else
   1.118 +	pixels += y * WIDTH + x;
   1.119 +	topleft = pixels;
   1.120 +	topright = topleft + w - 1;
   1.121 +#endif
   1.122 +	botleft = topleft + (ysz - 1) * ROWADV;
   1.123 +
   1.124 +#ifdef PALMODE
   1.125 +	if(x & 1) {
   1.126 +		*topleft = (*topleft & 0xff) | (color & 0xff00);
   1.127 +		*botleft = (*topleft & 0xff) | (color & 0xff00);
   1.128 +		++topleft;
   1.129 +		++botleft;
   1.130 +		xsz -= 1;
   1.131 +	}
   1.132 +#endif
   1.133 +	for(i=0; i<xsz; i++) {
   1.134 +		*topleft++ = color;
   1.135 +		*botleft++ = color;
   1.136 +	}
   1.137 +
   1.138 +	topleft = pixels;
   1.139 +	for(i=0; i<ysz; i++) {
   1.140 +#ifdef PALMODE
   1.141 +		if(x & 1) {
   1.142 +			*topleft = (*topleft & 0xff) | (color & 0xff00);
   1.143 +		} else {
   1.144 +			*topleft = (*topleft & 0xff00) | (color & 0xff);
   1.145 +		}
   1.146 +
   1.147 +		if((x + w - 1) & 1) {
   1.148 +			*topright = (*topright & 0xff) | (color & 0xff00);
   1.149 +		} else {
   1.150 +			*topright = (*topright & 0xff00) | (color & 0xff);
   1.151 +		}
   1.152 +#else
   1.153 +		*topleft = color;
   1.154 +		*topright = color;
   1.155 +#endif
   1.156 +
   1.157 +		topleft += ROWADV;
   1.158 +		topright += ROWADV;
   1.159 +	}
   1.160 +}