Writting Callback functions?
Hi I want to write a similar functions to those of the GLUT library of OpenGL...
Bassicly, I want to pass a function to another one and have it called whene ver an event hppens just like GLUT example.
GLUT has on size method and you can pass to it you viewport function to resize it properley, can somebody point me to the right direction...
Thanx
Hardcore Until The End.
Hi,
I'm not overly familiar with GLUT or OpenGL so I'm not sure in what context you want to use the function pointer. I can still help you with the callback though
Say you have this function:
you would make a function pointer to it by declaring the following:
If you have to write the above code everytime you wanted to declare a function pointer it would be a pain. That is why you can do a typedef:
Then you could create a function pointer variable like so:
Much easier eh?
Here is a simple working example:
Just remember something:
MYFUNCTIONPTR can be used to point to anything that has the void Function(void) signature. This means that if you had a function named void Function2(void*, int) and assign its address to a MYFUNCTIONPTR variable you will get an error - most likely a stack overflow.
Also do not forget about operator precedence when typedef'ing your function pointers:
void *(MYFUNCTIONPTR)(void) and void (*MYFUNCTIONPTR)(void) are NOT equivalent. The first one is a function that takes no parameters and returns a void POINTER. The second one is a function pointer that returns nothing and takes nothing.
I hope this answers your question.
Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on November 23, 2000 9:07:43 PM
I'm not overly familiar with GLUT or OpenGL so I'm not sure in what context you want to use the function pointer. I can still help you with the callback though
Say you have this function:
void MyCallback(void);
you would make a function pointer to it by declaring the following:
void (*MyCallback)(void) pFuncPtr;
If you have to write the above code everytime you wanted to declare a function pointer it would be a pain. That is why you can do a typedef:
typedef void (*CallbackPtr)(void);
Then you could create a function pointer variable like so:
CallbackPtr pPtr;
Much easier eh?
Here is a simple working example:
#include <windows.h>#include <iostream>typedef void (*MYFUNCTIONPTR)(void);void MyFunctionFoo(void){ using std::cout; using std::endl; cout << "MyFunctionFoo says, \"Fool on the Hill\"." << endl;}void MyFunctionFoo2(void){ using std::cout; using std::endl; cout << "MyFunctionFoo2 says, \"Hello, Goodbye\"." << endl;}DWORD GraphicsLibraryFunction_DoSomething(MYFUNCTIONPTR pfn){ Sleep(1000); pfn(); // this is how you use the function pointer return 0;}int main(){ // passing a function name without the parenthesis passes // the address of the function. Since MyFunctionFoo is a // global/static function this is OK. GraphicsLibraryFunction_DoSomething(MyFunctionFoo); // another way you could do this is to create a pointer // variable and assign the address of the function to it MYFUNCTIONPTR pFunc; pFunc = MyFunctionFoo; GraphicsLibraryFunction_DoSomething(pFunc); pFunc = MyFunctionFoo2; GraphicsLibraryFunction_DoSomething(pFunc); return 0;}
Just remember something:
MYFUNCTIONPTR can be used to point to anything that has the void Function(void) signature. This means that if you had a function named void Function2(void*, int) and assign its address to a MYFUNCTIONPTR variable you will get an error - most likely a stack overflow.
Also do not forget about operator precedence when typedef'ing your function pointers:
void *(MYFUNCTIONPTR)(void) and void (*MYFUNCTIONPTR)(void) are NOT equivalent. The first one is a function that takes no parameters and returns a void POINTER. The second one is a function pointer that returns nothing and takes nothing.
I hope this answers your question.
Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on November 23, 2000 9:07:43 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
November 24, 2000 09:18 PM
How about for multiple parameters and also, lets say I want to pass a method of an object...
Example...
I have a frame work class, which more or less Initializes windows and has the event loop etc...
Now in my frame work class I have a method called onResize, witch is called by the WM_RESIZE event...
Now I have another class clalled OpenGL, whitch handles some bassic openGL stuff. This class contains a method called viewportResize(int x, int y).
In my init section I want to be bale to do something like this...
FrameWork *frameWork;
OpenGL *OpenGL;
WINAPI winmain(......)
{
// Init stuff
frameWork = new FrameWork(appInstance);
openGL = new OpenGL();
frameWork->createWindow(....)
openGL->Init();
frameWork->onResize(openGL->vieportResize);
// Main Loop
frameWork->loop(gameMain);
}
void gameMain()
{
// Lots and Lots of blood and violence.
}
So here actually there''s 2 callback''s and obviously even more....
Thanx.
Example...
I have a frame work class, which more or less Initializes windows and has the event loop etc...
Now in my frame work class I have a method called onResize, witch is called by the WM_RESIZE event...
Now I have another class clalled OpenGL, whitch handles some bassic openGL stuff. This class contains a method called viewportResize(int x, int y).
In my init section I want to be bale to do something like this...
FrameWork *frameWork;
OpenGL *OpenGL;
WINAPI winmain(......)
{
// Init stuff
frameWork = new FrameWork(appInstance);
openGL = new OpenGL();
frameWork->createWindow(....)
openGL->Init();
frameWork->onResize(openGL->vieportResize);
// Main Loop
frameWork->loop(gameMain);
}
void gameMain()
{
// Lots and Lots of blood and violence.
}
So here actually there''s 2 callback''s and obviously even more....
Thanx.
typedef void (CClass::*MethodPtr)();
But that''s not want you''d want to do. You''d want to create a base class CFrameWork, and make all of the functions you''d want as callbacks, virtuals, or even every function virtual. Then when a coder wanted to use your framework, they would derive a new class, say CMyFrameWork : public CFrameWork, and override the appropreite(sp) methods to do what they wanted (and often call the base class function for that method as well).
ie if CFrameWork had an virtual OnIdle() method, you could over ride that class and have it perform you''re games idle work.
Even if you did get the method callsback working, it wouldn''y be any faster.
You could also use MFC, which already has all this implemented for you. Of course it wouldn''t be portable then... If you want a decent OOD game engine, check out www.auran.com, llok for something called Jet.
But that''s not want you''d want to do. You''d want to create a base class CFrameWork, and make all of the functions you''d want as callbacks, virtuals, or even every function virtual. Then when a coder wanted to use your framework, they would derive a new class, say CMyFrameWork : public CFrameWork, and override the appropreite(sp) methods to do what they wanted (and often call the base class function for that method as well).
ie if CFrameWork had an virtual OnIdle() method, you could over ride that class and have it perform you''re games idle work.
Even if you did get the method callsback working, it wouldn''y be any faster.
You could also use MFC, which already has all this implemented for you. Of course it wouldn''t be portable then... If you want a decent OOD game engine, check out www.auran.com, llok for something called Jet.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Look for MFC Fog in the Dx samples for an MFC&Dx example
download Jet from www.auran.com for an OOD engine example.
download Jet from www.auran.com for an OOD engine example.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement