Kaynağa Gözat

Make it possible to invoke the game from an external runner.

Sam Jaffe 6 yıl önce
ebeveyn
işleme
6ae30e62b9

+ 8 - 5
engine/include/game/engine/game_dispatch.hpp

@@ -26,14 +26,17 @@ namespace engine {
     void process_key_event(raw_key_t key, bool press);
     void process_mouse_event(math::vec2 click, bool press);
 
-    [[noreturn]] void gameloop();
-    void quit();
+    bool is_running() const { return running; }
+    void quit() { running = false; }
+
+  public:
+    void update();
+    void render();
 
   private:
-    void single_frame(double) {}
+    float next_frame();
     tick get_tick();
-    double next_frame();
-    void cleanup_resources() {} // TODO ???
+
   private:
     math::vec2 screen_size;
     duration minimum_frame_duration;

+ 8 - 15
engine/src/game_dispatch.cpp

@@ -11,6 +11,7 @@
 
 #include "game/engine/events.hpp"
 #include "game/engine/scene.hpp"
+#include "game/util/env.hpp"
 
 namespace engine {
   namespace {
@@ -21,8 +22,9 @@ namespace engine {
   }
 
   game_dispatch::game_dispatch()
-      : screen_size({1920.f, 1080.f}), minimum_frame_duration(engine::fps::v60),
-        scenes(), current_timestamp(clock::now()), curr_scene() {}
+      : screen_size(env::screen_size()),
+        minimum_frame_duration(engine::fps::v60), scenes(),
+        current_timestamp(clock::now()), curr_scene() {}
 
   game_dispatch::current_scene_info::current_scene_info() {}
 
@@ -53,7 +55,7 @@ namespace engine {
     return {now, now - current_timestamp};
   }
 
-  double game_dispatch::next_frame() {
+  float game_dispatch::next_frame() {
     tick t = get_tick();
 
     while (t.since < minimum_frame_duration) {
@@ -62,19 +64,10 @@ namespace engine {
     }
 
     current_timestamp = t.now;
-    return std::chrono::duration<double>(t.since).count();
+    return std::chrono::duration<float>(t.since).count();
   }
 
-  void game_dispatch::gameloop() try {
-    while (running) {
-      single_frame(next_frame());
-    }
-    cleanup_resources();
-    exit(EXIT_SUCCESS);
-  } catch (std::exception const & ex) {
-    // todo: logging
-    exit(EXIT_FAILURE);
-  }
+  void game_dispatch::update() { curr_scene.ptr->update(next_frame()); }
 
-  void game_dispatch::quit() { running = false; }
+  void game_dispatch::render() { curr_scene.ptr->render(); }
 }