fixed bugs, added progress bar, and more
[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 #include "gfxutil.h"
8 #include "timer.h"
9
10 #define DBG_SCRCHG \
11         do { \
12                 dbg_curscr_name = cur->name ? cur->name : "<unknown>"; \
13                 dbg_curscr_name_len = strlen(dbg_curscr_name); \
14                 dbg_curscr_name_pos = 320 - dbg_curscr_name_len * 9; \
15         } while(0)
16
17 struct screen *tunnel_screen(void);
18 struct screen *fract_screen(void);
19 struct screen *grise_screen(void);
20 struct screen *polytest_screen(void);
21 struct screen *plasma_screen(void);
22 struct screen *bump_screen(void);
23 struct screen *thunder_screen(void);
24 struct screen *metaballs_screen(void);
25 struct screen *greets_screen(void);
26 struct screen *infcubes_screen(void);
27 struct screen *hairball_screen(void);
28
29 void start_loadscr(void);
30 void end_loadscr(void);
31 void loadscr(int n, int count);
32
33 #define NUM_SCR 32
34 static struct screen *scr[NUM_SCR];
35 static int num_screens;
36
37 static struct screen *cur, *prev, *next;
38 static long trans_start, trans_dur;
39
40 int scr_init(void)
41 {
42         int i, idx = 0;
43
44         start_loadscr();
45
46         if(!(scr[idx++] = tunnel_screen())) {
47                 return -1;
48         }
49         if(!(scr[idx++] = fract_screen())) {
50                 return -1;
51         }
52         if (!(scr[idx++] = grise_screen())) {
53                 return -1;
54         }
55         if(!(scr[idx++] = polytest_screen())) {
56                 return -1;
57         }
58         if (!(scr[idx++] = plasma_screen())) {
59                 return -1;
60         }
61         if (!(scr[idx++] = bump_screen())) {
62                 return -1;
63         }
64         if (!(scr[idx++] = thunder_screen())) {
65                 return -1;
66         }
67         if(!(scr[idx++] = metaballs_screen())) {
68                 return -1;
69         }
70         if(!(scr[idx++] = greets_screen())) {
71                 return -1;
72         }
73         if(!(scr[idx++] = infcubes_screen())) {
74                 return -1;
75         }
76         if(!(scr[idx++] = hairball_screen())) {
77                 return -1;
78         }
79         num_screens = idx;
80
81         assert(num_screens <= NUM_SCR);
82
83         for(i=0; i<num_screens; i++) {
84                 loadscr(i, num_screens);
85                 if(scr[i]->init() == -1) {
86                         return -1;
87                 }
88         }
89
90         end_loadscr();
91         return 0;
92 }
93
94 void scr_shutdown(void)
95 {
96         int i;
97         for(i=0; i<num_screens; i++) {
98                 scr[i]->shutdown();
99         }
100 }
101
102 void scr_update(void)
103 {
104         if(prev) {  /* we're in the middle of a transition */
105                 long interval = time_msec - trans_start;
106                 if(interval >= trans_dur) {
107                         if(next->start) {
108                                 next->start(trans_dur);
109                         }
110                         prev = 0;
111                         cur = next;
112                         next = 0;
113
114                         DBG_SCRCHG;
115                 }
116         }
117 }
118
119
120 void scr_draw(void)
121 {
122         if(cur) {
123                 cur->draw();
124         }
125 }
126
127 void scr_keypress(int key)
128 {
129         if(cur && cur->keypress) {
130                 cur->keypress(key);
131         }
132 }
133
134 struct screen *scr_lookup(const char *name)
135 {
136         int i;
137         for(i=0; i<num_screens; i++) {
138                 if(strcmp(scr[i]->name, name) == 0) {
139                         return scr[i];
140                 }
141         }
142         return 0;
143 }
144
145 struct screen *scr_screen(int idx)
146 {
147         return scr[idx];
148 }
149
150 int scr_num_screens(void)
151 {
152         return num_screens;
153 }
154
155 int scr_change(struct screen *s, long trans_time)
156 {
157         if(!s) return -1;
158         if(s == cur) return 0;
159
160         if(trans_time) {
161                 trans_dur = trans_time / 2; /* half for each part transition out then in */
162                 trans_start = time_msec;
163         } else {
164                 trans_dur = 0;
165         }
166
167         if(cur && cur->stop) {
168                 cur->stop(trans_dur);
169                 prev = cur;
170                 next = s;
171         } else {
172                 if(s->start) {
173                         s->start(trans_dur);
174                 }
175
176                 cur = s;
177                 prev = 0;
178
179                 DBG_SCRCHG;
180         }
181         return 0;
182 }
183
184 /* loading screen */
185 extern uint16_t loading_pixels[];
186
187 void start_loadscr(void)
188 {
189         swap_buffers(loading_pixels);
190 }
191
192 #define SPLAT_X 288
193 #define SPLAT_Y 104
194
195 #define FING_X  217
196 #define FING_LAST_X     291
197 #define FING_Y  151
198 #define FING_W  7
199 #define FING_H  8
200
201 void end_loadscr(void)
202 {
203         blitfb(loading_pixels + SPLAT_Y * 320 + SPLAT_X, loading_pixels + 320 * 240, 32, 72, 32);
204         blitfb_key(loading_pixels + FING_Y * 320 + FING_LAST_X, loading_pixels + 247 * 320 + 64,
205                         FING_W, FING_H, FING_W, 0);
206         swap_buffers(loading_pixels);
207         sleep_msec(300);
208 }
209
210 void loadscr(int n, int count)
211 {
212         int xoffs = 75 * n / (count - 1);
213         static int prev_xoffs;
214         uint16_t *sptr, *dptr;
215
216         sptr = loading_pixels + 247 * 320 + 64;
217         dptr = loading_pixels + FING_Y * 320 + FING_X + prev_xoffs;
218
219         while(prev_xoffs < xoffs) {
220                 blitfb_key(dptr, sptr, FING_W, FING_H, FING_W, 0);
221                 dptr++;
222                 prev_xoffs++;
223         }
224
225         swap_buffers(loading_pixels);
226
227         /*sleep_msec(200);*/
228 }