فهرست منبع

Have engine start using manager to provide graphics objects, thus isolating the API.

Sam Jaffe 6 سال پیش
والد
کامیت
9ef0432fe5

+ 6 - 1
engine/include/game/engine/entity.hpp

@@ -14,11 +14,16 @@
 #include "game/graphics/object.hpp"
 #include "game/util/identity.hpp"
 
+namespace graphics {
+  class manager;
+}
+
 namespace engine {
   class scene;
   class entity : identity<entity> {
   public:
-    entity(Json::Value const & json);
+    // TODO: Extract this out?
+    entity(Json::Value const & json, graphics::manager const & mgr);
     void update(float delta);
     void collide(entity const &);
 

+ 3 - 1
engine/include/game/engine/fps_counter.hpp

@@ -14,12 +14,14 @@
 
 namespace graphics {
   class object;
+  class manager;
 }
 
 namespace engine {
   class fps_counter {
   public:
-    fps_counter(env::clock::duration const & fpscap, std::size_t precision = 3);
+    fps_counter(env::clock::duration const & fpscap,
+                graphics::manager const & mgr, std::size_t precision = 3);
     ~fps_counter();
 
     void set_frame_step(env::clock::duration const & since);

+ 3 - 3
engine/include/game/engine/serial.hpp

@@ -12,14 +12,14 @@
 
 #include "game/math/math_fwd.hpp"
 
-template <typename> class flyweight;
-
 namespace graphics {
   struct object;
+  class manager;
 }
 
 namespace engine {
   math::vec2 to_vec2(Json::Value const & json);
   math::vec2 to_vec2(Json::Value const & json, math::vec2 const & backup);
-  graphics::object to_object(Json::Value const & json);
+  graphics::object to_object(Json::Value const & json,
+                             graphics::manager const & mgr);
 }

+ 2 - 2
engine/src/entity.cxx

@@ -21,10 +21,10 @@ static unsigned int next_id() {
   return ++id;
 }
 
-entity::entity(Json::Value const & json)
+entity::entity(Json::Value const & json, graphics::manager const & mgr)
     : identity<entity>(next_id()), in_scene(), last_position({-1, -1}),
       velocity(to_vec2(json["velocity"])), acceleration(),
-      angular_velocity(0.f), render_info_(to_object(json)), frame_index(0),
+      angular_velocity(0.f), render_info_(to_object(json, mgr)), 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.size *= scale;

+ 5 - 5
engine/src/fps_counter.cxx

@@ -9,18 +9,18 @@
 #include "game/engine/fps_counter.hpp"
 
 #include <json/value.h>
+#include <unordered_map>
 
 #include "game/engine/serial.hpp"
 #include "game/graphics/object.hpp"
-#include <iostream>
 
 using namespace engine;
 
-static graphics::object create() {
+static graphics::object create(graphics::manager const & mgr) {
   Json::Value value;
   value["material"]["texture"]["file"] = "data/images/font.bmp";
   value["material"]["texture"]["uniform"] = "u_normalMap";
-  return engine::to_object(value);
+  return engine::to_object(value, mgr);
 }
 
 static std::size_t digits(env::clock::duration const & fpscap) {
@@ -47,9 +47,9 @@ math::vec2 index(char value) {
 math::vec2 number_index(int num) { return index(char(num + '0')); }
 
 fps_counter::fps_counter(env::clock::duration const & fpscap,
-                         std::size_t precision)
+                         graphics::manager const & mgr, std::size_t precision)
     : digits_(precision), magnitude_(std::pow(10, digits_)),
-      glyphs_(digits(fpscap) + digits_ + 1, create()) {
+      glyphs_(digits(fpscap) + digits_ + 1, create(mgr)) {
   auto pixels = make_vector(0.125f, 0.125f);
   for (std::size_t i = 0; i < glyphs_.size(); ++i) {
     glyphs_[i].location.origin =

+ 2 - 2
engine/src/game_dispatch.cpp

@@ -27,8 +27,8 @@ namespace engine {
   game_dispatch::game_dispatch(std::shared_ptr<graphics::renderer> renderer)
       : renderer(renderer), screen_size(env::screen_size()),
         minimum_frame_duration(env::fps::v60),
-        counter(new fps_counter(minimum_frame_duration)), scenes(),
-        current_timestamp(env::clock::now()), curr_scene() {}
+        counter(new fps_counter(minimum_frame_duration, *renderer->manager())),
+        scenes(), current_timestamp(env::clock::now()), curr_scene() {}
 
   game_dispatch::current_scene_info::current_scene_info() {}
 

+ 21 - 16
engine/src/serial.cxx

@@ -10,9 +10,10 @@
 
 #include <json/json.h>
 
+#include "game/graphics/manager.hpp"
 #include "game/graphics/material.hpp"
 #include "game/graphics/object.hpp"
-#include "game/util/flyweight.hpp"
+#include "game/util/identity.hpp"
 #include "vector/vector.hpp"
 
 namespace engine {
@@ -24,27 +25,31 @@ namespace engine {
     return json.isNull() ? backup : to_vec2(json);
   }
 
-  flyweight<graphics::shader_program> to_program(Json::Value const & json) {
-    using graphics::shader_program;
+  identity<graphics::shader_program> to_program(Json::Value const & json,
+                                                graphics::manager const & mgr) {
     if (json.empty()) {
-      return shader_program::create("data/shaders/BlankShader.fragment.glsl",
-                                    "data/shaders/BlankShader.vertex.glsl");
+      return mgr.get("data/shaders/BlankShader.fragment.glsl",
+                     "data/shaders/BlankShader.vertex.glsl");
     }
-    return shader_program::create(json["fragmentShader"].asString(),
-                                  json["vertexShader"].asString());
+    return mgr.get(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());
+  identity<graphics::material> to_material(Json::Value const & json,
+                                           graphics::manager const & mgr) {
+    return mgr.get(to_program(json["shaderProgram"], mgr),
+                   json["texture"]["file"].asString(),
+                   json["texture"]["uniform"].asString());
   }
 
-  graphics::object to_object(Json::Value const & json) {
-    flyweight<graphics::material> mat = to_material(json["material"]);
+  graphics::object to_object(Json::Value const & json,
+                             graphics::manager const & mgr) {
+    identity<graphics::material> mat = to_material(json["material"], mgr);
     auto frame_size = to_vec2(json["frameSize"], make_vector(1.f, 1.f));
-    math::dim2::rectangle pos = {to_vec2(json["position"]),
-                                 mat.actual().size() * frame_size};
-    return {pos, pos, mat, {{{0, 0}}, frame_size}};
+    auto origin = to_vec2(json["position"]);
+    return mgr.create_object(mat, origin, frame_size);
+    //    math::dim2::rectangle pos = {to_vec2(json["position"]),
+    //                                 mat.actual().size() * frame_size};
+    //    return {pos, pos, mat, {{{0, 0}}, frame_size}};
   }
 }

+ 5 - 5
graphics/graphics.xcodeproj/project.pbxproj

@@ -8,11 +8,11 @@
 
 /* Begin PBXBuildFile section */
 		CD1C82BD22988EC700825C4E /* matrix.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C82BC22988EC700825C4E /* matrix.cxx */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
-		CD1C83E922998E2600825C4E /* manager.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C83E722998E2600825C4E /* manager.cxx */; };
-		CD3AC6F21D2C03B7002B4BB0 /* material.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6F01D2C03B7002B4BB0 /* material.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
-		CD3AC6F81D2C0518002B4BB0 /* texture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6F61D2C0518002B4BB0 /* texture.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
-		CD3AC6FD1D2C06B5002B4BB0 /* shader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6FB1D2C06B5002B4BB0 /* shader.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
-		CD3AC7191D2C0950002B4BB0 /* shader_program.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC7171D2C0950002B4BB0 /* shader_program.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
+		CD1C83E922998E2600825C4E /* manager.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C83E722998E2600825C4E /* manager.cxx */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
+		CD3AC6F21D2C03B7002B4BB0 /* material.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6F01D2C03B7002B4BB0 /* material.cpp */; };
+		CD3AC6F81D2C0518002B4BB0 /* texture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6F61D2C0518002B4BB0 /* texture.cpp */; };
+		CD3AC6FD1D2C06B5002B4BB0 /* shader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6FB1D2C06B5002B4BB0 /* shader.cpp */; };
+		CD3AC7191D2C0950002B4BB0 /* shader_program.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC7171D2C0950002B4BB0 /* shader_program.cpp */; };
 		CD3AC7261D2C0C63002B4BB0 /* object.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC7241D2C0C63002B4BB0 /* object.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CD62FCF72290DC9000376440 /* helper.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CD62FCF52290DC9000376440 /* helper.hpp */; };
 		CD62FCF82290DC9000376440 /* opengl_helper.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FCF62290DC9000376440 /* opengl_helper.cxx */; };

+ 5 - 0
graphics/include/game/graphics/manager.hpp

@@ -8,8 +8,10 @@
 
 #pragma once
 
+#include <memory>
 #include <unordered_map>
 
+#include "game/math/math_fwd.hpp"
 #include "game/util/identity.hpp"
 
 namespace graphics {
@@ -17,6 +19,7 @@ namespace graphics {
     enum class type : unsigned int;
   }
   class material;
+  class object;
   class shader;
   class shader_program;
   class texture;
@@ -34,6 +37,8 @@ namespace graphics {
                                  std::string const & vertex) const;
     identity<texture> get(std::string const & path) const;
 
+    object create_object(identity<material> fromMaterial, math::vec2 atPosition,
+                         math::vec2 frameWidth) const;
     material const & get(identity<material> identity) const;
 
   private:

+ 6 - 0
graphics/include/game/graphics/renderer.hpp

@@ -16,6 +16,7 @@
 #include "vector/vector.hpp"
 
 namespace graphics {
+  class manager;
   class material;
   struct object;
   struct renderer_impl;
@@ -23,6 +24,7 @@ namespace graphics {
 
   struct renderer {
     virtual ~renderer() {}
+    virtual std::shared_ptr<manager const> manager() const = 0;
     virtual void draw(object const & obj) = 0;
     virtual void draw(identity<material>, math::matr4 const &,
                       std::vector<vertex> const &) = 0;
@@ -35,6 +37,7 @@ namespace graphics {
   class direct_renderer : public renderer {
   public:
     direct_renderer(driver d);
+    std::shared_ptr<class manager const> manager() const override;
     void draw(object const & obj) override;
     void draw(identity<material>, math::matr4 const &,
               std::vector<vertex> const &) override;
@@ -49,6 +52,9 @@ namespace graphics {
   public:
     batch_renderer(renderer * impl, std::size_t batch_size = 0);
     ~batch_renderer();
+    std::shared_ptr<class manager const> manager() const override {
+      return impl_->manager();
+    }
     void draw(object const & obj) override;
     void draw(identity<material>, math::matr4 const &,
               std::vector<vertex> const &) override;

+ 9 - 0
graphics/src/manager.cxx

@@ -9,9 +9,11 @@
 #include "game/graphics/manager.hpp"
 
 #include "game/graphics/material.hpp"
+#include "game/graphics/object.hpp"
 #include "game/graphics/shader.hpp"
 #include "game/graphics/shader_program.hpp"
 #include "game/graphics/texture.hpp"
+#include "game/math/shape.hpp"
 #include "game/util/hash.hpp"
 #include "helper.hpp"
 
@@ -121,3 +123,10 @@ identity<texture> manager::get(std::string const & path) const {
   if (found != cache.values.end()) { return found->second; }
   return cache.emplace(path, texture(path));
 }
+
+object manager::create_object(identity<material> fromMaterial,
+                              math::vec2 atPosition,
+                              math::vec2 frameWidth) const {
+  math::dim2::rectangle bounds{atPosition, frameWidth * get(fromMaterial).size};
+  return {bounds, bounds, fromMaterial, {make_vector(0.f, 0.f), frameWidth}};
+}

+ 2 - 1
graphics/src/opengl_renderer.cxx

@@ -46,7 +46,8 @@ private:
   unsigned int vertex_array_object{0}, vertex_buffer_object{0};
 };
 
-opengl_renderer::opengl_renderer() : mgr(), active_material(0) {
+opengl_renderer::opengl_renderer()
+    : mgr(new class manager), active_material(0) {
   glGenVertexArrays(1, &vertex_array_object);
   glBindVertexArray(vertex_array_object);
   glGenBuffers(1, &vertex_buffer_object);

+ 4 - 0
graphics/src/renderer.cxx

@@ -28,6 +28,10 @@ renderer_impl * get_renderer_impl(driver d) {
 
 direct_renderer::direct_renderer(driver d) : pimpl(::get_renderer_impl(d)) {}
 
+std::shared_ptr<class manager const> direct_renderer::manager() const {
+  return pimpl->manager();
+}
+
 void direct_renderer::draw(object const & obj) {
   std::vector<vertex> verts;
   vertices(verts, obj);

+ 3 - 4
math/math.xcodeproj/project.pbxproj

@@ -10,9 +10,9 @@
 		CD1FCFD2227E194D00F9BF93 /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD3786181CF9F61100BE89B2 /* libmath.dylib */; };
 		CD1FCFD8227E195B00F9BF93 /* shape_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1FCFC8227E193000F9BF93 /* shape_test.cxx */; };
 		CD1FCFE9227E198100F9BF93 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD1FCFE1227E197800F9BF93 /* GoogleMock.framework */; };
-		CD1FCFEC227E4C2E00F9BF93 /* common.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3C80BA1D68902300ACC795 /* common.cpp */; };
-		CD3AC71E1D2C0AF8002B4BB0 /* shape.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC71C1D2C0AF8002B4BB0 /* shape.cpp */; };
-		CD3C809F1D675AB100ACC795 /* angle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3C809D1D675AB100ACC795 /* angle.cpp */; };
+		CD1FCFEC227E4C2E00F9BF93 /* common.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3C80BA1D68902300ACC795 /* common.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
+		CD3AC71E1D2C0AF8002B4BB0 /* shape.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC71C1D2C0AF8002B4BB0 /* shape.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
+		CD3C809F1D675AB100ACC795 /* angle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3C809D1D675AB100ACC795 /* angle.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CDA34D9522517967008036A7 /* matrix_helpers.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CDA34D8C22517680008036A7 /* matrix_helpers.hpp */; };
 		CDA34D9622517969008036A7 /* matrix.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CDA34D8D22517680008036A7 /* matrix.hpp */; };
 		CDA34D972251796B008036A7 /* vector.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CDA34D9022517689008036A7 /* vector.hpp */; };
@@ -544,7 +544,6 @@
 				GCC_ENABLE_CPP_EXCEPTIONS = YES;
 				GCC_ENABLE_CPP_RTTI = YES;
 				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
-				OTHER_LDFLAGS = "-fvisibility=default";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Release;

+ 3 - 4
util/gameutils.xcodeproj/project.pbxproj

@@ -7,9 +7,9 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		CD62FD04229195FF00376440 /* files.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD02229195FF00376440 /* files.cxx */; };
-		CD62FD082291988F00376440 /* osx_env.mm in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD072291988F00376440 /* osx_env.mm */; };
-		CD7E87772295FA1F00D877FE /* time.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD7E876F2295FA1F00D877FE /* time.cpp */; };
+		CD62FD04229195FF00376440 /* files.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD02229195FF00376440 /* files.cxx */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
+		CD62FD082291988F00376440 /* osx_env.mm in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD072291988F00376440 /* osx_env.mm */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
+		CD7E87772295FA1F00D877FE /* time.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD7E876F2295FA1F00D877FE /* time.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CD7E884822960F4800D877FE /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD7E884722960F4800D877FE /* AppKit.framework */; };
 /* End PBXBuildFile section */
 
@@ -372,7 +372,6 @@
 				GCC_ENABLE_CPP_EXCEPTIONS = YES;
 				GCC_ENABLE_CPP_RTTI = YES;
 				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
-				OTHER_LDFLAGS = "-fvisibility=default";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Release;