gba-x3dtest

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