Skip to main content
gamedev.net
🔒 Locked

Calling External Functions???

Started by siliconsurfer 1 reply 350+ views
Original Post
siliconsurfer
siliconsurfer
Hi, I have a class called engine, which does things like initalises the graphics and sound clases. So In my main program I include engine.h And I create a object called engine. Through this I can access the functions in display and sound by doing theEngine->Sound->play(file); I now want to add other functions to the engine class such as collide. Collide checks to see if two sprites have a collision. But I was wondering is it possible to write these functions, so that rather than writing theEngine->spriteCollide(sprite1, sprite2); I could just write include engine.h spriteCollide(sprite1, sprite2); I just want to be able to use the function as if it had been defined in the main file. The reason being is that I don't think it really is part of a engine object, although possilby I should just use a differnt class?? I looked at some files and saw that the "extern" keyword is used, is that just for using functions written in a external language? I tried writting extern "C++" before my function in the engine.h file, but it didn't work. Any ideas?
DarkSlayer
DarkSlayer
// get the global *ptrObject pointer, so you can use it
extern Object *ptrObject;

just use the extern in your .cpp file where you reference to your global function

I guess the code is..
extern void funct(int i, char c);
Fil
Fil
If you want a set of function (and you have no state data) you can use a class with all the methods static. For example:

class CMyFunctions
{
static void FunctionOne(...);
static int FunctionTwo();
...
};

The calls are:
CMyFunctions::FunctionOne(parameters);
int x=CMyFunctions::FunctionTwo();

But I think you are writing in c++ but thinking in c, so your object are not very usefull and you prefer functions (I may be wrong :)). Anyway, I'd write 2 classes (singletons): one for the graphic engine and the other for the sound engine. The first will have its graphics data (for example, info about fps, # of triangles/sec, camera position, last rendering states,...). The second has sound data (current wav files loaded, user volume preferences, playing musics,...).
That way you will have a better source (more clear). So for collision another class maintains info about your scene and moving models, so it can reply very fast to collision queries. The answer could be not only true or false (there's a collision or not), but can give other collision information, if you like (eg. colliding triangles and/or other information usefull to a physic engine, for example).

Hope this helps.
Fil (il genio)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.