Create a animated menu
Alexandre Bencz

Hi :)

I whant to know, how I can create a animated menu, with a animated background... so, when I enter in new option of the menu, the background continue with the 'animation...', for this, I create a thread with new event system ???

How I can make this :???

Thanks

Aikei_c

You can make a simple task manager which should be updated every tick, like this:

#SelectExpand
1class TaskManager 2{ 3 std::list<ITask*> m_tasks; 4 5public: 6 7 Update() 8 { 9 for (std::list<ITask*>::iterator it = m_tasks.begin(); it != m_tasks.end(); it++) 10 (*it)->VUpdate(); 11 } 12 13 AddTask(ITask* task) 14 { 15 tasks.push_back(task); 16 } 17};

Then you need the ITask:

class ITask
{
public:
    virtual void VUpdate()=0;
};

Now you just inherit from the ITask interface all your future tasks:

class AnimateButtonsTask
  : public ITask
{
   std::list<IButton*> m_buttons;

public:

   void VUpdate()
   {
      for (std::list<ITask*>::iterator it = m_buttons.begin(); it != m_buttons.end(); it++)
        (*it)->Animate(); //you need to define it yourself obviously. it should change the button's bitmap or increase counter or anything.
   }
};

Now you create a new AnimateTask and add it to the task manager:

TaskManager* g_taskManager = new TaskManager;
g_taskManager->AddTask(new AnimateButtonsTask);

You only need to insert the g_taskManager->Update(); into your main loop now so that it is executed every tick. This way you can execute your menu animation without interference with your other code.
Mind you, you can make other tasks this way, too.

Evert

I know I'm shamelessly plugging myself here, but this is how you could do it with EGGDialog:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_ttf.h> 3#include <allegro5/allegro_image.h> 4#include <allegro5/allegro_primitives.h> 5#include <allegro5/allegro_opengl.h> 6#include <allegro5/allegro_native_dialog.h> 7#include <allegro5/allegro_audio.h> 8#include <allegro5/allegro_acodec.h> 9#include <allegro5/allegro_opengl.h> 10#include <time.h> 11#include <stdio.h> 12#include <math.h> 13#include "egg_dialog/egg_dialog.h"; 14 15static ALLEGRO_BITMAP *bkg = NULL; 16 17/* Draw the background image when the dialog is drawn */ 18static int draw_background(int msg, EGG_DIALOG *d, int c) 19{ 20 if (msg == MSG_TIMER) 21 d->flags |= D_DIRTY; 22 if (msg == MSG_DRAW && bkg) { 23 al_set_clipping_rectangle(0,0,al_get_display_width(al_get_current_display()),al_get_display_height(al_get_current_display())); 24 al_draw_bitmap(bkg, 0, 0, 0); 25 26 for (int n = 0; n<100; n++) { 27 int x = rand() % d->w; 28 int y = rand() % d->h; 29 int r = 8 + rand() % 40; 30 31 int rr = rand() % 256; 32 int gg = rand() % 256; 33 int bb = rand() % 256; 34 int aa = rand() % 256; 35 ALLEGRO_COLOR c = al_map_rgba(rr, gg, bb, aa); 36 37 al_draw_filled_circle(x, y, rr, c); 38 } 39 } 40 return D_O_K; 41} 42 43static EGG_DIALOG hello_dlg[] = { 44 /* Vertical frame, containing a text label and a second frame */ 45 { egg_frame_proc, .w = 520, .h = 340, .d1 = EGG_WALIGN_VERTICAL, .d2 = EGG_WALIGN_CENTRE, .d3 = EGG_WALIGN_CENTRE, .callback = draw_background, .id = 1 }, 46 47 /* Horizontal frame, containing two buttons */ 48 { egg_rounded_button_proc, .w = 120, .h = 64, .fg = egg_black, .bg = egg_white, .flags = D_EXIT, .dp = "New Game", .parent = 1 }, 49 { egg_rounded_button_proc, .w = 120, .h = 64, .fg = egg_black, .bg = egg_white, .flags = D_EXIT, .dp = "Continue", .parent = 1 }, 50 { egg_rounded_button_proc, .w = 120, .h = 64, .fg = egg_black, .bg = egg_white, .flags = D_EXIT, .dp = "Options", .parent = 1 }, 51 { egg_rounded_button_proc, .w = 120, .h = 64, .fg = egg_black, .bg = egg_white, .flags = D_EXIT, .dp = "Quit", .parent = 1 }, 52 { NULL } 53}; 54 55int main(void) 56{ 57 if (!al_init()) { 58 fprintf(stderr, "Failed to initialise Allegro!\n"); 59 return EXIT_FAILURE; 60 } 61 al_init_image_addon(); 62 al_init_font_addon(); 63 al_init_ttf_addon(); 64 al_init_acodec_addon(); 65 al_init_primitives_addon(); 66 egg_initialise(); 67 68 al_install_mouse(); 69 al_install_joystick(); 70 al_install_keyboard(); 71 72 al_create_display(800, 600); 73 74 ALLEGRO_FONT *font = al_load_font("data/DejaVuSans.ttf", 18, 0); 75 bkg = al_load_bitmap("data/bkg.png"); 76 77 if (!font) { 78 return EXIT_FAILURE; 79 } 80 81 egg_centre_dialog(hello_dlg); 82 egg_set_gui_font(font); 83 84 /* Animate the dialog box, set timer frequency to 10Hz */ 85 egg_do_dialog_interval(hello_dlg, 0.1, -1); 86 87 return 0; 88}

Hopefully this makes the idea clear though: you have a timer running (in this case at a frequency of 10 Hz) and at each tick, you update the state of the animation and trigger a redraw of the background (as well as the rest of the menu).
In this case it just draws a different set of circles each time the background is drawn; you'd want a cleaner separation between drawing and logic in general.

Alexandre Bencz

Very interesting the two examples... btw... I'm using the allegro, just the allegro, and nothing more, I'm using C to code my simple game... I try to create a simple game menu, when I press the down key for example, I want the selected option blink or make a simple zoom... but, when I make this, I update the screen color, and, not work...

Thomas Fjellstrom

You'd create a loop where you draw the menu, and accept input like the up and down arrows. when a key is pressed you act on it to change which item is selected. then when the drawing code is run, it will draw the selected item differently.

Alexandre Bencz

Oook, that I know, but, the animated background ?
I just create a new 'event' timer in new thread ?

Thomas Fjellstrom

No threads. Just draw each frame of the animation every time you draw.

AmnesiA

The animated background would be like any animation you've programmed and if you want it to keep animating between menus you just don't reset the frame variable when you enter a new menu

Thread #613243. Printed from Allegro.cc