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