So I am trying to implement a component design and I am currently unsure if I am implemented it effectively or correctly.
My classes and their children are as follow:
1. MainGame
2. VideoManager
3. EntityManager
4. Entity
|---6. InputComponent |---8. CharacterInputComponent
5. Component |
|---7. RenderComponent |---9. CharacterRenderComponent
10. SmartList
1. So the main game class holds function for initializing, running, and terminating the game. It also has a pointer to the various managers( more will be added for like sounds or resources ) which manage more specialized parts of the game.
2. The video manager has collection of the shaders, the projection and view matrices, etc.. all for handling the drawing commands.
3. The entity manager has a list of all entities that are in the world and knows to update them if need be( drawing, input, etc. ). The list is my SmartList.
4. The entity has a list of components that are linked to it. These components give all functionality to the entity other than the basics. It operates kind of like a unique component manager.
5. This is a base class that only really defines a function( update call ) used by all components and the unique string id of all components( for use in my smart list ).
6 & 7. These are specialized components that have specific roles, one for rendering and one for input. More will inevitably be made.
8 & 9. These would be the actual components that I would link to my entities since they implement the functions more specifically.
10. It's my own template class that can store and sort through all the objects. It uses double pointers and a binary search to look up things and a merge sort to keep everything in order. Memory is automatically allocated as the list grows/shrinks in exponents of 2. Through template classes and function pointers I can use this for any value and sort by any value such is integers, strings( as for my use ), or even complicated sets of data with specialized comparison.
Now here is how the flow of a input and render call.
//Draw a single scene and starts in the main game loop
MainGame -> VideoManager -> EntityManager -> Entity -> RenderComponent
//Input detected
GLFWCallback -> EntityManager -> Entity -> InputComponent
Here is my main.cpp and how it starts/run the game + making entities and components.
#include "GameClass.h"
#include "EntityManager.h"
#include "Entity.h"
#include "components/InputComponent.h"
#include "components/RenderComponent.h"
#include "Logger.h"
using namespace std;
struct MyInputComponent : public InputComponent
{
void KeyPressed( int iKey, int iAction )
{
if( iKey == GLFW_KEY_ESCAPE && iAction == GLFW_PRESS )
{
GameClass::GetGameInstance()->EndGame();
}
}
};
int main( int argc, char* argv[] )
{
GameClass* pGame = GameClass::GetGameInstance();
if( !pGame->InitGame( 640, 480, 640, 480, "My Game" ) )
{
Logger::Log( "", "Couldn't initialize game." );
return 1;
}
EntityManager* pEntityManager = pGame->GetEntityManager();
MyInputComponent* c = new MyInputComponent();
b->SetComponentName( "InputComponent Dude" );
Entity* a = new Entity();
a->SetEntityName( "EntityA" );
Entity* b = new Entity();
b->SetEntityName( "EntityB" );
pEntityManager->AddEntity( a );
pEntityManager->AddEntity( b );
b->BindComponent( c );
pEntityManager->RemoveEntity( pEntityManager->LookUpEntity( "EntityA" ) );
pEngine->RunEngine();
pEngine->TerminateEngine();
delete a;
delete b;
delete c;
return 0;
}
I'm also trying to do my best to stop decoupling. For example my VideoManager shouldn't need to know about entities or components hence why the flow chart goes in such steps, but I am worried it's adding extra overhead.
This is my first time working a larger project. I would like some suggestions or notable problems with my design so I can refactor my code. I usually develop for a while, assess all issues, and rebuild it from scratch more streamlined, but my limited experience doesn't make it easy to assess everything completely.