I'm working on a basic game loop with a GameObject/Entity system, but I'm really new to C/C++ (but I have used the bindings for Raylib, which is the library I am using, in C# before, so I understand how it works).
I've been having some struggles mainly getting my grips around how C/++ handles pointers and also virtual functions. I have no errors and I have found my problem after inserting a few debug calls, which show that my background isn't actually having any operations performed on it, despite them being called, which leads be to believe that my dodgy virtual functions are to blame inside the class Background.
Honestly, I'd love any help I can get (please roast my code, I need to learn a LOT, I have also removed superficial code):
//main.cpp
int main(void)
{
// Init
std::vector<GameObject*> gameObjects;
Background bg;
gameObjects.push_back(&bg);
// Main game loop
while (!WindowShouldClose())
{
// Update
for (GameObject* gameObject : gameObjects)
{
gameObject->Update();
}
// Draw
for (GameObject *gameObject : gameObjects)
{
gameObject->Render();
}
}
//End
return 0;
}
//gameobject.h
class GameObject
{
public:
GameObject(void) {};
virtual void Update() = 0;
virtual void Render() = 0;
~GameObject(void) {};
};
//background.h
#include "gameobject.h"
class Background : public GameObject
{
public:
Background(void){};
virtual void Update(){};
virtual void Render(){};
~Background(void){};
private:
Texture texture;
};
//background.cpp
#include "background.h"
#include "gameobject.h"
#include <iostream>
//Constructor
Background::Background()
{
...
}
//Methods
void Background::Update()
{
...
}
void Background::Render()
{
...
}
//Deconstructor
Background::~Background()
{
...
}
The problem comes however, that none of my functions inside of the Background class are called. I don't think that could be a result of any of my header locks, but I could add them in if anyone wants to look at them.
Edit: Solved! (I need to remember to mark this solved soon)
class GameObject
{
public:
GameObject(void) = default;
virtual void Update() =0;
virtual void Render() =0;
~GameObject(void) = default;
};
class Background : public GameObject
{
public:
Background(void);
virtual void Update();
virtual void Render();
~Background(void);
private:
Texture texture;
};