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
|
#include <allegro.h>
#include <string>
using std::string;
volatile int timerCounter = 0;
static void timerCounterUpdater() {
timerCounter++;
} END_OF_STATIC_FUNCTION(timerCounterUpdater);
int mode;
bool set_graphic_mode(int width, int height) {
mode = mode > 1 ? 0 : mode;
static int colorDepths[] = { 16, 24, 15, 32 };
static int modes[] = {
GFX_AUTODETECT_FULLSCREEN,
GFX_AUTODETECT_FULLSCREEN,
GFX_AUTODETECT_WINDOWED
};
for (int i = 0; i < 4; i++) {
set_color_depth(colorDepths[i]);
if (set_gfx_mode(modes[i], width, height, 0, 0) >= 0) {
return true;
}
}
return false;
}
void fatal_error(string msg) {
allegro_message(msg.c_str());
exit(0);
}
void init() {
allegro_init();
install_keyboard();
if (!set_graphic_mode(800, 800)) {
fatal_error("Grafikmodus konnte nicht gesetzt werden");
}
LOCK_FUNCTION(timerCounterUpdater);
LOCK_VARIABLE(timerCounter);
install_int_ex(timerCounterUpdater, BPS_TO_TIMER(60));
}
int main() {
init();
BITMAP * logo = load_bitmap("asteroid.bmp", NULL);
BITMAP * doublebuffer = create_bitmap(800, 800);
int x = 0;
int y = 0;
int dx = 1;
int dy = 1;
int maxSkip = 4;
int curSkip = 0;
bool needRefresh = false;
while (!keypressed()) {
curSkip = 0;
if (timerCounter > 0) {
do {
if ((x + dx) < 0) {
dx *= -1;
} else if ((x + dx + logo->w) >= 800) {
dx *= -1;
}
if ((y + dy) < 0) {
dy *= -1;
} else if ((y + dy + logo->h) >= 800) {
dy *= -1;
}
x += dx;
y += dy;
timerCounter--;
curSkip++;
if (curSkip >= maxSkip) {
timerCounter = 0;
break;
}
} while(timerCounter > 0);
needRefresh = true;
}
if (needRefresh) {
acquire_bitmap(doublebuffer);
clear(doublebuffer);
blit(
logo, doublebuffer,
x, y,
(800 - logo->w) / 2,
(800 - logo->h) / 2,
logo->w,
logo->h
);
release_bitmap(doublebuffer);
blit(
doublebuffer, screen,
0, 0, 0, 0,
800, 800
);
needRefresh = false;
}
}
} END_OF_MAIN();
|