gba-x3dtest

view src/game.c @ 13:2070a81127f2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 08:28:28 +0300
parents ecc022a21279
children c398d834d64a
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 static void draw_rect(int x, int y, int w, int h, uint16_t color);
12 #define X16INT(x) ((x) << 16)
13 #define X16FLT(x) ((int32_t)((x) * 65536.0))
15 static const int32_t poly[] = {
16 X16INT(-1), X16INT(-1), 0,
17 X16INT(-1), X16INT(1), 0,
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 };
25 static const int32_t colors[] = {
26 65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0,
27 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0
28 };
29 static const short vcount = sizeof poly / sizeof *poly / 3;
31 static struct mesh box;
34 int game_init(void)
35 {
36 sincos_init();
37 #ifdef PALMODE
38 palman_init();
39 #endif
41 x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500);
43 init_mesh(&box);
44 gen_box(&box);
46 return 0;
47 }
49 static int32_t cam_theta, cam_phi = 25 << 16;
50 static short keyrot;
51 static int autorot = 1;
53 void game_draw(void)
54 {
55 unsigned long msec = get_millisec();
57 clear_buffer(back_buffer, 0);
59 x3d_load_identity();
60 /*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
61 if(autorot) {
62 x3d_rotate((msec / 64) << 16, 0, 0, 65536);
63 } else {
64 x3d_rotate(keyrot << 16, 0, 0, 65536);
65 }
66 x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
68 x3d_rotate(cam_phi, 65536, 0, 0);
69 x3d_rotate(keyrot << 16, 0, 65536, 0);
70 x3d_translate(0, 0, X16INT(6));
72 #ifdef PALMODE
73 x3d_color_index(255);
74 #else
75 x3d_color(65536, 65536, 65536);
76 #endif
78 draw_mesh(&box);
80 /*
81 x3d_vertex_array(vcount, poly);
82 x3d_color_array(vcount, colors);
83 x3d_draw(X3D_QUADS, vcount);
84 x3d_color_array(0, 0);
86 #ifdef PALMODE
87 x3d_color_index(RGBPAL(0, 255, 0));
88 #else
89 x3d_color(0, 65536, 0);
90 #endif
92 x3d_draw(X3D_POINTS, vcount);
93 x3d_vertex_array(0, 0);
94 */
96 flip();
97 }
99 void game_keyb(int key, int pressed)
100 {
101 if(!pressed) return;
103 switch(key) {
104 case KEY_LEFT:
105 keyrot--;
106 break;
108 case KEY_RIGHT:
109 keyrot++;
110 break;
112 case KEY_A:
113 autorot = !autorot;
114 break;
116 default:
117 break;
118 }
119 }
121 #ifdef PALMODE
122 #define ROWADV (WIDTH / 2)
123 #else
124 #define ROWADV WIDTH
125 #endif
127 static void draw_rect(int x, int y, int w, int h, uint16_t color)
128 {
129 int i, xsz = w, ysz = h;
130 uint16_t *pixels = back_buffer->pixels;
131 uint16_t *topleft, *topright, *botleft;
133 #ifdef PALMODE
134 pixels += (y * WIDTH + x) / 2;
135 topleft = pixels;
136 topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2;
138 color |= color << 8;
139 xsz /= 2;
140 #else
141 pixels += y * WIDTH + x;
142 topleft = pixels;
143 topright = topleft + w - 1;
144 #endif
145 botleft = topleft + (ysz - 1) * ROWADV;
147 #ifdef PALMODE
148 if(x & 1) {
149 *topleft = (*topleft & 0xff) | (color & 0xff00);
150 *botleft = (*topleft & 0xff) | (color & 0xff00);
151 ++topleft;
152 ++botleft;
153 xsz -= 1;
154 }
155 #endif
156 for(i=0; i<xsz; i++) {
157 *topleft++ = color;
158 *botleft++ = color;
159 }
161 topleft = pixels;
162 for(i=0; i<ysz; i++) {
163 #ifdef PALMODE
164 if(x & 1) {
165 *topleft = (*topleft & 0xff) | (color & 0xff00);
166 } else {
167 *topleft = (*topleft & 0xff00) | (color & 0xff);
168 }
170 if((x + w - 1) & 1) {
171 *topright = (*topright & 0xff) | (color & 0xff00);
172 } else {
173 *topright = (*topright & 0xff00) | (color & 0xff);
174 }
175 #else
176 *topleft = color;
177 *topright = color;
178 #endif
180 topleft += ROWADV;
181 topright += ROWADV;
182 }
183 }