mouse cursor drawing part2
[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         int ylines = FB_HEIGHT - mouse_y;
115
116         switch(ylines) {
117         default:
118         case 10:
119                 DEST(0, 9) = 0xffff;
120         case 9:
121                 DEST(0, 8) = 0xffff;
122                 DEST(1, 8) = 0xffff;
123         case 8:
124                 DEST(0, 7) = 0xffff;
125                 DEST(2, 7) = 0xffff;
126                 DEST(1, 7) = 0;
127         case 7:
128                 DEST(6, 6) = 0xffff;
129                 DEST(0, 6) = 0xffff;
130                 DEST(3, 6) = 0xffff;
131                 DEST(4, 6) = 0xffff;
132                 DEST(5, 6) = 0xffff;
133                 DEST(1, 6) = 0;
134                 DEST(2, 6) = 0;
135         case 6:
136                 DEST(5, 5) = 0xffff;
137                 DEST(0, 5) = 0xffff;
138                 DEST(1, 5) = 0;
139                 DEST(2, 5) = 0;
140                 DEST(3, 5) = 0;
141                 DEST(4, 5) = 0;
142         case 5:
143                 DEST(4, 4) = 0xffff;
144                 DEST(0, 4) = 0xffff;
145                 DEST(1, 4) = 0;
146                 DEST(2, 4) = 0;
147                 DEST(3, 4) = 0;
148         case 4:
149                 DEST(3, 3) = 0xffff;
150                 DEST(0, 3) = 0xffff;
151                 DEST(1, 3) = 0;
152                 DEST(2, 3) = 0;
153         case 3:
154                 DEST(2, 2) = 0xffff;
155                 DEST(0, 2) = 0xffff;
156                 DEST(1, 2) = 0;
157         case 2:
158                 DEST(1, 1) = 0xffff;
159                 DEST(0, 1) = 0xffff;
160         case 1:
161                 DEST(0, 0) = 0xffff;
162         }
163 }
164
165 static void change_screen(int idx)
166 {
167         printf("change screen %d\n", idx);
168         scr_change(scr_screen(idx), 4000);
169 }
170
171 #define CBUF_SIZE       64
172 #define CBUF_MASK       (CBUF_SIZE - 1)
173 void demo_keyboard(int key, int press)
174 {
175         static char cbuf[CBUF_SIZE];
176         static int rd, wr;
177         char inp[CBUF_SIZE + 1], *dptr;
178         int i, nscr;
179
180         if(press) {
181                 switch(key) {
182                 case 27:
183                         demo_quit();
184                         return;
185
186                 case 127:
187                         debug_break();
188                         return;
189
190                 case '`':
191                         console_active = !console_active;
192                         if(console_active) {
193                                 printf("> ");
194                                 fflush(stdout);
195                         } else {
196                                 putchar('\n');
197                         }
198                         return;
199
200                 case '\b':
201                         if(console_active) {
202                                 if(wr != rd) {
203                                         printf("\b \b");
204                                         fflush(stdout);
205                                         wr = (wr + CBUF_SIZE - 1) & CBUF_MASK;
206                                 }
207                                 return;
208                         }
209                         break;
210
211                 case '\n':
212                 case '\r':
213                         if(console_active) {
214                                 dptr = inp;
215                                 while(rd != wr) {
216                                         *dptr++ = cbuf[rd];
217                                         rd = (rd + 1) & CBUF_MASK;
218                                 }
219                                 *dptr = 0;
220                                 if(inp[0]) {
221                                         printf("\ntrying to match: %s\n", inp);
222                                         nscr = scr_num_screens();
223                                         for(i=0; i<nscr; i++) {
224                                                 if(strstr(scr_screen(i)->name, inp)) {
225                                                         change_screen(i);
226                                                         break;
227                                                 }
228                                         }
229                                 }
230                                 console_active = 0;
231                                 return;
232                         }
233                         break;
234
235                 default:
236                         if(key >= '1' && key <= '9' && key <= '1' + scr_num_screens()) {
237                                 change_screen(key - '1');
238                         } else if(key == '0' && scr_num_screens() >= 10) {
239                                 change_screen(9);
240                         }
241
242                         if(console_active) {
243                                 if(key < 256 && isprint(key)) {
244                                         putchar(key);
245                                         fflush(stdout);
246
247                                         cbuf[wr] = key;
248                                         wr = (wr + 1) & CBUF_MASK;
249                                         if(wr == rd) { /* overflow */
250                                                 rd = (rd + 1) & CBUF_MASK;
251                                         }
252                                 }
253                                 return;
254                         }
255                         break;
256                 }
257
258                 scr_keypress(key);
259         }
260 }
261
262
263 void mouse_orbit_update(float *theta, float *phi, float *dist)
264 {
265         static int prev_mx, prev_my;
266         static unsigned int prev_bmask;
267
268         if(mouse_bmask) {
269                 if((mouse_bmask ^ prev_bmask) == 0) {
270                         int dx = mouse_x - prev_mx;
271                         int dy = mouse_y - prev_my;
272
273                         if(dx || dy) {
274                                 if(mouse_bmask & MOUSE_BN_LEFT) {
275                                         float p = *phi;
276                                         *theta += dx * 1.0;
277                                         p += dy * 1.0;
278
279                                         if(p < -90) p = -90;
280                                         if(p > 90) p = 90;
281                                         *phi = p;
282                                 }
283                                 if(mouse_bmask & MOUSE_BN_RIGHT) {
284                                         *dist += dy * 0.5;
285
286                                         if(*dist < 0) *dist = 0;
287                                 }
288                         }
289                 }
290         }
291
292         prev_mx = mouse_x;
293         prev_my = mouse_y;
294         prev_bmask = mouse_bmask;
295 }