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