Jump to content
  • Advertisement
Sign in to follow this  
MikeRow

Breaking up main.cpp into parts help

This topic is 4579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been studing C++ for about a year now. (Steve Hellers book) I've learned enough to start putting together my first game. (a 3d chat room) I apply the things I learn from the book, to what I need in my game, but run into problems attempting to switch from "coding from scratch" to using an already made lib. I use Irrlicht for the graphics. (and currently for the collision as well) I have my map done, lit, textured, and ready to go. I've made my main.cpp and compiled it. It looks great, movement is good, and so on. Problem is, I still have to add a proper collision and networking lib to this, and my main.cpp is getting quite large and hard to read. What I want to do is to break up what I have into seperate cpp files. IE: main.cpp //game init, etc... Game.cpp //map files, doors, elevators, etc... Collision.cpp //apply all the collision here Network.cpp // etc... Sound.cpp // well, you get the hint My goal is to make this simple enough to expand as I need to. I want to expand on my little chatroom later down the road, but the current state of my main.cpp is just getting out of hand. All the tutorials, books, and other peoples code that I've looked at, seem to me like "reenventing the wheel". I don't want to rewrite the sound engine so that my main will look just like it does now. An example of adding audiere sound to an environment:
#include "audiere.h"

using namespace audiere;



//initialize audio 

   AudioDevicePtr audiereDevice; 
    OutputStreamPtr stream; 

    audiereDevice = OpenDevice(); 
    if (!audiereDevice) 
     return 1; 

    stream = OpenSound(audiereDevice.get(), "YOURSOUNDFILE.wav", true); 
     
    if (!stream) 
     return 2; 

    stream->setRepeat(true); 
    stream->setVolume(0.15f); // 15% volume 
    stream->play(); 
How could I take this and put it in it's own class so that I can just call it in my main.cpp? (this file would have all the different sounds listed by name, such as:
stream1=OpenSound(audiereDevice.get(), "YOURSOUNDFILE1.wav", true); 
stream2=OpenSound(audiereDevice.get(), "YOURSOUNDFILE2.wav", true); 
etc..... I don't want to rewrite audiere. I just want to be able to use it in my program by calling it's name. I just don't know how, and all attempts I've made just fail. Can anyone give me some pointers? I know this has been a long post, I do appreciate your reading, I just wanted to make sure to get my point across as to what I needed.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SiCrane
Does this article answer your questions?


Sort of. It cemented that I am on the right thought pattern as to how my game should be put together. :)

My biggest problem is "How to put what I have into indevidule classes"??

I'll take the code that I am actually using for sound (names changed)as an example.

//Sound.cpp (just an example cpp page)

#ifndef INC_SOUND_H
#define INC_SOUND_H

#include "audiere.h"

using namespace audiere;

class Sound

{

//initialize audio

InitSound(

AudioDevicePtr audiereDevice;
OutputStreamPtr stream1, stream2;

audiereDevice = OpenDevice();
if (!audiereDevice)
return 1;

stream1 = OpenSound(audiereDevice.get(), "media/sound1.wav", true);

if (!stream1)
return 2;

stream1->setRepeat(true);
stream1->setVolume(0.15f); // 15% volume
stream1->play();

stream2 = OpenSound(audiereDevice.get(), "media/sound2.mp3", true);

if (!stream2)
return 2;

stream2->setRepeat(false);
stream2->setVolume(0.25f); // 25% volume
stream2->play(); );
}

#endif /* INC_SOUND_H */


