3

I'm working on my first game in Allegro 5, I've got the title menu rendering as such, however I want to add clickable text in the menu. How would I make it so that, when you hover over the text you can click it? I'm thinking having a for statement checking the pixels would be very bad for performance, here's what I have so far:

#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;
}

I'm using codeblocks on windows XP SP3

Bugster
  • 1,552
  • 8
  • 34
  • 56
  • Going from "drawing a static image to the screen" to "make a GUI work" is a *long* step. You need to have a functioning render loop first. – Nicol Bolas Sep 02 '12 at 20:47
  • I haven't found this information anywhere, what is a game without some GUI? If you could explain a possible very basic method of working GUI in allegro I'd be very much appreciative, as I simply haven't found any information on how to do it anywhere. :( – Bugster Sep 03 '12 at 05:53

2 Answers2

7

To do it "properly," you'd need to use some sort of GUI library. But you can easily create a clickable section of the screen by hardcoding some rectangle's coordinates.

First you'll need to set up your event handling:

ALLEGRO_EVENT_QUEUE *queue;
queue = al_create_event_queue();
al_install_keyboard();
al_register_event_source(queue, al_get_keyboard_event_source());

Without getting into the specifics of event handling (it's an entire topic of its own), here's the relevant bit:

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;          
    }
  }
}

There are many ways to improve upon this example... This is just to get you started.

You should carefully read through the documentation regarding event handling and examine the bundled examples and check out the wiki for more information.

PS: Use forward slashes when using file paths, as they are cross platform:

#include <allegro5/allegro.h>
Matthew
  • 47,584
  • 11
  • 86
  • 98
0

The above isn't true; You can add the keyboard to your game and then you have 2 options for collision detection, pixel perfect and bounding box, both covered in the allegro 5 wiki. The tick here to create the best menu IMO is to create a loop and timer for the menu, then list the keyboard, mouse, and timer events seperatley. Next make some if statements so the mouse or keyboard event only triggers when you actually click it, this is needed so you can scroll through a menu with both mouse and keypad, or you could just make it so the mouse wont affect the screen until after you click, but former looks better, at least IMO.