gba-x3dtest

annotate 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
rev   line source
nuclear@6 1 #include "config.h"
nuclear@5 2 #include "game.h"
nuclear@5 3 #include "gbasys.h"
nuclear@8 4 #include "x3d.h"
nuclear@8 5 #include "sincos.h"
nuclear@8 6 #include "fixed.h"
nuclear@9 7 #include "palman.h"
nuclear@13 8 #include "ggen.h"
nuclear@17 9 #include "data.h"
nuclear@18 10 #include "logger.h"
nuclear@9 11
nuclear@14 12 extern int dbg_fill_dump;
nuclear@14 13
nuclear@18 14 static void draw_fps_meter(unsigned long msec);
nuclear@9 15 static void draw_rect(int x, int y, int w, int h, uint16_t color);
nuclear@5 16
nuclear@5 17 #define X16INT(x) ((x) << 16)
nuclear@12 18 #define X16FLT(x) ((int32_t)((x) * 65536.0))
nuclear@5 19
nuclear@18 20 static int32_t cam_theta, cam_phi = 25 << 16;
nuclear@18 21 static int autorot = 1;
nuclear@8 22
nuclear@13 23 static struct mesh box;
nuclear@17 24 static int tex;
nuclear@18 25 static int fps;
nuclear@13 26
nuclear@8 27 int game_init(void)
nuclear@8 28 {
nuclear@8 29 sincos_init();
nuclear@9 30 #ifdef PALMODE
nuclear@9 31 palman_init();
nuclear@9 32 #endif
nuclear@8 33
nuclear@17 34 tex = x3d_create_texture_rgb(test_width, test_height, test_pixels);
nuclear@17 35
nuclear@12 36 x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500);
nuclear@12 37
nuclear@13 38 init_mesh(&box);
nuclear@13 39 gen_box(&box);
nuclear@13 40
nuclear@8 41 return 0;
nuclear@8 42 }
nuclear@5 43
nuclear@18 44 static void update(unsigned long msec)
nuclear@18 45 {
nuclear@18 46 static unsigned long prev_upd;
nuclear@18 47 unsigned long dt = msec - prev_upd;
nuclear@18 48 int keys = get_key_state(KEY_ALL);
nuclear@18 49 prev_upd = msec;
nuclear@18 50
nuclear@18 51 if(keys & KEY_LEFT) {
nuclear@18 52 cam_theta += dt << 12;
nuclear@18 53 }
nuclear@18 54 if(keys & KEY_RIGHT) {
nuclear@18 55 cam_theta -= dt << 12;
nuclear@18 56 }
nuclear@18 57 if(keys & KEY_UP) {
nuclear@18 58 cam_phi += dt << 12;
nuclear@18 59 if(cam_phi > (90 << 16)) {
nuclear@18 60 cam_phi = 90 << 16;
nuclear@18 61 }
nuclear@18 62 }
nuclear@18 63 if(keys & KEY_DOWN) {
nuclear@18 64 cam_phi -= dt << 12;
nuclear@18 65 if(cam_phi < -(90 << 16)) {
nuclear@18 66 cam_phi = -90 << 16;
nuclear@18 67 }
nuclear@18 68 }
nuclear@18 69 }
nuclear@9 70
nuclear@5 71 void game_draw(void)
nuclear@5 72 {
nuclear@8 73 unsigned long msec = get_millisec();
nuclear@8 74
nuclear@18 75 update(msec);
nuclear@18 76
nuclear@5 77 clear_buffer(back_buffer, 0);
nuclear@5 78
nuclear@8 79 x3d_load_identity();
nuclear@14 80 x3d_translate(0, 0, X16INT(6));
nuclear@13 81 x3d_rotate(cam_phi, 65536, 0, 0);
nuclear@14 82 if(autorot) {
nuclear@14 83 x3d_rotate((msec / 64) << 16, 0, 65536, 0);
nuclear@14 84 } else {
nuclear@14 85 x3d_rotate(cam_theta, 0, 65536, 0);
nuclear@14 86 }
nuclear@6 87
nuclear@9 88 #ifdef PALMODE
nuclear@9 89 x3d_color_index(255);
nuclear@9 90 #else
nuclear@8 91 x3d_color(65536, 65536, 65536);
nuclear@9 92 #endif
nuclear@13 93
nuclear@17 94 x3d_enable_texture(tex);
nuclear@13 95 draw_mesh(&box);
nuclear@17 96 x3d_disable_texture();
nuclear@13 97
nuclear@18 98 draw_fps_meter(msec);
nuclear@6 99
nuclear@5 100 flip();
nuclear@5 101 }
nuclear@5 102
nuclear@18 103 static void draw_fps_meter(unsigned long msec)
nuclear@18 104 {
nuclear@18 105 static unsigned long last_msec;
nuclear@18 106 static unsigned long nframes;
nuclear@18 107 unsigned long dt = msec - last_msec;
nuclear@18 108 int bar_height;
nuclear@18 109
nuclear@18 110 ++nframes;
nuclear@18 111
nuclear@18 112 if(dt >= 1000) {
nuclear@18 113 last_msec = msec;
nuclear@18 114 fps = 1000 * nframes / dt;
nuclear@18 115 nframes = 0;
nuclear@18 116 logmsg(LOG_DBG, "fps: %d\n", fps);
nuclear@18 117 }
nuclear@18 118
nuclear@18 119 bar_height = fps * 4;
nuclear@18 120 if(bar_height > HEIGHT) bar_height = HEIGHT;
nuclear@18 121
nuclear@18 122 draw_rect(0, HEIGHT - bar_height, 1, bar_height, RGB(0, 255, 0));
nuclear@18 123 }
nuclear@18 124
nuclear@5 125 void game_keyb(int key, int pressed)
nuclear@5 126 {
nuclear@9 127 if(!pressed) return;
nuclear@9 128
nuclear@9 129 switch(key) {
nuclear@9 130 case KEY_A:
nuclear@9 131 autorot = !autorot;
nuclear@9 132 break;
nuclear@9 133
nuclear@14 134 case KEY_SELECT:
nuclear@14 135 dbg_fill_dump = 1;
nuclear@14 136 break;
nuclear@14 137
nuclear@9 138 default:
nuclear@9 139 break;
nuclear@9 140 }
nuclear@5 141 }
nuclear@9 142
nuclear@9 143 #ifdef PALMODE
nuclear@9 144 #define ROWADV (WIDTH / 2)
nuclear@9 145 #else
nuclear@9 146 #define ROWADV WIDTH
nuclear@9 147 #endif
nuclear@9 148
nuclear@9 149 static void draw_rect(int x, int y, int w, int h, uint16_t color)
nuclear@9 150 {
nuclear@9 151 int i, xsz = w, ysz = h;
nuclear@9 152 uint16_t *pixels = back_buffer->pixels;
nuclear@9 153 uint16_t *topleft, *topright, *botleft;
nuclear@9 154
nuclear@9 155 #ifdef PALMODE
nuclear@9 156 pixels += (y * WIDTH + x) / 2;
nuclear@9 157 topleft = pixels;
nuclear@9 158 topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2;
nuclear@9 159
nuclear@9 160 color |= color << 8;
nuclear@9 161 xsz /= 2;
nuclear@9 162 #else
nuclear@9 163 pixels += y * WIDTH + x;
nuclear@9 164 topleft = pixels;
nuclear@9 165 topright = topleft + w - 1;
nuclear@9 166 #endif
nuclear@9 167 botleft = topleft + (ysz - 1) * ROWADV;
nuclear@9 168
nuclear@9 169 #ifdef PALMODE
nuclear@9 170 if(x & 1) {
nuclear@9 171 *topleft = (*topleft & 0xff) | (color & 0xff00);
nuclear@9 172 *botleft = (*topleft & 0xff) | (color & 0xff00);
nuclear@9 173 ++topleft;
nuclear@9 174 ++botleft;
nuclear@9 175 xsz -= 1;
nuclear@9 176 }
nuclear@9 177 #endif
nuclear@9 178 for(i=0; i<xsz; i++) {
nuclear@9 179 *topleft++ = color;
nuclear@9 180 *botleft++ = color;
nuclear@9 181 }
nuclear@9 182
nuclear@9 183 topleft = pixels;
nuclear@9 184 for(i=0; i<ysz; i++) {
nuclear@9 185 #ifdef PALMODE
nuclear@9 186 if(x & 1) {
nuclear@9 187 *topleft = (*topleft & 0xff) | (color & 0xff00);
nuclear@9 188 } else {
nuclear@9 189 *topleft = (*topleft & 0xff00) | (color & 0xff);
nuclear@9 190 }
nuclear@9 191
nuclear@9 192 if((x + w - 1) & 1) {
nuclear@9 193 *topright = (*topright & 0xff) | (color & 0xff00);
nuclear@9 194 } else {
nuclear@9 195 *topright = (*topright & 0xff00) | (color & 0xff);
nuclear@9 196 }
nuclear@9 197 #else
nuclear@9 198 *topleft = color;
nuclear@9 199 *topright = color;
nuclear@9 200 #endif
nuclear@9 201
nuclear@9 202 topleft += ROWADV;
nuclear@9 203 topright += ROWADV;
nuclear@9 204 }
nuclear@9 205 }