Hi everybody !.
I'm on my way to learn programming games using Allegro 5.I've written a function which simply act as a game Intro ,displaying a message on screen,resting for 1,05 seconds and continue with the game...unless the player (me ,for the moment) press ESCAPE to skip the intro .I have this code :
void Intro1()
{
bool esc = false;
while(!esc)
{
ALLEGRO_EVENT_QUEUE *eventIntro1 = NULL;
eventIntro1=al_create_event_queue();
al_register_event_source(eventIntro1,al_get_keyboard_event_source());
ALLEGRO_EVENT evIntro1;
al_wait_for_event(eventIntro1 ,&evIntro1);
ALLEGRO_SAMPLE *sampleIntro1 = NULL;
sampleIntro1 = al_load_sample("Intro1.ogg");
if(al_event_queue_is_empty(eventIntro1))
{
for(int i=0;i<=256;i++)
{
al_play_sample(sampleIntro1,1,0,1,ALLEGRO_PLAYMODE_ONCE, NULL);
ALLEGRO_FONT *font44 = al_load_font("215000EURO.ttf",44,0);
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_text(font44,al_map_rgb(0,0,i),WIDTH/2,HEIGHT/3,
ALLEGRO_ALIGN_CENTER,"WELCOME TO ...");
al_flip_display();
al_destroy_font (font44);
al_destroy_sample(sampleIntro1);
}
}
else if(evIntro1.type == ALLEGRO_KEY_UP)
{
switch(evIntro1.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
esc = true;
break;
}
}
al_destroy_event_queue(eventIntro1);
}
al_rest(1.05);
esc = true;
}
It compiles without any problems ,but nothing appears on the screen ,and when i press ESCAPE,i've got a "Segmentation Fault (core dumped)" error and i do not know why .What i expect for this function to do is : there are no events in the queue ,so it draws "WELCOME TO..." on the screen ,rests for 1,05 seconds and terminates,unless a key is pressed (ESCAPE) and terminates immediately .Am i wrong?.Could somebody help me please ? .
I really apreciate it !:D
Edit:Sorry ,i forgot to tell i 'm using Ubuntu and Code::Blocks .And the function is part of a larger program which works fine when i comment or simply delete the function from it .
Are you sure the Allegro is setup properly BEFORE you call the function, and all bitmaps are allocated?
Also, don't forget to use codeblocks so we can read it easier:
I'm no A5 guy... but... why are you making new event queues over and over in the while loop?
Uff ,sorry about codeblocks !.
And for the Allegro setup and fonts and sounds ,I'm pretty sure they are ok ,was the first thing i checked when i received the error.
Are you saying i should have place the event queue outside and right before the while loop ? I did it in that way because i thought the function needs to read if an event has ocurred (in this case press ESCAPE key) to interrupt the loop .And i thought that was the way to do it...
But i suppose i was wrong.
You create an event queue, and then set it to process events. A queue holds events. You shouldn't need to create an event queue for every event, or for every frame drawn.
Your code should look more like this example:
https://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Events#Event_Queues
Notice how there is only one event queue, it is created (once), used, and then removed (once).
Yes ,i had one event queue created in the main function but when i tried to register events from it inside the function i wrote (Intro) i received "event_queue X was not declared in this scope" ,that's why i created another inside the function .With the intention of destroy it after use it .
Well, you should always use "dependency injection." That means anything you use in main, is forwarded to the function you use as a function argument, so it forces you to think about how much your function really touches.
Secondly, even if you used a second event queue, you are creating it INSIDE the while loop, which means you're creating a new one EVERY TIME that while loop cycles.
I've never heard about "Dependency Injection ",so i think i need to read something (or a lot) of it .
Thanks very much for the teachings Chris !!.
Or, in a global variable could also work, although some people would recommend against the use of global variables because they can get quite messy if abused.
Thanks for the advice beoran!.I need to read about that too!.
Meanwhile i will try to rewrite the function or integrate it in a game state.