gba-trycatch

view src/game.c @ 12:ecc022a21279

more tuff
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 06:44:04 +0300
parents b0ed38f13261
children 2070a81127f2
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)
12 #define X16FLT(x) ((int32_t)((x) * 65536.0))
14 static const int32_t poly[] = {
15 X16FLT(0), X16FLT(1), 0,
16 X16FLT(1), X16FLT(-0.8), 0,
17 X16FLT(-1), X16FLT(-0.5), 0
18 };
19 static const short vcount = sizeof poly / sizeof *poly / 3;
21 int game_init(void)
22 {
23 sincos_init();
24 #ifdef PALMODE
25 palman_init();
26 #endif
28 x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500);
30 return 0;
31 }
33 static short keyrot;
34 static int autorot = 1;
36 void game_draw(void)
37 {
38 unsigned long msec = get_millisec();
40 clear_buffer(back_buffer, 0);
42 x3d_load_identity();
43 /*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0);
44 if(autorot) {
45 x3d_rotate((msec / 64) << 16, 0, 0, 65536);
46 } else {
47 x3d_rotate(keyrot << 16, 0, 0, 65536);
48 }
49 x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
51 if(autorot) {
52 x3d_rotate((msec / 64) << 16, 0, 65536, 0);
53 } else {
54 x3d_rotate(keyrot << 16, 0, 65536, 0);
55 }
56 x3d_translate(0, 0, X16INT(2));
58 x3d_vertex_array(vcount, poly);
60 #ifdef PALMODE
61 x3d_color_index(255);
62 #else
63 x3d_color(65536, 65536, 65536);
64 #endif
65 x3d_draw(X3D_TRIANGLES, vcount);
67 #ifdef PALMODE
68 x3d_color_index(RGBPAL(0, 255, 0));
69 #else
70 x3d_color(0, 65536, 0);
71 #endif
72 x3d_draw(X3D_POINTS, vcount);
73 x3d_vertex_array(0, 0);
75 draw_rect(0, 0, WIDTH, HEIGHT, RGBPAL(255, 0, 0));
76 draw_rect(1, 1, WIDTH - 2, HEIGHT - 2, RGBPAL(0, 255, 0));
77 draw_rect(2, 2, WIDTH - 4, HEIGHT - 4, RGBPAL(32, 64, 255));
79 flip();
80 }
82 void game_keyb(int key, int pressed)
83 {
84 if(!pressed) return;
86 switch(key) {
87 case KEY_LEFT:
88 keyrot--;
89 break;
91 case KEY_RIGHT:
92 keyrot++;
93 break;
95 case KEY_A:
96 autorot = !autorot;
97 break;
99 default:
100 break;
101 }
102 }
104 #ifdef PALMODE
105 #define ROWADV (WIDTH / 2)
106 #else
107 #define ROWADV WIDTH
108 #endif
110 static void draw_rect(int x, int y, int w, int h, uint16_t color)
111 {
112 int i, xsz = w, ysz = h;
113 uint16_t *pixels = back_buffer->pixels;
114 uint16_t *topleft, *topright, *botleft;
116 #ifdef PALMODE
117 pixels += (y * WIDTH + x) / 2;
118 topleft = pixels;
119 topright = (uint16_t*)back_buffer->pixels + (y * WIDTH + x + w - 1) / 2;
121 color |= color << 8;
122 xsz /= 2;
123 #else
124 pixels += y * WIDTH + x;
125 topleft = pixels;
126 topright = topleft + w - 1;
127 #endif
128 botleft = topleft + (ysz - 1) * ROWADV;
130 #ifdef PALMODE
131 if(x & 1) {
132 *topleft = (*topleft & 0xff) | (color & 0xff00);
133 *botleft = (*topleft & 0xff) | (color & 0xff00);
134 ++topleft;
135 ++botleft;
136 xsz -= 1;
137 }
138 #endif
139 for(i=0; i<xsz; i++) {
140 *topleft++ = color;
141 *botleft++ = color;
142 }
144 topleft = pixels;
145 for(i=0; i<ysz; i++) {
146 #ifdef PALMODE
147 if(x & 1) {
148 *topleft = (*topleft & 0xff) | (color & 0xff00);
149 } else {
150 *topleft = (*topleft & 0xff00) | (color & 0xff);
151 }
153 if((x + w - 1) & 1) {
154 *topright = (*topright & 0xff) | (color & 0xff00);
155 } else {
156 *topright = (*topright & 0xff00) | (color & 0xff);
157 }
158 #else
159 *topleft = color;
160 *topright = color;
161 #endif
163 topleft += ROWADV;
164 topright += ROWADV;
165 }
166 }