Skip to content

Commit

Permalink
Move file factory creation into a header so it can be instantiated in…
Browse files Browse the repository at this point in the history
… both client and server (#186)
  • Loading branch information
adam4813 authored Feb 13, 2021
1 parent 704a981 commit 175168b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
1 change: 0 additions & 1 deletion client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ endif ()
set(trillek-client_SOURCES # don't include main.cpp to keep it out of tests
${trillek-client_SOURCE_DIR}/game.cpp
${trillek-client_SOURCE_DIR}/imgui-system.cpp
${trillek-client_SOURCE_DIR}/file-factories.cpp
${trillek-client_SOURCE_DIR}/os.cpp
${trillek-client_SOURCE_DIR}/render-system.cpp
${trillek-client_SOURCE_DIR}/server-connection.cpp
Expand Down
17 changes: 15 additions & 2 deletions client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <thread>

#include <default-config.hpp>
#include <file-factories.hpp>

#include "game.hpp"

Expand All @@ -17,11 +18,23 @@
#include "imgui-system.hpp"
#include "os.hpp"

#include "resources/md5mesh.hpp"
#include "resources/obj.hpp"
#include "resources/md5anim.hpp"
#include "resources/vorbis-stream.hpp"
#include "resources/script-file.hpp"

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_sinks.h>

namespace tec {
extern void InitializeFileFactories();
void RegisterFileFactories() {
AddFileFactory<MD5Mesh>();
AddFileFactory<MD5Anim>();
AddFileFactory<OBJ>();
AddFileFactory<VorbisStream>();
AddFileFactory<ScriptFile>();
}
extern void BuildTestVoxelVolume();
extern void ProtoLoad(std::string filename);
}
Expand Down Expand Up @@ -144,7 +157,7 @@ int main(int argc, char* argv[]) {
tec::LuaSystem* lua_sys = game.GetLuaSystem();
os.LuaStateRegistration(lua_sys->GetGlobalState());

tec::InitializeFileFactories();
tec::RegisterFileFactories();
tec::BuildTestVoxelVolume();
tec::ProtoLoad("json/test.json");

Expand Down
18 changes: 3 additions & 15 deletions client/file-factories.cpp → common/file-factories.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#pragma once

#include <functional>
#include <unordered_map>

#include "resources/md5mesh.hpp"
#include "resources/obj.hpp"
#include "resources/md5anim.hpp"
#include "resources/vorbis-stream.hpp"
#include "resources/script-file.hpp"

#include "tec-types.hpp"
#include "filesystem.hpp"

namespace tec {
std::unordered_map<std::string, std::function<void(std::string)>> file_factories;
template <typename T>
template <typename T>
void AddFileFactory() {
file_factories[GetTypeEXT<T>()] = [] (std::string fname) {
FilePath path(fname);
Expand All @@ -24,12 +20,4 @@ namespace tec {
}
};
}

void InitializeFileFactories() {
AddFileFactory<MD5Mesh>();
AddFileFactory<MD5Anim>();
AddFileFactory<OBJ>();
AddFileFactory<VorbisStream>();
AddFileFactory<ScriptFile>();
}
}
18 changes: 9 additions & 9 deletions common/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ namespace tec {
using EventQueue<ClientCommandsEvent>::On;
using EventQueue<ControllerAddedEvent>::On;
using EventQueue<ControllerRemovedEvent>::On;
void On(std::shared_ptr<KeyboardEvent> data);
void On(std::shared_ptr<MouseBtnEvent> data);
void On(std::shared_ptr<MouseMoveEvent> data);
void On(std::shared_ptr<MouseClickEvent> data);
void On(std::shared_ptr<ClientCommandsEvent> data);
void On(std::shared_ptr<ControllerAddedEvent> data);
void On(std::shared_ptr<ControllerRemovedEvent> data);
void On(std::shared_ptr<FocusCapturedEvent> data);
void On(std::shared_ptr<FocusBlurEvent> data);
void On(std::shared_ptr<KeyboardEvent> data) override;
void On(std::shared_ptr<MouseBtnEvent> data) override;
void On(std::shared_ptr<MouseMoveEvent> data) override;
void On(std::shared_ptr<MouseClickEvent> data) override;
void On(std::shared_ptr<ClientCommandsEvent> data) override;
void On(std::shared_ptr<ControllerAddedEvent> data) override;
void On(std::shared_ptr<ControllerRemovedEvent> data) override;
void On(std::shared_ptr<FocusCapturedEvent> data) override;
void On(std::shared_ptr<FocusBlurEvent> data) override;
private:
PhysicsSystem phys_sys;
VComputerSystem vcomp_sys;
Expand Down
8 changes: 8 additions & 0 deletions server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@
#include <google/protobuf/util/json_util.h>
#include <game_state.pb.h>

#include <file-factories.hpp>

#include "filesystem.hpp"
#include "server.hpp"
#include "client-connection.hpp"
#include "game-state-queue.hpp"
#include "simulation.hpp"

#include "resources/script-file.hpp"

using asio::ip::tcp;

tec::state_id_t current_state_id = 0;

namespace tec {
void RegisterFileFactories() {
AddFileFactory<ScriptFile>();
}
std::string LoadJSON(const FilePath& fname) {
std::fstream input(fname.GetNativePath(), std::ios::in | std::ios::binary);
if (!input.good())
Expand All @@ -43,6 +50,7 @@ namespace tec {
}

int main() {
tec::RegisterFileFactories();
std::chrono::high_resolution_clock::time_point last_time, next_time;
std::chrono::duration<double> elapsed_seconds;
bool closing = false;
Expand Down

0 comments on commit 175168b

Please sign in to comment.