gba-x3dtest

view src/main.c @ 3:8e9225853d75

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