gba-trycatch

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