a949fc467a127e14ee51764ca8c0931a72be79ca
[dosdemo] / src / screen.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "screen.h"
5 #include "demo.h"
6
7 struct screen *tunnel_screen(void);
8 struct screen *fract_screen(void);
9
10 #define NUM_SCR 32
11 static struct screen *scr[NUM_SCR];
12
13 static struct screen *cur, *prev, *next;
14 static long trans_start, trans_dur;
15
16 int scr_init(void)
17 {
18         int i, idx = 0;
19
20         if(!(scr[idx++] = tunnel_screen())) {
21                 return -1;
22         }
23         if(!(scr[idx++] = fract_screen())) {
24                 return -1;
25         }
26
27         for(i=0; i<NUM_SCR; i++) {
28                 if(scr[i] && scr[i]->init() == -1) {
29                         return -1;
30                 }
31         }
32         return 0;
33 }
34
35 void scr_shutdown(void)
36 {
37         int i;
38         for(i=0; i<NUM_SCR; i++) {
39                 if(!scr[i]) break;
40                 scr[i]->shutdown();
41         }
42 }
43
44 void scr_update(void)
45 {
46         if(prev) {      /* we're in the middle of a transition */
47                 long interval = time_msec - trans_start;
48                 if(interval >= trans_dur) {
49                         if(next->start) {
50                                 next->start(trans_dur);
51                         }
52                         prev = 0;
53                         cur = next;
54                         next = 0;
55                 }
56         }
57 }
58
59 void scr_draw(void)
60 {
61         if(cur) cur->draw();
62 }
63
64 struct screen *scr_lookup(const char *name)
65 {
66         int i;
67         for(i=0; i<NUM_SCR; i++) {
68                 if(!scr[i]) break;
69                 if(strcmp(scr[i]->name, name) == 0) {
70                         return scr[i];
71                 }
72         }
73         return 0;
74 }
75
76 int scr_change(struct screen *s, long trans_time)
77 {
78         if(!s) return -1;
79         if(s == cur) return 0;
80
81         if(trans_time) {
82                 trans_dur = trans_time / 2;     /* half for each part transition out then in */
83                 trans_start = time_msec;
84         } else {
85                 trans_dur = 0;
86         }
87
88         if(cur) {
89                 if(cur->stop) {
90                         cur->stop(trans_dur);
91                 }
92
93                 prev = cur;
94                 next = s;
95         } else {
96                 if(s->start) {
97                         s->start(trans_dur);
98                 }
99
100                 cur = s;
101                 prev = 0;
102         }
103         return 0;
104 }