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