event track loading, no relative events yet
[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
55         glEnable(GL_BLEND);
56         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
57
58         glBindTexture(GL_TEXTURE_2D, tex_logo);
59         glUseProgram(sdr_foo);
60         gl_begin(GL_QUADS);
61         gl_color4f(1, 1, 1, dsys_value("flashlogo"));
62         gl_texcoord2f(0, 1);
63         gl_vertex2f(-1, -1);
64         gl_texcoord2f(1, 1);
65         gl_vertex2f(1, -1);
66         gl_texcoord2f(1, 0);
67         gl_vertex2f(1, 1);
68         gl_texcoord2f(0, 0);
69         gl_vertex2f(-1, 1);
70         gl_end();
71         glDisable(GL_BLEND);
72
73         dsys_draw();
74 }
75
76 void demo_reshape(int x, int y)
77 {
78         int i;
79
80         glViewport(0, 0, x, y);
81
82         for(i=0; i<dsys.num_screens; i++) {
83                 if(dsys.screens[i]->reshape) {
84                         dsys.screens[i]->reshape(x, y);
85                 }
86         }
87 }
88
89 void demo_keyboard(int key, int pressed)
90 {
91         if(!pressed) return;
92
93         switch(key) {
94         case ' ':
95                 if(dsys.running) {
96                         dsys_stop();
97                 } else {
98                         dsys_run();
99                 }
100                 break;
101
102         case '\b':
103                 dsys_seek_abs(0);
104                 break;
105
106         default:
107                 if(key >= '0' && key <= '9') {
108                         dsys_seek_rel((float)(key - '0') / 9.0f);
109
110                 } else if(key >= KEY_F1 && key <= KEY_F12) {
111                         int idx = key - KEY_F1;
112                         if(idx < dsys.num_screens) {
113                                 dsys_run_screen(dsys.screens[idx]);
114                         }
115
116                 } else {
117                         int i;
118                         for(i=0; i<dsys.num_act; i++) {
119                                 struct demoscreen *scr = dsys.act[i];
120                                 if(scr->keyboard) scr->keyboard(key, pressed);
121                         }
122                 }
123         }
124 }
125
126 void demo_mouse(int bn, int pressed, int x, int y)
127 {
128         int i;
129         for(i=0; i<dsys.num_act; i++) {
130                 struct demoscreen *scr = dsys.act[i];
131                 if(scr->mouse) scr->mouse(bn, pressed, x, y);
132         }
133 }
134
135 void demo_motion(int x, int y)
136 {
137         int i;
138         for(i=0; i<dsys.num_act; i++) {
139                 struct demoscreen *scr = dsys.act[i];
140                 if(scr->motion) scr->motion(x, y);
141         }
142 }