|
Switching between displayed windows |
Bater55
Member #16,769
November 2017
|
Hi! What to do to switch windows by using key "1"? "1" means "new game". First starts menu, then I want to display game menu by clicking "1". 1#include <stdio.h>
2#include <allegro5/allegro.h>
3#include <allegro5/allegro_image.h>
4#include <allegro5/allegro_font.h>
5#include <allegro5/allegro_ttf.h>
6
7
8const float FPS = 60;
9const int SCREEN_W = 1280;
10const int SCREEN_H = 720;
11const int WARRIOR_SIZE = 84;
12enum MYKEYS {
13 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
14};
15
16int main(int argc, char **argv)
17{
18/* ALLEGRO_DISPLAY *displaystart = NULL;
19 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
20 if (!al_init()) {
21 fprintf(stderr, "failed to initialize allegro!\n");
22 return -1;
23 }
24 al_init_font_addon(); // initialize the font addon
25 al_init_ttf_addon();// initialize the ttf (True Type Font) addon
26 al_install_keyboard();
27
28 displaystart = al_create_display(1024, 768);
29 if (!displaystart) {
30 fprintf(stderr, "failed to create displaystart!\n");
31 return -1;
32 }
33 ALLEGRO_FONT *font = al_load_ttf_font("Artesana.ttf", 64, 0);
34 ALLEGRO_FONT *fonttytul = al_load_ttf_font("ARRIBAAR.TTF", 100, 0);
35
36 if (!font) {
37 fprintf(stderr, "Could not load 'Artesana.ttf'.\n");
38 return -1;
39 }
40 if (!fonttytul) {
41 fprintf(stderr, "Could not load 'ARRIBAAR.TTF'.\n");
42 return -1;
43 }
44 event_queue = al_create_event_queue();
45 if (!event_queue) {
46 fprintf(stderr, "failed to create event_queue!\n");
47 al_destroy_display(displaystart);
48 return -1;
49 }
50
51 al_register_event_source(event_queue, al_get_display_event_source(displaystart));
52 al_register_event_source(event_queue, al_get_keyboard_event_source(displaystart));
53 al_clear_to_color(al_map_rgb(2, 147, 0));
54 al_draw_text(fonttytul, al_map_rgb(223, 234, 18), 1024 / 2, 80, ALLEGRO_ALIGN_CENTRE, "Indi w tarapatach!");
55 al_draw_text(font, al_map_rgb(255, 255, 255), 1024 / 2, 250, ALLEGRO_ALIGN_CENTRE, "Nowa gra");
56 al_draw_text(font, al_map_rgb(255, 255, 255), 1024 / 2, 400, ALLEGRO_ALIGN_CENTRE, "Wyjdz z gry");
57 al_flip_display();
58 while (1)
59 {
60 ALLEGRO_EVENT ev;
61 ALLEGRO_TIMEOUT timeout;
62 al_init_timeout(&timeout, 0.06);
63
64 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
65
66
67 if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
68 break;
69 }
70 else if (get_event && ev.type == ALLEGRO_EVENT_KEY_DOWN) {
71 break;
72 }
73 }
74
75 al_destroy_display(displaystart);
76 al_destroy_event_queue(event_queue);
77 return 0;
78} */
79
80 if (!al_init()) {
81 printf("failed to create allegro!\n");
82 return -1;
83 }
84 ALLEGRO_DISPLAY *display = NULL;
85 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
86 ALLEGRO_TIMER *timer = NULL;
87 float warrior_x = SCREEN_W / 2.0 - WARRIOR_SIZE / 2.0;
88 float warrior_y = SCREEN_H / 2.0 - WARRIOR_SIZE / 2.0;
89 bool key[4] = { false, false, false, false };
90 bool redraw = true;
91 bool doexit = false;
92 if (!al_install_keyboard()) {
93 fprintf(stderr, "failed to initialize the keyboard!\n");
94 return -1;
95 }
96
97 timer = al_create_timer(0.8 / FPS);
98 if (!timer) {
99 fprintf(stderr, "failed to create timer!\n");
100 return -1;
101 }
102 if (!al_init_image_addon()) {
103 fprintf(stderr, "Failed to initialize al_init_image_addon!\n");
104 return -1;
105 }
106 display = al_create_display(SCREEN_W, SCREEN_H);
107 if (!display) {
108 fprintf(stderr, "failed to create display!\n");
109 al_destroy_timer(timer);
110 return -1;
111 }
112 ALLEGRO_BITMAP *warrior = al_load_bitmap("indiana.png");
113 ALLEGRO_BITMAP *mapa1 = NULL;
114 ALLEGRO_BITMAP *mapa2 = NULL;
115 ALLEGRO_BITMAP *mapa3 = NULL;
116 ALLEGRO_BITMAP *mapa4 = NULL;
117 ALLEGRO_BITMAP *mapa5 = NULL;
118 warrior = al_load_bitmap("indiana.png");
119 al_convert_mask_to_alpha(warrior, al_map_rgb(255, 255, 255));
120 if (!warrior) {
121 fprintf(stderr, "failed to create warrior!\n");
122 al_destroy_display(display);
123 al_destroy_timer(timer);
124 return 0;
125 }
126 mapa1 = al_load_bitmap("mapa5.png");
127 if (!mapa1)
128 {
129 fprintf(stderr, "failed to load mapa1 bitmap!\n");
130 return -1;
131 }
132
133 al_set_target_bitmap(warrior);
134
135 al_set_target_bitmap(al_get_backbuffer(display));
136
137 event_queue = al_create_event_queue();
138 if (!event_queue) {
139 fprintf(stderr, "failed to create event_queue!\n");
140 al_destroy_bitmap(warrior);
141 al_destroy_display(display);
142 al_destroy_timer(timer);
143 return -1;
144 }
145
146 al_register_event_source(event_queue, al_get_display_event_source(display));
147
148 al_register_event_source(event_queue, al_get_timer_event_source(timer));
149
150 al_register_event_source(event_queue, al_get_keyboard_event_source());
151
152 al_draw_bitmap(mapa1, 0, 0, 0);
153
154 al_flip_display();
155
156 al_start_timer(timer);
157
158 while (!doexit)
159 {
160 ALLEGRO_EVENT ev;
161 al_wait_for_event(event_queue, &ev);
162
163 if (ev.type == ALLEGRO_EVENT_TIMER) {
164 if (key[KEY_UP] && warrior_y >= 4.0) {
165 warrior_y -= 4.0;
166 }
167
168 if (key[KEY_DOWN] && warrior_y <= SCREEN_H - WARRIOR_SIZE - 4.0) {
169 warrior_y += 4.0;
170 }
171
172 if (key[KEY_LEFT] && warrior_x >= 4.0) {
173 warrior_x -= 4.0;
174 }
175
176 if (key[KEY_RIGHT] && warrior_x <= SCREEN_W - WARRIOR_SIZE - 4.0) {
177 warrior_x += 4.0;
178 }
179 redraw = true;
180 }
181 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
182 break;
183 }
184 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
185 switch (ev.keyboard.keycode) {
186 case ALLEGRO_KEY_UP:
187 key[KEY_UP] = true;
188 break;
189
190 case ALLEGRO_KEY_DOWN:
191 key[KEY_DOWN] = true;
192 break;
193
194 case ALLEGRO_KEY_LEFT:
195 key[KEY_LEFT] = true;
196 break;
197
198 case ALLEGRO_KEY_RIGHT:
199 key[KEY_RIGHT] = true;
200 break;
201 }
202 }
203 else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
204 switch (ev.keyboard.keycode) {
205 case ALLEGRO_KEY_UP:
206 key[KEY_UP] = false;
207 break;
208
209 case ALLEGRO_KEY_DOWN:
210 key[KEY_DOWN] = false;
211 break;
212
213 case ALLEGRO_KEY_LEFT:
214 key[KEY_LEFT] = false;
215 break;
216
217 case ALLEGRO_KEY_RIGHT:
218 key[KEY_RIGHT] = false;
219 break;
220
221 case ALLEGRO_KEY_ESCAPE:
222 doexit = true;
223 break;
224 }
225 }
226
227 if (redraw && al_is_event_queue_empty(event_queue)) {
228 redraw = false;
229
230
231 al_draw_bitmap(warrior, warrior_x, warrior_y, 0);
232 al_flip_display();
233 al_draw_bitmap(mapa1, 0, 0, 0);
234
235 }
236 }
237 al_destroy_bitmap(warrior);
238 al_destroy_bitmap(mapa1);
239 al_destroy_timer(timer);
240 al_destroy_display(display);
241 al_destroy_event_queue(event_queue);
242
243 return 0;
244 }
245 ;
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Wait for a keypress : ALLEGRO_EVENT ev; do { al_wait_for_event(queue , &ev); } while (ev.type != ALLEGRO_EVENT_KEY_DOWN); Check for key 1 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_1) {} Draw a new screen : al_draw_bitmap(game , 0 , 0); al_flip_display();
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|