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