fullscreen
[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         if(opt.scrname) {
41                 struct demoscreen *scr = dsys_find_screen(opt.scrname);
42                 if(scr) {
43                         dsys_run_screen(scr);
44                 } else {
45                         fprintf(stderr, "ignoring screen option, no such screen: %s\n", opt.scrname);
46                 }
47         }
48
49         return 0;
50 }
51
52 void demo_cleanup(void)
53 {
54         dsys_destroy();
55 }
56
57 void demo_display(void)
58 {
59         dsys_update();
60
61         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62
63         glEnable(GL_BLEND);
64         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
65
66         glBindTexture(GL_TEXTURE_2D, tex_logo);
67         glUseProgram(sdr_foo);
68         gl_begin(GL_QUADS);
69         gl_color4f(1, 1, 1, dsys_value("flashlogo"));
70         gl_texcoord2f(0, 1);
71         gl_vertex2f(-1, -1);
72         gl_texcoord2f(1, 1);
73         gl_vertex2f(1, -1);
74         gl_texcoord2f(1, 0);
75         gl_vertex2f(1, 1);
76         gl_texcoord2f(0, 0);
77         gl_vertex2f(-1, 1);
78         gl_end();
79         glDisable(GL_BLEND);
80
81         dsys_draw();
82 }
83
84 void demo_reshape(int x, int y)
85 {
86         int i;
87
88         glViewport(0, 0, x, y);
89
90         for(i=0; i<dsys.num_screens; i++) {
91                 if(dsys.screens[i]->reshape) {
92                         dsys.screens[i]->reshape(x, y);
93                 }
94         }
95 }
96
97 void demo_keyboard(int key, int pressed)
98 {
99         if(!pressed) return;
100
101         switch(key) {
102         case ' ':
103                 if(dsys.running) {
104                         dsys_stop();
105                 } else {
106                         dsys_run();
107                 }
108                 break;
109
110         case '\b':
111                 dsys_seek_abs(0);
112                 break;
113
114         default:
115                 if(key >= '0' && key <= '9') {
116                         dsys_seek_rel((float)(key - '0') / 9.0f);
117
118                 } else if(key >= KEY_F1 && key <= KEY_F12) {
119                         int idx = key - KEY_F1;
120                         if(idx < dsys.num_screens) {
121                                 dsys_run_screen(dsys.screens[idx]);
122                         }
123
124                 } else {
125                         int i;
126                         for(i=0; i<dsys.num_act; i++) {
127                                 struct demoscreen *scr = dsys.act[i];
128                                 if(scr->keyboard) scr->keyboard(key, pressed);
129                         }
130                 }
131         }
132 }
133
134 void demo_mouse(int bn, int pressed, int x, int y)
135 {
136         int i;
137         for(i=0; i<dsys.num_act; i++) {
138                 struct demoscreen *scr = dsys.act[i];
139                 if(scr->mouse) scr->mouse(bn, pressed, x, y);
140         }
141 }
142
143 void demo_motion(int x, int y)
144 {
145         int i;
146         for(i=0; i<dsys.num_act; i++) {
147                 struct demoscreen *scr = dsys.act[i];
148                 if(scr->motion) scr->motion(x, y);
149         }
150 }