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