gba-trycatch

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