fixed 6dof camera
[o2demo] / 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 *example_screen(void);
9 /*
10 struct screen *another_screen(void);
11 struct screen *athird_screen(void);
12 */
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++] = example_screen())) {
26                 return -1;
27         }
28         /*
29         if(!(scr[idx++] = another_screen())) {
30                 return -1;
31         }
32         if(!(scr[idx++] = athird_screen())) {
33                 return -1;
34         }
35         */
36         num_screens = idx;
37
38         assert(num_screens <= NUM_SCR);
39
40         for(i=0; i<num_screens; i++) {
41                 if(scr[i]->init() == -1) {
42                         return -1;
43                 }
44         }
45         return 0;
46 }
47
48 void scr_shutdown(void)
49 {
50         int i;
51         for(i=0; i<num_screens; i++) {
52                 scr[i]->shutdown();
53         }
54 }
55
56 void scr_update(void)
57 {
58         if(prev) {  /* we're in the middle of a transition */
59                 long interval = time_msec - trans_start;
60                 if(interval >= trans_dur) {
61                         if(next->start) {
62                                 next->start(trans_dur);
63                         }
64                         prev = 0;
65                         cur = next;
66                         next = 0;
67                 }
68         }
69 }
70
71 void scr_draw(void)
72 {
73         if(cur) cur->draw();
74 }
75
76 void scr_keypress(int key)
77 {
78         if(cur && cur->keypress) {
79                 cur->keypress(key);
80         }
81 }
82
83 struct screen *scr_lookup(const char *name)
84 {
85         int i;
86         for(i=0; i<num_screens; i++) {
87                 if(strcmp(scr[i]->name, name) == 0) {
88                         return scr[i];
89                 }
90         }
91         return 0;
92 }
93
94 struct screen *scr_screen(int idx)
95 {
96         return scr[idx];
97 }
98
99 int scr_num_screens(void)
100 {
101         return num_screens;
102 }
103
104 int scr_change(struct screen *s, long trans_time)
105 {
106         if(!s) return -1;
107         if(s == cur) return 0;
108
109         if(trans_time) {
110                 trans_dur = trans_time / 2; /* half for each part transition out then in */
111                 trans_start = time_msec;
112         } else {
113                 trans_dur = 0;
114         }
115
116         if(cur && cur->stop) {
117                 cur->stop(trans_dur);
118                 prev = cur;
119                 next = s;
120         } else {
121                 if(s->start) {
122                         s->start(trans_dur);
123                 }
124
125                 cur = s;
126                 prev = 0;
127         }
128         return 0;
129 }