Thunder... thunder... THUNDER SCREEN! HOOOOOOOOOOOOO
[dosdemo] / src / screen.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "screen.h"
6 #include "demo.h"
7
8 struct screen *tunnel_screen(void);
9 struct screen *fract_screen(void);
10 struct screen *grise_screen(void);
11 struct screen *polytest_screen(void);
12 struct screen *plasma_screen(void);
13 struct screen *bump_screen(void);
14 struct screen *thunder_screen(void);
15
16 #define NUM_SCR 32
17 static struct screen *scr[NUM_SCR];
18 static int num_screens;
19
20 static struct screen *cur, *prev, *next;
21 static long trans_start, trans_dur;
22
23 int scr_init(void)
24 {
25         int i, idx = 0;
26
27         if(!(scr[idx++] = tunnel_screen())) {
28                 return -1;
29         }
30         if(!(scr[idx++] = fract_screen())) {
31                 return -1;
32         }
33         if (!(scr[idx++] = grise_screen())) {
34                 return -1;
35         }
36         if(!(scr[idx++] = polytest_screen())) {
37                 return -1;
38         }
39         if (!(scr[idx++] = plasma_screen())) {
40                 return -1;
41         }
42         if (!(scr[idx++] = bump_screen())) {
43                 return -1;
44         }
45         if (!(scr[idx++] = thunder_screen())) {
46                 return -1;
47         }
48         num_screens = idx;
49
50         assert(num_screens <= NUM_SCR);
51
52         for(i=0; i<num_screens; i++) {
53                 if(scr[i]->init() == -1) {
54                         return -1;
55                 }
56         }
57         return 0;
58 }
59
60 void scr_shutdown(void)
61 {
62         int i;
63         for(i=0; i<num_screens; i++) {
64                 scr[i]->shutdown();
65         }
66 }
67
68 void scr_update(void)
69 {
70         if(prev) {  /* we're in the middle of a transition */
71                 long interval = time_msec - trans_start;
72                 if(interval >= trans_dur) {
73                         if(next->start) {
74                                 next->start(trans_dur);
75                         }
76                         prev = 0;
77                         cur = next;
78                         next = 0;
79                 }
80         }
81 }
82
83 void scr_draw(void)
84 {
85         if(cur) cur->draw();
86 }
87
88 struct screen *scr_lookup(const char *name)
89 {
90         int i;
91         for(i=0; i<num_screens; i++) {
92                 if(strcmp(scr[i]->name, name) == 0) {
93                         return scr[i];
94                 }
95         }
96         return 0;
97 }
98
99 struct screen *scr_screen(int idx)
100 {
101         return scr[idx];
102 }
103
104 int scr_num_screens(void)
105 {
106         return num_screens;
107 }
108
109 int scr_change(struct screen *s, long trans_time)
110 {
111         if(!s) return -1;
112         if(s == cur) return 0;
113
114         if(trans_time) {
115                 trans_dur = trans_time / 2; /* half for each part transition out then in */
116                 trans_start = time_msec;
117         } else {
118                 trans_dur = 0;
119         }
120
121         if(cur && cur->stop) {
122                 cur->stop(trans_dur);
123                 prev = cur;
124                 next = s;
125         } else {
126                 if(s->start) {
127                         s->start(trans_dur);
128                 }
129
130                 cur = s;
131                 prev = 0;
132         }
133         return 0;
134 }