# HG changeset patch # User John Tsiombikas # Date 1403808489 -10800 # Node ID f907b2c50a8bef06f9307961d157a65c63bdbd5a # Parent 0a7f402892b383ff6fdcc3f284d5e7347a3dd8f9 added fps bar diff -r 0a7f402892b3 -r f907b2c50a8b .hgignore --- a/.hgignore Thu Jun 26 06:57:51 2014 +0300 +++ b/.hgignore Thu Jun 26 21:48:09 2014 +0300 @@ -3,4 +3,6 @@ \.d$ \.elf$ \.gba$ -trycatch$ +data\.h$ +\.img\.c$ +x3dtest$ diff -r 0a7f402892b3 -r f907b2c50a8b Makefile --- a/Makefile Thu Jun 26 06:57:51 2014 +0300 +++ b/Makefile Thu Jun 26 21:48:09 2014 +0300 @@ -21,7 +21,7 @@ CFLAGS = $(opt) $(dbg) -pedantic -Wall -I. -I../gbasys/src LDFLAGS = ../gbasys/libgbasys.a -lm -EMUFLAGS = -T 100 -f 1 +EMUFLAGS = -T 100 -f 1 --agb-print .PHONY: all all: $(bin) $(bin_mb) diff -r 0a7f402892b3 -r f907b2c50a8b src/game.c --- a/src/game.c Thu Jun 26 06:57:51 2014 +0300 +++ b/src/game.c Thu Jun 26 21:48:09 2014 +0300 @@ -7,33 +7,22 @@ #include "palman.h" #include "ggen.h" #include "data.h" +#include "logger.h" extern int dbg_fill_dump; +static void draw_fps_meter(unsigned long msec); static void draw_rect(int x, int y, int w, int h, uint16_t color); #define X16INT(x) ((x) << 16) #define X16FLT(x) ((int32_t)((x) * 65536.0)) -static const int32_t poly[] = { - X16INT(-1), X16INT(-1), 0, - X16INT(-1), X16INT(1), 0, - X16INT(1), X16INT(1), 0, - X16INT(1), X16INT(-1), 0, - X16INT(1), X16INT(-1), 0, - X16INT(1), X16INT(1), 0, - X16INT(-1), X16INT(1), 0, - X16INT(-1), X16INT(-1), 0 -}; -static const int32_t colors[] = { - 65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0, - 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0 -}; -static const short vcount = sizeof poly / sizeof *poly / 3; +static int32_t cam_theta, cam_phi = 25 << 16; +static int autorot = 1; static struct mesh box; static int tex; - +static int fps; int game_init(void) { @@ -52,25 +41,42 @@ return 0; } -static int32_t cam_theta, cam_phi = 25 << 16; -static short keyrot; -static int autorot = 1; +static void update(unsigned long msec) +{ + static unsigned long prev_upd; + unsigned long dt = msec - prev_upd; + int keys = get_key_state(KEY_ALL); + prev_upd = msec; + + if(keys & KEY_LEFT) { + cam_theta += dt << 12; + } + if(keys & KEY_RIGHT) { + cam_theta -= dt << 12; + } + if(keys & KEY_UP) { + cam_phi += dt << 12; + if(cam_phi > (90 << 16)) { + cam_phi = 90 << 16; + } + } + if(keys & KEY_DOWN) { + cam_phi -= dt << 12; + if(cam_phi < -(90 << 16)) { + cam_phi = -90 << 16; + } + } +} void game_draw(void) { unsigned long msec = get_millisec(); + update(msec); + clear_buffer(back_buffer, 0); x3d_load_identity(); - /*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0); - if(autorot) { - x3d_rotate((msec / 64) << 16, 0, 0, 65536); - } else { - x3d_rotate(keyrot << 16, 0, 0, 65536); - } - x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/ - x3d_translate(0, 0, X16INT(6)); x3d_rotate(cam_phi, 65536, 0, 0); if(autorot) { @@ -89,46 +95,38 @@ draw_mesh(&box); x3d_disable_texture(); - /* - x3d_vertex_array(vcount, poly); - x3d_color_array(vcount, colors); - x3d_draw(X3D_QUADS, vcount); - x3d_color_array(0, 0); - -#ifdef PALMODE - x3d_color_index(RGBPAL(0, 255, 0)); -#else - x3d_color(0, 65536, 0); -#endif - - x3d_draw(X3D_POINTS, vcount); - x3d_vertex_array(0, 0); - */ + draw_fps_meter(msec); flip(); } +static void draw_fps_meter(unsigned long msec) +{ + static unsigned long last_msec; + static unsigned long nframes; + unsigned long dt = msec - last_msec; + int bar_height; + + ++nframes; + + if(dt >= 1000) { + last_msec = msec; + fps = 1000 * nframes / dt; + nframes = 0; + logmsg(LOG_DBG, "fps: %d\n", fps); + } + + bar_height = fps * 4; + if(bar_height > HEIGHT) bar_height = HEIGHT; + + draw_rect(0, HEIGHT - bar_height, 1, bar_height, RGB(0, 255, 0)); +} + void game_keyb(int key, int pressed) { if(!pressed) return; switch(key) { - case KEY_LEFT: - cam_theta += 65536; - break; - - case KEY_RIGHT: - cam_theta -= 65536; - break; - - case KEY_UP: - cam_phi += 65536; - break; - - case KEY_DOWN: - cam_phi -= 65536; - break; - case KEY_A: autorot = !autorot; break; diff -r 0a7f402892b3 -r f907b2c50a8b src/main_sdl.c --- a/src/main_sdl.c Thu Jun 26 06:57:51 2014 +0300 +++ b/src/main_sdl.c Thu Jun 26 21:48:09 2014 +0300 @@ -104,7 +104,6 @@ if(SDL_MUSTLOCK(surf)) { SDL_UnlockSurface(surf); } - keystate = 0; } done: @@ -160,7 +159,7 @@ switch(ev->type) { case SDL_KEYDOWN: case SDL_KEYUP: - handle_keyboard(ev->key.keysym.sym, ev->key.state); + handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0); break; case SDL_QUIT: @@ -233,6 +232,14 @@ return; } - keystate |= gba_key; + if(gba_key == -1) { + return; + } + + if(state) { + keystate |= gba_key; + } else { + keystate &= ~gba_key; + } game_keyb(gba_key, state); }