fa704138ef4682ecd5ec68acc0110068e09e14c2
[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 struct screen *metaballs_screen(void);
16 struct screen *greets_screen(void);
17 struct screen *infcubes_screen(void);
18
19 #define NUM_SCR 32
20 static struct screen *scr[NUM_SCR];
21 static int num_screens;
22
23 static struct screen *cur, *prev, *next;
24 static long trans_start, trans_dur;
25
26 int scr_init(void)
27 {
28         int i, idx = 0;
29
30         if(!(scr[idx++] = tunnel_screen())) {
31                 return -1;
32         }
33         if(!(scr[idx++] = fract_screen())) {
34                 return -1;
35         }
36         if (!(scr[idx++] = grise_screen())) {
37                 return -1;
38         }
39         if(!(scr[idx++] = polytest_screen())) {
40                 return -1;
41         }
42         if (!(scr[idx++] = plasma_screen())) {
43                 return -1;
44         }
45         if (!(scr[idx++] = bump_screen())) {
46                 return -1;
47         }
48         if (!(scr[idx++] = thunder_screen())) {
49                 return -1;
50         }
51         if(!(scr[idx++] = metaballs_screen())) {
52                 return -1;
53         }
54         if(!(scr[idx++] = greets_screen())) {
55                 return -1;
56         }
57         if(!(scr[idx++] = infcubes_screen())) {
58                 return -1;
59         }
60         num_screens = idx;
61
62         assert(num_screens <= NUM_SCR);
63
64         for(i=0; i<num_screens; i++) {
65                 if(scr[i]->init() == -1) {
66                         return -1;
67                 }
68         }
69         return 0;
70 }
71
72 void scr_shutdown(void)
73 {
74         int i;
75         for(i=0; i<num_screens; i++) {
76                 scr[i]->shutdown();
77         }
78 }
79
80 void scr_update(void)
81 {
82         if(prev) {  /* we're in the middle of a transition */
83                 long interval = time_msec - trans_start;
84                 if(interval >= trans_dur) {
85                         if(next->start) {
86                                 next->start(trans_dur);
87                         }
88                         prev = 0;
89                         cur = next;
90                         next = 0;
91                 }
92         }
93 }
94
95 void scr_draw(void)
96 {
97         if(cur) cur->draw();
98 }
99
100 void scr_keypress(int key)
101 {
102         if(cur && cur->keypress) {
103                 cur->keypress(key);
104         }
105 }
106
107 struct screen *scr_lookup(const char *name)
108 {
109         int i;
110         for(i=0; i<num_screens; i++) {
111                 if(strcmp(scr[i]->name, name) == 0) {
112                         return scr[i];
113                 }
114         }
115         return 0;
116 }
117
118 struct screen *scr_screen(int idx)
119 {
120         return scr[idx];
121 }
122
123 int scr_num_screens(void)
124 {
125         return num_screens;
126 }
127
128 int scr_change(struct screen *s, long trans_time)
129 {
130         if(!s) return -1;
131         if(s == cur) return 0;
132
133         if(trans_time) {
134                 trans_dur = trans_time / 2; /* half for each part transition out then in */
135                 trans_start = time_msec;
136         } else {
137                 trans_dur = 0;
138         }
139
140         if(cur && cur->stop) {
141                 cur->stop(trans_dur);
142                 prev = cur;
143                 next = s;
144         } else {
145                 if(s->start) {
146                         s->start(trans_dur);
147                 }
148
149                 cur = s;
150                 prev = 0;
151         }
152         return 0;
153 }