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