Start with a boolean, this will be used for an update statement at the end. Another variable was used for the FPS in case you change it later, so you don't have to change it throughout the program.
bool draw = true;
const int FPS = 60;
Then make an event queue pointer and a timer pointer. 1 / 60 means 60 times a second.
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
Register the timer into the event queue and start the timer. Putting anything other than the game loop after this statement might fuck up the timer.
al_register_event_source(event_queue, al_get_timer_event_source(timer));//put the timer into the queue
al_start_timer(timer);
Inside the game loop, make the event.
ALLEGRO_EVENT events;
al_wait_for_event(event_queue, &events);
If the event timer ticks into the queue, update the draw variable.
if(events.type == ALLEGRO_EVENT_TIMER)
{
draw = true;//only draw if something updated
}
If something changed the update the graphics.
if(draw)
{
do all your graphics stuff here
}
Wednesday, 26 February 2014
Allegro Colour Variables
Rather than using the rgb colour every time you need a colour argument you can use ALLEGRO_COLOR to set a global colour.
ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255);
This was you only need the variable name rather than the hex code.
ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255);
This was you only need the variable name rather than the hex code.
Location:
Halkirk, Highland KW12, UK
Tuesday, 25 February 2014
Allegro Fonts
The first thing you'll need to do is include the font files and the true type font (ttf) file.
#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_font.h>
Both modules need to be added to the program too.
al_init_font_addon();
al_init_ttf_addon();
Now you'll need to acquire a font file. I got mine from this website for free.
http://www.webpagepublicity.com/free-fonts.html
Once downloaded, put the ttf file in your assets folder of the program.
To load the font into the program you'll need the following a font pointer. The first argument is where you put the file location of the font file. The second argument is the size of the text and the third is for any flags.
ALLEGRO_FONT *font = al_load_font("Assets/Fonts/Airmole Shaded.ttf", 36, NULL);
To display the text on screen use the following function, using the font pointer as the first argument. Next is colour, x position and y position. Fifth is a flag to centralize the text, you could also use ALIGN_LEFT or ALIGN_RIGHT. Lastly, the message you want displayed.
al_draw_text(font, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "HELLO");
#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_font.h>
Both modules need to be added to the program too.
al_init_font_addon();
al_init_ttf_addon();
Now you'll need to acquire a font file. I got mine from this website for free.
http://www.webpagepublicity.com/free-fonts.html
Once downloaded, put the ttf file in your assets folder of the program.
To load the font into the program you'll need the following a font pointer. The first argument is where you put the file location of the font file. The second argument is the size of the text and the third is for any flags.
ALLEGRO_FONT *font = al_load_font("Assets/Fonts/Airmole Shaded.ttf", 36, NULL);
To display the text on screen use the following function, using the font pointer as the first argument. Next is colour, x position and y position. Fifth is a flag to centralize the text, you could also use ALIGN_LEFT or ALIGN_RIGHT. Lastly, the message you want displayed.
al_draw_text(font, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "HELLO");
Location:
Halkirk, Highland KW12, UK
Allegro Message Boxes
To start using message boxes in Allegro 5 you'll need to include this file:
#include<allegro5\allegro_native_dialog.h>
This will allow you to use boxes that match your operating system.
The following function sets up the message box. Using display as the first argument will place the window in front of the display. Leaving it as NULL will leave the message box behind the display. The second argument is the box title and the third is the message heading that will display inside the box. Forth argument is where you put your message.
al_show_native_message_box(display, "Error Message", "Error", "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);
The last argument represents the type of message box. The following is a list of available options.
ALLEGRO_MESSAGEBOX_WARN
ALLEGRO_MESSAGEBOX_ERROR
ALLEGRO_MESSAGEBOX_OK_CANCEL
ALLEGRO_MESSAGEBOX_YES_NO
ALLEGRO_MESSAGEBOX_QUESTION
You can use two options at once for example ALLEGRO_MESSAGEBOX_WARN | ALLEGRO_MESSAGEBOX_YES_NO.
The al_show_native_message_box function returns an int that represents which answer the user chose. To access it allocate a variable to it.
int answer = al_show_native_message_box(display, "Error Message", "Error", "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);
If the answer is no the return value will be 0. If yes then 1.
#include<allegro5\allegro_native_dialog.h>
This will allow you to use boxes that match your operating system.
The following function sets up the message box. Using display as the first argument will place the window in front of the display. Leaving it as NULL will leave the message box behind the display. The second argument is the box title and the third is the message heading that will display inside the box. Forth argument is where you put your message.
al_show_native_message_box(display, "Error Message", "Error", "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);
The last argument represents the type of message box. The following is a list of available options.
ALLEGRO_MESSAGEBOX_WARN
ALLEGRO_MESSAGEBOX_ERROR
ALLEGRO_MESSAGEBOX_OK_CANCEL
ALLEGRO_MESSAGEBOX_YES_NO
ALLEGRO_MESSAGEBOX_QUESTION
You can use two options at once for example ALLEGRO_MESSAGEBOX_WARN | ALLEGRO_MESSAGEBOX_YES_NO.
The al_show_native_message_box function returns an int that represents which answer the user chose. To access it allocate a variable to it.
int answer = al_show_native_message_box(display, "Error Message", "Error", "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);
If the answer is no the return value will be 0. If yes then 1.
Location:
Halkirk, Highland KW12, UK
Subscribe to:
Posts (Atom)