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