-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_allegro.c
165 lines (135 loc) · 2.74 KB
/
sys_allegro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Allegro system driver
// This will be the only driver we will use
// It's too tricky to build an API
#include "common.h"
BITMAP *buf = NULL;
int sys_mbcount = 0;
int curcol = 0;
int tick_current = 0;
int tick_logic = 0;
char key_fired[KEY_MAX];
// this is just a timer thread
// it is not part of the API
void sys_allegro_timer_proc()
{
tick_current++;
}
/* err = int sys_init():
* Initialise the main system library.
*/
int sys_init(void)
{
allegro_init();
printf("initialising system...");
// do this early as it appears in the taskbar
set_window_title("french ingenuity - a #botb production - dear germany, fix your software");
// this should do
if(install_timer())
{
printf("FAIL: timer init\n");
return 1;
}
if(install_keyboard())
{
printf("FAIL: keyboard init\n");
return 1;
}
int tmp = install_mouse();
if(tmp == -1)
{
printf("FAIL: mouse init\n");
return 1;
}
if(tmp > 0)
sys_mbcount = tmp;
// gfx mode, yay
// use 32-bit where available
set_color_depth(32);
if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_WIDTH,GFX_HEIGHT, 0,0))
{
printf("FAIL: gfx mode\n");
return 1;
}
buf = create_bitmap(GFX_WIDTH, GFX_HEIGHT);
if(buf == NULL)
{
printf("FAIL: back buffer\n");
return 1;
}
printf("OK!\n");
/*printf("- system: Allegro\n");
printf("- mouse buttons: %i\n", sys_mbcount);*/
return 0;
}
/* sys_exit():
* Shut down the main system library.
*/
void sys_exit(void)
{
// allegro will deal with itself
}
/* sys_msleep(int msec):
* Sleep for msec milliseconds.
*/
void sys_msleep(int msec)
{
rest(msec);
}
/* sys_main():
* Run logic / video threads (keep it all going smoothly).
*/
void sys_main(void)
{
sys_msleep(200); // FreeBSD likes it when threads sleep a bit
install_int_ex(sys_allegro_timer_proc, BPS_TO_TIMER(50));
while(fptr_logic != NULL)
{
if(keyboard_needs_poll())
poll_keyboard();
if(mouse_needs_poll())
poll_mouse();
if(tick_current > tick_logic)
{
// So we don't overshoot.
if(tick_current - 10 > tick_logic)
tick_logic = tick_current - 10;
fptr_logic();
tick_logic++;
} else {
if(fptr_video != NULL)
fptr_video();
sys_msleep(5); // Like I said wrt FreeBSD
}
}
}
/* sys_declock():
* Resets the timer used for fptr_logic.
*/
void sys_declock(void)
{
tick_logic = tick_current;
}
/* sys_gfx_flip():
* Flip / blit / whatever for the buffers.
*/
void sys_gfx_flip(void)
{
blit(buf, screen, 0,0, 0,0, GFX_WIDTH,GFX_HEIGHT);
}
/* sys_gfx_clear():
* Clear the drawing buffer.
*/
void sys_gfx_clear(void)
{
clear_bitmap(buf);
}
/* waspressed = int sys_key_once(int key)
* Check if a key was pressed.
*/
int sys_key_once(int k)
{
int p = key_fired[k];
int q = key[k];
key_fired[k] = q;
return (!p) && q;
}