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