no-thread option, coalesced tunnel lut, more accurate fps counter
[fbgfx] / src / screen.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "screen.h"
5 #include "demo.h"
6
7 struct screen *console_screen(void);
8 struct screen *tunnel_screen(void);
9
10 #define NUM_SCR 2
11 static struct screen *scr[NUM_SCR];
12
13 static struct screen *cur, *prev, *next;
14 static long trans_start, trans_dur;
15
16 int scr_init(void)
17 {
18         int i;
19
20         if(!(scr[0] = console_screen())) {
21                 return -1;
22         }
23         if(!(scr[1] = tunnel_screen())) {
24                 return -1;
25         }
26
27         for(i=0; i<NUM_SCR; i++) {
28                 if(scr[i]->init() == -1) {
29                         return -1;
30                 }
31         }
32         return 0;
33 }
34
35 void scr_shutdown(void)
36 {
37         int i;
38         for(i=0; i<NUM_SCR; i++) {
39                 scr[i]->shutdown();
40         }
41 }
42
43 void scr_update(void)
44 {
45         if(prev) {      /* we're in the middle of a transition */
46                 long interval = time_msec - trans_start;
47                 if(interval >= trans_dur) {
48                         next->start(trans_dur);
49                         prev = 0;
50                         cur = next;
51                         next = 0;
52                 }
53         }
54 }
55
56 void scr_draw(void)
57 {
58         if(cur) cur->draw();
59 }
60
61 struct screen *scr_lookup(const char *name)
62 {
63         int i;
64         for(i=0; i<NUM_SCR; i++) {
65                 if(strcmp(scr[i]->name, name) == 0) {
66                         return scr[i];
67                 }
68         }
69         return 0;
70 }
71
72 int scr_change(struct screen *s, long trans_time)
73 {
74         if(!s) return -1;
75         if(s == cur) return 0;
76
77         if(trans_time && cur) {
78                 trans_dur = trans_time / 2;     /* half for each part transition out then in */
79                 trans_start = time_msec;
80
81                 if(cur) cur->stop(trans_dur);
82
83                 prev = cur;
84                 next = s;
85         } else {
86                 if(cur) cur->stop(0);
87                 s->start(0);
88
89                 cur = s;
90                 prev = 0;
91         }
92         return 0;
93 }