Would this be the proper way to do the cpp file? (This is emplimented in my game now and works properly, but it's in the one and only file I have thus far.My whole game is in 1 cpp file.) If so, what do I put in the header file?

InitSound(stream1) ??

[Edited by - MikeRow on December 31, 2005 12:30:03 PM]

Share this post


Link to post
Share on other sites
Usually, you split up your class over two files: a H-file (header) and a CPP-file (implementation). The header contains only the declaration of the class i.e. its structure, data and methods while the implementation contains the actual implementation.

As a simple example, see the following:

// Header file Sound.h
#ifndef _SOUND_H_
#define _SOUND_H_

// Class declaration
class CSound
{
// Members:
protected:

std::string m_sFilename;

bool m_bLoaded;

// Constructors:
(skipped)

// Methods:

// No implementation; just declaration.
void Play( void );

};

#endif



// Implementation file Sound.cpp
#include "Sound.h"

// Implementation of Play()
void CSound::Play( void )
{
if ( m_bLoaded )
...
}


Illco

Share this post


Link to post
Share on other sites
Ok, I'm trying to get this straight. The book I'm reading teaches how to code from scratch. I'm trying to convert what i've learned to using a premade lib.

So, in using what you stated above for the h file, I would put the following in my cpp??


// Implementation of Play()
void CSound::Play( void )
{
if ( m_bLoaded )


stream1 = OpenSound(audiereDevice.get(), "media/sound1.wav", true);

if (!stream1)
return 2;

stream1->setRepeat(true);
stream1->setVolume(0.15f); // 15% volume
stream1->play();
}


Share this post


Link to post
Share on other sites
Yes. Correct.

And, obviously, you'd put all the functions for that class into the same source file. This way, if you have even a halfway decent IDE (integrated development environment - like Visual C++ or Code::Blocks, or whatever), the source files only get compiled again when there is a change.

So, if you have 30 source files and make a change to only 1 of them, only that gets compiled again, and the IDE keeps track of the compiled versions of the other 29 source files, and links them all together. This greatly speeds up compiling.

Edit:: also remember to include the header file for that class at the top of the source file, otherwise the compiler will complain.

Share this post


Link to post
Share on other sites
Thankyou. Please bear with me, I'm still learning.
So, my h and cpp files should look like the following?


// Header file Sound.h
#ifndef _SOUND_H_
#define _SOUND_H_

// Class declaration
class CSound
{
// Members:
protected:

std::string m_sFilename;

bool m_bLoaded;

// Constructors:
(skipped)

// Methods:

// No implementation; just declaration.
void Play( void );

};

#endif

////////////////////END OF H FILE/////////////////////////////////////////////

///////////////////BEGINNING OF CPP/////////////////////////////////////////
#ifndef INC_SOUND_H
#define INC_SOUND_H
// Implementation file Sound.cpp
#include "Sound.h"
#include "audiere.h"

using namespace audiere;

//initialize audio

AudioDevicePtr audiereDevice;
OutputStreamPtr stream;


// Implementation of Play()
void CSound::Play( void )
{
if ( m_bLoaded )
audiereDevice = OpenDevice();
if (!audiereDevice)
return 1;

stream1 = OpenSound(audiereDevice.get(), "media/HWentro.mp3", true);

if (!stream1)
return 2;

stream1->setRepeat(false);
stream1->setVolume(0.25f); // 25% volume
stream1->play();


stream2 = OpenSound(audiereDevice.get(), "media/Quake.wav", true);

if (!stream2)
return 2;

stream2->setRepeat(true);
stream2->setVolume(0.05f); // 5% volume
stream2->play();

}
#endif
//////////////////////////END OF CPP///////////////////////////////

Then in my main.cpp, I just call:
void Play( void );
???


Or would I call:
CSound::void Play(); ??

Share this post


Link to post
Share on other sites
Mostly, yes. You don't need the #ifndef and #define in the source file.

And, to call the CSound::Play function, unless it is a static function, which you probably don't care about or need to know about right now, you have to create a sound object and call the function from there.


CSound sound_obj;

// blah blah, more code

sound_obj.play();


Something like that.

Share this post


Link to post
Share on other sites
Ok, I think I get what you are saying. I'll give a few things a try.
creating classes has been a sticking point for me. I really don't understand how to write them. I'm getting there tho. Your help has been great. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!