7d927ea63975f7496620673f4c41adcb1b1f8492
[dosrtxon] / src / demo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include "demo.h"
9 #include "screen.h"
10 #include "3dgfx.h"
11 #include "music.h"
12 #include "cfgopt.h"
13 #include "tinyfps.h"
14 #include "util.h"
15
16 #define FB_WIDTH        320
17 #define FB_HEIGHT       240
18
19 int fb_width = FB_WIDTH;
20 int fb_height = FB_HEIGHT;
21 int fb_bpp = 16;
22 uint16_t *fb_pixels, *vmem_back, *vmem_front;
23 unsigned long time_msec;
24 int mouse_x, mouse_y;
25 unsigned int mouse_bmask;
26
27 float sball_matrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
28
29 static unsigned long nframes;
30 static int console_active;
31
32 int demo_init(int argc, char **argv)
33 {
34         struct screen *scr;
35         char *env;
36
37         if(load_config("demo.cfg") == -1) {
38                 return -1;
39         }
40         if((env = getenv("START_SCR"))) {
41                 opt.start_scr = env;
42         }
43         if(parse_args(argc, argv) == -1) {
44                 return -1;
45         }
46
47         initFpsFonts();
48
49         if(g3d_init() == -1) {
50                 return -1;
51         }
52         g3d_framebuffer(fb_width, fb_height, fb_pixels);
53
54         if(opt.music) {
55                 if(music_open("data/test.mod") == -1) {
56                         return -1;
57                 }
58         }
59
60         if(scr_init() == -1) {
61                 return -1;
62         }
63         if(opt.start_scr) {
64                 scr = scr_lookup(opt.start_scr);
65         } else {
66                 scr = scr_screen(0);
67         }
68
69         if(!scr || scr_change(scr, 4000) == -1) {
70                 fprintf(stderr, "screen %s not found\n", opt.start_scr ? opt.start_scr : "0");
71                 return -1;
72         }
73
74         /* clear the framebuffer at least once */
75         memset(fb_pixels, 0, fb_width * fb_height * fb_bpp / CHAR_BIT);
76
77         if(opt.music) {
78                 music_play();
79         }
80         return 0;
81 }
82
83 void demo_cleanup(void)
84 {
85         if(opt.music) {
86                 music_close();
87         }
88         scr_shutdown();
89         g3d_destroy();
90
91         if(time_msec) {
92                 float fps = (float)nframes / ((float)time_msec / 1000.0f);
93                 printf("average framerate: %.1f\n", fps);
94         }
95 }
96
97 void demo_draw(void)
98 {
99         if(opt.music) {
100                 music_update();
101         }
102         scr_update();
103         scr_draw();
104
105         ++nframes;
106 }
107
108
109
110 #define DEST(x, y)      dest[(y) * FB_WIDTH + (x)]
111 void draw_mouse_pointer(uint16_t *fb)
112 {
113         uint16_t *dest = fb + mouse_y * FB_WIDTH + mouse_x;
114
115         DEST(0, 0) = 0xffff;
116         DEST(1, 1) = 0xffff;
117         DEST(2, 2) = 0xffff;
118         DEST(3, 3) = 0xffff;
119         DEST(4, 4) = 0xffff;
120         DEST(5, 5) = 0xffff;
121         DEST(6, 6) = 0xffff;
122         DEST(0, 1) = 0xffff;
123         DEST(0, 2) = 0xffff;
124         DEST(0, 3) = 0xffff;
125         DEST(0, 4) = 0xffff;
126         DEST(0, 5) = 0xffff;
127         DEST(0, 6) = 0xffff;
128         DEST(0, 7) = 0xffff;
129         DEST(0, 8) = 0xffff;
130         DEST(0, 9) = 0xffff;
131         DEST(1, 8) = 0xffff;
132         DEST(2, 7) = 0xffff;
133         DEST(3, 6) = 0xffff;
134         DEST(4, 6) = 0xffff;
135         DEST(5, 6) = 0xffff;
136         DEST(1, 2) = 0;
137         DEST(1, 3) = 0;
138         DEST(2, 3) = 0;
139         DEST(1, 4) = 0;
140         DEST(2, 4) = 0;
141         DEST(3, 4) = 0;
142         DEST(1, 5) = 0;
143         DEST(2, 5) = 0;
144         DEST(3, 5) = 0;
145         DEST(4, 5) = 0;
146         DEST(1, 6) = 0;
147         DEST(2, 6) = 0;
148         DEST(1, 7) = 0;
149 }
150
151 static void change_screen(int idx)
152 {
153         printf("change screen %d\n", idx);
154         scr_change(scr_screen(idx), 4000);
155 }
156
157 #define CBUF_SIZE       64
158 #define CBUF_MASK       (CBUF_SIZE - 1)
159 void demo_keyboard(int key, int press)
160 {
161         static char cbuf[CBUF_SIZE];
162         static int rd, wr;
163         char inp[CBUF_SIZE + 1], *dptr;
164         int i, nscr;
165
166         if(press) {
167                 switch(key) {
168                 case 27:
169                         demo_quit();
170                         return;
171
172                 case 127:
173                         debug_break();
174                         return;
175
176                 case '`':
177                         console_active = !console_active;
178                         if(console_active) {
179                                 printf("> ");
180                                 fflush(stdout);
181                         } else {
182                                 putchar('\n');
183                         }
184                         return;
185
186                 case '\b':
187                         if(console_active) {
188                                 if(wr != rd) {
189                                         printf("\b \b");
190                                         fflush(stdout);
191                                         wr = (wr + CBUF_SIZE - 1) & CBUF_MASK;
192                                 }
193                                 return;
194                         }
195                         break;
196
197                 case '\n':
198                 case '\r':
199                         if(console_active) {
200                                 dptr = inp;
201                                 while(rd != wr) {
202                                         *dptr++ = cbuf[rd];
203                                         rd = (rd + 1) & CBUF_MASK;
204                                 }
205                                 *dptr = 0;
206                                 if(inp[0]) {
207                                         printf("\ntrying to match: %s\n", inp);
208                                         nscr = scr_num_screens();
209                                         for(i=0; i<nscr; i++) {
210                                                 if(strstr(scr_screen(i)->name, inp)) {
211                                                         change_screen(i);
212                                                         break;
213                                                 }
214                                         }
215                                 }
216                                 console_active = 0;
217                                 return;
218                         }
219                         break;
220
221                 default:
222                         if(key >= '1' && key <= '9' && key <= '1' + scr_num_screens()) {
223                                 change_screen(key - '1');
224                         } else if(key == '0' && scr_num_screens() >= 10) {
225                                 change_screen(9);
226                         }
227
228                         if(console_active) {
229                                 if(key < 256 && isprint(key)) {
230                                         putchar(key);
231                                         fflush(stdout);
232
233                                         cbuf[wr] = key;
234                                         wr = (wr + 1) & CBUF_MASK;
235                                         if(wr == rd) { /* overflow */
236                                                 rd = (rd + 1) & CBUF_MASK;
237                                         }
238                                 }
239                                 return;
240                         }
241                         break;
242                 }
243
244                 scr_keypress(key);
245         }
246 }
247
248
249 void mouse_orbit_update(float *theta, float *phi, float *dist)
250 {
251         static int prev_mx, prev_my;
252         static unsigned int prev_bmask;
253
254         if(mouse_bmask) {
255                 if((mouse_bmask ^ prev_bmask) == 0) {
256                         int dx = mouse_x - prev_mx;
257                         int dy = mouse_y - prev_my;
258
259                         if(dx || dy) {
260                                 if(mouse_bmask & MOUSE_BN_LEFT) {
261                                         float p = *phi;
262                                         *theta += dx * 1.0;
263                                         p += dy * 1.0;
264
265                                         if(p < -90) p = -90;
266                                         if(p > 90) p = 90;
267                                         *phi = p;
268                                 }
269                                 if(mouse_bmask & MOUSE_BN_RIGHT) {
270                                         *dist += dy * 0.5;
271
272                                         if(*dist < 0) *dist = 0;
273                                 }
274                         }
275                 }
276         }
277
278         prev_mx = mouse_x;
279         prev_my = mouse_y;
280         prev_bmask = mouse_bmask;
281 }