gba-trycatch

annotate src/main.c @ 2:5143908d0220

fixed
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Jun 2014 01:56:42 +0300
parents b7130fe3f073
children 78d1664c2443
rev   line source
nuclear@2 1 #include <stdint.h>
nuclear@0 2 #include "gbasys.h"
nuclear@1 3 #include "logger.h"
nuclear@2 4 #include "sincos.h"
nuclear@2 5
nuclear@2 6 #define GFX_MODE VMODE_LFB_160x128_16
nuclear@2 7 #define WIDTH 160
nuclear@2 8 #define HEIGHT 128
nuclear@0 9
nuclear@0 10 static void draw(void);
nuclear@1 11 static void keyb(int key, int pressed);
nuclear@1 12 static void keyb_intr(void);
nuclear@0 13
nuclear@1 14 static unsigned short keystate;
nuclear@1 15
nuclear@1 16 #define EVQ_SIZE 8
nuclear@1 17 volatile static unsigned short evhead, evtail;
nuclear@1 18 static unsigned short events[EVQ_SIZE];
nuclear@0 19
nuclear@0 20 int main(void)
nuclear@0 21 {
nuclear@0 22 gba_init();
nuclear@2 23
nuclear@1 24 interrupt(INTR_KEY, keyb_intr);
nuclear@0 25
nuclear@0 26 set_video_mode(VMODE_LFB_160x128_16, 1);
nuclear@0 27
nuclear@0 28 clear_buffer(front_buffer, 0);
nuclear@1 29 set_text_writebg(1);
nuclear@1 30 logmsg(LOG_ALL, "please wait...\n");
nuclear@0 31
nuclear@2 32 sincos_init();
nuclear@2 33
nuclear@0 34 for(;;) {
nuclear@1 35 /* process events */
nuclear@1 36 while(evhead != evtail) {
nuclear@1 37 unsigned short ev = events[evhead];
nuclear@1 38 evhead = (evhead + 1) % EVQ_SIZE;
nuclear@1 39 keyb(ev, 1);
nuclear@1 40 }
nuclear@2 41
nuclear@2 42 /* this is needed because there's no key release interrupt (!) */
nuclear@2 43 keystate = get_key_state(KEY_ALL);
nuclear@2 44
nuclear@0 45 draw();
nuclear@0 46 }
nuclear@0 47 return 0;
nuclear@0 48 }
nuclear@0 49
nuclear@2 50 static int offset;
nuclear@2 51
nuclear@0 52 static void draw(void)
nuclear@0 53 {
nuclear@2 54 int i, j;
nuclear@2 55 unsigned long msec = get_millisec();
nuclear@2 56 uint16_t *pixels = back_buffer->pixels;
nuclear@2 57
nuclear@2 58 clear_buffer(back_buffer, 0);
nuclear@2 59
nuclear@2 60 for(i=0; i<WIDTH; i++) {
nuclear@2 61 int16_t x = 2 * (i - offset) * SINLUT_SIZE / WIDTH;
nuclear@2 62 int16_t y = sin_int(x);
nuclear@2 63 y = HEIGHT - (y * (HEIGHT / 2) / SINLUT_SCALE / 2 + HEIGHT / 2);
nuclear@2 64
nuclear@2 65 pixels[y * WIDTH + i] = RGB(255, 0, 0);
nuclear@2 66
nuclear@2 67 y = cos_int(x);
nuclear@2 68 y = HEIGHT - (y * (HEIGHT / 2) / SINLUT_SCALE / 2 + HEIGHT / 2);
nuclear@2 69
nuclear@2 70 pixels[y * WIDTH + i] = RGB(0, 255, 0);
nuclear@2 71
nuclear@2 72 if(i == offset) {
nuclear@2 73 for(j=0; j<HEIGHT; j++) {
nuclear@2 74 pixels[j * WIDTH + i] = RGB(255, 255, 0);
nuclear@2 75 }
nuclear@2 76 }
nuclear@2 77 }
nuclear@2 78
nuclear@2 79 flip();
nuclear@0 80 }
nuclear@0 81
nuclear@1 82 static void keyb(int key, int pressed)
nuclear@0 83 {
nuclear@2 84 switch(key) {
nuclear@2 85 case KEY_LEFT:
nuclear@2 86 logmsg(LOG_DBG, "foo! (%d)\n", offset);
nuclear@2 87 --offset;
nuclear@2 88 break;
nuclear@2 89
nuclear@2 90 case KEY_RIGHT:
nuclear@2 91 logmsg(LOG_DBG, "bar! (%d)\n", offset);
nuclear@2 92 ++offset;
nuclear@2 93 break;
nuclear@2 94
nuclear@2 95 default:
nuclear@2 96 break;
nuclear@2 97 }
nuclear@2 98 /*static const char *keyname[] = {
nuclear@1 99 "A", "B", "select", "start", "right", "left", "up", "down", "RT", "LT"
nuclear@1 100 };
nuclear@1 101 int i;
nuclear@1 102 for(i=0; i<32; i++) {
nuclear@1 103 if(key & 1) {
nuclear@1 104 break;
nuclear@1 105 }
nuclear@1 106 key >>= 1;
nuclear@1 107 }
nuclear@1 108 if(i < sizeof keyname / sizeof *keyname) {
nuclear@1 109 logmsg(LOG_ALL, "key %s %s\n", keyname[i], pressed ? "pressed" : "released");
nuclear@1 110 }
nuclear@2 111 */
nuclear@0 112 }
nuclear@1 113
nuclear@1 114 static void keyb_intr(void)
nuclear@1 115 {
nuclear@1 116 int i;
nuclear@1 117 unsigned short prev_keystate = keystate;
nuclear@1 118 unsigned short diff;
nuclear@1 119 keystate = get_key_state(KEY_ALL);
nuclear@1 120 diff = keystate ^ prev_keystate;
nuclear@1 121
nuclear@1 122 for(i=0; i<32; i++) {
nuclear@1 123 int key = 1 << i;
nuclear@1 124
nuclear@1 125 if(diff & key) {
nuclear@1 126 events[evtail] = key;
nuclear@1 127 evtail = (evtail + 1) % EVQ_SIZE;
nuclear@1 128 }
nuclear@1 129 }
nuclear@1 130 }