nuclear@6: #include "config.h" nuclear@5: #include "game.h" nuclear@5: #include "gbasys.h" nuclear@8: #include "x3d.h" nuclear@8: #include "sincos.h" nuclear@8: #include "fixed.h" nuclear@9: #include "palman.h" nuclear@13: #include "ggen.h" nuclear@17: #include "data.h" nuclear@18: #include "logger.h" nuclear@9: nuclear@14: extern int dbg_fill_dump; nuclear@14: nuclear@18: static void draw_fps_meter(unsigned long msec); nuclear@9: static void draw_rect(int x, int y, int w, int h, uint16_t color); nuclear@5: nuclear@5: #define X16INT(x) ((x) << 16) nuclear@12: #define X16FLT(x) ((int32_t)((x) * 65536.0)) nuclear@5: nuclear@18: static int32_t cam_theta, cam_phi = 25 << 16; nuclear@18: static int autorot = 1; nuclear@8: nuclear@13: static struct mesh box; nuclear@17: static int tex; nuclear@18: static int fps; nuclear@13: nuclear@8: int game_init(void) nuclear@8: { nuclear@8: sincos_init(); nuclear@9: #ifdef PALMODE nuclear@9: palman_init(); nuclear@9: #endif nuclear@8: nuclear@17: tex = x3d_create_texture_rgb(test_width, test_height, test_pixels); nuclear@17: nuclear@12: x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500); nuclear@12: nuclear@13: init_mesh(&box); nuclear@13: gen_box(&box); nuclear@13: nuclear@8: return 0; nuclear@8: } nuclear@5: nuclear@18: static void update(unsigned long msec) nuclear@18: { nuclear@18: static unsigned long prev_upd; nuclear@18: unsigned long dt = msec - prev_upd; nuclear@18: int keys = get_key_state(KEY_ALL); nuclear@18: prev_upd = msec; nuclear@18: nuclear@18: if(keys & KEY_LEFT) { nuclear@18: cam_theta += dt << 12; nuclear@18: } nuclear@18: if(keys & KEY_RIGHT) { nuclear@18: cam_theta -= dt << 12; nuclear@18: } nuclear@18: if(keys & KEY_UP) { nuclear@18: cam_phi += dt << 12; nuclear@18: if(cam_phi > (90 << 16)) { nuclear@18: cam_phi = 90 << 16; nuclear@18: } nuclear@18: } nuclear@18: if(keys & KEY_DOWN) { nuclear@18: cam_phi -= dt << 12; nuclear@18: if(cam_phi < -(90 << 16)) { nuclear@18: cam_phi = -90 << 16; nuclear@18: } nuclear@18: } nuclear@18: } nuclear@9: nuclear@5: void game_draw(void) nuclear@5: { nuclear@8: unsigned long msec = get_millisec(); nuclear@8: nuclear@18: update(msec); nuclear@18: nuclear@5: clear_buffer(back_buffer, 0); nuclear@5: nuclear@8: x3d_load_identity(); nuclear@14: x3d_translate(0, 0, X16INT(6)); nuclear@13: x3d_rotate(cam_phi, 65536, 0, 0); nuclear@14: if(autorot) { nuclear@14: x3d_rotate((msec / 64) << 16, 0, 65536, 0); nuclear@14: } else { nuclear@14: x3d_rotate(cam_theta, 0, 65536, 0); nuclear@14: } nuclear@6: nuclear@9: #ifdef PALMODE nuclear@9: x3d_color_index(255); nuclear@9: #else nuclear@8: x3d_color(65536, 65536, 65536); nuclear@9: #endif nuclear@13: nuclear@17: x3d_enable_texture(tex); nuclear@13: draw_mesh(&box); nuclear@17: x3d_disable_texture(); nuclear@13: nuclear@18: draw_fps_meter(msec); nuclear@6: nuclear@5: flip(); nuclear@5: } nuclear@5: nuclear@18: static void draw_fps_meter(unsigned long msec) nuclear@18: { nuclear@18: static unsigned long last_msec; nuclear@18: static unsigned long nframes; nuclear@18: unsigned long dt = msec - last_msec; nuclear@18: int bar_height; nuclear@18: nuclear@18: ++nframes; nuclear@18: nuclear@18: if(dt >= 1000) { nuclear@18: last_msec = msec; nuclear@18: fps = 1000 * nframes / dt; nuclear@18: nframes = 0; nuclear@18: logmsg(LOG_DBG, "fps: %d\n", fps); nuclear@18: } nuclear@18: nuclear@18: bar_height = fps * 4; nuclear@18: if(bar_height > HEIGHT) bar_height = HEIGHT; nuclear@18: nuclear@18: draw_rect(0, HEIGHT - bar_height, 1, bar_height, RGB(0, 255, 0)); nuclear@18: } nuclear@18: nuclear@5: void game_keyb(int key, int pressed) nuclear@5: { nuclear@9: if(!pressed) return; nuclear@9: nuclear@9: switch(key) { nuclear@9: case KEY_A: nuclear@9: autorot = !autorot; nuclear@9: break; nuclear@9: nuclear@14: case KEY_SELECT: nuclear@14: dbg_fill_dump = 1; nuclear@14: break; nuclear@14: nuclear@9: default: nuclear@9: break; nuclear@9: } nuclear@5: } nuclear@9: nuclear@9: #ifdef PALMODE nuclear@9: #define ROWADV (WIDTH / 2) nuclear@9: #else nuclear@9: #define ROWADV WIDTH nuclear@9: #endif nuclear@9: nuclear@9: static void draw_rect(int x, int y, int w, int h, uint16_t color) nuclear@9: { nuclear@9: int i, xsz = w, ysz = h; nuclear@9: uint16_t *pixels = back_buffer->pixels; nuclear@9: uint16_t *topleft, *topright, *botleft; nuclear@9: nuclear@9: #ifdef PALMODE nuclear@9: pixels += (y * WIDTH + x) / 2; nuclear@9: topleft = pixels; nuclear@9: topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2; nuclear@9: nuclear@9: color |= color << 8; nuclear@9: xsz /= 2; nuclear@9: #else nuclear@9: pixels += y * WIDTH + x; nuclear@9: topleft = pixels; nuclear@9: topright = topleft + w - 1; nuclear@9: #endif nuclear@9: botleft = topleft + (ysz - 1) * ROWADV; nuclear@9: nuclear@9: #ifdef PALMODE nuclear@9: if(x & 1) { nuclear@9: *topleft = (*topleft & 0xff) | (color & 0xff00); nuclear@9: *botleft = (*topleft & 0xff) | (color & 0xff00); nuclear@9: ++topleft; nuclear@9: ++botleft; nuclear@9: xsz -= 1; nuclear@9: } nuclear@9: #endif nuclear@9: for(i=0; i