Browse Source

Add update/render propagation to scenes.

Sam Jaffe 6 years ago
parent
commit
7edbd6010f

+ 2 - 2
engine/engine.xcodeproj/project.pbxproj

@@ -166,11 +166,11 @@
 		CDB1F8B01D7A30CD00700C6B /* src */ = {
 			isa = PBXGroup;
 			children = (
+				CD62FD33229364DB00376440 /* entity.cxx */,
+				CDB1F8D01D7A32A300700C6B /* events.cpp */,
 				CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */,
 				CDB1F8CA1D7A319A00700C6B /* scene.cpp */,
-				CD62FD33229364DB00376440 /* entity.cxx */,
 				CD62FD3E2293746900376440 /* serial.cxx */,
-				CDB1F8D01D7A32A300700C6B /* events.cpp */,
 				CD2973931D7B117E00E37217 /* time.cpp */,
 			);
 			path = src;

+ 4 - 2
engine/include/game/engine/entity.hpp

@@ -19,7 +19,9 @@ namespace engine {
   class entity : identity<entity> {
   public:
     entity(Json::Value const & json);
-    void update(tick const & tk);
+    void update(float delta);
+
+    graphics::object const & render_info() const { return render_info_; }
 
   private:
     // The scene that owns this object
@@ -31,7 +33,7 @@ namespace engine {
     math::vec2 acceleration;
     float angular_velocity;
     // A mix of position and graphics info
-    graphics::object render_info;
+    graphics::object render_info_;
     // Graphics info
     std::size_t frame_index;
     std::vector<math::vec2> frame_texture_coords;

+ 4 - 1
engine/include/game/engine/scene.hpp

@@ -21,12 +21,14 @@ namespace graphics {
 }
 
 namespace engine {
+  class entity;
+
   class scene : public identity<scene, std::string> {
   public:
     using identity<scene, std::string>::identity;
     virtual ~scene();
 
-    virtual void update(tick);
+    virtual void update(float delta);
     virtual void render();
     virtual void handle_key_event(event::key_event evt);
     virtual void handle_mouse_event(event::mouse_event evt);
@@ -39,6 +41,7 @@ namespace engine {
 
   protected:
     graphics::renderer * renderer;
+    std::vector<entity> entities;
 
   private:
     math::vec2 local_scene_dimension_;

+ 10 - 11
engine/src/entity.cxx

@@ -24,22 +24,21 @@ static unsigned int next_id() {
 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_object(json)), frame_index(0),
+      angular_velocity(0.f), render_info_(to_object(json)), 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;
-  render_info.frame.origin = frame_texture_coords[frame_index];
+  render_info_.location.size *= scale;
+  render_info_.frame.origin = frame_texture_coords[frame_index];
 }
 
-void entity::update(tick const & tk) {
-  last_position = render_info.location.origin;
-  float delta = tk.since.count();
+void entity::update(float delta) {
+  last_position = render_info_.location.origin;
   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;
+  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;
 }

+ 12 - 2
engine/src/scene.cpp

@@ -7,16 +7,26 @@
 
 #include "game/engine/scene.hpp"
 
+#include "game/engine/entity.hpp"
 #include "game/engine/events.hpp"
 #include "game/engine/game_dispatch.hpp"
+#include "game/graphics/renderer.hpp"
 
 namespace engine {
 
   scene::~scene() {}
 
-  void scene::update(tick) {}
+  void scene::update(float delta) {
+    for (auto & ent : entities) {
+      ent.update(delta);
+    }
+  }
 
-  void scene::render() {}
+  void scene::render() {
+    for (auto & ent : entities) {
+      renderer->draw(ent.render_info());
+    }
+  }
 
   void scene::handle_key_event(event::key_event evt) {
     if (evt.type & event::PRESSED_MASK && evt.key == key::QUIT) {

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

@@ -15,11 +15,13 @@
 
 namespace graphics {
   class material;
+  struct object;
   struct renderer_impl;
   struct vertex;
 
   struct renderer {
     virtual ~renderer() {}
+    virtual void draw(object const & obj) = 0;
     virtual void draw(flyweight<material>, math::matr4 const &,
                       std::vector<vertex> const &) = 0;
     virtual void clear() = 0;
@@ -31,6 +33,7 @@ namespace graphics {
   class direct_renderer : public renderer {
   public:
     direct_renderer(driver d);
+    void draw(object const & obj) override;
     void draw(flyweight<material>, math::matr4 const &,
               std::vector<vertex> const &) override;
     void clear() override;
@@ -43,6 +46,7 @@ namespace graphics {
   class batch_renderer : public renderer {
   public:
     batch_renderer(renderer * impl, std::size_t batch_size = 0);
+    void draw(object const & obj) override;
     void draw(flyweight<material>, math::matr4 const &,
               std::vector<vertex> const &) override;
     void clear() override { impl_->clear(); }

+ 16 - 0
graphics/src/renderer.cxx

@@ -9,6 +9,7 @@
 #include "game/graphics/renderer.hpp"
 #include <vector>
 
+#include "game/graphics/object.hpp"
 #include "game/graphics/vertex.h"
 #include "matrix/matrix.hpp"
 #include "renderer_impl.hpp"
@@ -26,6 +27,12 @@ renderer_impl * get_renderer_impl(driver d) {
 
 direct_renderer::direct_renderer(driver d) : pimpl(::get_renderer_impl(d)) {}
 
+void direct_renderer::draw(object const & obj) {
+  std::vector<vertex> verts;
+  vertices(verts, obj);
+  draw(obj.material, math::matr4(), verts);
+}
+
 void direct_renderer::draw(flyweight<material> material, math::matr4 const &,
                            std::vector<vertex> const & verts) {
   pimpl->draw(material, {}, verts);
@@ -37,11 +44,20 @@ void direct_renderer::flush() { pimpl->flush(); }
 batch_renderer::batch_renderer(renderer * impl, std::size_t batch_size)
     : impl_(impl), batches_(), batch_size_(batch_size), elements_(0) {}
 
+void batch_renderer::draw(object const & obj) {
+  std::vector<vertex> & batch_verts = batches_[obj.material];
+  auto old_size = batch_verts.size();
+  vertices(batch_verts, obj);
+  elements_ += batch_verts.size() - old_size;
+  check();
+}
+
 // TODO (sjaffe): object-to-world matrix...
 void batch_renderer::draw(flyweight<material> material, math::matr4 const &,
                           std::vector<vertex> const & verts) {
   auto & batch_verts = batches_[material];
   batch_verts.insert(batch_verts.end(), verts.begin(), verts.end());
+  elements_ += verts.size();
   check();
 }