Browse Source

Merge branch 'scene_sizing' into batching

* scene_sizing:
  Fix naming conventions.
  Do a little cleanup on the screen size calcs.
  Make it so each batch renderer can have a different scale-factor.
  Push bounds check into scene.

# Conflicts:
#	engine/src/game_dispatch.cpp
#	graphics/src/renderer.cxx
Sam Jaffe 6 years ago
parent
commit
c8073116de

+ 7 - 6
engine/include/game/engine/game_dispatch.hpp

@@ -39,6 +39,7 @@ namespace engine {
     void update();
     void render();
 
+    std::shared_ptr<graphics::renderer> make_renderer(math::vec2 const &) const;
     void register_scene(scene_t scn);
     void activate_scene(scene_id_t const & id);
     void set_current_timestamp();
@@ -47,13 +48,13 @@ namespace engine {
     env::clock::tick next_frame();
 
   private:
-    std::shared_ptr<graphics::renderer> renderer;
-    math::vec2 screen_size;
-    env::clock::duration minimum_frame_duration;
+    math::vec2 screen_size_;
+    std::shared_ptr<graphics::renderer> renderer_, batch_renderer_;
+    env::clock::duration minimum_frame_duration_;
     std::shared_ptr<text_engine> text_engine_;
-    std::unique_ptr<fps_counter> counter;
+    std::unique_ptr<fps_counter> counter_;
 
-    std::unordered_map<scene_id_t, scene_t> scenes;
+    std::unordered_map<scene_id_t, scene_t> scenes_;
 
     bool running = true;
     env::clock::timestamp current_timestamp;
@@ -67,6 +68,6 @@ namespace engine {
       math::vec2 local_size;        // metadata
       bool is_mouse_event;
       bool is_keyboard_event;
-    } curr_scene;
+    } current_scene_;
   };
 }

+ 9 - 3
engine/include/game/engine/scene.hpp

