Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with porting allegro timer
#1
I'm trying to copy the method of using a timer for Allegro that is in C++. The problem is I don't know how to convert some things from C++ to FB:
Code:
volatile long speed_counter = 0; //what would I use for volatiles?

void increment_speed_counter() //a function
{
  speed_counter++;  //easy: speed_counter = speed_counter + 1
}
END_OF_FUNCTION(increment_speed_counter);  //that is a macro,
                                                                         //how would I use?

//more macros...

LOCK_VARIABLE(speed_counter);
LOCK_FUNCTION(increment_speed_counter);

I don't know how to do the volatile variables and the macros. How would I make those?
Reply
#2
IIRC (don't quote me on this!) on Windows, the LOCK_VARIABLE and LOCK_FUNCTION don't do anything, nor does END_OF_FUNCTION - but I could be wrong!!!! I'll read the headers in a minute, but I think it should work without the macros.

Regarding volatile variables: FreeBASIC doesn't do any optimizations (yet) that would require a 'volatile' keyword, so you shouldn't need it.


EDIT: Yup, LOCK_FUNCTION etc. don't do anything on Windows. I'll add them sometime soon for DOS, though - DOS and Mac need them.
Reply
#3
Ok thanks, but I'm still having some trouble making the timer in allegro. Here is the basic parts of my timer:

Code:
allegro_init
install_timer

thetimer = 0                                      'the timer variable

declare function increment_timer()   'creates the timer incrementer

function increment_timer()                'function to increment timer
   thetimer = thetimer + 1
end function

install_int_ex(increment_timer, BPS_TO_TIMER(60))  'sets the BPS

while thetimer > 0
   ... (logical operations)
   thetimer = thetimer - 1
wend

The while loop seems to only run once. I know that it doesn't set thetimer back to 60, but it's supposed to already do it. Help would be appreciated.
Reply
#4
Quote:
Code:
allegro_init
install_timer

thetimer = 0                                      'the timer variable
...

while thetimer > 0
   ... (logical operations)
wend


thetimer is 0 when it reaches the loop containing the condition thetimer > 0.

By the way, after one call to your timer function, that while loop will become an infinite loop... thetimer is always increased, which means it will always be >0.

-shiftLynx
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#5
Oops, sorry I forgot to add:

Code:
while thetimer > 0
   ... (logical operations)
   thetimer = thetimer - 1
wend
Reply
#6
Well, I've tried to set the timer back to 60 every time after the while loop, but it either crashes or doesn't work. Also, one of the C++ examples of it said that the volatile keyword is needed or else it would not work. I'm worried because I already did a lot of my game in Allegro and I don't want to have to convert all of it into FBGFX or SDL. Does anyone know how to create the timer?
Reply
#7
If i remember, the Allegro callbacks use the C calling convention, try redeclaring them as function mycallback cdecl ... (...) ... .
Reply
#8
Ok, I tried changing it:

Code:
declare function increment_timer cdecl()

function increment_timer cdecl()
   thetimer = thetimer + 1
end function

But it still produced similar results. The tutorials and forum posts I read said that the volatile keyword and END_OF_FUNCTION(), LOCK_FUNCTION(), etc. macros are necessary for the timer. I still don't know, how would I make it without the them? If it may help, here is a link to one of the tutorials: http://www.glost.eclipse.co.uk/gfoot/viv...tml#Timers
Reply
#9
this line:
install_int_ex(increment_timer, BPS_TO_TIMER(60)) 'sets the BPS
should be:
install_int_ex(@increment_timer, BPS_TO_TIMER(60)) 'sets the BPS
or:
install_int_ex(procptr(increment_timer), BPS_TO_TIMER(60)) 'sets the BPS
Reply
#10
I tried that, but it still didn't affect it. Argh I can't get it to work without the macros + volatile. Could it have something to do with the fact that the Allegro header is incomplete?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)