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