简体   繁体   English

如何在 Allegro 5 中使用浏览过的文件?

[英]How to Use a Browsed File in Allegro 5?

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.我知道如何使用 Allegro 5 创建一个简单的本机文件对话框,但我不知道如何将所选文件用于我的位图。

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?如何使用所选图像创建位图?

Let's start by looking at what the API defines:让我们先看看API定义了什么:

  • ALLEGRO_FILECHOOSER : the handle to the file dialog box. ALLEGRO_FILECHOOSER :文件对话框的句柄。
  • al_show_native_file_dialog : the method to display the dialog associated with the handle. al_show_native_file_dialog :显示与句柄关联的对话框的方法。

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.但是,此对话框支持一次选择多个文件,这就是al_get_native_file_dialog_pathsize_t ial_get_native_file_dialog_path

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.为了让您知道用户选择了多少个文件,您必须调用al_get_native_file_dialog_count并将它返回的值存储在某处。

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!稍后,您现在将在 al_load_bitmap 函数中调用al_get_native_file_dialog_path并使用您要打开的文件编号,瞧! 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.在此之后,您将存储在该ALLEGRO_BITMAP上的图像显示到屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM