Skip to content

Commit

Permalink
Moved Config and Scheduler to the base game class
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasan-Jawaheri committed Jan 19, 2020
1 parent 0b23557 commit 17e8708
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
3 changes: 3 additions & 0 deletions include/WasabiGame/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "WasabiGame/Maps/MapLoader.hpp"
#include "WasabiGame/Units/UnitsManager.hpp"
#include "WasabiGame/Utilities/Scheduler.hpp"
#include "WasabiGame/Utilities/Config.hpp"


namespace WasabiGame {
Expand Down Expand Up @@ -35,6 +36,8 @@ namespace WasabiGame {
virtual WError SetupRenderer();
virtual WPhysicsComponent* CreatePhysicsComponent();

std::shared_ptr<GameScheduler> Scheduler;
std::shared_ptr<GameConfig> Config;
std::shared_ptr<ResourceManager> Resources;
std::shared_ptr<UserInterface> UI;
std::shared_ptr<MapLoader> Maps;
Expand Down
24 changes: 12 additions & 12 deletions include/WasabiGame/Networking/NetworkListener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace WasabiGame {
} m_UDPServer;

public:
NetworkListener(CreateNetworkClientFunction createClient) : Config(), Scheduler() {
NetworkListener(CreateNetworkClientFunction createClient, std::shared_ptr<GameConfig> config, std::shared_ptr<GameScheduler> scheduler) : Config(config), Scheduler(scheduler) {
m_isRunning = true;
m_createClient = createClient;
m_clientConnected = nullptr;
Expand All @@ -160,8 +160,8 @@ namespace WasabiGame {
virtual ~NetworkListener() {
}

WasabiGame::Scheduler Scheduler;
WasabiGame::Config Config;
std::shared_ptr<GameConfig> Config;
std::shared_ptr<GameScheduler> Scheduler;

void RegisterSelectable(Selectable* client, bool deleteOnDisconnect = true) {
ClientMetadata meta;
Expand All @@ -179,20 +179,20 @@ namespace WasabiGame {
return;
}

m_TCPServer.port = Config.Get<int>("tcpPort");
m_UDPServer.port = Config.Get<int>("udpPort");
m_TCPServer.port = Config->Get<int>("tcpPort");
m_UDPServer.port = Config->Get<int>("udpPort");

strcpy_s(m_TCPServer.host, sizeof(m_TCPServer.host), Config.Get<char*>("hostname"));
strcpy_s(m_UDPServer.host, sizeof(m_UDPServer.host), Config.Get<char*>("hostname"));
strcpy_s(m_TCPServer.host, sizeof(m_TCPServer.host), Config->Get<char*>("hostname"));
strcpy_s(m_UDPServer.host, sizeof(m_UDPServer.host), Config->Get<char*>("hostname"));

if (m_TCPServer.port <= 0 || m_TCPServer.Initialize() == 0) {
if (m_UDPServer.port <= 0 || m_UDPServer.Initialize() == 0) {
Scheduler.LaunchWorkers(Config.Get<int>("numWorkers"));
Scheduler.LaunchThread("selectables-loop", [this]() {
Scheduler->LaunchWorkers(Config->Get<int>("numWorkers"));
Scheduler->LaunchThread("selectables-loop", [this]() {
this->SelectablesLoop();
this->Scheduler.Stop();
this->Scheduler->Stop();
});
Scheduler.Run();
Scheduler->Run();

for (auto client : m_clients) {
if (client.second.deleteOnDisconnect) {
Expand Down Expand Up @@ -239,7 +239,7 @@ namespace WasabiGame {

void SelectablesLoop() {
std::vector<std::pair<Selectable*, ClientMetadata>> clientsToDelete;
while (m_isRunning && Scheduler.IsRunning()) {
while (m_isRunning && Scheduler->IsRunning()) {
fd_set readFDs, writeFDs;
int maxFDs = 0;

Expand Down
4 changes: 2 additions & 2 deletions include/WasabiGame/Utilities/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace WasabiGame {

class Config {
class GameConfig {
std::unordered_map<std::string, void*> m_configTable;

public:
Config() {
GameConfig() {
m_configTable = {
{"numWorkers", (void*)8},
{"tcpPort", (void*)9965},
Expand Down
10 changes: 5 additions & 5 deletions include/WasabiGame/Utilities/Scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
namespace WasabiGame {

class SchedulerThread {
friend class Scheduler;
friend class GameScheduler;

protected:
class WasabiGame::Scheduler* m_scheduler;
class WasabiGame::GameScheduler* m_scheduler;
bool m_isRunning;

public:
Expand All @@ -38,7 +38,7 @@ namespace WasabiGame {
virtual void Run() = 0;
};

class Scheduler {
class GameScheduler {
bool m_isRunning;
std::mutex m_threadsLock;
std::unordered_map<std::string, std::pair<std::thread*, SchedulerThread*>> m_threads;
Expand All @@ -60,7 +60,7 @@ namespace WasabiGame {

virtual void Run() {
while (m_isRunning) {
WasabiGame::Scheduler::WorkUnit w = m_scheduler->GetWork();
WasabiGame::GameScheduler::WorkUnit w = m_scheduler->GetWork();
if (w.perform) {
void* result = w.perform();
if (w.callback)
Expand Down Expand Up @@ -89,7 +89,7 @@ namespace WasabiGame {
}

public:
Scheduler() {
GameScheduler() {
m_isRunning = true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/WasabiGame/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ WasabiGame::WasabiBaseGame::WasabiBaseGame() : Wasabi(), std::enable_shared_from
m_settings.fullscreen = true;
#endif

Config = std::make_shared<GameConfig>();
Scheduler = std::make_shared<GameScheduler>();
Resources = std::make_shared<ResourceManager>(shared_from_this());
UI = std::make_shared<UserInterface>(shared_from_this());
Maps = std::make_shared<MapLoader>(shared_from_this(), Resources);
Expand Down

0 comments on commit 17e8708

Please sign in to comment.