Reputation: 161
I need to create a very simple program that will let the user browse an image, which will then be used for a bitmap that will be displayed in the screen.
I know how to create a simple native file dialog with Allegro 5, but I don't know how to use the selected file for my bitmap.
This is my code:
ALLEGRO_FILECHOOSER *filechooser;
filechooser = al_create_native_file_dialog("C:", "Choose a file.", "*.*;*.jpg;", 1);
al_show_native_file_dialog(display, filechooser);
When I click on the files, the native file dialog disappears and nothing happens at all. I searched a lot for this matter, but I could not find an answer to my problem.
How do I create a bitmap with the selected image?
Upvotes: 2
Views: 384
Reputation: 603
Let's start by looking at what the API defines:
ALLEGRO_FILECHOOSER
: the handle to the file dialog box.al_show_native_file_dialog
: the method to display the dialog associated with the handle.So, after you create the dialog, initialize and display it, the user will select a file. However, this dialog supports the selection of multiple files at a time, thats what the size_t i
in al_get_native_file_dialog_path
is for.
In order for you to know how many files the user selected you must then call al_get_native_file_dialog_count
and store the value it returned somewhere.
Later on, you will now call al_get_native_file_dialog_path
inside the al_load_bitmap function with the number of the file you would like to open and voila! You have the image the user requested, or the images if that the case, but implementing that is a good exercise to do a slideshow app.
Now an example:
ALLEGRO_FILECHOOSER *filechooser;
filechooser = al_create_native_file_dialog("C:", "Choose a file.", "*.*;*.jpg;", 1);
al_show_native_file_dialog(display, filechooser);
/* Actually I will not use this but leaving it here as example only*/
int counter = al_get_native_file_dialog_count(filechooser);
/* Instead of cycling counter, I will select 1 to refer to the first image selected*/
const char* path = al_get_native_file_dialog_path(filechooser, 1);
ALLEGRO_BITMAP *image = al_load_bitmap(path);
After this, you display the image stored on that ALLEGRO_BITMAP
to the screen.
Upvotes: 3