|
[A5] Timer & Keyboard issues |
Joined
Member #12,691
March 2011
|
Hi to all. Can you help me with the debugging? 1#include <stdio.h>
2#include <allegro5/allegro.h>
3#include <allegro5/allegro_font.h>
4#include <allegro5/allegro_ttf.h>
5#include <allegro5/allegro_audio.h>
6#include <allegro5/allegro_acodec.h>
7#include <allegro5/allegro_image.h>
8#include <allegro5/allegro_primitives.h>
9#define SCREEN_W 800
10#define SCREEN_H 600
11#define FPS 60
12
13int main()
14{
15 if(!al_init())
16 {
17 fprintf(stderr, "Failed to initialize Allegro.\n");
18 system("pause");
19 return -1;
20 }
21 if(!al_install_keyboard())
22 {
23 fprintf(stderr, "Failed to initialize the keyboard!\n");
24 system("pause");
25 return -1;
26 }
27 al_init_font_addon();
28 if (!al_init_ttf_addon())
29 {
30 fprintf(stderr, "Failed to initialize the TTF addon!\n");
31 system("pause");
32 return -1;
33 }
34 if (!al_install_audio())
35 {
36 fprintf(stderr, "Failed to initialize the audio addon!\n");
37 system("pause");
38 return -1;
39 }
40 if (!al_init_acodec_addon())
41 {
42 fprintf(stderr, "Failed to initialize the acodec addon!\n");
43 system("pause");
44 return -1;
45 }
46 if (!al_reserve_samples(2))
47 {
48 fprintf(stderr, "Failed to reserve the samples!\n");
49 system("pause");
50 return -1;
51 }
52 if (!al_init_image_addon())
53 {
54 fprintf(stderr, "Failed to initialize image addon!\n");
55 system("pause");
56 return -1;
57 }
58 if(!al_init_primitives_addon())
59 {
60 fprintf(stderr, "Failed to initialize the primitives addon!\n");
61 system("pause");
62 return -1;
63 }
64
65 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf",96,0 ); // load the font | Font from Larabie Free Fonts
66 ALLEGRO_FONT *font2 = al_load_ttf_font("pirulen.ttf", 30,0 ); // load the font | Font from Larabie Free Fonts
67 ALLEGRO_FONT *font3 = al_load_ttf_font("pirulen.ttf", 12,0 ); // load the font | Font from Larabie Free Fonts
68 ALLEGRO_DISPLAY *display = NULL;
69 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
70 ALLEGRO_SAMPLE *sample = NULL;
71 ALLEGRO_BITMAP *sfondo = NULL;
72 ALLEGRO_TIMER *timer = NULL;
73
74 timer = al_create_timer(1.0 / FPS);
75
76 bool redraw = true;
77
78 bool doexit = false;
79 bool inizia = false;//false = iniziano i rossi; true = iniziano i gialli
80 bool suoni = false;//true = attivati; false = spenti
81 int index = 0;//indica su quale voce del menu sono posizionato inizialmente
82
83 //carico lo sfondo
84 sfondo = al_load_bitmap("background_t_p.jpg");
85
86 //creo il display principale
87 display = al_create_display(SCREEN_W,SCREEN_H);
88
89 //carico la musica
90 sample = al_load_sample("around-the-world.wav");
91
92 if (!sample)
93 {
94 fprintf(stderr, "Failed to load sample.\n");
95 system("pause");
96 return -1;
97 }
98
99 if (!display)
100 {
101 fprintf(stderr, "Failed to create display.\n");
102 system("pause");
103 return -1;
104 }
105
106 event_queue = al_create_event_queue();
107
108 if(!event_queue)
109 {
110 fprintf(stderr, "failed to create event_queue!\n");
111 al_destroy_display(display);
112 return -1;
113 }
114
115 al_register_event_source(event_queue, al_get_timer_event_source(timer));
116 al_register_event_source(event_queue, al_get_keyboard_event_source());
117 al_register_event_source(event_queue, al_get_display_event_source(display));
118
119 al_start_timer(timer);
120
121 while(!doexit)
122 {
123 ALLEGRO_EVENT ev;
124 al_wait_for_event(event_queue, &ev);
125
126 if ((ev.type == ALLEGRO_EVENT_TIMER)&&(redraw))
127 {
128 printf("entro");
129 redraw = false;
130
131 al_draw_bitmap(sfondo, 0, 0, 0);
132
133 al_draw_text(font, al_map_rgb(204,0,0), SCREEN_W/2, (SCREEN_H/8)-10,ALLEGRO_ALIGN_CENTRE, "FORZA 4");// draw the text
134
135 if (index==0) al_draw_text(font2, al_map_rgb(255,0,0), SCREEN_W/2-20, (SCREEN_H/2)-60,ALLEGRO_ALIGN_CENTRE, "GIOCA");// draw the text
136 else al_draw_text(font2, al_map_rgb(0,0,0), SCREEN_W/2-20, (SCREEN_H/2)-60,ALLEGRO_ALIGN_CENTRE, "GIOCA");// draw the text
137
138 if (index==1) al_draw_text(font2, al_map_rgb(255,0,0), SCREEN_W/2-10, (SCREEN_H/2)-20,ALLEGRO_ALIGN_CENTRE, "INIZIA < >");// draw the text
139 else al_draw_text(font2, al_map_rgb(0,0,0), SCREEN_W/2-10, (SCREEN_H/2)-20,ALLEGRO_ALIGN_CENTRE, "INIZIA < >");// draw the text
140
141 if (inizia) al_draw_text(font2, al_map_rgb(255,255,0), SCREEN_W/2+55, (SCREEN_H/2)-20,ALLEGRO_ALIGN_CENTRE, "O");// draw the text
142 else al_draw_text(font2, al_map_rgb(255,0,0), SCREEN_W/2+55, (SCREEN_H/2)-20,ALLEGRO_ALIGN_CENTRE, "O");// draw the text
143
144 if (index==2)
145 {
146 al_draw_text(font2, al_map_rgb(255,0,0), SCREEN_W/2-70, (SCREEN_H/2)+20,ALLEGRO_ALIGN_CENTRE, "SUONI");// draw the text
147
148 if (suoni) al_draw_text(font2, al_map_rgb(255,0,0), SCREEN_W/2+50, (SCREEN_H/2)+20,ALLEGRO_ALIGN_CENTRE, "<ON>");// draw the text
149 else al_draw_text(font2, al_map_rgb(255,0,0), SCREEN_W/2+50, (SCREEN_H/2)+20,ALLEGRO_ALIGN_CENTRE, "<OFF>");// draw the text
150 }
151 else
152 {
153 al_draw_text(font2, al_map_rgb(0,0,0), SCREEN_W/2-70, (SCREEN_H/2)+20,ALLEGRO_ALIGN_CENTRE, "SUONI");// draw the text
154
155 if (suoni) al_draw_text(font2, al_map_rgb(0,0,0), SCREEN_W/2+50, (SCREEN_H/2)+20,ALLEGRO_ALIGN_CENTRE, "<ON>");// draw the text
156 else al_draw_text(font2, al_map_rgb(0,0,0), SCREEN_W/2+50, (SCREEN_H/2)+20,ALLEGRO_ALIGN_CENTRE, "<OFF>");// draw the text
157 }
158
159 if (index==3) al_draw_text(font2, al_map_rgb(255,0,0), SCREEN_W/2-20, (SCREEN_H/2)+100,ALLEGRO_ALIGN_CENTRE, "ESCI");// draw the text
160 else al_draw_text(font2, al_map_rgb(0,0,0), SCREEN_W/2-20, (SCREEN_H/2)+100,ALLEGRO_ALIGN_CENTRE, "ESCI");// draw the text
161
162 al_draw_text(font3, al_map_rgb(0,0,0), SCREEN_W-100, SCREEN_H-50,ALLEGRO_ALIGN_CENTRE, "by Joined ® 2011");// draw the text
163
164 al_flip_display();
165 }
166 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
167 break;
168 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
169 {
170 switch(ev.keyboard.keycode)
171 {
172 case ALLEGRO_KEY_UP:
173 {
174 if (index>0)
175 {
176 index--;
177 redraw = true;
178 }
179 break;
180 }
181 case ALLEGRO_KEY_DOWN:
182 {
183 if (index<3)
184 {
185 index++;
186 redraw = true;
187 }
188 break;
189 }
190 case ALLEGRO_KEY_LEFT:
191 {
192 if (index == 1)
193 {
194 if (inizia == true)
195 {
196 inizia = false;
197 redraw = true;
198 }
199 }
200 else if (index == 2)
201 {
202 if (suoni == true)
203 {
204 suoni = false;
205 redraw = true;
206 al_stop_samples();
207 }
208 }
209 break;
210 }
211 case ALLEGRO_KEY_RIGHT:
212 {
213 if (index == 1)
214 {
215 if (inizia == false)
216 {
217 inizia = true;
218 redraw = true;
219 }
220 }
221 else if (index == 2)
222 {
223 if (suoni == false)
224 {
225 suoni = true;
226 redraw = true;
227 al_play_sample(sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
228 }
229 }
230 break;
231 }
232 case ALLEGRO_KEY_ENTER:
233 {
234 if (index == 3)
235 doexit=true;
236 break;
237 }
238 }
239 }
240 }
241
242 al_destroy_display(display);
243 al_uninstall_audio();
244 al_destroy_timer(timer);
245 al_destroy_bitmap(sfondo);
246 al_destroy_event_queue(event_queue);
247
248 return 0;
249}
|
kenmasters1976
Member #8,794
July 2007
|
As for your program being slow I suggest you move the addon initialization code (like primitives and font addons) and put it after display creation. That should help.
|
Peter Wang
Member #23
April 2000
|
As a clarification of the previous comment, it's not so much that you should move the initialisation code after creating the display, but you should load bitmaps and fonts after creating the display. Otherwise the bitmaps will be created as 'memory' bitmaps, which are always drawn with relatively slow software routines, instead of 'video' bitmaps, which can take advantage of hardware rendering.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Your structure is like this (psuedocode) : while (!quit) { wait_for_event(); if (is_timer_event() && redraw) { Redraw(); redraw = false; } // logic if (stuff_changes) {redraw = true;} } The problem with it is that redraw may be set to true by your logic, but it has to wait until the next event fires to perform the Redraw(). You won't have to wait very long because of the timer events, but it's still a little funky. A better layout would be : while (!quit) { while (1) { wait_for_event(); if (stuff_changes) {redraw = true;} if (is_timer_event()) {update_logic();redraw = true;} if (!events_in_queue) {break;} } if (redraw) { Redraw(); redraw = false; } }
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 |
|