Added mike.c for testing
[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 *mike_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++] = mike_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                 if(scr[i]->init() == -1) {
38                         return -1;
39                 }
40         }
41         return 0;
42 }
43
44 void scr_shutdown(void)
45 {
46         int i;
47         for(i=0; i<num_screens; i++) {
48                 scr[i]->shutdown();
49         }
50 }
51
52 void scr_update(void)
53 {
54         if(prev) {      /* we're in the middle of a transition */
55                 long interval = time_msec - trans_start;
56                 if(interval >= trans_dur) {
57                         if(next->start) {
58                                 next->start(trans_dur);
59                         }
60                         prev = 0;
61                         cur = next;
62                         next = 0;
63                 }
64         }
65 }
66
67 void scr_draw(void)
68 {
69         if(cur) cur->draw();
70 }
71
72 struct screen *scr_lookup(const char *name)
73 {
74         int i;
75         for(i=0; i<num_screens; i++) {
76                 if(strcmp(scr[i]->name, name) == 0) {
77                         return scr[i];
78                 }
79         }
80         return 0;
81 }
82
83 struct screen *scr_screen(int idx)
84 {
85         return scr[idx];
86 }
87
88 int scr_num_screens(void)
89 {
90         return num_screens;
91 }
92
93 int scr_change(struct screen *s, long trans_time)
94 {
95         if(!s) return -1;
96         if(s == cur) return 0;
97
98         if(trans_time) {
99                 trans_dur = trans_time / 2;     /* half for each part transition out then in */
100                 trans_start = time_msec;
101         } else {
102                 trans_dur = 0;
103         }
104
105         if(cur) {
106                 if(cur->stop) {
107                         cur->stop(trans_dur);
108                 }
109
110                 prev = cur;
111                 next = s;
112         } else {
113                 if(s->start) {
114                         s->start(trans_dur);
115                 }
116
117                 cur = s;
118                 prev = 0;
119         }
120         return 0;
121 }