gba-x3dtest
view src/game.c @ 17:0a7f402892b3
texture mapping
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Jun 2014 06:57:51 +0300 |
parents | c398d834d64a |
children | f907b2c50a8b |
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"
11 extern int dbg_fill_dump;
13 static void draw_rect(int x, int y, int w, int h, uint16_t color);
15 #define X16INT(x) ((x) << 16)
16 #define X16FLT(x) ((int32_t)((x) * 65536.0))
18 static const int32_t poly[] = {
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 X16INT(-1), X16INT(-1), 0
27 };
28 static const int32_t colors[] = {
29 65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0,
30 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0
31 };
32 static const short vcount = sizeof poly / sizeof *poly / 3;
34 static struct mesh box;
35 static int tex;
38 int game_init(void)
39 {
40 sincos_init();
41 #ifdef PALMODE
42 palman_init();
43 #endif
45 tex = x3d_create_texture_rgb(test_width, test_height, test_pixels);
47 x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500);
49 init_mesh(&box);
50 gen_box(&box);
52 return 0;
53 }
55 static int32_t cam_theta, cam_phi = 25 << 16;
56 static short keyrot;
57 static int autorot = 1;
59 void game_draw(void)
60 {
61 unsigned long msec = get_millisec();
63 clear_buffer(back_buffer, 0);
65 x3d_load_identity();
66 /*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
67 if(autorot) {
68 x3d_rotate((msec / 64) << 16, 0, 0, 65536);
69 } else {
70 x3d_rotate(keyrot << 16, 0, 0, 65536);
71 }
72 x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
74 x3d_translate(0, 0, X16INT(6));
75 x3d_rotate(cam_phi, 65536, 0, 0);
76 if(autorot) {
77 x3d_rotate((msec / 64) << 16, 0, 65536, 0);
78 } else {
79 x3d_rotate(cam_theta, 0, 65536, 0);
80 }
82 #ifdef PALMODE
83 x3d_color_index(255);
84 #else
85 x3d_color(65536, 65536, 65536);
86 #endif
88 x3d_enable_texture(tex);
89 draw_mesh(&box);
90 x3d_disable_texture();
92 /*
93 x3d_vertex_array(vcount, poly);
94 x3d_color_array(vcount, colors);
95 x3d_draw(X3D_QUADS, vcount);
96 x3d_color_array(0, 0);
98 #ifdef PALMODE
99 x3d_color_index(RGBPAL(0, 255, 0));
100 #else
101 x3d_color(0, 65536, 0);
102 #endif
104 x3d_draw(X3D_POINTS, vcount);
105 x3d_vertex_array(0, 0);
106 */
108 flip();
109 }
111 void game_keyb(int key, int pressed)
112 {
113 if(!pressed) return;
115 switch(key) {
116 case KEY_LEFT:
117 cam_theta += 65536;
118 break;
120 case KEY_RIGHT:
121 cam_theta -= 65536;
122 break;
124 case KEY_UP:
125 cam_phi += 65536;
126 break;
128 case KEY_DOWN:
129 cam_phi -= 65536;
130 break;
132 case KEY_A:
133 autorot = !autorot;
134 break;
136 case KEY_SELECT:
137 dbg_fill_dump = 1;
138 break;
140 default:
141 break;
142 }
143 }
145 #ifdef PALMODE
146 #define ROWADV (WIDTH / 2)
147 #else
148 #define ROWADV WIDTH
149 #endif
151 static void draw_rect(int x, int y, int w, int h, uint16_t color)
152 {
153 int i, xsz = w, ysz = h;
154 uint16_t *pixels = back_buffer->pixels;
155 uint16_t *topleft, *topright, *botleft;
157 #ifdef PALMODE
158 pixels += (y * WIDTH + x) / 2;
159 topleft = pixels;
160 topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2;
162 color |= color << 8;
163 xsz /= 2;
164 #else
165 pixels += y * WIDTH + x;
166 topleft = pixels;
167 topright = topleft + w - 1;
168 #endif
169 botleft = topleft + (ysz - 1) * ROWADV;
171 #ifdef PALMODE
172 if(x & 1) {
173 *topleft = (*topleft & 0xff) | (color & 0xff00);
174 *botleft = (*topleft & 0xff) | (color & 0xff00);
175 ++topleft;
176 ++botleft;
177 xsz -= 1;
178 }
179 #endif
180 for(i=0; i<xsz; i++) {
181 *topleft++ = color;
182 *botleft++ = color;
183 }
185 topleft = pixels;
186 for(i=0; i<ysz; i++) {
187 #ifdef PALMODE
188 if(x & 1) {
189 *topleft = (*topleft & 0xff) | (color & 0xff00);
190 } else {
191 *topleft = (*topleft & 0xff00) | (color & 0xff);
192 }
194 if((x + w - 1) & 1) {
195 *topright = (*topright & 0xff) | (color & 0xff00);
196 } else {
197 *topright = (*topright & 0xff00) | (color & 0xff);
198 }
199 #else
200 *topleft = color;
201 *topright = color;
202 #endif
204 topleft += ROWADV;
205 topright += ROWADV;
206 }
207 }