gba-x3dtest

view src/game.c @ 14:c398d834d64a

fixed the rendering bugs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 10:33:24 +0300
parents 2070a81127f2
children 0a7f402892b3
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"
10 extern int dbg_fill_dump;
12 static void draw_rect(int x, int y, int w, int h, uint16_t color);
14 #define X16INT(x) ((x) << 16)
15 #define X16FLT(x) ((int32_t)((x) * 65536.0))
17 static const int32_t poly[] = {
18 X16INT(-1), X16INT(-1), 0,
19 X16INT(-1), X16INT(1), 0,
20 X16INT(1), X16INT(1), 0,
21 X16INT(1), X16INT(-1), 0,
22 X16INT(1), X16INT(-1), 0,
23 X16INT(1), X16INT(1), 0,
24 X16INT(-1), X16INT(1), 0,
25 X16INT(-1), X16INT(-1), 0
26 };
27 static const int32_t colors[] = {
28 65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0,
29 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0
30 };
31 static const short vcount = sizeof poly / sizeof *poly / 3;
33 static struct mesh box;
36 int game_init(void)
37 {
38 sincos_init();
39 #ifdef PALMODE
40 palman_init();
41 #endif
43 x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500);
45 init_mesh(&box);
46 gen_box(&box);
48 return 0;
49 }
51 static int32_t cam_theta, cam_phi = 25 << 16;
52 static short keyrot;
53 static int autorot = 1;
55 void game_draw(void)
56 {
57 unsigned long msec = get_millisec();
59 clear_buffer(back_buffer, 0);
61 x3d_load_identity();
62 /*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
63 if(autorot) {
64 x3d_rotate((msec / 64) << 16, 0, 0, 65536);
65 } else {
66 x3d_rotate(keyrot << 16, 0, 0, 65536);
67 }
68 x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
70 x3d_translate(0, 0, X16INT(6));
71 x3d_rotate(cam_phi, 65536, 0, 0);
72 if(autorot) {
73 x3d_rotate((msec / 64) << 16, 0, 65536, 0);
74 } else {
75 x3d_rotate(cam_theta, 0, 65536, 0);
76 }
78 #ifdef PALMODE
79 x3d_color_index(255);
80 #else
81 x3d_color(65536, 65536, 65536);
82 #endif
84 draw_mesh(&box);
86 /*
87 x3d_vertex_array(vcount, poly);
88 x3d_color_array(vcount, colors);
89 x3d_draw(X3D_QUADS, vcount);
90 x3d_color_array(0, 0);
92 #ifdef PALMODE
93 x3d_color_index(RGBPAL(0, 255, 0));
94 #else
95 x3d_color(0, 65536, 0);
96 #endif
98 x3d_draw(X3D_POINTS, vcount);
99 x3d_vertex_array(0, 0);
100 */
102 flip();
103 }
105 void game_keyb(int key, int pressed)
106 {
107 if(!pressed) return;
109 switch(key) {
110 case KEY_LEFT:
111 cam_theta += 65536;
112 break;
114 case KEY_RIGHT:
115 cam_theta -= 65536;
116 break;
118 case KEY_UP:
119 cam_phi += 65536;
120 break;
122 case KEY_DOWN:
123 cam_phi -= 65536;
124 break;
126 case KEY_A:
127 autorot = !autorot;
128 break;
130 case KEY_SELECT:
131 dbg_fill_dump = 1;
132 break;
134 default:
135 break;
136 }
137 }
139 #ifdef PALMODE
140 #define ROWADV (WIDTH / 2)
141 #else
142 #define ROWADV WIDTH
143 #endif
145 static void draw_rect(int x, int y, int w, int h, uint16_t color)
146 {
147 int i, xsz = w, ysz = h;
148 uint16_t *pixels = back_buffer->pixels;
149 uint16_t *topleft, *topright, *botleft;
151 #ifdef PALMODE
152 pixels += (y * WIDTH + x) / 2;
153 topleft = pixels;
154 topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2;
156 color |= color << 8;
157 xsz /= 2;
158 #else
159 pixels += y * WIDTH + x;
160 topleft = pixels;
161 topright = topleft + w - 1;
162 #endif
163 botleft = topleft + (ysz - 1) * ROWADV;
165 #ifdef PALMODE
166 if(x & 1) {
167 *topleft = (*topleft & 0xff) | (color & 0xff00);
168 *botleft = (*topleft & 0xff) | (color & 0xff00);
169 ++topleft;
170 ++botleft;
171 xsz -= 1;
172 }
173 #endif
174 for(i=0; i<xsz; i++) {
175 *topleft++ = color;
176 *botleft++ = color;
177 }
179 topleft = pixels;
180 for(i=0; i<ysz; i++) {
181 #ifdef PALMODE
182 if(x & 1) {
183 *topleft = (*topleft & 0xff) | (color & 0xff00);
184 } else {
185 *topleft = (*topleft & 0xff00) | (color & 0xff);
186 }
188 if((x + w - 1) & 1) {
189 *topright = (*topright & 0xff) | (color & 0xff00);
190 } else {
191 *topright = (*topright & 0xff00) | (color & 0xff);
192 }
193 #else
194 *topleft = color;
195 *topright = color;
196 #endif
198 topleft += ROWADV;
199 topright += ROWADV;
200 }
201 }