music playback works on GNU/Linux
[dosdemo] / src / demo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <errno.h>
6 #include <limits.h>
7 #include "demo.h"
8 #include "screen.h"
9 #include "3dgfx.h"
10 #include "music.h"
11
12 int fb_width = 320;
13 int fb_height = 240;
14 int fb_bpp = 16;
15 uint16_t *fb_pixels, *vmem_back, *vmem_front;
16 unsigned long time_msec;
17 int mouse_x, mouse_y;
18 unsigned int mouse_bmask;
19
20 static unsigned long nframes;
21 static const char *start_scr_name;
22
23 int demo_init(int argc, char **argv)
24 {
25         struct screen *scr;
26
27         start_scr_name = getenv("START_SCR");
28         if(argv[1]) {
29                 start_scr_name = argv[1];
30         }
31
32         if(g3d_init() == -1) {
33                 return -1;
34         }
35         g3d_framebuffer(fb_width, fb_height, fb_pixels);
36
37         if(music_open("data/test.mod") == -1) {
38                 fprintf(stderr, "failed to open music: data/test.mod\n");
39                 return -1;
40         }
41
42         if(scr_init() == -1) {
43                 return -1;
44         }
45         if(start_scr_name) {
46                 scr = scr_lookup(start_scr_name);
47         } else {
48                 scr = scr_screen(0);
49         }
50
51         if(!scr || scr_change(scr, 4000) == -1) {
52                 fprintf(stderr, "screen %s not found\n", start_scr_name ? start_scr_name : "0");
53                 return -1;
54         }
55
56         /* clear the framebuffer at least once */
57         memset(fb_pixels, 0, fb_width * fb_height * fb_bpp / CHAR_BIT);
58
59         music_play();
60         return 0;
61 }
62
63 void demo_cleanup(void)
64 {
65         music_close();
66         scr_shutdown();
67         g3d_destroy();
68
69         if(time_msec) {
70                 float fps = (float)nframes / ((float)time_msec / 1000.0f);
71                 printf("average framerate: %.1f\n", fps);
72         }
73 }
74
75 void demo_draw(void)
76 {
77         music_update();
78         scr_update();
79         scr_draw();
80
81         ++nframes;
82 }
83
84 void demo_keyboard(int key, int state)
85 {
86         if(state) {
87                 switch(key) {
88                 case 27:
89                         demo_quit();
90                         break;
91
92                 default:
93                         if(key >= '1' && key <= '1' + scr_num_screens()) {
94                                 int idx = key - '1';
95                                 printf("change screen %d\n", idx);
96                                 scr_change(scr_screen(idx), 4000);
97                         }
98                         break;
99                 }
100         }
101 }