gba-trycatch

annotate src/main.c @ 4:78d1664c2443

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