你怎么能有效地创建一个allegro 5标题菜单?

Bug*_*ter 3 c++ allegro codeblocks allegro5

我正在使用Allegro 5中的第一个游戏,我已经有了标题菜单渲染,但是我想在菜单中添加可点击的文本.如何将其设置为当您将鼠标悬停在文本上时可以单击它?我正在考虑使用for语句检查像素对性能非常不利,这是我到目前为止所做的:

#include <allegro5\allegro.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>

const int width = 1280;
const int height = 720;

int main(void)
{
    al_init();

    al_init_primitives_addon();
    al_init_image_addon();

    ALLEGRO_DISPLAY *display = al_create_display(width, height);
    ALLEGRO_BITMAP *title = al_load_bitmap("titlemenu.bmp");

    al_clear_to_color(al_map_rgb(0, 0, 0));
    al_draw_bitmap(title, 0, 0, 0);
    al_flip_display();
    al_rest(3.0);
    al_destroy_display(display);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Windows XP SP3上的代码块

Mat*_*hew 7

要"正确"地执行此操作,您需要使用某种GUI库.但您可以通过硬编码某些矩形坐标轻松创建屏幕的可点击部分.

首先,您需要设置事件处理:

ALLEGRO_EVENT_QUEUE *queue;
queue = al_create_event_queue();
al_install_keyboard();
al_register_event_source(queue, al_get_keyboard_event_source());
Run Code Online (Sandbox Code Playgroud)

没有深入了解事件处理的细节(这是它自己的整个主题),这里是相关的一点:

int selection = 0;

while (!selection)
{
  ALLEGRO_EVENT event;
  al_wait_for_event(queue, &event);
  if (event.type == ALLEGRO_EVENT_KEY_UP)
  {
    if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
      selection = MYGAME_QUIT;
  }
  else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
  {
    if (event.mouse.x >= MYGAME_MENU_X1 && event.mouse.x < MYGAME_MENU_X2 &&
        event.mouse.y >= MYGAME_MENU_Y1 && event.mouse.y < MYGAME_MENU_Y2)
    {
      selection = MYGAME_OPTION1;          
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

很多方法可以改进这个例子...这只是为了让你开始.

您应仔细阅读有关事件处理的文档,并检查捆绑的示例并查看Wiki以获取更多信息.

PS:使用文件路径时使用正斜杠,因为它们是跨平台的:

#include <allegro5/allegro.h>
Run Code Online (Sandbox Code Playgroud)