Browse Source

Start writing load-from-json code.

Sam Jaffe 6 years ago
parent
commit
0dbb2d6847

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

@@ -12,6 +12,8 @@
 		CD62D8482251A9500023219A /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8472251A9500023219A /* libmath.dylib */; };
 		CD62FCCE22904A8900376440 /* libengine.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDB1F8AE1D7A30CD00700C6B /* libengine.dylib */; };
 		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 */; };
 		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 */; };
@@ -69,6 +71,8 @@
 		CD62FCB622904A7B00376440 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
 		CD62FCC922904A8900376440 /* engine-test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "engine-test.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
 		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>"; };
 		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>"; };
@@ -90,6 +94,7 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD62FD3C229370E200376440 /* libjsoncpp.1.8.4.dylib in Frameworks */,
 				CD62D8482251A9500023219A /* libmath.dylib in Frameworks */,
 				CD62D8462251A94C0023219A /* libgraphics.dylib in Frameworks */,
 			);
@@ -101,6 +106,7 @@
 		CD62D8442251A94C0023219A /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				CD62FD3922936E9C00376440 /* libjsoncpp.1.8.4.dylib */,
 				CD62D8452251A94C0023219A /* libgraphics.dylib */,
 				CD62D8472251A9500023219A /* libmath.dylib */,
 			);
@@ -160,6 +166,7 @@
 			children = (
 				CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */,
 				CDB1F8CA1D7A319A00700C6B /* scene.cpp */,
+				CD62FD33229364DB00376440 /* entity.cxx */,
 				CDB1F8D01D7A32A300700C6B /* events.cpp */,
 				CD2973931D7B117E00E37217 /* time.cpp */,
 			);
@@ -332,6 +339,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD62FD35229364DB00376440 /* entity.cxx in Sources */,
 				CD2973951D7B117E00E37217 /* time.cpp in Sources */,
 				CDB1F8D21D7A32A300700C6B /* events.cpp in Sources */,
 				CDB1F8CC1D7A319A00700C6B /* scene.cpp in Sources */,
@@ -370,6 +378,10 @@
 				GCC_C_LANGUAGE_STANDARD = gnu11;
 				INFOPLIST_FILE = "engine-test/Info.plist";
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
+				LIBRARY_SEARCH_PATHS = (
+					"$(inherited)",
+					/opt/local/lib,
+				);
 				MACOSX_DEPLOYMENT_TARGET = 10.13;
 				MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
 				MTL_FAST_MATH = YES;
@@ -396,6 +408,10 @@
 				GCC_C_LANGUAGE_STANDARD = gnu11;
 				INFOPLIST_FILE = "engine-test/Info.plist";
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
+				LIBRARY_SEARCH_PATHS = (
+					"$(inherited)",
+					/opt/local/lib,
+				);
 				MACOSX_DEPLOYMENT_TARGET = 10.13;
 				MTL_FAST_MATH = YES;
 				PRODUCT_BUNDLE_IDENTIFIER = "leumasjaffe.engine-test";
@@ -454,6 +470,7 @@
 				MTL_ENABLE_DEBUG_INFO = YES;
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = macosx;
+				SYSTEM_HEADER_SEARCH_PATHS = /opt/local/include/;
 				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include/ $(PROJECT_DIR)/../include/expect/include $(PROJECT_DIR)/../ $(PROJECT_DIR)/../graphics/include/ $(PROJECT_DIR)/../math/include/ $(PROJECT_DIR)/../util/include/ $(PROJECT_DIR)/../math/";
 			};
 			name = Debug;
@@ -500,6 +517,7 @@
 				MACOSX_DEPLOYMENT_TARGET = 10.10;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = macosx;
+				SYSTEM_HEADER_SEARCH_PATHS = /opt/local/include/;
 				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include/ $(PROJECT_DIR)/../include/expect/include $(PROJECT_DIR)/../ $(PROJECT_DIR)/../graphics/include/ $(PROJECT_DIR)/../math/include/ $(PROJECT_DIR)/../util/include/ $(PROJECT_DIR)/../math/";
 			};
 			name = Release;
@@ -513,6 +531,10 @@
 				GCC_ENABLE_CPP_EXCEPTIONS = YES;
 				GCC_ENABLE_CPP_RTTI = YES;
 				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
+				LIBRARY_SEARCH_PATHS = (
+					"$(inherited)",
+					/opt/local/lib,
+				);
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Debug;
@@ -526,6 +548,10 @@
 				GCC_ENABLE_CPP_EXCEPTIONS = YES;
 				GCC_ENABLE_CPP_RTTI = YES;
 				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
+				LIBRARY_SEARCH_PATHS = (
+					"$(inherited)",
+					/opt/local/lib,
+				);
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Release;

+ 44 - 0
engine/include/game/engine/entity.hpp

