gba-trycatch

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