-
Using a timer in allegro
I have started trying to learn Allegro, so i'm quite new to it, and it is really my first experience with an API.
What i want to do is similar to this (pseudo-code) :
Code:
while (!exit)
{
if (NextUpdate < CurrentTime)
{
UpdateScreen();
UpdatePlayer();
UpdateEnemies();
GetInput();
Blah();
NextUpdate = CurrentTime + 33.333; //milliseconds
}
}
I've read the allegro docs, and the stuff about using the timer, but i still have no clue as to how i would do this.
Help/code examples would be great, thanks
-
Well, I'm assuming you only want the code for using a timer so here goes...
This program is going to show you how to draw to the screen and when to draw to the screen next by using a timer.
Code:
#include <allegro.h>
void setup();
void redraw();
void update_counter();
int counter = 0;
int x, y;
BITMAP *bmp;
BITMAP *buffer;
int main()
{
setup();
redraw();
counter = 0; //setting the counter to 0 because it has been running
while(!keypressed()) /*this loop is SUPPOSED TO run until a key is pressed
however there is constant updating which is preventing
this from happening so the program won't terminate
until you a special windows key command, such as
ctrl+alt+del or alt+tab*/
{
if(counter == 180) //this is going to happen every 3 seconds
{
redraw();
counter = 0;
}
}
// allegro_exit();
return 0;
}
END_OF_MAIN();
void setup()
{
allegro_init();
install_timer(); //this must be called before any other timer functions are called
install_int_ex( update_counter, BPS_TO_TIMER(60)); //this is the "actual" timer function
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0);
bmp = load_bitmap("name_of_bmp.bmp", NULL);
buffer = create_bitmap(SCREEN_W, SCREEN_H);
}
void update_counter()
{
counter++;
}
void redraw()
{
clear_bitmap(buffer); //clearing the buffer to not leave any trails
blit(bmp, buffer, 0, 0, x, y, SCREEN_W, SCREEN_H); //blitting the image to the buffer
blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); //blitting the buffer to the screen
if(x < SCREEN_W - 100)
{
x += 10;
}
if(y < SCREEN_H - 100)
{
y += 5;
}
}
The program is going to draw your bitmap to the screen then every 3 seconds it is going to redraw the image to the screen and after every redraw it is going to move the image over 10 pixels to the right and 5 pixels down until each of them reach their limit
Hope that helps you out and now I have another tutorial to add to my site.
EDIT: had a few things wrong w/ the program so I fixed them
-
oh and this link will probably be able to help you out.
-
declare counter as a volatile int, not just a regular int, and use LOCK_FUNCTION() and LOCK_VARIABLE() on the counter and the counter function
Example:
Code:
// int counter = 0; Replace this line with the next one
volatile int counter = 0;
and after you call install_timer(); put this in
Code:
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
This is just personal preference, but I like to use install_int() instead of install_int_ex() and pass 1000 as the second argument. But, like I said, just personal preference. You can use it any way you like.
-
I can't remember why right now, but I know that I don't like to include the LOCK_FUNCTION and LOCK_VARIABLE for a certain reason.
n/m I remember now
If you do LOCK_VARIABLE then you can't ever redeclare the value of int. So if you were to LOCK_VARIABLE(counter) then my example program wouldn't work. So in some instances you don't want to LOCK_VARIABLE.
Regardless, what exactly is the benefit of LOCK_VARIABLE, and the same goes with LOCK_FUNCTION?:)
-
it's for cross platfrom stuff. On some sytems it could crash the computer if you don't lock em' and declare the counter volatile.
-
oh, and you can change the value of int, because I do with my FPS counters.