@@ -21,11 +21,12 @@
 namespace engine {
   class scene : public identity<scene, std::string> {
   public:
-    scene(std::string const &, std::shared_ptr<game_dispatch>);
+    scene(std::string const &, math::vec2 const &,
+          std::shared_ptr<game_dispatch>);
     virtual ~scene();
 
     virtual void update(float delta) = 0;
-    virtual void render(graphics::renderer & renderer) = 0;
+    virtual void render() = 0;
     virtual void handle_key_event(event::key_event evt);
     virtual void handle_mouse_event(event::mouse_event evt);
 
@@ -35,11 +36,15 @@ namespace engine {
   protected:
     graphics::manager const & graphics_manager() const;
     void change_scene(std::string const & scene_id);
+    bool in_bounds(math::dim2::point c) const;
+    bool in_bounds(collidable * c) const;
     void check_collisions();
     static void check_collisions(std::vector<collidable *> const & from,
                                  std::vector<collidable *> const & to);
 
   protected:
+    graphics::renderer & renderer() { return *renderer_; }
+
     std::vector<entity> entities;
 
     // Map from entity::collides_with -> [entity*]
@@ -48,7 +53,8 @@ namespace engine {
     std::unordered_map<collision_t, std::vector<collidable *>> collidables;
 
   private:
-    math::vec2 local_scene_dimension_;
+    math::vec2 local_bounds_;
+    std::shared_ptr<graphics::renderer> renderer_;
     key_binding keys_;
     std::weak_ptr<game_dispatch> dispatch_;
   };

+ 1 - 1
engine/src/fps_counter.cxx

@@ -35,5 +35,5 @@ void fps_counter::set_frame_step(env::clock::duration const & since) {
   if (++counter_ != change_after_) { return; }
   counter_ = 0;
   text_engine_->create_text_cells(
-      glyphs_, {make_vector(5.f, 680.f), make_vector(10.f, 20.f), fps(since)});
+      glyphs_, {make_vector(5.f, 780.f), make_vector(10.f, 20.f), fps(since)});
 }

+ 33 - 22
engine/src/game_dispatch.cpp

@@ -16,6 +16,7 @@
 #include "game/graphics/object.hpp"
 #include "game/graphics/renderer.hpp"
 #include "game/util/env.hpp"
+#include "matrix/matrix_helpers.hpp"
 
 namespace engine {
   namespace {
@@ -26,21 +27,22 @@ namespace engine {
   }
 
   game_dispatch::game_dispatch(std::shared_ptr<graphics::renderer> renderer)
-      : renderer(new graphics::batch_renderer(renderer.get())),
-        screen_size(env::screen_size()), minimum_frame_duration(0),
+      : screen_size_(env::screen_size()), renderer_(renderer),
+        batch_renderer_(make_renderer(screen_size_)),
+        minimum_frame_duration_(0),
         text_engine_(new text_engine("font", renderer->manager())),
-        counter(new fps_counter(text_engine_)), scenes(),
-        current_timestamp(env::clock::now()), curr_scene() {}
+        counter_(new fps_counter(text_engine_)), scenes_(),
+        current_timestamp(env::clock::now()), current_scene_() {}
 
   game_dispatch::~game_dispatch() {}
 
   void game_dispatch::register_scene(scene_t scn) {
-    scenes.emplace(scn->id, scn);
+    scenes_.emplace(scn->id, scn);
   }
 
   void game_dispatch::activate_scene(scene_id_t const & id) {
     // TODO: Cleanup
-    curr_scene = current_scene_info(scenes[id].get());
+    current_scene_ = current_scene_info(scenes_[id].get());
   }
 
   void game_dispatch::set_current_timestamp() {
@@ -53,33 +55,33 @@ namespace engine {
       : ptr(curr), current_scene_id(ptr->id), local_size(ptr->size()) {}
 
   graphics::manager const & game_dispatch::graphics_manager() const {
-    return *renderer->manager();
+    return *renderer_->manager();
   }
 
   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->keys();
+    if (!current_scene_.is_keyboard_event) return;
+    auto const & binding = current_scene_.ptr->keys();
     auto it = binding.find(key);
     if (it == binding.end()) return;
 
-    curr_scene.ptr->handle_key_event(
+    current_scene_.ptr->handle_key_event(
         {it->second, mask(event::KEY_MASK, press)});
   }
 
   void game_dispatch::process_mouse_event(math::vec2 mouse_pos, bool press) {
-    if (!curr_scene.is_mouse_event) return;
+    if (!current_scene_.is_mouse_event) return;
     math::vec2 local_scene_position =
-        mouse_pos * curr_scene.local_size / screen_size;
+        mouse_pos * current_scene_.local_size / screen_size_;
 
-    curr_scene.ptr->handle_mouse_event(
+    current_scene_.ptr->handle_mouse_event(
         {local_scene_position, mask(event::MOUSE_MASK, press)});
   }
 
   env::clock::tick game_dispatch::next_frame() {
     env::clock::tick t(current_timestamp);
 
-    while (t.since < minimum_frame_duration) {
-      std::this_thread::sleep_for(minimum_frame_duration - t.since);
+    while (t.since < minimum_frame_duration_) {
+      std::this_thread::sleep_for(minimum_frame_duration_ - t.since);
       t = env::clock::tick(current_timestamp);
     }
 
@@ -89,16 +91,25 @@ namespace engine {
 
   void game_dispatch::update() {
     env::clock::tick t = next_frame();
-    counter->set_frame_step(t.since);
-    curr_scene.ptr->update(std::chrono::duration<float>(t.since).count());
+    counter_->set_frame_step(t.since);
+    current_scene_.ptr->update(std::chrono::duration<float>(t.since).count());
   }
 
   void game_dispatch::render() {
-    renderer->clear();
-    for (auto & obj : counter->glyphs()) {
-      renderer->draw(obj);
+    batch_renderer_->clear();
+    for (auto & obj : counter_->glyphs()) {
+      batch_renderer_->draw(obj);
     }
-    curr_scene.ptr->render(*renderer);
-    renderer->flush();
+    batch_renderer_->flush();
+    current_scene_.ptr->render();
+    renderer_->flush();
+  }
+
+  std::shared_ptr<graphics::renderer>
+  game_dispatch::make_renderer(math::vec2 const & bounds) const {
+    math::vec2 const factor = screen_size_ / bounds;
+    float const scale = std::min(factor[0], factor[1]);
+    return std::make_shared<graphics::batch_renderer>(
+        renderer_.get(), math::matrix::scalar(make_vector(scale, scale, 1.f)));
   }
 }

+ 21 - 3
engine/src/scene.cpp

@@ -12,14 +12,29 @@
 #include "game/engine/game_dispatch.hpp"
 #include "game/graphics/renderer.hpp"
 #include "game/math/common.hpp"
+#include "game/math/compare.hpp"
+#include "game/util/env.hpp"
 
 using namespace engine;
 
-scene::scene(std::string const & str, std::shared_ptr<game_dispatch> dispatch)
-    : identity<scene, std::string>(str), dispatch_(dispatch) {}
+scene::scene(std::string const & str, math::vec2 const & bounds,
+             std::shared_ptr<game_dispatch> dispatch)
+    : identity<scene, std::string>(str), local_bounds_(bounds),
+      renderer_(dispatch->make_renderer(size())), dispatch_(dispatch) {}
 
 scene::~scene() {}
 
+bool scene::in_bounds(math::dim2::point p) const {
+  return math::between(p[0], 0.f, local_bounds_[0]) &&
+         math::between(p[1], 0.f, local_bounds_[1]);
+}
+
+bool scene::in_bounds(collidable * c) const {
+  auto & quad = c->render_info().points;
+  return in_bounds(quad.ll) || in_bounds(quad.lr) || in_bounds(quad.ur) ||
+         in_bounds(quad.ul);
+}
+
 void scene::check_collisions(std::vector<collidable *> const & from,
                              std::vector<collidable *> const & to) {
   for (auto & ent : from) {
@@ -63,6 +78,9 @@ void scene::handle_key_event(event::key_event evt) {
 
 void scene::handle_mouse_event(event::mouse_event evt) {}
 
-math::vec2 scene::size() const { return local_scene_dimension_; }
+math::vec2 scene::size() const {
+  return (local_bounds_ == math::vec2()) ? math::vec2(env::screen_size())
+                                         : local_bounds_;
+}
 
 key_binding const & scene::keys() const { return keys_; }

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

@@ -13,6 +13,7 @@
 #include "game/graphics/graphics_fwd.h"
 #include "game/math/math_fwd.hpp"
 #include "game/util/hash.hpp"
+#include "matrix/matrix.hpp"
 #include "vector/vector.hpp"
 
 namespace graphics {
@@ -45,6 +46,8 @@ namespace graphics {
   class batch_renderer : public renderer {
   public:
     batch_renderer(renderer * impl, std::size_t batch_size = 0);
+    batch_renderer(renderer * impl, math::matr4 const &,
+                   std::size_t batch_size = 0);
     ~batch_renderer();
     std::shared_ptr<class manager const> manager() const override {
       return impl_->manager();
@@ -62,6 +65,7 @@ namespace graphics {
   private:
     renderer * impl_;
     std::unordered_map<identity<material>, std::vector<vertex>> batches_;
+    math::matr4 object_to_world_;
     std::size_t batch_size_;
     std::size_t elements_;
   };

+ 13 - 4
graphics/src/renderer.cxx

@@ -26,6 +26,10 @@ renderer_impl * get_renderer_impl(driver d) {
   }
 }
 
+namespace {
+  const math::matr4 identity4 = math::matrix::identity<float, 4>();
+}
+
 direct_renderer::direct_renderer(driver d) : pimpl(::get_renderer_impl(d)) {}
 
 std::shared_ptr<class manager const> direct_renderer::manager() const {
@@ -35,7 +39,7 @@ std::shared_ptr<class manager const> direct_renderer::manager() const {
 void direct_renderer::draw(object const & obj) {
   std::vector<vertex> verts;
   vertices(verts, obj);
-  draw(obj.material, math::matr4(), verts);
+  draw(obj.material, identity4, verts);
 }
 
 void direct_renderer::draw(identity<material> material,
@@ -48,7 +52,12 @@ void direct_renderer::clear() { pimpl->clear(); }
 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) {}
+    : batch_renderer(impl, identity4, batch_size) {}
+
+batch_renderer::batch_renderer(renderer * impl, math::matr4 const & to_world,
+                               std::size_t batch_size)
+    : impl_(impl), batches_(), object_to_world_(to_world),
+      batch_size_(batch_size), elements_(0) {}
 
 batch_renderer::~batch_renderer() { flush(); }
 
@@ -71,7 +80,7 @@ void batch_renderer::draw(identity<material> material, math::matr4 const &,
 
 void batch_renderer::flush() {
   write();
-  impl_->flush();
+  //  impl_->flush();
 }
 
 void batch_renderer::check() {
@@ -80,7 +89,7 @@ void batch_renderer::check() {
 
 void batch_renderer::write() {
   for (auto & pair : batches_) {
-    impl_->draw(pair.first, math::matrix::identity<float, 4>(), pair.second);
+    impl_->draw(pair.first, object_to_world_, pair.second);
     pair.second.clear();
   }
   elements_ = 0;