Skip to content

Commit

Permalink
Exposes time object and control
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeblanc committed Sep 30, 2023
1 parent ef358d9 commit 4188d73
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 29 deletions.
5 changes: 3 additions & 2 deletions include/flip/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// flip::Application implementation.
namespace flip {
class Application;
}
struct Time;
} // namespace flip
extern std::unique_ptr<flip::Application> InstantiateApplication();

// Sokol forward declaration
Expand Down Expand Up @@ -41,7 +42,7 @@ class Application {
private:
virtual bool Initialize(bool _headless) { return true; }

virtual LoopControl Update(float _time, float _dt, float _inv_dt) {
virtual LoopControl Update(const Time& _time) {
return LoopControl::kContinue;
}

Expand Down
4 changes: 3 additions & 1 deletion include/flip/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct sapp_event;

namespace flip {

struct Time;

// Camera view parameters, passed to the renderer.
struct CameraView {
float fov;
Expand All @@ -19,7 +21,7 @@ class Camera {
public:
virtual ~Camera() = default;

virtual bool Update(float _time, float _dt, float _inv_dt) { return true; }
virtual bool Update(const Time& _time) { return true; }
virtual bool Event(const sapp_event& _event) { return false; }
virtual bool Menu() { return true; }

Expand Down
35 changes: 35 additions & 0 deletions include/flip/utils/time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

namespace flip {

struct Time {
float elapsed;
float dt;
float inv_dt;
};

class TimeControl {
public:
Time Update(float _dt);

bool Gui();

private:
// Current time, including scaling and freezes..
float elapsed_ = 0.f;

// Update time scale factor.
float scale_ = 1;

// Update time freeze state.
bool freeze_ = false;

// Fixed update rate, only applies to application update dt, not the real fps.
const float kFixedRateDefault = 60.f;
float fixed_rate_ = kFixedRateDefault;

// Fixes update rat to a fixed value, instead of real_time.
bool fix_rate_ = false;
};

} // namespace flip
12 changes: 7 additions & 5 deletions samples/imdraw/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

#include "flip/application.h"
#include "flip/imdraw.h"
#include "flip/utils/time.h"

// Implement the minimal flip::Application.
class ImDraw : public flip::Application {
public:
ImDraw() : flip::Application(Settings{.title = "ImDraw"}) {}

private:
virtual LoopControl Update(float _time, float _dt, float _inv_dt) override {
transform1_ = HMM_Rotate_RH(_time, HMM_Vec3{0, 1, 0}) *
virtual LoopControl Update(const flip::Time& _time) override {
auto elapsed = _time.elapsed;
transform1_ = HMM_Rotate_RH(elapsed, HMM_Vec3{0, 1, 0}) *
HMM_Translate(HMM_Vec3{0, 6, 0}) *
HMM_Rotate_RH(HMM_PI / 4,
HMM_Vec3{std::cos(_time), std::sin(_time), 0}) *
HMM_Rotate_RH(HMM_PI / 4, HMM_Vec3{std::cos(elapsed),
std::sin(elapsed), 0}) *
HMM_Scale(HMM_Vec3{3, 3, 3});
transform2_ = transform1_ * HMM_Rotate_RH(_time, HMM_Vec3{0, 1, 0});
transform2_ = transform1_ * HMM_Rotate_RH(elapsed, HMM_Vec3{0, 1, 0});

return LoopControl::kContinue;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/input/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Input : public flip::Application {
Input() : flip::Application(Settings{.title = "Input"}) {}

private:
virtual LoopControl Update(float _time, float _dt, float _inv_dt) override {
virtual LoopControl Update(const flip::Time& _time) override {
if (keyboard_[SAPP_KEYCODE_Q].released()) {
return LoopControl::kBreak;
}
Expand Down
Binary file added samples/media/flip.xcf
Binary file not shown.
Binary file added samples/media/flip_grey.xcf
Binary file not shown.
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_library(flip
${PROJECT_SOURCE_DIR}/include/flip/utils/loader.h
${PROJECT_SOURCE_DIR}/include/flip/utils/profile.h
${PROJECT_SOURCE_DIR}/include/flip/utils/sokol_gfx.h
${PROJECT_SOURCE_DIR}/include/flip/utils/time.h
application.cpp
impl/imdrawer.h
impl/imdrawer.cpp
Expand All @@ -28,6 +29,7 @@ add_library(flip
utils/keyboard.cpp
utils/loader.cpp
utils/profile.cpp
utils/sokol_gfx.cpp)
utils/sokol_gfx.cpp
utils/time.cpp)
target_include_directories(flip PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(flip sokol hmm stb_image)
33 changes: 16 additions & 17 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "flip/camera.h"
#include "flip/renderer.h"
#include "flip/utils/profile.h"
#include "flip/utils/time.h"
#include "impl/factory.h"

// Sokol library
Expand Down Expand Up @@ -138,22 +139,21 @@ class ApplicationCb {
sfetch_dowork();

// Updates time.
const auto dt = static_cast<float>(stm_sec(stm_laptime(&last_time_)));
const auto inv_dt = dt == 0.f ? 0.f : (1.f / dt);
const auto time = static_cast<float>(stm_sec(last_time_));
profile_frame_.push(dt * 1e3f);
const auto sys_dt = static_cast<float>(stm_sec(stm_laptime(&last_time_)));
profile_frame_.push(sys_dt * 1e3f);
const auto time = time_control_.Update(sys_dt);

{ // Updates application
Profile profile(profile_update_);
const auto control = application_->Update(time, dt, inv_dt);
const auto control = application_->Update(time);
success &= control != Application::LoopControl::kBreakFailure;
exit = control != Application::LoopControl::kContinue;
}

// Renders application
if (!headless_) {
Profile profile(profile_render_);
success &= Display(time, dt, inv_dt);
success &= Display(time);
}

// Manages exit
Expand All @@ -162,11 +162,11 @@ class ApplicationCb {
}
}

bool Display(float _time, float _dt, float _inv_dt) {
bool Display(const Time& _time) {
assert(!headless_);

bool success = true;
success &= camera_->Update(_time, _dt, _inv_dt);
success &= camera_->Update(_time);

{ // Default Rendering pass raii
auto pass = Renderer::DefaultPass(*renderer_, camera_->GetCameraView());
Expand Down Expand Up @@ -208,18 +208,16 @@ class ApplicationCb {
plot("Frame", profile_frame_);
};

static bool pop_up = false;
if (ImGui::BeginMenu("Performance")) {
ImGui::MenuItem("Pop up", 0, &pop_up);
plot_all();
ImGui::EndMenu();
}
if (pop_up) {
ImGui::SetNextWindowSize(ImVec2(240, 320), ImGuiCond_Once);
if (ImGui::Begin("Performance", &pop_up, 0)) {
if (ImGui::TreeNodeEx("Time control")) {
time_control_.Gui();
ImGui::TreePop();
}
if (ImGui::TreeNodeEx("Performance")) {
plot_all();
ImGui::TreePop();
}
ImGui::End();
ImGui::EndMenu();
}
return true;
}
Expand All @@ -243,6 +241,7 @@ class ApplicationCb {
ProfileRecord profile_frame_;

// Time management
TimeControl time_control_;
uint64_t last_time_ = 0;

// Does application has a windows (head)
Expand Down
2 changes: 1 addition & 1 deletion src/impl/orbit_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace flip {

bool OrbitCamera::Update(float _time, float _dt, float _inv_dt) {
bool OrbitCamera::Update(const Time& _time) {
bool success = true;

// Camera to eye vector
Expand Down
2 changes: 1 addition & 1 deletion src/impl/orbit_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OrbitCamera : public Camera {
virtual ~OrbitCamera() = default;

protected:
virtual bool Update(float _time, float _dt, float _inv_dt) override;
virtual bool Update(const Time& _time) override;
virtual bool Event(const sapp_event& _event) override;
virtual bool Menu() override;

Expand Down
39 changes: 39 additions & 0 deletions src/utils/time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "flip/utils/time.h"

#include "imgui/imgui.h"

namespace flip {

Time TimeControl::Update(float _dt) {
auto dt = 0.f;
if (!freeze_) {
if (fix_rate_) {
dt = scale_ / fixed_rate_;
} else {
dt = _dt * scale_;
}
}
elapsed_ += dt;

return {elapsed_, dt, dt == 0.f ? 0.f : (1.f / dt)};
}

bool TimeControl::Gui() {
ImGui::Checkbox("Freeze", &freeze_);
ImGui::SameLine();
ImGui::Checkbox("Fix update rate", &fix_rate_);
if (ImGui::Button("Reset")) {
fixed_rate_ = kFixedRateDefault;
scale_ = 1.f;
}
ImGui::SameLine();
if (fix_rate_) {
ImGui::SliderFloat("Update rate", &fixed_rate_, 1.f, 200.f, "%.2f fps");
} else {
ImGui::SliderFloat("Time scale", &scale_, -10.f, 10.f, "%.2f",
ImGuiSliderFlags_Logarithmic);
}
return true;
}

} // namespace flip

0 comments on commit 4188d73

Please sign in to comment.