Browse Source

Move code into serialization file

Sam Jaffe 6 years ago
parent
commit
0f9d7d026d

+ 4 - 0
engine/engine.xcodeproj/project.pbxproj

@@ -14,6 +14,7 @@
 		CD62FCD622904A9B00376440 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FCBE22904A7C00376440 /* GoogleMock.framework */; };
 		CD62FD35229364DB00376440 /* entity.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD33229364DB00376440 /* entity.cxx */; };
 		CD62FD3C229370E200376440 /* libjsoncpp.1.8.4.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FD3922936E9C00376440 /* libjsoncpp.1.8.4.dylib */; };
+		CD62FD402293746900376440 /* serial.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD3E2293746900376440 /* serial.cxx */; };
 		CDB1F8C81D7A312B00700C6B /* game_dispatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */; };
 		CDB1F8CC1D7A319A00700C6B /* scene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8CA1D7A319A00700C6B /* scene.cpp */; };
 		CDB1F8D21D7A32A300700C6B /* events.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8D01D7A32A300700C6B /* events.cpp */; };
@@ -73,6 +74,7 @@
 		CD62FCCD22904A8900376440 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		CD62FD33229364DB00376440 /* entity.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = entity.cxx; sourceTree = "<group>"; };
 		CD62FD3922936E9C00376440 /* libjsoncpp.1.8.4.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libjsoncpp.1.8.4.dylib; path = ../../../../../../../opt/local/lib/libjsoncpp.1.8.4.dylib; sourceTree = "<group>"; };
+		CD62FD3E2293746900376440 /* serial.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = serial.cxx; sourceTree = "<group>"; };
 		CDA34D8422515C99008036A7 /* game */ = {isa = PBXFileReference; lastKnownFileType = folder; name = game; path = include/game; sourceTree = "<group>"; };
 		CDB1F8AE1D7A30CD00700C6B /* libengine.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libengine.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = game_dispatch.cpp; sourceTree = "<group>"; };
@@ -167,6 +169,7 @@
 				CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */,
 				CDB1F8CA1D7A319A00700C6B /* scene.cpp */,
 				CD62FD33229364DB00376440 /* entity.cxx */,
+				CD62FD3E2293746900376440 /* serial.cxx */,
 				CDB1F8D01D7A32A300700C6B /* events.cpp */,
 				CD2973931D7B117E00E37217 /* time.cpp */,
 			);
@@ -344,6 +347,7 @@
 				CDB1F8D21D7A32A300700C6B /* events.cpp in Sources */,
 				CDB1F8CC1D7A319A00700C6B /* scene.cpp in Sources */,
 				CDB1F8C81D7A312B00700C6B /* game_dispatch.cpp in Sources */,
+				CD62FD402293746900376440 /* serial.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 24 - 0
engine/include/game/engine/serial.hpp

@@ -0,0 +1,24 @@
+//
+//  serial.hpp
+//  engine
+//
+//  Created by Sam Jaffe on 5/20/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <json/forwards.h>
+
+#include "game/math/math_fwd.hpp"
+
+template <typename> class flyweight;
+
+namespace graphics {
+  struct object;
+}
+
+namespace engine {
+  math::vec2 to_vec2(Json::Value const & json);
+  graphics::object to_object(Json::Value const & json);
+}

+ 1 - 25
engine/src/entity.cxx

@@ -10,6 +10,7 @@
 
 #include <json/json.h>
 
+#include "game/engine/serial.hpp"
 #include "game/engine/time.hpp"
 #include "game/graphics/material.hpp"
 
@@ -20,31 +21,6 @@ static unsigned int next_id() {
   return ++id;
 }
 
-static math::vec2 to_vec2(Json::Value const & json) {
-  return make_vector(json[0].asFloat(), json[1].asFloat());
-}
-
-static flyweight<graphics::shader_program>
-to_program(Json::Value const & json) {
-  return graphics::shader_program::create(json["fragmentShader"].asString(),
-                                          json["vertexShader"].asString());
-}
-
-static flyweight<graphics::material> to_material(Json::Value const & json) {
-  graphics::shader_program prog = to_program(json["shaderProgram"]).actual();
-  return graphics::material::create(prog, json["texture"]["file"].asString(),
-                                    json["texture"]["uniform"].asString());
-}
-
-static graphics::object to_object(Json::Value const & json) {
-  flyweight<graphics::material> mat = to_material(json["material"]);
-  auto frame_size = to_vec2(json["frameSize"]);
-  return {{to_vec2(json["position"]), mat.actual().size() * frame_size},
-          {},
-          mat,
-          {{}, frame_size}};
-}
-
 entity::entity(Json::Value const & json)
     : identity<entity>(next_id()), in_scene(), last_position({-1, -1}),
       velocity(to_vec2(json["velocity"])), acceleration(),

+ 42 - 0
engine/src/serial.cxx

@@ -0,0 +1,42 @@
+//
+//  serial.cxx
+//  engine
+//
+//  Created by Sam Jaffe on 5/20/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "game/engine/serial.hpp"
+
+#include <json/json.h>
+
+#include "game/graphics/material.hpp"
+#include "game/graphics/object.hpp"
+#include "game/util/flyweight.hpp"
+#include "vector/vector.hpp"
+
+namespace engine {
+  math::vec2 to_vec2(Json::Value const & json) {
+    return make_vector(json[0].asFloat(), json[1].asFloat());
+  }
+
+  flyweight<graphics::shader_program> to_program(Json::Value const & json) {
+    return graphics::shader_program::create(json["fragmentShader"].asString(),
+                                            json["vertexShader"].asString());
+  }
+
+  flyweight<graphics::material> to_material(Json::Value const & json) {
+    auto & prog = to_program(json["shaderProgram"]).actual();
+    return graphics::material::create(prog, json["texture"]["file"].asString(),
+                                      json["texture"]["uniform"].asString());
+  }
+
+  graphics::object to_object(Json::Value const & json) {
+    flyweight<graphics::material> mat = to_material(json["material"]);
+    auto frame_size = to_vec2(json["frameSize"]);
+    return {{to_vec2(json["position"]), mat.actual().size() * frame_size},
+            {},
+            mat,
+            {{}, frame_size}};
+  }
+}