@@ -0,0 +1,44 @@
+//
+//  entity.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/engine/engine_fwd.hpp"
+#include "game/graphics/object.hpp"
+#include "game/util/identity.hpp"
+
+namespace engine {
+  class scene;
+  class entity : identity<entity> {
+  public:
+    entity(Json::Value const & json);
+    void update(tick const & tk);
+
+  private:
+    // The scene that owns this object
+    std::weak_ptr<scene> in_scene;
+
+    // Position info
+    math::vec2 last_position;
+    math::vec2 velocity;
+    math::vec2 acceleration;
+    float angular_velocity;
+    // A mix of position and graphics info
+    graphics::object render_info;
+    // Graphics info
+    std::size_t frame_index;
+    std::vector<math::vec2> frame_texture_coords;
+
+    float scale;
+
+    int collides_with;
+    int collides_as;
+  };
+}

+ 2 - 2
engine/include/game/engine/game_dispatch.hpp

@@ -38,7 +38,7 @@ namespace engine {
     math::vec2 screen_size;
     duration minimum_frame_duration;
 
-    using scene_t = std::unique_ptr<scene>;
+    using scene_t = std::shared_ptr<scene>;
     std::unordered_map<scene_id_t, scene_t> scenes;
 
     bool running = true;
@@ -47,7 +47,7 @@ namespace engine {
       current_scene_info();
       current_scene_info(scene *);
 
-      scene * ptr; // weak
+      scene_t ptr;
 
       std::string current_scene_id; // metadata
       math::vec2 local_size;        // metadata

+ 12 - 5
engine/include/game/engine/scene.hpp

@@ -16,6 +16,10 @@
 
 #include "engine_fwd.hpp"
 
+namespace graphics {
+  class renderer;
+}
+
 namespace engine {
   class scene : public identity<scene, std::string> {
   public:
@@ -27,15 +31,18 @@ namespace engine {
     virtual void handle_key_event(event::key_event evt);
     virtual void handle_mouse_event(event::mouse_event evt);
 
-    math::vec2 get_size() const;
-    key_binding const & get_binding() const;
+    math::vec2 size() const;
+    key_binding const & keys() const;
 
   protected:
     void change_scene(std::string const & scene_id);
 
+  protected:
+    graphics::renderer * renderer;
+
   private:
-    math::vec2 local_scene_dimension;
-    key_binding keys;
-    std::weak_ptr<game_dispatch> dispatch;
+    math::vec2 local_scene_dimension_;
+    key_binding keys_;
+    std::weak_ptr<game_dispatch> dispatch_;
   };
 }

+ 60 - 0
engine/src/entity.cxx

@@ -0,0 +1,60 @@
+//
+//  entity.cxx
+//  engine
+//
+//  Created by Sam Jaffe on 5/20/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "game/engine/entity.hpp"
+
+#include <json/json.h>
+
+#include "game/engine/time.hpp"
+#include "game/graphics/material.hpp"
+
+using namespace engine;
+
+static unsigned int next_id() {
+  static unsigned int id{0};
+  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());
+}
+
+entity::entity(Json::Value const & json)
+    : identity<entity>(next_id()), in_scene(), last_position({-1, -1}),
+      velocity(to_vec2(json["velocity"])), acceleration(),
+      angular_velocity(0.f),
+      render_info({{}, {}, to_material(json["material"]), {}}), frame_index(0),
+      frame_texture_coords({make_vector(0.f, 0.f)}),
+      scale(json["size"].asFloat()), collides_with(0), collides_as(0) {
+  render_info.location.origin = to_vec2(json["position"]);
+}
+
+void entity::update(tick const & tk) {
+  last_position = render_info.location.origin;
+  float delta = tk.since.count();
+  auto delta_pos = velocity * delta;
+  render_info.location.origin += delta_pos;
+  render_info.points.ll += delta_pos;
+  render_info.points.lr += delta_pos;
+  render_info.points.ur += delta_pos;
+  render_info.points.ul += delta_pos;
+  velocity += acceleration * delta;
+  //  render_info.angle += angular_velocity * delta;
+}

+ 2 - 2
engine/src/game_dispatch.cpp

@@ -27,11 +27,11 @@ namespace engine {
   game_dispatch::current_scene_info::current_scene_info() {}
 
   game_dispatch::current_scene_info::current_scene_info(scene * curr)
-      : ptr(curr), current_scene_id(ptr->id), local_size(ptr->get_size()) {}
+      : ptr(curr), current_scene_id(ptr->id), local_size(ptr->size()) {}
 
   void game_dispatch::process_key_event(raw_key_t key, bool press) {
     if (!curr_scene.is_keyboard_event) return;
-    auto const & binding = curr_scene.ptr->get_binding();
+    auto const & binding = curr_scene.ptr->keys();
     auto it = binding.find(key);
     if (it == binding.end()) return;
 

+ 3 - 3
engine/src/scene.cpp

@@ -20,13 +20,13 @@ namespace engine {
 
   void scene::handle_key_event(event::key_event evt) {
     if (evt.type & event::PRESSED_MASK && evt.key == key::QUIT) {
-      dispatch.lock()->quit();
+      dispatch_.lock()->quit();
     }
   }
 
   void scene::handle_mouse_event(event::mouse_event evt) {}
 
-  math::vec2 scene::get_size() const { return local_scene_dimension; }
+  math::vec2 scene::size() const { return local_scene_dimension_; }
 
-  key_binding const & scene::get_binding() const { return keys; }
+  key_binding const & scene::keys() const { return keys_; }
 }