gba-x3dtest

view src/game.c @ 9:b0ed38f13261

working on the rasterizer
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Jun 2014 05:16:10 +0300
parents fb0a0d6a8b52
children ecc022a21279
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"
9 static void draw_rect(int x, int y, int w, int h, uint16_t color);
11 #define X16INT(x) ((x) << 16)
13 static const int32_t poly[] = {
14 X16INT(120), X16INT(20), 0,
15 X16INT(200), X16INT(130), 0,
16 X16INT(40), X16INT(100), 0
17 };
18 static const short vcount = sizeof poly / sizeof *poly / 3;
20 int game_init(void)
21 {
22 sincos_init();
23 #ifdef PALMODE
24 palman_init();
25 #endif
27 return 0;
28 }
30 static short keyrot;
31 static int autorot = 1;
33 void game_draw(void)
34 {
35 unsigned long msec = get_millisec();
37 clear_buffer(back_buffer, 0);
39 x3d_load_identity();
40 x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
41 if(autorot) {
42 x3d_rotate((msec / 64) << 16, 0, 0, 65536);
43 } else {
44 x3d_rotate(keyrot << 16, 0, 0, 65536);
45 }
46 x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);
48 x3d_vertex_array(vcount, poly);
50 #ifdef PALMODE
51 x3d_color_index(255);
52 #else
53 x3d_color(65536, 65536, 65536);
54 #endif
55 x3d_draw_arrays(X3D_TRIANGLES, vcount);
57 #ifdef PALMODE
58 x3d_color_index(RGBPAL(0, 255, 0));
59 #else
60 x3d_color(0, 65536, 0);
61 #endif
62 x3d_draw_arrays(X3D_POINTS, vcount);
63 x3d_vertex_array(0, 0);
65 draw_rect(0, 0, WIDTH, HEIGHT, RGBPAL(255, 0, 0));
66 draw_rect(1, 1, WIDTH - 2, HEIGHT - 2, RGBPAL(0, 255, 0));
67 draw_rect(2, 2, WIDTH - 4, HEIGHT - 4, RGBPAL(32, 64, 255));
69 flip();
70 }
72 void game_keyb(int key, int pressed)
73 {
74 if(!pressed) return;
76 switch(key) {
77 case KEY_LEFT:
78 keyrot--;
79 break;
81 case KEY_RIGHT:
82 keyrot++;
83 break;
85 case KEY_A:
86 autorot = !autorot;
87 break;
89 default:
90 break;
91 }
92 }
94 #ifdef PALMODE
95 #define ROWADV (WIDTH / 2)
96 #else
97 #define ROWADV WIDTH
98 #endif
100 static void draw_rect(int x, int y, int w, int h, uint16_t color)
101 {
102 int i, xsz = w, ysz = h;
103 uint16_t *pixels = back_buffer->pixels;
104 uint16_t *topleft, *topright, *botleft;
106 #ifdef PALMODE
107 pixels += (y * WIDTH + x) / 2;
108 topleft = pixels;
109 topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2;
111 color |= color << 8;
112 xsz /= 2;
113 #else
114 pixels += y * WIDTH + x;
115 topleft = pixels;
116 topright = topleft + w - 1;
117 #endif
118 botleft = topleft + (ysz - 1) * ROWADV;
120 #ifdef PALMODE
121 if(x & 1) {
122 *topleft = (*topleft & 0xff) | (color & 0xff00);
123 *botleft = (*topleft & 0xff) | (color & 0xff00);
124 ++topleft;
125 ++botleft;
126 xsz -= 1;
127 }
128 #endif
129 for(i=0; i<xsz; i++) {
130 *topleft++ = color;
131 *botleft++ = color;
132 }
134 topleft = pixels;
135 for(i=0; i<ysz; i++) {
136 #ifdef PALMODE
137 if(x & 1) {
138 *topleft = (*topleft & 0xff) | (color & 0xff00);
139 } else {
140 *topleft = (*topleft & 0xff00) | (color & 0xff);
141 }
143 if((x + w - 1) & 1) {
144 *topright = (*topright & 0xff) | (color & 0xff00);
145 } else {
146 *topright = (*topright & 0xff00) | (color & 0xff);
147 }
148 #else
149 *topleft = color;
150 *topright = color;
151 #endif
153 topleft += ROWADV;
154 topright += ROWADV;
155 }
156 }