50232756f48fc1af6c96559936bee7e276e3effd
[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         glUseProgram(sdr_foo);
24         gl_begin(GL_QUADS);
25         gl_texcoord2f(0, 1);
26         gl_vertex2f(-1, -1);
27         gl_texcoord2f(1, 1);
28         gl_vertex2f(1, -1);
29         gl_texcoord2f(1, 0);
30         gl_vertex2f(1, 1);
31         gl_texcoord2f(0, 0);
32         gl_vertex2f(-1, 1);
33         gl_end();
34         swap_buffers();
35
36         if(dsys_init("data/demoscript") == -1) {
37                 return -1;
38         }
39
40         return 0;
41 }
42
43 void demo_cleanup(void)
44 {
45         dsys_destroy();
46 }
47
48 void demo_display(void)
49 {
50         struct demoscreen *scr;
51
52         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
53
54         scr = dsys_act_scr;
55         while(scr) {
56                 if(scr->update) {
57                         scr->update(dsys_time);
58                 }
59                 scr->draw();
60                 scr = scr->next;
61         }
62 }
63
64 void demo_reshape(int x, int y)
65 {
66         int i;
67
68         glViewport(0, 0, x, y);
69
70         for(i=0; i<dsys_num_screens; i++) {
71                 if(dsys_screens[i]->reshape) {
72                         dsys_screens[i]->reshape(x, y);
73                 }
74         }
75 }
76
77 void demo_keyboard(int key, int pressed)
78 {
79         if(!pressed) return;
80
81         switch(key) {
82         case ' ':
83                 if(dsys_running) {
84                         dsys_stop();
85                 } else {
86                         dsys_run();
87                 }
88                 break;
89
90         case '\b':
91                 dsys_seek_abs(0);
92                 break;
93
94         default:
95                 if(key >= '0' && key <= '9') {
96                         dsys_seek_rel((float)(key - '0') / 9.0f);
97
98                 } else if(key >= KEY_F1 && key <= KEY_F12) {
99                         int idx = key - KEY_F1;
100                         if(idx < dsys_num_screens) {
101                                 dsys_run_screen(dsys_screens[idx]);
102                         }
103
104                 } else {
105                         struct demoscreen *scr = dsys_act_scr;
106                         while(scr) {
107                                 if(scr->keyboard) {
108                                         scr->keyboard(key, pressed);
109                                 }
110                                 scr = scr->next;
111                         }
112                 }
113         }
114 }
115
116 void demo_mouse(int bn, int pressed, int x, int y)
117 {
118         struct demoscreen *scr = dsys_act_scr;
119         while(scr) {
120                 if(scr->mouse) {
121                         scr->mouse(bn, pressed, x, y);
122                 }
123                 scr = scr->next;
124         }
125 }
126
127 void demo_motion(int x, int y)
128 {
129         struct demoscreen *scr = dsys_act_scr;
130         while(scr) {
131                 if(scr->motion) {
132                         scr->motion(x, y);
133                 }
134                 scr = scr->next;
135         }
136 }