gba-x3dtest

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