demosystem tracks
[andemo] / src / demo.c
1 #include <stdio.h>
2 #include "demo.h"
3 #include "opengl.h"
4 #include "sanegl.h"
5 #include "assman.h"
6 #include "demosys.h"
7
8 static unsigned int sdr_foo;
9 static unsigned int tex_logo;
10
11 int demo_init(void)
12 {
13         if(init_opengl() == -1) {
14                 return -1;
15         }
16
17         if(!(sdr_foo = get_sdrprog("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
18                 return -1;
19         }
20         if(!(tex_logo = get_tex2d("data/ml_logo_old.png"))) {
21                 return -1;
22         }
23         glBindTexture(GL_TEXTURE_2D, tex_logo);
24         glUseProgram(sdr_foo);
25         gl_begin(GL_QUADS);
26         gl_texcoord2f(0, 1);
27         gl_vertex2f(-1, -1);
28         gl_texcoord2f(1, 1);
29         gl_vertex2f(1, -1);
30         gl_texcoord2f(1, 0);
31         gl_vertex2f(1, 1);
32         gl_texcoord2f(0, 0);
33         gl_vertex2f(-1, 1);
34         gl_end();
35         swap_buffers();
36
37         if(dsys_init("data/demoscript") == -1) {
38                 return -1;
39         }
40
41         return 0;
42 }
43
44 void demo_cleanup(void)
45 {
46         dsys_destroy();
47 }
48
49 void demo_display(void)
50 {
51         dsys_update();
52
53         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
54         dsys_draw();
55 }
56
57 void demo_reshape(int x, int y)
58 {
59         int i;
60
61         glViewport(0, 0, x, y);
62
63         for(i=0; i<dsys.num_screens; i++) {
64                 if(dsys.screens[i]->reshape) {
65                         dsys.screens[i]->reshape(x, y);
66                 }
67         }
68 }
69
70 void demo_keyboard(int key, int pressed)
71 {
72         if(!pressed) return;
73
74         switch(key) {
75         case ' ':
76                 if(dsys.running) {
77                         dsys_stop();
78                 } else {
79                         dsys_run();
80                 }
81                 break;
82
83         case '\b':
84                 dsys_seek_abs(0);
85                 break;
86
87         default:
88                 if(key >= '0' && key <= '9') {
89                         dsys_seek_rel((float)(key - '0') / 9.0f);
90
91                 } else if(key >= KEY_F1 && key <= KEY_F12) {
92                         int idx = key - KEY_F1;
93                         if(idx < dsys.num_screens) {
94                                 dsys_run_screen(dsys.screens[idx]);
95                         }
96
97                 } else {
98                         int i;
99                         for(i=0; i<dsys.num_act; i++) {
100                                 struct demoscreen *scr = dsys.act[i];
101                                 if(scr->keyboard) scr->keyboard(key, pressed);
102                         }
103                 }
104         }
105 }
106
107 void demo_mouse(int bn, int pressed, int x, int y)
108 {
109         int i;
110         for(i=0; i<dsys.num_act; i++) {
111                 struct demoscreen *scr = dsys.act[i];
112                 if(scr->mouse) scr->mouse(bn, pressed, x, y);
113         }
114 }
115
116 void demo_motion(int x, int y)
117 {
118         int i;
119         for(i=0; i<dsys.num_act; i++) {
120                 struct demoscreen *scr = dsys.act[i];
121                 if(scr->motion) scr->motion(x, y);
122         }
123 }