Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Allegro] Timer Problem

This thread is locked; no one can reply to it. rss feed Print
[Allegro] Timer Problem
Felipe Maia
Member #6,190
September 2005
avatar

I'm having a very odd problem. I'm wanting to do a fps check, something very simple, my problem is when I do install_int_ex(fpsControl, SECS_TO_TIMER(1));to get 1 tick per second, it just doesn't work, fpsControl is never called, what's wrong with this? Is allegro bugged or what? If so, why isn't it on the manual?

Onewing
Member #6,152
August 2005
avatar

Quote:

fpsControl is never called

What's your fpsControl function do?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Felipe Maia
Member #6,190
September 2005
avatar

Change 2 volatile int variables, it is never called, really, I've debugged and it never get's there, never at least in less than a minute.

Onewing
Member #6,152
August 2005
avatar

Could you show me a small snippet of your code. From what I've seen so far, it should be working.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Felipe Maia
Member #6,190
September 2005
avatar

1volatile int countFPS;
2volatile int lastFPS;
3 
4void fpsControl() {
5 lastFPS = countFPS;
6 countFPS = 0;
7}
8END_OF_FUNCTION(fpsControl);
9 
10void drawFrame() {
11//draw stuff
12 countFPS++;
13}
14 
15void init() {
16 LOCK_VARIABLE(lastFPS);
17 LOCK_VARIABLE(countFPS);
18 LOCK_FUNCTION(fpsControl);
19 install_int_ex(fpsControl, SECS_TO_TIMER(1));
20}

That's all it touches, I think the problem is the library and not the code, I'm gonna recompile everything and see.

Onewing
Member #6,152
August 2005
avatar

Everything looks good to me. However, I usually put the LOCK functions after the install_int_ex. Don't know if that'd make a difference. Did recompiling work?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Felipe Maia
Member #6,190
September 2005
avatar

I need to recompile allegro, delete everything and recompile from scratch, haven't got the time to do that now since I have to do a lot of things. I'll let you know when I do so.

EDIT: No, still not working, I've recompiled everything. Something odd happened though, the debug version did not self install and the lib is small than the release one, how is that possible???

EDIT2: Very very odd: if I do

install_int_ex(fpsControl, BPS_TO_TIMER(80));

it works ok, the function beeing called every now and then, but if I do install_int_ex(fpsControl, BPS_TO_TIMER(60)); It doesn't, it get's very odd.

EDIT3: I figured it out the problem, finally, the problem was that I was adding the function and then removing and then adding again.
something like

install_int_ex(fpsControl, BPS_TO_TIMER(1));
remove_int(fpsControl);
fpsControl, BPS_TO_TIMER(1));

I was removing it when the player would pause the game. Anyone know why this?

scriptX
Member #6,574
November 2005
avatar

Try:

install_int_ex(fpsCounter, BPS_TO_TIMER(1));

Onewing
Member #6,152
August 2005
avatar

Quote:

I was removing it when the player would pause the game.

You were manually removing it? I don't see why you would need to do that...

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Sirocco
Member #88
April 2000
avatar

Don't bother removing it. Just place something like this at the start of the timer routine:

If(flags.paused) { return; }

That way you don't have to muck about with installing/removing timers unnecessarily.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Felipe Maia
Member #6,190
September 2005
avatar

Quote:

You were manually removing it? I don't see why you would need to do that...

Not the fps, but the mainCounter needs to be removed on pauses, I think Sirocco's idea is good.

Thomas Harte
Member #33
April 2000
avatar

Seems obvious but nobody else seems to have asked - do you have install_timer() anywhere in your code? You should call that before install_int_ex or else expect undefined results.

zer0
Member #6,501
October 2005
avatar

Thomas: Allegro calls install_timer() automatically for you if you don't call it before installing any timers, so that shouldn't be a problem.

Thomas Harte
Member #33
April 2000
avatar

Quote:

Thomas: Allegro calls install_timer() automatically for you if you don't call it before installing any timers, so that shouldn't be a problem.

To directly quote the manual:

Quote:

int install_timer()

Installs the Allegro timer interrupt handler. You must do this before installing any user timer routines

(emphasis added)

So even if it says elsewhere that you don't need to then the best you can say is that there is no clear line and in my opinion you should err on the side of caution.

zer0
Member #6,501
October 2005
avatar

Yes, I agree that it is better coding practice to make a call to install_timer() first and "err on the side of caution".

Felipe Maia
Member #6,190
September 2005
avatar

I do, of course, the first call after allegro_init. I don't know exactly the problem the more obvious to me is that the library is not working as it should.

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

However, I usually put the LOCK functions after the install_int_ex. Don't know if that'd make a difference.

You're doing it wrong, and it will make a difference in DOS. In windows (and AFAIK linux and OSX too) it doesn't matter since the timers don't really run in an interrupt context, but rather in a separate thread. In DOS, however, they are interrupt routines, which means no virtual memory swap may occur while you're executing the code, hence the LOCK (which basically makes sure that all timer code and data are always in physical RAM). If you don't LOCK, and the timer code is swapped out, then the next time it is called you will crash.

Quote:

I do, of course, the first call after allegro_init. I don't know exactly the problem the more obvious to me is that the library is not working as it should.

Have you tried installing the timer api after setting a gfx mode? It is possible that allegro needs a window handle before doing anything useful with timers and threads and whatnot.

Quote:

I was removing it when the player would pause the game. Anyone know why this?

Since the timer runs in its own thread; maybe removing and installing it in a very short time doesn't give the other thread enough time to terminate, messing things up? Threads aren't my strong side, though, so I better STFU.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Onewing
Member #6,152
August 2005
avatar

Quote:

You're doing it wrong

Really? I went back and looked closer at the manual and noticed you are definitely right. The first time I read it, I mainly got "you need to lock your handler functions and variables" and "it should go in your init routine".

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Tobias Dammers
Member #2,604
August 2002
avatar

Think about it: From the moment on when you install a timer "interrupt", it is possible that such an interrupt occurs. Even if the very next statement is the lock statement, the interrupt can occur before that. That's why you need to make sure that everything is locked before you use it in an interrupt context. Again, this only applies to interrupt-driven timers like the ones used in the DOS version.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: