Open tamtaasatiani opened 2 months ago
- the functions can't be defined multiple times
For the time being, you could move the main.cpp functions inside their own namespace. That way other files can also define their own update, etc. functions without collision. Those other files could also use their own namespaces, which would avoid the issue even in the current system.
*ahem* Disclaimer: pls note that I'm not very experienced with C++. I hope I'm not misunderstanding namespaces here ^^
We can wrap start()
, update()
, draw()
and keypressed(Event)
into std::function
objects.
At tahm.h
#include <functional>
class Tahm {
...
private:
std::function<void()> callbackUpdate;
std::function<void()> callbackDraw;
std::function<void(Event)> callbackKeypressed;
public:
void init(std::function<void()> start,
std::function<void()> update,
std::function<void()> draw,
std::function<void(Event)> keypressed);
...
};
At tahm.cpp
void Tahm::init(std::function<void()> start,
std::function<void()> update,
std::function<void()> draw,
std::function<void(Event)> keypressed)
{
// Store callbacks
callbackUpdate = update;
callbackDraw = draw;
callbackKeypressed = keypressed;
start(); // We could call start right away
window->init();
renderer->init();
audio->setupDevice();
}
void Tahm::run() // We can now move the main loop inside the class
{
handleEvents(); // callbackKeypressed runs inside here
callbackUpdate();
renderer->prepare();
callbackDraw();
renderer->present();
}
update, start, keypressed, and draw aren't proper callback functions, which causes multiple problems: