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