gba-x3dtest

view src/game.c @ 20:2e903e27e35a

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