Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeblanc committed Nov 10, 2023
2 parents bf3bd18 + 4188d73 commit b18a312
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 41 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
4 changes: 2 additions & 2 deletions include/flip/imdraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ struct ImMode {
// Culling
sg_cull_mode cull_mode = SG_CULLMODE_BACK;

// Blending
bool blending = false;
// Alpha blending
bool alpha_blending = false;
bool alpha_test = false;
bool alpha_to_coverage = false;
};
Expand Down
2 changes: 1 addition & 1 deletion include/flip/utils/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ class SgAsyncImage {
AsyncBuffer buffer_;
};

} // namespace flip
} // namespace flip
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
14 changes: 8 additions & 6 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 All @@ -41,7 +43,7 @@ class ImDraw : public flip::Application {
transform1_,
{.type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.cull_mode = SG_CULLMODE_NONE,
.blending = true}};
.alpha_blending = true}};

drawer.color(flip::kYellow, .7f);

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.
12 changes: 9 additions & 3 deletions samples/texture/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Texture : public flip::Application {
flip::kIdentity4,
{.type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.cull_mode = SG_CULLMODE_NONE,
.blending = true,
.alpha_test = true,
.alpha_to_coverage = true}};
.alpha_blending = alpha_blending_,
.alpha_test = alpha_test_,
.alpha_to_coverage = alpha_to_coverage_}};

if (texture_) {
drawer.texture(image_.id(), sampler_.id());
Expand Down Expand Up @@ -56,6 +56,9 @@ class Texture : public flip::Application {
if (ImGui::Checkbox("Linear filtering", &linear_)) {
sampler_ = SetupSampler(linear_);
}
ImGui::Checkbox("Enable alpha alpha_blending", &alpha_blending_);
ImGui::Checkbox("Enable alpha test", &alpha_test_);
ImGui::Checkbox("Enable alpha to coverage", &alpha_to_coverage_);
ImGui::ColorEdit4("Color", color_.rgba);
ImGui::EndMenu();
}
Expand All @@ -70,6 +73,9 @@ class Texture : public flip::Application {
flip::Color color_ = flip::kYellow;
bool texture_ = true;
bool linear_ = true;
bool alpha_blending_ = true;
bool alpha_test_ = true;
bool alpha_to_coverage_ = true;
};

// Application instantiation function
Expand Down
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/extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if(EMSCRIPTEN)
target_compile_definitions(sokol PUBLIC SOKOL_GLES3)
target_link_options(sokol PUBLIC -sUSE_WEBGL2=1)
target_link_options(sokol PUBLIC -sMALLOC=emmalloc)
target_link_options(sokol PUBLIC -sALLOW_MEMORY_GROWTH=1)
target_link_options(sokol PUBLIC -sFILESYSTEM=0)
else()
target_compile_definitions(sokol PUBLIC SOKOL_GLCORE33)
Expand All @@ -51,7 +52,6 @@ endif()

if(UNIX AND NOT APPLE AND NOT EMSCRIPTEN)
find_package(X11 REQUIRED)

target_include_directories(sokol PRIVATE ${X11_INCLUDE_DIR})
target_link_libraries(sokol ${X11_LIBRARIES} Xi Xcursor)
endif()
2 changes: 1 addition & 1 deletion src/impl/imdrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void ImDrawer::Begin(const HMM_Mat4& _view_proj, const HMM_Mat4& _transform,
{.format = SG_VERTEXFORMAT_FLOAT4},
{.format = SG_VERTEXFORMAT_FLOAT2}}},
.depth = {.compare = _mode.z_compare, .write_enabled = _mode.z_write},
.colors = {{.blend = {.enabled = _mode.blending,
.colors = {{.blend = {.enabled = _mode.alpha_blending,
.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA,
.dst_factor_rgb =
SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA
Expand Down
5 changes: 3 additions & 2 deletions src/impl/imdrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace flip {
inline bool operator==(ImMode const& _a, ImMode const& _b) noexcept {
return _a.type == _b.type && _a.z_write == _b.z_write &&
_a.z_compare == _b.z_compare && _a.cull_mode == _b.cull_mode &&
_a.blending == _b.blending && _a.alpha_test == _b.alpha_test &&
_a.alpha_blending == _b.alpha_blending &&
_a.alpha_test == _b.alpha_test &&
_a.alpha_to_coverage == _b.alpha_to_coverage;
}

Expand All @@ -18,7 +19,7 @@ struct ModeHash {
auto hash = std::size_t(_mode.type) << 0 | std::size_t(_mode.z_write) << 3 |
std::size_t(_mode.z_compare) << 4 |
std::size_t(_mode.cull_mode) << 13 |
std::size_t(_mode.blending) << 15 |
std::size_t(_mode.alpha_blending) << 15 |
std::size_t(_mode.alpha_test) << 16 |
std::size_t(_mode.alpha_to_coverage) << 17;
return hash;
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
2 changes: 1 addition & 1 deletion src/impl/renderer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ bool RendererImpl::DrawGrids(std::span<const HMM_Mat4> _transforms,
{.type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.z_write = false,
.cull_mode = SG_CULLMODE_NONE,
.blending = true}};
.alpha_blending = true}};

drawer.color(.5f, .7f, .8f, .6f);
drawer.vertex(corner.X, corner.Y, corner.Z);
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 b18a312

Please sign in to comment.