|
[a5] add a initial menu |
Jefferson Almeida
Member #12,659
March 2011
|
Hi friends I'm trying to do my first game and I'm reading the API and tutorials of the website. 1#include "header.h"
2
3const float FPS = 60;
4const int SCREEN_W = 800;
5const int SCREEN_H = 600;
6enum MYKEYS {
7 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
8};
9
10ALLEGRO_FONT *font;
11
12void loadfont()
13{
14 ALLEGRO_FONT *font = al_load_font("DejaVuSans.ttf", 10, 0);
15 if(!font) {
16 fprintf(stderr, "Failed to load font ttf!\n");
17 }
18}
19
20void menu()
21{
22
23}
24
25
26int main(void)
27{
28//ALLEGRO_BITMAP *swordcursor;
29//ALLEGRO_MOUSE_CURSOR *cursor;
30
31 float bouncer_x = SCREEN_W / 2.0;
32 float bouncer_y = SCREEN_H / 2.0;
33 bool key[4] = { false, false, false, false };
34 bool redraw = true;
35 bool doexit = false;
36
37 if(!al_init()) {
38 fprintf(stderr, "Failed to initialize Allegro!\n");
39 return -1;
40 }
41
42 if(!al_init_primitives_addon()) {
43 fprintf(stderr, "Failed to initialize Addon primitives!\n");
44 return -1;
45 }
46 al_init_font_addon();
47 al_init_ttf_addon();
48 if(!al_init_image_addon()) {
49 fprintf(stderr, "Failed to initialize Image addon!\n");
50 return -1;
51 }
52 if(!al_install_keyboard()) {
53 fprintf(stderr, "Failed to initialize Keyboard!\n");
54 return -1;
55 }
56
57 if(!al_install_mouse()) {
58 fprintf(stderr, "Failed to initialize Mouse!\n");
59 return -1;
60 }
61 ALLEGRO_TIMER *timer;
62 timer = al_create_timer(1.0 / FPS);
63 if(!timer) {
64 fprintf(stderr, "Failed to created timer!\n");
65 return -1;
66 }
67 ALLEGRO_DISPLAY *display;
68 display = al_create_display(SCREEN_W, SCREEN_H);
69 if(!display) {
70 fprintf(stderr, "Failed to initialize Display!\n");
71 al_destroy_timer(timer);
72 return -1;
73 }
74 ALLEGRO_BITMAP *logo = al_load_bitmap("images/logo.png");
75 al_clear_to_color(al_map_rgb(0,0,0));
76 al_draw_bitmap_region(logo, 0, 0, al_get_bitmap_width(logo), al_get_bitmap_height(logo),
77 (al_get_bitmap_width(al_get_backbuffer(al_get_current_display())) - al_get_bitmap_width(logo))/2,
78 (al_get_bitmap_height(al_get_backbuffer(al_get_current_display())) - al_get_bitmap_height(logo))/2, 0);
79 al_destroy_bitmap(logo);
80 al_flip_display();
81
82
83
84// if(!al_show_mouse_cursor(display)) {
85// fprintf(stderr, "Failed...");
86// return -1;
87//}
88// ALLEGRO_BITMAP = al_load_bitmap("swordcursor.jpeg");
89// if(!swordcursor) {
90// fprintf(stderr, "Failed to load bmp cursor!\n");
91// return -1;
92// }
93// cursor = al_create_mouse_cursor(swordcursor, 16, 16);
94// if(!cursor) {
95// fprintf(stderr, "Failed to create cursor");
96// return -1;
97// }
98 //
99 // al_set_target_bitmap(swordcursor);
100 // al_set_mouse_cursor(display, cursor);
101
102 int gfxmode = FULLSCREEN ? ALLEGRO_FULLSCREEN : ALLEGRO_WINDOWED;
103 al_set_new_display_flags(ALLEGRO_RESIZABLE);
104 ALLEGRO_EVENT_QUEUE *queue;
105 queue = al_create_event_queue();
106 if(!queue) {
107 fprintf(stderr, "failed to create queue!\n");
108 al_destroy_display(display);
109 al_destroy_timer(timer);
110 return -1;
111 }
112
113 al_register_event_source(queue, al_get_display_event_source(display));
114
115 al_register_event_source(queue, al_get_timer_event_source(timer));
116
117 al_register_event_source(queue, al_get_keyboard_event_source());
118
119 // al_clear_to_color(al_map_rgb(0,0,0));
120
121 // al_flip_display();
122
123 al_start_timer(timer);
124
125 while(!doexit)
126 {
127 ALLEGRO_EVENT ev;
128 al_wait_for_event(queue, &ev);
129
130 if(ev.type == ALLEGRO_EVENT_TIMER) {
131 if(key[KEY_UP] && bouncer_y >= 4.0) {
132 bouncer_y -= 4.0;
133 }
134
135 if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - 4.0) {
136 bouncer_y += 4.0;
137 }
138
139 if(key[KEY_LEFT] && bouncer_x >= 4.0) {
140 bouncer_x -= 4.0;
141 }
142
143 if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - 4.0) {
144 bouncer_x += 4.0;
145 }
146
147 redraw = true;
148 }
149 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
150 break;
151 }
152 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
153 switch(ev.keyboard.keycode) {
154 case ALLEGRO_KEY_UP:
155 key[KEY_UP] = true;
156 break;
157
158 case ALLEGRO_KEY_DOWN:
159 key[KEY_DOWN] = true;
160 break;
161
162 case ALLEGRO_KEY_LEFT:
163 key[KEY_LEFT] = true;
164 break;
165
166 case ALLEGRO_KEY_RIGHT:
167 key[KEY_RIGHT] = true;
168 break;
169 }
170 }
171 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
172 switch(ev.keyboard.keycode) {
173 case ALLEGRO_KEY_UP:
174 key[KEY_UP] = false;
175 break;
176
177 case ALLEGRO_KEY_DOWN:
178 key[KEY_DOWN] = false;
179 break;
180
181 case ALLEGRO_KEY_LEFT:
182 key[KEY_LEFT] = false;
183 break;
184
185 case ALLEGRO_KEY_RIGHT:
186 key[KEY_RIGHT] = false;
187 break;
188
189 case ALLEGRO_KEY_ESCAPE:
190 doexit = true;
191 break;
192 }
193 }
194 if(redraw && al_is_event_queue_empty(queue)) {
195 redraw = false;
196
197// al_clear_to_color(al_map_rgb(0,0,0));
198
199// al_flip_display();
200 }
201 }
202
203 al_destroy_timer(timer);
204 al_destroy_display(display);
205 al_destroy_event_queue(queue);
206
207
208 return 0;
209}
210//END_OF_MAIN()
|
William Labbett
Member #4,486
March 2004
|
you could do it either way but it would probably be more manageable to make a seperate function. You could have : int run_menu(ALLEGRO_DISPLAY *display, ...whatever \else you need...) { } ...and use the return value to decide what to do back in the caller.
|
Jefferson Almeida
Member #12,659
March 2011
|
I can not get =\ |
J-Gamer
Member #12,491
January 2011
|
Which part is not understandable to you? " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
|