Page 1 of 1

Static function pointer questions

Posted: Tue Jan 18, 2011 10:21 am
by Esler
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.

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 10:44 am
by avansc
Esler wrote: Thanks, sorry... but my english is a deepshit.
Brilliant.

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.

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 10:54 am
by Esler
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

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 11:45 am
by avansc
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
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.

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;
}

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 11:58 am
by N64vSNES
avansc wrote:
Esler wrote: Thanks, sorry... but my english is a deepshit.
Brilliant.

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.
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?

But on topic:
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
1- The is a new thing called code tags ;)

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();
     }
}
To keep this compatible you could have the declarations like so

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

Posted: Tue Jan 18, 2011 12:05 pm
by avansc
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.

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 12:29 pm
by Esler
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.

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 1:33 pm
by Falco Girgis
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. :)

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 1:38 pm
by Falco Girgis
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.
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.

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

Posted: Tue Jan 18, 2011 4:32 pm
by Esler
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;

Re: Static function pointer questions

Posted: Tue Jan 18, 2011 4:40 pm
by Falco Girgis
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;
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.

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

Posted: Tue Jan 18, 2011 4:47 pm
by N64vSNES
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"