Просмотр исходного кода

testbed: trivial movement system for demonstration purposes

skypjack 1 день назад
Родитель
Сommit
fe6bbd5108

+ 12 - 3
testbed/application/application.cpp

@@ -1,5 +1,6 @@
 #include <cstdint>
 #include <SDL3/SDL.h>
+#include <SDL3/SDL_stdinc.h>
 #include <application/application.h>
 #include <application/context.h>
 #include <backends/imgui_impl_sdl3.h>
@@ -21,13 +22,13 @@
 
 namespace testbed {
 
-void application::update(entt::registry &registry) {
+void application::update(entt::registry &registry, const context &ctx, const double delta) {
     ImGui_ImplSDLRenderer3_NewFrame();
     ImGui_ImplSDL3_NewFrame();
     ImGui::NewFrame();
 
     command_system(registry);
-    movement_system(registry);
+    movement_system(registry, ctx, delta);
 }
 
 void application::draw(entt::registry &registry, const context &context) const {
@@ -87,10 +88,18 @@ int application::run() {
     meta_setup();
     static_setup_for_dev_purposes(registry);
 
+    Uint64 last{SDL_GetPerformanceCounter()};
+    Uint64 current{};
+    double delta{};
+
     quit = false;
 
     while(!quit) {
-        update(registry);
+        current = SDL_GetPerformanceCounter();
+        delta = (current - last) / static_cast<double>(SDL_GetPerformanceFrequency());
+        last = current;
+
+        update(registry, context, delta);
         draw(registry, context);
         input(registry);
     }

+ 1 - 1
testbed/application/application.h

@@ -11,7 +11,7 @@ struct config;
 struct context;
 
 class application {
-    void update(entt::registry &);
+    void update(entt::registry &, const context &, const double);
     void draw(entt::registry &, const context &) const;
     void input(entt::registry &);
 

+ 11 - 2
testbed/system/movement_system.cpp

@@ -1,10 +1,19 @@
+#include <application/context.h>
+#include <component/position_component.h>
+#include <component/velocity_component.h>
 #include <entt/entity/registry.hpp>
 #include <system/movement_system.h>
 
 namespace testbed {
 
-void movement_system(entt::registry &) {
-    // TODO
+void movement_system(entt::registry &registry, const context &ctx, const double delta) {
+    int width{};
+    int height{};
+
+    for(auto [entity, position, velocity]: registry.view<position_component, velocity_component>().each()) {
+        position.x += (velocity.dx * delta) * (ctx.logical_width() / 10.);
+        position.y += velocity.dy * delta * (ctx.logical_height() / 10.);
+    }
 }
 
 } // namespace testbed

+ 4 - 2
testbed/system/movement_system.h

@@ -4,6 +4,8 @@
 
 namespace testbed {
 
-void movement_system(entt::registry &);
+struct context;
 
-}
+void movement_system(entt::registry &, const context &, const double);
+
+} // namespace testbed