game_dispatch.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // game_dispatch.cpp
  3. // gameutils
  4. //
  5. // Created by Sam Jaffe on 9/2/16.
  6. //
  7. #include <thread>
  8. #include "game/engine/game_dispatch.hpp"
  9. #include <game/graphics/object.hpp>
  10. #include <game/graphics/renderer.hpp>
  11. #include <game/util/env.hpp>
  12. #include <math/matrix/matrix_helpers.hpp>
  13. #include "game/engine/events.hpp"
  14. #include "game/engine/fps_counter.hpp"
  15. #include "game/engine/scene.hpp"
  16. #include "game/engine/text_engine.hpp"
  17. namespace engine {
  18. namespace {
  19. event::event_type mask(event::event_type t, bool pressed) {
  20. return static_cast<event::event_type>(
  21. t | (pressed ? event::PRESSED_MASK : event::RELEASED_MASK));
  22. }
  23. }
  24. game_dispatch::game_dispatch(std::shared_ptr<graphics::renderer> renderer)
  25. : screen_size_(env::screen_size()), renderer_(renderer),
  26. batch_renderer_(make_renderer(screen_size_)), minimum_frame_duration_(0),
  27. text_engine_(new text_engine("font", renderer->manager())),
  28. counter_(new fps_counter(text_engine_)), scenes_(),
  29. current_timestamp(env::clock::now()), current_scene_() {}
  30. game_dispatch::~game_dispatch() {}
  31. void game_dispatch::register_scene(scene_t scn) {
  32. scenes_.emplace(scn->id, scn);
  33. }
  34. void game_dispatch::activate_scene(scene_id_t const & id) {
  35. // TODO: Cleanup
  36. current_scene_ = current_scene_info(scenes_[id]);
  37. }
  38. void game_dispatch::set_current_timestamp() {
  39. current_timestamp = env::clock::now();
  40. }
  41. game_dispatch::current_scene_info::current_scene_info() {}
  42. game_dispatch::current_scene_info::current_scene_info(scene_t curr)
  43. : ptr(curr), current_scene_id(ptr->id), local_size(ptr->size()) {}
  44. graphics::manager const & game_dispatch::graphics_manager() const {
  45. return *renderer_->manager();
  46. }
  47. void game_dispatch::process_key_event(raw_key_t key, bool press) {
  48. // if (!current_scene_.is_keyboard_event) return;
  49. auto const & binding = current_scene_.ptr->keys();
  50. auto it = binding.find(key);
  51. if (it == binding.end()) return;
  52. current_scene_.ptr->handle_key_event(
  53. {it->second, mask(event::KEY_MASK, press)});
  54. }
  55. void game_dispatch::process_mouse_event(math::vec2 mouse_pos, bool press) {
  56. // if (!current_scene_.is_mouse_event) return;
  57. math::vec2 local_scene_position =
  58. mouse_pos * current_scene_.local_size / screen_size_;
  59. current_scene_.ptr->handle_mouse_event(
  60. {local_scene_position, mask(event::MOUSE_MASK, press)});
  61. }
  62. env::clock::tick game_dispatch::next_frame() {
  63. env::clock::tick t(current_timestamp);
  64. while (t.since < minimum_frame_duration_) {
  65. std::this_thread::sleep_for(minimum_frame_duration_ - t.since);
  66. t = env::clock::tick(current_timestamp);
  67. }
  68. current_timestamp = t.now;
  69. return t;
  70. }
  71. void game_dispatch::update() {
  72. env::clock::tick t = next_frame();
  73. counter_->set_frame_step(t.since);
  74. current_scene_.ptr->update(std::chrono::duration<float>(t.since).count());
  75. }
  76. void game_dispatch::render() {
  77. batch_renderer_->clear();
  78. counter_->render(*batch_renderer_);
  79. batch_renderer_->flush();
  80. current_scene_.ptr->render();
  81. renderer_->flush();
  82. }
  83. std::shared_ptr<graphics::renderer>
  84. game_dispatch::make_renderer(math::vec2 const & bounds) const {
  85. math::vec2 const factor = screen_size_ / bounds;
  86. float const scale = std::min(factor[0], factor[1]);
  87. return std::make_shared<graphics::batch_renderer>(
  88. renderer_.get(), math::matrix::scalar(make_vector(scale, scale, 1.f)));
  89. }
  90. }