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