Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » using allegro timers in C++ programs

Credits go to da_flo, decsonic, Fladimir da Gorf, and Marco Radaelli for helping out!
This thread is locked; no one can reply to it. rss feed Print
using allegro timers in C++ programs
Alexis FREY
Member #5,164
October 2004
avatar

Hello to everybody
I'm rather new in programming with allegro using DJGPP.
I've encountered a little problem which have forced me to convert my C++ programs into C in order to be able to use allegro timers but I would like to be able to switch again to C++. In C the usual code is :
volatile int counter = 0;
void my_callback_func(){ counter++;} END_OF_FUNCTION (my_callback_func);
which is after following in the main fonction by the locking of the int and fonction :
LOCK_FUNCTION (my_callback_func);
LOCK_VARIABLE (counter);
In C++, locking the function create the error in compilation "invalid conversion from 'void (*)()' to 'void*'. The vivace documentation isn't very clear about that and seems to say not to write the END_OF_FUNCTION, but I have tried and but same error appears with another one which says that it doesn't found the 'my_callback_func_end'.
It would be very nice if you know something about this issue to answer my message. (I apologize for my textual expression in english)
I thank you in advance
Truly yours
afrey

- - - - - - - - - - - - - - - - - -
monitor's radiations fade my genes

Fladimir da Gorf
Member #1,565
October 2001
avatar

Have you tried the following?
LOCK_FUNCTION((void *) my_callback_func );

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Alexis FREY
Member #5,164
October 2004
avatar

First Fladimir da Gorf, I want to thank you, I haven't expected such a short-in time answer.
Your suggestion is good to counter the compilation error "invalid conversion from 'void(*)()' to 'void*'.
However, it doesn't solve completely the problem.
With your suggestion, if I keep the "END_OF_FUNCTION (my_callback_func);" after the declaration and definition of the function, the code is entirely clean but the compilation failed with the error :" c:/djgpp/tmp/ccf7xMoz.o(.eh_frame+0X11) : essai.cpp : undefined reference to '___gxx_personality_vO' collect 2 : Id returned 1 exit status.
If I don't keep the "END_OF_FUNCTION (my_callback_func);", the code is not clean and return the error on the line of the LOCK_FUNCTION, 'my_callback_func_end' undeclared (first use this function).
Here is a cut and paste about what is written in vivace documentation :
-----------------------------------
Here's an example function for C:
volatile int counter = 0; /* We'll increase this in the callback, so it must be volatile. */
void my_callback_func()
{
counter++; /* Nice and simple */
} END_OF_FUNCTION (my_callback_func); /* Note the syntax here */
Note that the callback function must be very simple. It must not take a long time, it must not call any C library functions, it must not do anything fancy like calling DOS routines. If it calls Allegro routines, they must be reentrant ones that obey these same rules.
If we were using C++, we'd have to change the function definition to:
void my_callback_func(...)
otherwise we'd get an error.
-----------------------------------
this documentation is really not clear because we don't understand what mean "void my_callback_func(...)"
Here is the C program I have written to test it and which works in C (to have an overview) after I compile it with the usual command:
gcc essai.c -o essai.exe -lalleg
-----------------------------------
#include <allegro.h>
volatile int counter=0;
void my_timer_handler()
{
counter++;
} END_OF_FUNCTION (my_timer_handler);
int main(void)
{
LOCK_VARIABLE(counter);
LOCK_FUNCTION(my_timer_handler);
allegro_init();
install_timer();
install_keyboard();
set_gfx_mode(GFX_VGA,320,200,0,0);
install_int(my_timer_handler, 1000);
textout_centre(screen,font,"Ready. Beep.",160,100,255);
readkey();
remove_int (my_timer_handler);
allegro_exit();
return 0;
}
-----------------------------------
So you have all my elements. Hope it will give you a new idea.
Friendly
afrey

- - - - - - - - - - - - - - - - - -
monitor's radiations fade my genes

da_flo
Member #1,907
February 2002
avatar

Quote:

he compilation failed with the error :" c:/djgpp/tmp/ccf7xMoz.o(.eh_frame+0X11) : essai.cpp : undefined reference to '___gxx_personality_vO' collect 2 : Id returned 1 exit status.

It is a linker error, and I think this occured because you forgot to link with the standard C++ library ( in C libc is automatically taken into account but in C++ you need to explicitely link with libstdc++ ).

Use gcc essai.cpp -o essai.exe -lalleg -lstdc++ to compile.

Note that you can also use g++ instead of gcc, which I think is a good habit when you know you are doing C++, even though gcc will call g++ if necessary without saying you a word ;D

decsonic
Member #4,150
December 2003

Please some indentation in your post and use the code tags ;) (in the future :P) .

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

Marco Radaelli
Member #3,028
December 2002
avatar

I've compiled your code.
Well, actually I get the same error only if I compile with gcc and the file extension is .cpp. Changing it to .c or using g++ resolves the problem.

decsonic said:

Please some indentation in your post and use the code tags (in the future ) .

He means this :)

Alexis FREY
Member #5,164
October 2004
avatar

Thank you da_flo as well as Marco Radaelli.
by using : gpp essai.cpp -o essai.exe -lalleg instead of gcc, it works perfectly. This will enable some major improvement for the maintenance of my programs.
Sorry decsonic, I'm not yet accostumed with all the features in posting message for underlining the important points. I have seen by listing the other posts, that there are a lot of possibilities, but at first glance I don't even see how to change the color of the text :-/
Anyway, thank you to all of you, you help me to solve an old mystery :)

- - - - - - - - - - - - - - - - - -
monitor's radiations fade my genes

Marco Radaelli
Member #3,028
December 2002
avatar

Alexis FREY said:

I'm not yet accostumed with all the features in posting message for underlining the important points

It takes some time for everyone. While waiting, click me :)

Welcome to the Allegro community 8-)

Alexis FREY
Member #5,164
October 2004
avatar

Marco Radaelli said:

While waiting, click me :)

OK, thank you Marco, I haven't understand first when you have written it, but now I can see, it just works like html code.
Thank you. Have a nice day. ;)

- - - - - - - - - - - - - - - - - -
monitor's radiations fade my genes

Go to: