Multiple of the same function?
Moderator: Coders of Rage
- MadPumpkin
- Chaos Rift Maniac
- Posts: 484
- Joined: Fri Feb 13, 2009 4:48 pm
- Current Project: Octopia
- Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
- Programming Language of Choice: C/++,Java,Py,LUA,XML
- Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk
Multiple of the same function?
Alright I am making an engine and API. I may be using different I/O such as GLUT, SDL, Win32 depending on the game I'm working on at the time, but I don't want to make an entirely different library and compile it just to change 3 lines of 5 functions. So does anyone know a way that I can put something in my code so that maybe something like defining a constant will make the compiler use different version of the function so that I can have a Win32 specific one, a SDL specific one and a GLUT specific one?
Much appreciated, Josh.
Much appreciated, Josh.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock



For God so loved the world that he blessed the thugs with rock



- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Multiple of the same function?
Code: Select all
#ifdef MP_WIN32
void DoStuff()
{
//Win32 code
}
#endif
#ifdef MP_SDL
void DoStuff()
{
//SDL code
}
#endif
#ifdef MP_GLUT
void DoStuff()
{
//GLUT code
}
#endif
"MP" is for MadPumpkin.. call the variables whatever you like.

Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MadPumpkin
- Chaos Rift Maniac
- Posts: 484
- Joined: Fri Feb 13, 2009 4:48 pm
- Current Project: Octopia
- Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
- Programming Language of Choice: C/++,Java,Py,LUA,XML
- Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk
Re: Multiple of the same function?
Damn, exactly what I was looking for, I knew it had something to do with this because I recalled it from something Falco said about LibVorbis in a video. Thanks man great help xDdandymcgee wrote:Then just put "#define MP_SDL" before including any of the files set up like this.Code: Select all
#ifdef MP_WIN32 void DoStuff() { //Win32 code } #endif #ifdef MP_SDL void DoStuff() { //SDL code } #endif #ifdef MP_GLUT void DoStuff() { //GLUT code } #endif
"MP" is for MadPumpkin.. call the variables whatever you like.
I guess it makes plenty of sense after looking at the fact that I put
Code: Select all
#define _WHATEVERSHIT_H_
#ifndef _WHATEVERSHIT_H_
#endif
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock



For God so loved the world that he blessed the thugs with rock



- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Multiple of the same function?
No problem and yes, this is exactly how header include guards work.MadPumpkin wrote: Damn, exactly what I was looking for, I knew it had something to do with this because I recalled it from something Falco said about LibVorbis in a video. Thanks man great help xD
I guess it makes plenty of sense after looking at the fact that I putIn every header anyways. So what is the difference between #ifdef and #ifndefCode: Select all
#define _WHATEVERSHIT_H_ #ifndef _WHATEVERSHIT_H_ #endif
#ifdef means if defined, #ifndef means if not defined. So actually, your include guard example is backwards and that code will never be included.

Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MadPumpkin
- Chaos Rift Maniac
- Posts: 484
- Joined: Fri Feb 13, 2009 4:48 pm
- Current Project: Octopia
- Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
- Programming Language of Choice: C/++,Java,Py,LUA,XML
- Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk
Re: Multiple of the same function?
Soo...
basically says,
I'm confused then, shouldn't it only do the code if it WAS defined? Because with the #ifndef, it still includes my class.
Code: Select all
#define _HEADER_H_
#ifndef _HEADER_H_
class someshit{
};
#endif
Code: Select all
Define header as _HEADER_H_
If it wasn't defined, then do all of this code
end if
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock



For God so loved the world that he blessed the thugs with rock



- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Multiple of the same function?
I told you, your example header guard is backwards.MadPumpkin wrote: I'm confused then, shouldn't it only do the code if it WAS defined? Because with the #ifndef, it still includes my class.
Code: Select all
#ifndef _HEADER_H_
#define _HEADER_H_
class someshit{
};
#endif
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MadPumpkin
- Chaos Rift Maniac
- Posts: 484
- Joined: Fri Feb 13, 2009 4:48 pm
- Current Project: Octopia
- Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
- Programming Language of Choice: C/++,Java,Py,LUA,XML
- Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk
Re: Multiple of the same function?
Thanks great help man.dandymcgee wrote:I told you, your example header guard is backwards.MadPumpkin wrote: I'm confused then, shouldn't it only do the code if it WAS defined? Because with the #ifndef, it still includes my class.If not defined, define it and include header. If already defined skip to endif (do not include header twice).Code: Select all
#ifndef _HEADER_H_ #define _HEADER_H_ class someshit{ }; #endif
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock



For God so loved the world that he blessed the thugs with rock


