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