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