Static function pointer questions
Moderator: Coders of Rage
Static function pointer questions
Hey guys.
i finally started my process to convert my psp game (oslib) to pc (sfml).... i'm creating my multiplataform lib using sfml and oslib
My objective is to debug de code properly, so when i was watching the gyro i found he uses a static pointer function, i was thinking about just a static function.
What's the difference, it is faster, easier to implement?
Thanks, sorry... but my english is a deepshit.
i finally started my process to convert my psp game (oslib) to pc (sfml).... i'm creating my multiplataform lib using sfml and oslib
My objective is to debug de code properly, so when i was watching the gyro i found he uses a static pointer function, i was thinking about just a static function.
What's the difference, it is faster, easier to implement?
Thanks, sorry... but my english is a deepshit.
Re: Static function pointer questions
Brilliant.Esler wrote: Thanks, sorry... but my english is a deepshit.
I think the reason he uses a function pointer, is so that he can have one function to XXXX, but it may point to XXXX_DC or XXXXX_WIN and so on.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Static function pointer questions
So, i can use something like
#ifdef PSP
// assign the pointer to the correct function
#endif
and i can use the same "function", but it points to diferent functions in each specific case...
lol... briliant.. XD
Thnaks
#ifdef PSP
// assign the pointer to the correct function
#endif
and i can use the same "function", but it points to diferent functions in each specific case...
lol... briliant.. XD
Thnaks
Re: Static function pointer questions
incase you wanted to see some code, note im not implying that falco or anyone does it this way, this is a very very simple case just to show you, also, that does not do any error checking, if you feed it anything other than '+' or '-' it will crash.Esler wrote:So, i can use something like
#ifdef PSP
// assign the pointer to the correct function
#endif
and i can use the same "function", but it points to diferent functions in each specific case...
lol... briliant.. XD
Thnaks
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int add(const int a, const int b)
{
return a+b;
}
int sub(const int a, const int b)
{
return a-b;
}
typedef int(*FP)(const int, const int);
FP op(const char op)
{
if(op == '+')
return add;
if(op == '-')
return sub;
return NULL;
}
int main (int argc, char * const argv[])
{
printf("%d\n", op('+')(3,4)); // 7
printf("%d\n", op('-')(3,4)); // -1
return 0;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Static function pointer questions
I would have though basic ifndef guards would do fine, I thought it was becasue Marcel needed to print debug output aswell due to it being the same codebase as the engine?avansc wrote:Brilliant.Esler wrote: Thanks, sorry... but my english is a deepshit.
I think the reason he uses a function pointer, is so that he can have one function to XXXX, but it may point to XXXX_DC or XXXXX_WIN and so on.
But on topic:
1- The is a new thing called code tagsEsler wrote:So, i can use something like
#ifdef PSP
// assign the pointer to the correct function
#endif
and i can use the same "function", but it points to diferent functions in each specific case...
lol... briliant.. XD
Thnaks
2- Why would you want a function pointer for this? why not just use preprocessor directives.
Lets say your program looks like this
Code: Select all
int main() {
InitializeCrap();
for(;;) {
ClearBuffer();
FlipBuffer();
}
}
Code: Select all
#ifdef PC
void InitializeCrap()
{
//Do stuff for PC
}
void ClearBuffer()
{
// Do stuff for PC
}
void FlipBuffer()
{
//Do stuff for PC
}
#endif
#ifdef PSP
void InitializeCrap()
{
//Do stuff for PSP
}
void ClearBuffer()
{
// Do stuff for PSP
}
void FlipBuffer()
{
//Do stuff for PSP
}
#endif
And then all you have to do is define PC or PSP, I don't see the need for function pointers, Perhaps I'm misunderstanding the question?
Re: Static function pointer questions
There are some instances where you would HAVE to do conditional compilation, but there are instances where a function_pointers is needed. I dont know if he uses callbacks, functors, and on and on and on. its 6 of one, half dozen of the other.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Static function pointer questions
i'm creating my classes for pc and psp, i call specific files using preprocessor.
I was thinking to use a static function to debug my code and create a screen to debug on psp.
i'm doing this, because my game have a lot of files and lines of code and i stopped to dev, now i have some memory problem and it is a hell to debug.
I was thinking to use a static function to debug my code and create a screen to debug on psp.
i'm doing this, because my game have a lot of files and lines of code and i stopped to dev, now i have some memory problem and it is a hell to debug.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Static function pointer questions
Yeah, you should do what avansc and N64 are recommending. Our "static function pointer" had nothing to do with "calling certain functions for certain platforms". It had to do with being able to dynamically register (and even change, not that we'd want to), a function to be called at RUN-TIME.
What you have to realize is that what you're trying to do is a decision that can be made at COMPILE TIME. You know which copy of the function should be compiled--the PC or the PSP one. There is no reason to waste additional resources and have to make a decision at runtime as to which to use.
Honestly, do as much as you can at compile-time. The more you can do, the more efficient your code will be.
And don't forget, that if these are critical system calls, being forced to invoke them via a function pointer is an extra level of indirection (late binding), which WILL incur performance overhead. Not much at all, BUT if that's important and is getting called every frame... it does start to matter.
What you have to realize is that what you're trying to do is a decision that can be made at COMPILE TIME. You know which copy of the function should be compiled--the PC or the PSP one. There is no reason to waste additional resources and have to make a decision at runtime as to which to use.
Honestly, do as much as you can at compile-time. The more you can do, the more efficient your code will be.
And don't forget, that if these are critical system calls, being forced to invoke them via a function pointer is an extra level of indirection (late binding), which WILL incur performance overhead. Not much at all, BUT if that's important and is getting called every frame... it does start to matter.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Static function pointer questions
Define "static function." This term doesn't really mean a whole lot in the C/++ world, where each "function" is already at global scope--thus already being static.Esler wrote:i'm creating my classes for pc and psp, i call specific files using preprocessor.
I was thinking to use a static function to debug my code and create a screen to debug on psp.
i'm doing this, because my game have a lot of files and lines of code and i stopped to dev, now i have some memory problem and it is a hell to debug.
printf() and cout are static member functions that you can use to debug?
I think what you mean is to use a static MEMBER FUNCTION/METHOD of a class that logs your debug output to a file or an internal buffer. If this is the case, go for it. I do agree.
You can either implement it with everything being static (if the static print function has to access a buffer or filestream, those need to be static as well), OR you have my blessing to use the only good excuse in fucking existence to use the singleton pattern...
Re: Static function pointer questions
Yes you're correct.
I'm trying to do the same thing you did(IO ASSET DEBUG), but i can do just using static methods.
I'm trying to understand why you use static function pointer instead of using just static methods.
Can you help me to understand why?
Sorry, but i am a little bit slow.
Thanks;
I'm trying to do the same thing you did(IO ASSET DEBUG), but i can do just using static methods.
I'm trying to understand why you use static function pointer instead of using just static methods.
Can you help me to understand why?
Sorry, but i am a little bit slow.
Thanks;
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Static function pointer questions
Yeah, no problem. I have my own debug system in the engine that essentially printfs to the console (and uses some special debug prints for Dreamcast/PSP) and shits the log to debug.txt via ofstream.Esler wrote:Yes you're correct.
I'm trying to do the same thing you did(IO ASSET DEBUG), but i can do just using static methods.
I'm trying to understand why you use static function pointer instead of using just static methods.
Can you help me to understand why?
Sorry, but i am a little bit slow.
Thanks;
MARCEL and the toolkit on the other hand capture this log, and put it into some sort of pretty little QT table so that you can view the error log later on at runtime.
It's to abstract the prototype of the debug system away from its implementation. They both take a const char * and a flag... but they do two completely different things with it.
Re: Static function pointer questions
Gyro probably cleared most things up but you do realize that this would be a complete waste of time without a AssetIO system like he's implemented for the Engine/Toolkit. If you want debug output then just have debug class that writes to a console/file.
When he says "AssetIO" he means "This code exists in two completly diffrent peices of software"
When he says "AssetIO" he means "This code exists in two completly diffrent peices of software"