Skip to content
This repository was archived by the owner on Nov 14, 2020. It is now read-only.
52 changes: 52 additions & 0 deletions Gamebase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "Gamebase.h"
#include "utils/TimerBase.h"

namespace gamebase
{
Gamebase::Gamebase(std::unique_ptr<TimerBase> timerImplementation)
: timer(std::move(timerImplementation)), _shouldQuit(false)
{

}

void Gamebase::Run()
{
Run(24);
}

void Gamebase::Run(unsigned int frames_per_second)
{
const unsigned int ms = 1000/frames_per_second;
if(!this->Initialize())
{
// Initialization failed
return;
}

this->Load();
while(!shouldQuit())
{
timer->Start();
this->Update();
this->Draw();
timer->Stop();

// Wait until the time for the given frame is elapsed
if(timer->getElapsedTime() < ms)
{
timer->Delay(ms - timer->getElapsedTime());
}
}
this->Unload();
}

bool Gamebase::shouldQuit()
{
return _shouldQuit;
}

void Gamebase::Quit()
{
_shouldQuit = true;
}
}
60 changes: 60 additions & 0 deletions Gamebase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
MIT License

Gamebase
Copyright (c) 2017 Galdin Raphael

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once

// Include core types
#include "core/IComponent.h"
#include "core/IDrawable.h"
#include "core/IDrawableComponent.h"
#include "core/ILoadable.h"

// Include utilities
#include "utils/TimerBase.h"

#include <memory>

namespace gamebase {
class Gamebase : IDrawableComponent, ILoadable
{
private:
bool _shouldQuit;

protected:
std::unique_ptr<TimerBase> timer;
bool shouldQuit();

public:
Gamebase(std::unique_ptr<TimerBase> timerImplementation);

virtual bool Initialize() = 0;
virtual void Uninitialize() = 0;

virtual void Run();
void Run(unsigned int fps);

virtual void Quit();
};
}
10 changes: 10 additions & 0 deletions core/IComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace gamebase
{
class IComponent
{
public:
virtual void Update() = 0;
};
}
10 changes: 10 additions & 0 deletions core/IDrawable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace gamebase
{
class IDrawable
{
public:
virtual void Draw() = 0;
};
}
12 changes: 12 additions & 0 deletions core/IDrawableComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "IComponent.h"
#include "IDrawable.h"

namespace gamebase
{
class IDrawableComponent : public IComponent, public IDrawable
{

};
}
11 changes: 11 additions & 0 deletions core/ILoadable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace gamebase
{
class ILoadable
{
public:
virtual void Load() = 0;
virtual void Unload() = 0;
};
}
51 changes: 51 additions & 0 deletions utils/TimerBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "TimerBase.h"

namespace gamebase
{
TimerBase::TimerBase()
: startTicks(0), stopTicks(0), _isRunning(false)
{

}

void TimerBase::Start()
{
_isRunning = true;
startTicks = getTicks();
}

void TimerBase::Stop()
{
if(!isRunning())
{
// Already stopped
return;
}
_isRunning = false;
stopTicks = getTicks();
}

void TimerBase::Reset()
{
_isRunning = false;
startTicks = 0;
stopTicks = 0;
}

unsigned int TimerBase::getElapsedTime()
{
if(isRunning())
{
return getTicks() - startTicks;
}
else
{
return stopTicks - startTicks;
}
}

bool TimerBase::isRunning()
{
return _isRunning;
}
}
24 changes: 24 additions & 0 deletions utils/TimerBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

namespace gamebase
{
class TimerBase
{
private:
unsigned int startTicks;
unsigned int stopTicks;
bool _isRunning;

public:
TimerBase();
bool isRunning();

void Start();
void Stop();
void Reset();

virtual void Delay(unsigned int ms) = 0;
virtual unsigned int getTicks() = 0;
unsigned int getElapsedTime();
};
}