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