win32
[censuslogo] / src / app.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <GL/gl.h>
6 #include "app.h"
7 #include "logo.h"
8
9 #ifndef GL_MULTISAMPLE
10 #define GL_MULTISAMPLE  0x809d
11 #endif
12
13 int nverts = 256;
14 int msaa = 1;
15 int fullscr = 0;
16
17
18 static void draw_disc(float x, float y, float rad, int sub);
19 static void draw_line(float x0, float y0, float x1, float y1, float rad);
20 static void print_usage(const char *argv0);
21
22 int app_init(void)
23 {
24         if(init_logo("data/census.curves") == -1) {
25                 return -1;
26         }
27
28 #ifdef MSAA
29         if(msaa) {
30                 glEnable(GL_MULTISAMPLE);
31         }
32 #endif
33
34         return 0;
35 }
36
37 #define LOOPTIME        1.45f
38
39 void app_display(void)
40 {
41         int i;
42         float t = (float)msec / 1000.0f;
43         float a[2], b[2], dt;
44         float anim, alpha;
45
46         glClear(GL_COLOR_BUFFER_BIT);
47
48         glLineWidth(5.0);
49
50         anim = fmod(t / 6.0f, LOOPTIME);
51         alpha = 1.0f - ((anim - (LOOPTIME - 0.075)) / 0.06f);
52         if(alpha < 0.0f) alpha = 0.0f;
53         if(alpha > 1.0f) alpha = 1.0f;
54
55         dt = (anim > 1.0f ? 1.0f : anim) / (float)(nverts - 1);
56
57         glColor4f(1, 1, 1, alpha);
58         for(i=0; i<nverts-1; i++) {
59                 float t0 = (float)i * dt;
60                 float t1 = (float)(i + 1) * dt;
61                 eval_logo(a, t0);
62                 eval_logo(b, t1);
63                 draw_line(a[0], a[1], b[0], b[1], 0.02);
64         }
65
66         if(anim > 0.0f) {
67                 eval_logo(a, 0);
68                 draw_disc(a[0], a[1], 0.05, 22);
69         }
70         if(anim >= 1.0f) {
71                 eval_logo(b, 1);
72                 draw_disc(b[0], b[1], 0.05, 22);
73         }
74
75         glEnable(GL_BLEND);
76         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
77
78         glColor4f(0.8, 0, 0, 2.0 * (anim - 1.0f) / (LOOPTIME - 1.0f));
79         draw_disc(0, 0, 0.14, 30);
80
81         if(alpha < 1.0f) {
82                 glBegin(GL_QUADS);
83                 glColor4f(0, 0, 0, 1.0f - alpha);
84                 glVertex2f(-1, -1);
85                 glVertex2f(1, -1);
86                 glVertex2f(1, 1);
87                 glVertex2f(-1, 1);
88                 glEnd();
89         }
90
91         glDisable(GL_BLEND);
92 }
93
94 static void draw_disc(float x, float y, float rad, int sub)
95 {
96         int i;
97         glBegin(GL_TRIANGLE_FAN);
98         glVertex2f(x, y);
99         for(i=0; i<sub; i++) {
100                 float t = (float)i / (float)(sub - 1);
101                 float theta = t * M_PI * 2.0;
102                 glVertex2f(cos(theta) * rad + x, sin(theta) * rad + y);
103         }
104         glEnd();
105 }
106
107 static void draw_line(float x0, float y0, float x1, float y1, float rad)
108 {
109         float dx, dy, rx, ry, len;
110
111         dx = x1 - x0;
112         dy = y1 - y0;
113         len = sqrt(dx * dx + dy * dy);
114
115         rx = rad * dy / len;
116         ry = -rad * dx / len;
117
118         draw_disc(x0, y0, rad, 12);
119         draw_disc(x1, y1, rad, 12);
120
121         glBegin(GL_QUADS);
122         glVertex2f(x0 + rx, y0 + ry);
123         glVertex2f(x1 + rx, y1 + ry);
124         glVertex2f(x1 - rx, y1 - ry);
125         glVertex2f(x0 - rx, y0 - ry);
126         glEnd();
127 }
128
129
130 void app_reshape(int x, int y)
131 {
132         float aspect = (float)x / (float)y;
133         glViewport(0, 0, x, y);
134         glMatrixMode(GL_PROJECTION);
135         glLoadIdentity();
136         glScalef(1.0f / aspect, 1.0f, 1.0f);
137 }
138
139 void app_keyboard(int key, int pressed)
140 {
141         switch(key) {
142         case 27:
143                 app_quit();
144
145         case '-':
146                 nverts -= 8;
147                 printf("nverts: %d\n", nverts);
148                 break;
149
150         case '=':
151                 nverts += 8;
152                 printf("nverts: %d\n", nverts);
153                 break;
154
155         case 'f':
156                 fullscr = !fullscr;
157                 if(fullscr) {
158                         app_fullscreen();
159                 } else {
160                         app_windowed();
161                 }
162                 break;
163         }
164 }
165
166
167 int app_parse_args(int argc, char **argv)
168 {
169         int i;
170
171         for(i=1; i<argc; i++) {
172                 if(argv[i][0] == '-') {
173                         if(strcmp(argv[i], "-fs") == 0) {
174                                 fullscr = 1;
175                         } else if(strcmp(argv[i], "-noaa") == 0) {
176                                 msaa = 0;
177                         } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
178                                 print_usage(argv[0]);
179                                 exit(0);
180                         } else {
181                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
182                                 return -1;
183                         }
184                 } else {
185                         fprintf(stderr, "unexpected argument: %s\n", argv[i]);
186                         return -1;
187                 }
188         }
189         return 0;
190 }
191
192 static void print_usage(const char *argv0)
193 {
194         printf("Usage: %s [options]\n", argv0);
195         printf("Options:\n");
196         printf(" -fs: fullscreen\n");
197         printf(" -noaa: disable anti-aliasing\n");
198         printf(" -h,-help: print usage and exit\n");
199 }