gba-x3dtest

diff src/game.c @ 18:f907b2c50a8b

added fps bar
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Jun 2014 21:48:09 +0300
parents 0a7f402892b3
children 62390f9cc93e
line diff
     1.1 --- a/src/game.c	Thu Jun 26 06:57:51 2014 +0300
     1.2 +++ b/src/game.c	Thu Jun 26 21:48:09 2014 +0300
     1.3 @@ -7,33 +7,22 @@
     1.4  #include "palman.h"
     1.5  #include "ggen.h"
     1.6  #include "data.h"
     1.7 +#include "logger.h"
     1.8  
     1.9  extern int dbg_fill_dump;
    1.10  
    1.11 +static void draw_fps_meter(unsigned long msec);
    1.12  static void draw_rect(int x, int y, int w, int h, uint16_t color);
    1.13  
    1.14  #define X16INT(x)	((x) << 16)
    1.15  #define X16FLT(x)	((int32_t)((x) * 65536.0))
    1.16  
    1.17 -static const int32_t poly[] = {
    1.18 -	X16INT(-1), X16INT(-1), 0,
    1.19 -	X16INT(-1), X16INT(1), 0,
    1.20 -	X16INT(1), X16INT(1), 0,
    1.21 -	X16INT(1), X16INT(-1), 0,
    1.22 -	X16INT(1), X16INT(-1), 0,
    1.23 -	X16INT(1), X16INT(1), 0,
    1.24 -	X16INT(-1), X16INT(1), 0,
    1.25 -	X16INT(-1), X16INT(-1), 0
    1.26 -};
    1.27 -static const int32_t colors[] = {
    1.28 -	65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0,
    1.29 -	65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0
    1.30 -};
    1.31 -static const short vcount = sizeof poly / sizeof *poly / 3;
    1.32 +static int32_t cam_theta, cam_phi = 25 << 16;
    1.33 +static int autorot = 1;
    1.34  
    1.35  static struct mesh box;
    1.36  static int tex;
    1.37 -
    1.38 +static int fps;
    1.39  
    1.40  int game_init(void)
    1.41  {
    1.42 @@ -52,25 +41,42 @@
    1.43  	return 0;
    1.44  }
    1.45  
    1.46 -static int32_t cam_theta, cam_phi = 25 << 16;
    1.47 -static short keyrot;
    1.48 -static int autorot = 1;
    1.49 +static void update(unsigned long msec)
    1.50 +{
    1.51 +	static unsigned long prev_upd;
    1.52 +	unsigned long dt = msec - prev_upd;
    1.53 +	int keys = get_key_state(KEY_ALL);
    1.54 +	prev_upd = msec;
    1.55 +
    1.56 +	if(keys & KEY_LEFT) {
    1.57 +		cam_theta += dt << 12;
    1.58 +	}
    1.59 +	if(keys & KEY_RIGHT) {
    1.60 +		cam_theta -= dt << 12;
    1.61 +	}
    1.62 +	if(keys & KEY_UP) {
    1.63 +		cam_phi += dt << 12;
    1.64 +		if(cam_phi > (90 << 16)) {
    1.65 +			cam_phi = 90 << 16;
    1.66 +		}
    1.67 +	}
    1.68 +	if(keys & KEY_DOWN) {
    1.69 +		cam_phi -= dt << 12;
    1.70 +		if(cam_phi < -(90 << 16)) {
    1.71 +			cam_phi = -90 << 16;
    1.72 +		}
    1.73 +	}
    1.74 +}
    1.75  
    1.76  void game_draw(void)
    1.77  {
    1.78  	unsigned long msec = get_millisec();
    1.79  
    1.80 +	update(msec);
    1.81 +
    1.82  	clear_buffer(back_buffer, 0);
    1.83  
    1.84  	x3d_load_identity();
    1.85 -	/*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
    1.86 -	if(autorot) {
    1.87 -		x3d_rotate((msec / 64) << 16, 0, 0, 65536);
    1.88 -	} else {
    1.89 -		x3d_rotate(keyrot << 16, 0, 0, 65536);
    1.90 -	}
    1.91 -	x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
    1.92 -
    1.93  	x3d_translate(0, 0, X16INT(6));
    1.94  	x3d_rotate(cam_phi, 65536, 0, 0);
    1.95  	if(autorot) {
    1.96 @@ -89,46 +95,38 @@
    1.97  	draw_mesh(&box);
    1.98  	x3d_disable_texture();
    1.99  
   1.100 -	/*
   1.101 -	x3d_vertex_array(vcount, poly);
   1.102 -	x3d_color_array(vcount, colors);
   1.103 -	x3d_draw(X3D_QUADS, vcount);
   1.104 -	x3d_color_array(0, 0);
   1.105 -
   1.106 -#ifdef PALMODE
   1.107 -	x3d_color_index(RGBPAL(0, 255, 0));
   1.108 -#else
   1.109 -	x3d_color(0, 65536, 0);
   1.110 -#endif
   1.111 -
   1.112 -	x3d_draw(X3D_POINTS, vcount);
   1.113 -	x3d_vertex_array(0, 0);
   1.114 -	*/
   1.115 +	draw_fps_meter(msec);
   1.116  
   1.117  	flip();
   1.118  }
   1.119  
   1.120 +static void draw_fps_meter(unsigned long msec)
   1.121 +{
   1.122 +	static unsigned long last_msec;
   1.123 +	static unsigned long nframes;
   1.124 +	unsigned long dt = msec - last_msec;
   1.125 +	int bar_height;
   1.126 +
   1.127 +	++nframes;
   1.128 +
   1.129 +	if(dt >= 1000) {
   1.130 +		last_msec = msec;
   1.131 +		fps = 1000 * nframes / dt;
   1.132 +		nframes = 0;
   1.133 +		logmsg(LOG_DBG, "fps: %d\n", fps);
   1.134 +	}
   1.135 +
   1.136 +	bar_height = fps * 4;
   1.137 +	if(bar_height > HEIGHT) bar_height = HEIGHT;
   1.138 +
   1.139 +	draw_rect(0, HEIGHT - bar_height, 1, bar_height, RGB(0, 255, 0));
   1.140 +}
   1.141 +
   1.142  void game_keyb(int key, int pressed)
   1.143  {
   1.144  	if(!pressed) return;
   1.145  
   1.146  	switch(key) {
   1.147 -	case KEY_LEFT:
   1.148 -		cam_theta += 65536;
   1.149 -		break;
   1.150 -
   1.151 -	case KEY_RIGHT:
   1.152 -		cam_theta -= 65536;
   1.153 -		break;
   1.154 -
   1.155 -	case KEY_UP:
   1.156 -		cam_phi += 65536;
   1.157 -		break;
   1.158 -
   1.159 -	case KEY_DOWN:
   1.160 -		cam_phi -= 65536;
   1.161 -		break;
   1.162 -
   1.163  	case KEY_A:
   1.164  		autorot = !autorot;
   1.165  		break;