Please sample code using multiple libraries?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Il_Pax
Posts: 7
Joined: Wed Feb 16, 2005 6:53 pm

Please sample code using multiple libraries?

Post by Il_Pax »

Hi, now a new problem. My code is getting kind of spaghetti, having initialization of openal + it's buffers + the sounds it must load, and then also my graphics, which are now somewhat in the count of 29, and with all the parameters, it might get kind of weird (still missing some more houses). I want to load elements separately beacause i believe they take less overhead on the system (pretty much using the same code as in the collision example, where 3 instances of the faerie are loadaed). That way i can have 1 house and then instance it 13 times or so, then a factory and do the same...i believe the way to do more readable and friendly code is to:

a) initialize graphics (main code) and load houses (external file)
b) initialize sound (main code) and laod sound (external file)
d) begin a loop, in each cycle check for logic and update
e) on exit kill sound, kill grapchis, close

If that's the kind of logic, can somebody please share some code showing:

1- Openl al implementation ? (can it be used form an external file?)
2- How to load object from another file (like an include file wchich has the code to load several objects). This possible or am i way off?

I know this is my own limitation beacause i'm not yet that fluent in c++, but some code will be really appreciated, thanks.
cartoonit
Posts: 286
Joined: Mon Nov 15, 2004 6:36 pm

Post by cartoonit »

If I understand properly you want to be able to do something like:

Code: Select all

#ifndef _THIS_CLASS_
#define _THIS_CLASS_

class MyClass
{
  MyClass();
};
#endif
This definition is saved in a header file called myclass.h

Code: Select all

#include "myclass.h"
MyClass::MyClass()
{ }
This the main functionality of the code therefore is saved in a .cpp called myclass.cpp.

So now say you have a main game class as follows, but want to include the other object myclass that you've just created??:

Code: Select all

#ifndef _MAIN_GAME_
#define _MAIN_GAME_

#include "myclass.h" //simply includes the definition

class TMainGame
{
public:
  LoadLevel();
  LoadData();
  InitSound();
private:
  MyClass* bob;
 };
#endif
Now once you've created a new instance of my class:

Code: Select all

TMainGame::TMainGame()
{
  bob = new MyClass();
}
All the functionality of the myclass object can be used.

The #ifndef and #define, prevent multiple inclusions of the same header, therefore only one definition cane be extracted by the precompiler, (that was on my exam last yr u know :D). I'd go in to more detail, but I did nearly 2 pages in the exam or more!!! Sometimes '#pragma once' is used as well but this to me seems like a MS implementation, and I don't like it but thats personal preference....

The main game then can contain all the objects that you want to be called, this keeps the code clean, but don't expect the main game .cpp to be small. I hope I've explained a few things to you, what your asking is to be taught the entire language in a way.... If you need any more info, I'll try and enlighten further...

In my main game application i have some of the following, I'm not saying this is clean, because I would actually think about having all the drivers for Irrlicht in a seperate class now:

Code: Select all

class TMainGame
{
  public:
  TMainGame(/*parameters but i can't remember hardly any of them*/);
  ~TMainGame();
   Run();
   LoadData(); 
   AddEnemyToLevel();
   AddWeaponToLevel(); 
   
private:
  ISceneManager* mp_Smgr;
  IrrlichtDevice*  mp_IrrDevice;
  VideoDriver*    mp_VideoDriver;
};
I then initialise all those within my constructor of the class, and call other methods which are applicable, for my game.

Oh another thing as well, if you want to include multiple libraries, then I usually place the #include's for those libraries in a global header which is available within every single file that i create, but once again this is personal preference...

Wow look at this post :lol: :lol:
Post Reply