|
|
@@ -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)));
|
|
|
}
|
|
|
}
|