gba-x3dtest

annotate src/game.c @ 19:62390f9cc93e

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