gba-x3dtest

changeset 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
files .hgignore Makefile src/game.c src/main_sdl.c
diffstat 4 files changed, 70 insertions(+), 63 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Thu Jun 26 06:57:51 2014 +0300
     1.2 +++ b/.hgignore	Thu Jun 26 21:48:09 2014 +0300
     1.3 @@ -3,4 +3,6 @@
     1.4  \.d$
     1.5  \.elf$
     1.6  \.gba$
     1.7 -trycatch$
     1.8 +data\.h$
     1.9 +\.img\.c$
    1.10 +x3dtest$
     2.1 --- a/Makefile	Thu Jun 26 06:57:51 2014 +0300
     2.2 +++ b/Makefile	Thu Jun 26 21:48:09 2014 +0300
     2.3 @@ -21,7 +21,7 @@
     2.4  
     2.5  CFLAGS = $(opt) $(dbg) -pedantic -Wall -I. -I../gbasys/src
     2.6  LDFLAGS = ../gbasys/libgbasys.a -lm
     2.7 -EMUFLAGS = -T 100 -f 1
     2.8 +EMUFLAGS = -T 100 -f 1 --agb-print
     2.9  
    2.10  .PHONY: all
    2.11  all: $(bin) $(bin_mb)
     3.1 --- a/src/game.c	Thu Jun 26 06:57:51 2014 +0300
     3.2 +++ b/src/game.c	Thu Jun 26 21:48:09 2014 +0300
     3.3 @@ -7,33 +7,22 @@
     3.4  #include "palman.h"
     3.5  #include "ggen.h"
     3.6  #include "data.h"
     3.7 +#include "logger.h"
     3.8  
     3.9  extern int dbg_fill_dump;
    3.10  
    3.11 +static void draw_fps_meter(unsigned long msec);
    3.12  static void draw_rect(int x, int y, int w, int h, uint16_t color);
    3.13  
    3.14  #define X16INT(x)	((x) << 16)
    3.15  #define X16FLT(x)	((int32_t)((x) * 65536.0))
    3.16  
    3.17 -static const int32_t poly[] = {
    3.18 -	X16INT(-1), X16INT(-1), 0,
    3.19 -	X16INT(-1), X16INT(1), 0,
    3.20 -	X16INT(1), X16INT(1), 0,
    3.21 -	X16INT(1), X16INT(-1), 0,
    3.22 -	X16INT(1), X16INT(-1), 0,
    3.23 -	X16INT(1), X16INT(1), 0,
    3.24 -	X16INT(-1), X16INT(1), 0,
    3.25 -	X16INT(-1), X16INT(-1), 0
    3.26 -};
    3.27 -static const int32_t colors[] = {
    3.28 -	65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0,
    3.29 -	65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0
    3.30 -};
    3.31 -static const short vcount = sizeof poly / sizeof *poly / 3;
    3.32 +static int32_t cam_theta, cam_phi = 25 << 16;
    3.33 +static int autorot = 1;
    3.34  
    3.35  static struct mesh box;
    3.36  static int tex;
    3.37 -
    3.38 +static int fps;
    3.39  
    3.40  int game_init(void)
    3.41  {
    3.42 @@ -52,25 +41,42 @@
    3.43  	return 0;
    3.44  }
    3.45  
    3.46 -static int32_t cam_theta, cam_phi = 25 << 16;
    3.47 -static short keyrot;
    3.48 -static int autorot = 1;
    3.49 +static void update(unsigned long msec)
    3.50 +{
    3.51 +	static unsigned long prev_upd;
    3.52 +	unsigned long dt = msec - prev_upd;
    3.53 +	int keys = get_key_state(KEY_ALL);
    3.54 +	prev_upd = msec;
    3.55 +
    3.56 +	if(keys & KEY_LEFT) {
    3.57 +		cam_theta += dt << 12;
    3.58 +	}
    3.59 +	if(keys & KEY_RIGHT) {
    3.60 +		cam_theta -= dt << 12;
    3.61 +	}
    3.62 +	if(keys & KEY_UP) {
    3.63 +		cam_phi += dt << 12;
    3.64 +		if(cam_phi > (90 << 16)) {
    3.65 +			cam_phi = 90 << 16;
    3.66 +		}
    3.67 +	}
    3.68 +	if(keys & KEY_DOWN) {
    3.69 +		cam_phi -= dt << 12;
    3.70 +		if(cam_phi < -(90 << 16)) {
    3.71 +			cam_phi = -90 << 16;
    3.72 +		}
    3.73 +	}
    3.74 +}
    3.75  
    3.76  void game_draw(void)
    3.77  {
    3.78  	unsigned long msec = get_millisec();
    3.79  
    3.80 +	update(msec);
    3.81 +
    3.82  	clear_buffer(back_buffer, 0);
    3.83  
    3.84  	x3d_load_identity();
    3.85 -	/*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
    3.86 -	if(autorot) {
    3.87 -		x3d_rotate((msec / 64) << 16, 0, 0, 65536);
    3.88 -	} else {
    3.89 -		x3d_rotate(keyrot << 16, 0, 0, 65536);
    3.90 -	}
    3.91 -	x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
    3.92 -
    3.93  	x3d_translate(0, 0, X16INT(6));
    3.94  	x3d_rotate(cam_phi, 65536, 0, 0);
    3.95  	if(autorot) {
    3.96 @@ -89,46 +95,38 @@
    3.97  	draw_mesh(&box);
    3.98  	x3d_disable_texture();
    3.99  
   3.100 -	/*
   3.101 -	x3d_vertex_array(vcount, poly);
   3.102 -	x3d_color_array(vcount, colors);
   3.103 -	x3d_draw(X3D_QUADS, vcount);
   3.104 -	x3d_color_array(0, 0);
   3.105 -
   3.106 -#ifdef PALMODE
   3.107 -	x3d_color_index(RGBPAL(0, 255, 0));
   3.108 -#else
   3.109 -	x3d_color(0, 65536, 0);
   3.110 -#endif
   3.111 -
   3.112 -	x3d_draw(X3D_POINTS, vcount);
   3.113 -	x3d_vertex_array(0, 0);
   3.114 -	*/
   3.115 +	draw_fps_meter(msec);
   3.116  
   3.117  	flip();
   3.118  }
   3.119  
   3.120 +static void draw_fps_meter(unsigned long msec)
   3.121 +{
   3.122 +	static unsigned long last_msec;
   3.123 +	static unsigned long nframes;
   3.124 +	unsigned long dt = msec - last_msec;
   3.125 +	int bar_height;
   3.126 +
   3.127 +	++nframes;
   3.128 +
   3.129 +	if(dt >= 1000) {
   3.130 +		last_msec = msec;
   3.131 +		fps = 1000 * nframes / dt;
   3.132 +		nframes = 0;
   3.133 +		logmsg(LOG_DBG, "fps: %d\n", fps);
   3.134 +	}
   3.135 +
   3.136 +	bar_height = fps * 4;
   3.137 +	if(bar_height > HEIGHT) bar_height = HEIGHT;
   3.138 +
   3.139 +	draw_rect(0, HEIGHT - bar_height, 1, bar_height, RGB(0, 255, 0));
   3.140 +}
   3.141 +
   3.142  void game_keyb(int key, int pressed)
   3.143  {
   3.144  	if(!pressed) return;
   3.145  
   3.146  	switch(key) {
   3.147 -	case KEY_LEFT:
   3.148 -		cam_theta += 65536;
   3.149 -		break;
   3.150 -
   3.151 -	case KEY_RIGHT:
   3.152 -		cam_theta -= 65536;
   3.153 -		break;
   3.154 -
   3.155 -	case KEY_UP:
   3.156 -		cam_phi += 65536;
   3.157 -		break;
   3.158 -
   3.159 -	case KEY_DOWN:
   3.160 -		cam_phi -= 65536;
   3.161 -		break;
   3.162 -
   3.163  	case KEY_A:
   3.164  		autorot = !autorot;
   3.165  		break;
     4.1 --- a/src/main_sdl.c	Thu Jun 26 06:57:51 2014 +0300
     4.2 +++ b/src/main_sdl.c	Thu Jun 26 21:48:09 2014 +0300
     4.3 @@ -104,7 +104,6 @@
     4.4  		if(SDL_MUSTLOCK(surf)) {
     4.5  			SDL_UnlockSurface(surf);
     4.6  		}
     4.7 -		keystate = 0;
     4.8  	}
     4.9  
    4.10  done:
    4.11 @@ -160,7 +159,7 @@
    4.12  	switch(ev->type) {
    4.13  	case SDL_KEYDOWN:
    4.14  	case SDL_KEYUP:
    4.15 -		handle_keyboard(ev->key.keysym.sym, ev->key.state);
    4.16 +		handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
    4.17  		break;
    4.18  
    4.19  	case SDL_QUIT:
    4.20 @@ -233,6 +232,14 @@
    4.21  		return;
    4.22  	}
    4.23  
    4.24 -	keystate |= gba_key;
    4.25 +	if(gba_key == -1) {
    4.26 +		return;
    4.27 +	}
    4.28 +
    4.29 +	if(state) {
    4.30 +		keystate |= gba_key;
    4.31 +	} else {
    4.32 +		keystate &= ~gba_key;
    4.33 +	}
    4.34  	game_keyb(gba_key, state);
    4.35  }