Jump to content
  • Advertisement
Sign in to follow this  
Waterlimon

How to properly organize parts of my "program"?

This topic is 2335 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

Im wondering whats the best way to organize the classes in my program so that there is no clashing names and stuff.

I have the classes split in .h and .cpp files each with its own pair of files, and have a few folders i sort them to (like IO and Math). But im wondering how to organize the actual classes and those, not the files.

I tried having a .h file that looked something like


namespace MyEngineModuleClassManagerThings
{
#include "this.h"
#include "that.h"
}


sometimes including other similiar headers, giving me something like

MyEngine::Math::Vector vec;

to declear a vector.

However, it was annoying to set up properly and i feel that its a bit messy.

How should i put the classes and such in a nice organized tree like that, including where to put each piece of code?

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Structuring something in a personal project is somewhat.. well.. personal tongue.png

So yeah, personally I try to stick with just 1 namespace that makes sense to me, in my case, I named my engine "Reloadead_ Engine" and use "Rld" as a namespace for all the components. Using extra namespaces might make it more clear what you are using, but they can also clutter up your syntax with a lot of namespace prefixes. You could of course use the using keyword, but I'm not always a fan of that. But that's personal.

To give you an example on how I manage my header files: (not gonna bother with fixing the spacing when posting code..., you get the idea)

#ifndef _MATERIAL_H
#define _MATERIAL_H
namespace Rld
{
class Material
{
public:
Material();
~Material();
void ApplyCurrentMaterial();
const std::string& GetName() const {return m_Name;}
const int& GetID() const {return m_ID;}
const colorf& GetAmbient() {return m_Ambient;}
const colorf& GetDiffuse() {return m_Diffuse;}
const colorf& GetSpecular() {return m_Specular;}
const float& GetShininess() {return m_Shininess;}
const int& GetDiffusemapID() const {return m_DiffuseMapID;}
void SetName(const std::string& a_Name) {m_Name = a_Name;}
void SetID(const int& a_ID) {m_ID = a_ID;}
void SetAmbient(const colorf& a_Ambient) {m_Ambient = a_Ambient;}
void SetDiffuse(const colorf& a_Diffuse) {m_Diffuse = a_Diffuse;}
void SetSpecular(const colorf& a_Specular) {m_Specular = a_Specular;}
void SetShininess(const float& a_Shininess) {m_Shininess = a_Shininess;}
void SetDiffuseMap(const int& a_MapKd_ID) {m_DiffuseMapID = a_MapKd_ID;}
private:
String m_Name;
int m_ID;
colorf m_Ambient;
colorf m_Diffuse;
colorf m_Specular;
float m_Shininess;
int m_DiffuseMapID;
};
};
#endif

Share this post


Link to post
Share on other sites
Personally I don't think [font=courier new,courier,monospace]MyEngine::Math::Vector[/font] is particularly messy. As a matter of fact it's very similar to the way I use namespaces myself (Project::Module::Class). As Reloaded_ suggested you can always use the [font=courier new,courier,monospace]using [/font]keyword if you don't want to write the entire namespace path each time. In this case you can still add the closest namespace if you encounter a conflict/ambiguity:

using namespace MyEngine;
using namespace MyEngine::Math;
using namespace MyEngine::Containers;

Matrix44 m1; // MyEngine::Math::Matrix44
Math::Vector v1; // MyEngine::Math::Vector
Containers::Vector v0; // MyEngine::Containers::Vector

Share this post


Link to post
Share on other sites
Yeah i think thats good, but what i meant was messy was making a separate header, putting the namespace there and including the stuff supposed to be in that namespace inside it.

Is it possible to just have something like

h1:
namespace Engine
{

}

h2:

class Engine::Car {

};

instead of including h2 inside Engine?

Share this post


Link to post
Share on other sites
As a general rule you should not place classes in a namespace using headers (because that is actually messy). Instead, I would personally do something like this:

// engine_vehicles_car.hpp --==>>
namespace MyEngine
{
namespace Vehicles
{
class Car
{
// ...
};
}
}
// <<==-- engine_vehicles_car.hpp

// engine_vehicles_motorcycle.hpp --==>>
namespace MyEngine
{
namespace Vehicles
{
class Motorcycle
{
// ...
};
}
}
// <<==-- engine_vehicles_motorcycle.hpp

// engine_vehicles.hpp --==>>
#include "engine_vehicles_car.hpp"
#include "engine_vehicles_motorcycle.hpp"
// <<==-- engine_vehicles.hpp

// myprogram.cpp --==>>
#include "engine_vehicles.hpp"

using namespace MyEngine; // optional
using namespace MyEngine::Vehicles; // optional
// <<==-- myprogram.cpp


Or, in case of multiple small classes, I would perhaps do like this:

// engine_vehicles.hpp --==>>
namespace MyEngine
{
namespace Vehicles
{
class Car
{
// ...
};
class Motorcycle
{
// ...
};
}
}
// <<==-- engine_vehicles.hpp

// myprogram.cpp --==>>
#include "engine_vehicles.hpp"

using namespace MyEngine; // optional
using namespace MyEngine::Vehicles; // optional
// <<==-- myprogram.cpp

Share this post


Link to post
Share on other sites
Can the classes access each other like MyEngine::Vehicles::Car from the motorcycle class if theyre in separate files because its not working for whatever reason im not going to bother to figure out today?
Edit:
Ok got it working, was a circularilirlyorly dependency that i had fixed earlier but for some reason reverted it to what it was before -.-

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!