Reputation: 23
I'm making a game using Visual Studio C++ and Allegro 5. To make a loading animation, I decided to make a create a thread using al_create_thread, load all my images and sounds with it, and then destroy it after everything loads. That way I can use a while loop to play an animation while the thread loads everything. Unfortunately, after switching to this method, my frame rate drops like crazy and basically makes the game unplayable, and if I load everything in my main function instead, the frame rate goes back to normal but I can't have a loading animation. I don't understand why the thread is causing issues if I destroy it after it's done.
Here is my thread function:
static void*loading_thread(ALLEGRO_THREAD*load, void*data)
{
al_init_image_addon();
al_init_primitives_addon();
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(20);
machinegun = al_load_sample("machine gun.ogg");
machinegun_instance = al_create_sample_instance(machinegun);
al_set_sample_instance_playmode(machinegun_instance, ALLEGRO_PLAYMODE_LOOP);
al_set_sample_instance_gain(machinegun_instance, 2);
al_attach_sample_instance_to_mixer(machinegun_instance, al_get_default_mixer());
zombie_moan = al_load_sample("zombie moan.ogg");
zombie_attack = al_load_sample("zombie attack.ogg");
jab = al_load_sample("jab.ogg");
all_nightmare_long = al_load_sample("All Nightmare Long.ogg");
all_nightmare_long_instance = al_create_sample_instance(all_nightmare_long);
al_set_sample_instance_playmode(all_nightmare_long_instance, ALLEGRO_PLAYMODE_LOOP);
al_attach_sample_instance_to_mixer(all_nightmare_long_instance,al_get_default_mixer());
playerImage = al_load_bitmap("soldier.bmp");
al_convert_mask_to_alpha(playerImage, al_map_rgb(110, 80, 52));
player->Init(playerImage);
objects.push_back(player);
zombieImage = al_load_bitmap("zombie3.bmp");
al_convert_mask_to_alpha(zombieImage, al_map_rgb(106, 76, 48));
done_loading = true;
return NULL;
}
Here is the code in my main that uses the thread function (the while loop is just a placeholder for my animation):
loading = al_create_thread(loading_thread, NULL);
al_start_thread(loading);
while(!done_loading)
{
al_draw_textf(font18, al_map_rgb(255,255,255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Loading... %i", a);
a++;
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
al_destroy_thread(loading);
Upvotes: 2
Views: 440
Reputation: 48294
When loading on a thread without a display, you will get memory bitmaps.
On Allegro 5.0 you can then al_clone_bitmap()
once the active thread is the one with a display. On 5.1, you can use al_convert_bitmap()
or al_convert_bitmaps()
.
Upvotes: 1