Repository navigation
Expand file tree
/
Copy pathNew Text Document.txt
More file actions
242 lines (187 loc) · 5.34 KB
/
Copy pathNew Text Document.txt
File metadata and controls
242 lines (187 loc) · 5.34 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#define BOARD_HEIGHT 500
#define BOARD_WIDTH 800
#define ALLEGRO_WHITE al_map_rgb(0,0,0)
#define GAME_TERMINATE 1
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font = NULL;
ALLEGRO_BITMAP *back_img = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_EVENT event;
void show_err_msg(int msg);
void game_init();
void game_begin();
void game_destroy();
int game_run();
int process_event();
int main(int argc, char *argv[]) {
int msg = 0;
game_init();
game_begin();
printf("Hello world!!!\n");
al_rest(2);
printf("Close window to terminate.\n");
while (msg != GAME_TERMINATE) {
msg = game_run();
if (msg == GAME_TERMINATE)
printf("See you, world\n");
}
al_rest(2);
game_destroy();
return 0;
}
void show_err_msg(int msg) {
fprintf(stderr, "unexpected msg: %d", msg);
game_destroy();
exit(9);
}
void game_init() {
al_init();
al_init_font_addon();
al_init_ttf_addon();
al_init_image_addon();
al_init_primitives_addon();
al_install_keyboard();
display = al_create_display(BOARD_WIDTH,BOARD_HEIGHT);
font = al_load_ttf_font("pirulen.ttf",72,0);
back_img = al_load_bitmap("htchen.jpg");
event_queue = al_create_event_queue();
al_clear_to_color(al_map_rgb(36,72,76));
al_draw_bitmap(back_img,0,0,0);
al_draw_rectangle(BOARD_WIDTH-600,BOARD_HEIGHT-100,BOARD_WIDTH-10,BOARD_HEIGHT-10,ALLEGRO_WHITE,10);
al_draw_text(font,al_map_rgb(0,255,0), BOARD_WIDTH-590, BOARD_HEIGHT-90, 0, "123");
al_flip_display();
}
void game_begin() {
}
int process_event() {
/*
* TODO: Get the earliest event from event_queue and judge whether it represents the closing of window.
* If so, return GAME_TERMINATE.
* Hint: using event.type == ALLEGRO_EVENT_DISPLAY_CLOSE to judge whether the display is closed just now.
*/
return event.type == MOUSEEVENTF_WHEEL;
}
int game_run() {
/*
* TODO: Judge whether there's any event in the queue; if so, call process_event() to process it.
*/
int msg = 0;
al_register_event_source(event_queue, al_get_display_event_source(display));
if(al_is_event_queue_empty(event_queue)){
al_wait_for_event(event_queue,&event);
msg = process_event();
}
else{
msg = process_event();
}
return msg;
}
void game_destroy() {
al_destroy_display(display);
al_destroy_bitmap(back_img);
al_destroy_font(font);
}
-----------------------------
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h> //Our primitive header file
#define GAME_TERMINATE 1
ALLEGRO_DISPLAY* display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_EVENT event;
ALLEGRO_EVENT_SOURCE *source = NULL;
const int width = 800;
const int height = 600;
int pos_x, pos_y; // The position of rectangle's left-up corner.
void show_err_msg(int msg);
void game_init();
void game_begin();
int process_event();
int game_run();
void game_destroy();
/*
You can declare some variables here if it is useful for you.
*/
int main(int argc, char *argv[]) {
int msg = 0;
game_init();
game_begin();
printf("Hello world!!!\n");
while (msg != GAME_TERMINATE) {
msg = game_run();
if (msg == GAME_TERMINATE)
printf("See you, world\n");
}
game_destroy();
return 0;
}
void show_err_msg(int msg) {
fprintf(stderr, "unexpected msg: %d", msg);
game_destroy();
exit(9);
}
void game_init() {
if (!al_init()) {
show_err_msg(-1);
}
al_init_primitives_addon();
al_install_keyboard();
al_get_keyboard_event_source();
event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_keyboard_event_source());
pos_x = width / 2;
pos_y = height / 2;
display = al_create_display(width, height);
if (display == NULL || event_queue == NULL) {
show_err_msg(-1);
}
/*
TODO: Something need to be created, initialized and registered.
*/
}
void game_begin() {
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_filled_rectangle(pos_x, pos_y, pos_x + 30, pos_y + 30, al_map_rgb(255,0,255));
al_flip_display();
}
int process_event() {
if(event.type == ALLEGRO_EVENT_KEY_DOWN){
if(event.keyboard.keycode == ALLEGRO_KEY_DOWN){
pos_y -= 30;
}
if(event.keyboard.keycode == ALLEGRO_KEY_UP){
pos_y += 30;
}
if(event.keyboard.keycode == ALLEGRO_KEY_LEFT){
pos_x -= 30;
}
if(event.keyboard.keycode == ALLEGRO_KEY_RIGHT){
pos_x += 30;
}
if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE){
return 1;
}
al_draw_filled_rectangle(pos_x, pos_y, pos_x + 30, pos_y + 30, al_map_rgb(255,0,255));
al_flip_display();
}
al_clear_to_color(al_map_rgb(0,0,0));
return 0;
}
int game_run() {
int error = 0;
if (!al_is_event_queue_empty(event_queue)) {
al_wait_for_event(event_queue, &event);
error = process_event();
}
return error;
}
void game_destroy() {
al_destroy_event_queue(event_queue);
al_destroy_display(display);
}