summaryrefslogtreecommitdiffhomepage
path: root/main/setup.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main/setup.cpp')
-rw-r--r--main/setup.cpp107
1 files changed, 70 insertions, 37 deletions
diff --git a/main/setup.cpp b/main/setup.cpp
index 7014cc0d..2c5600e3 100644
--- a/main/setup.cpp
+++ b/main/setup.cpp
@@ -1,10 +1,70 @@
#include "main-impl.hpp"
-#include <algorithm>
-#include <Corrade/Containers/StringView.h>
-#include <Corrade/Containers/StringIterable.h>
+#include "floormat/app.hpp"
+#include <cr/StringView.h>
+#include <cr/StringIterable.h>
+#include <mg/Functions.h>
+#include <Magnum/GL/Renderer.h>
+#include <Magnum/GL/RenderbufferFormat.h>
+#include <Magnum/GL/TextureFormat.h>
+#include <algorithm> // todo! std::minmax
namespace floormat {
+void main_impl::recalc_viewport(Vector2i fb_size, Vector2i win_size) noexcept
+{
+ _dpi_scale = dpiScaling();
+ _virtual_scale = Vector2(fb_size) / Vector2(win_size);
+ update_window_state();
+ _shader.set_scale(Vector2{fb_size});
+
+ GL::Renderer::setDepthMask(true);
+
+#ifdef FM_USE_DEPTH32
+ {
+ framebuffer.fb = GL::Framebuffer{{ {}, fb_size }};
+
+ framebuffer.color = GL::Texture2D{};
+ framebuffer.color.setStorage(1, GL::TextureFormat::RGBA8, fb_size);
+ framebuffer.depth = GL::Renderbuffer{};
+ framebuffer.depth.setStorage(GL::RenderbufferFormat::DepthComponent32F, fb_size);
+
+ framebuffer.fb.attachTexture(GL::Framebuffer::ColorAttachment{0}, framebuffer.color, 0);
+ framebuffer.fb.attachRenderbuffer(GL::Framebuffer::BufferAttachment::Depth, framebuffer.depth);
+ framebuffer.fb.clearColor(0, Color4{0.f, 0.f, 0.f, 1.f});
+ framebuffer.fb.clearDepth(0);
+
+ framebuffer.fb.bind();
+ }
+#else
+ GL::defaultFramebuffer.setViewport({{}, fb_size });
+ GL::defaultFramebuffer.clearColor(Color4{0.f, 0.f, 0.f, 1.f});
+ GL::defaultFramebuffer.clearDepthStencil(0, 0);
+ GL::defaultFramebuffer.bind();
+#endif
+
+ // -- state ---
+ glEnable(GL_LINE_SMOOTH);
+ using BlendEquation = GL::Renderer::BlendEquation;
+ using BlendFunction = GL::Renderer::BlendFunction;
+ using DepthFunction = GL::Renderer::DepthFunction;
+ using ProvokingVertex = GL::Renderer::ProvokingVertex;
+ using Feature = GL::Renderer::Feature;
+ GL::Renderer::setBlendEquation(BlendEquation::Add, BlendEquation::Add);
+ GL::Renderer::setBlendFunction(BlendFunction::SourceAlpha, BlendFunction::OneMinusSourceAlpha);
+ GL::Renderer::disable(Feature::FaceCulling);
+ GL::Renderer::disable(Feature::DepthTest);
+ GL::Renderer::enable(Feature::Blending);
+ GL::Renderer::enable(Feature::ScissorTest);
+ GL::Renderer::enable(Feature::DepthClamp);
+ GL::Renderer::setDepthFunction(DepthFunction::Greater);
+ GL::Renderer::setScissor({{}, fb_size});
+ GL::Renderer::setProvokingVertex(ProvokingVertex::FirstVertexConvention);
+
+ // -- user--
+ app.on_viewport_event(fb_size);
+ timeline = Time::now();
+}
+
auto main_impl::make_window_flags(const fm_settings& s) -> Configuration::WindowFlags
{
using flag = Configuration::WindowFlag;
@@ -43,50 +103,23 @@ auto main_impl::make_gl_conf(const fm_settings&) -> GLConfiguration
.setStencilBufferSize(0);
}
+void main_impl::update_window_state() // todo! window minimized, out of focus, fake vsync etc
+{
+}
+
+
static int get_window_refresh_rate(SDL_Window* window)
{
fm_assert(window != nullptr);
if (int index = SDL_GetWindowDisplayIndex(window); index < 0)
fm_warn_once("SDL_GetWindowDisplayIndex: %s", SDL_GetError());
- else if (SDL_DisplayMode dpymode; SDL_GetCurrentDisplayMode(index, &dpymode) < 0)
+ else if (SDL_DisplayMode dpymode{}; SDL_GetCurrentDisplayMode(index, &dpymode) < 0)
fm_warn_once("SDL_GetCurrentDisplayMode: %s", SDL_GetError());
else
- return std::max(30, dpymode.refresh_rate);
+ return Math::clamp(dpymode.refresh_rate, 30, 400);
return 30;
}
-void main_impl::update_window_state()
-{
- const auto flags = (SDL_WindowFlags)SDL_GetWindowFlags(window());
-
-#if 0
- dt_expected.do_sleep = true;
- dt_expected.jitter = 0;
- dt_expected.has_focus = true;
- if (flags & SDL_WINDOW_HIDDEN)
- {
- dt_expected.has_focus = false;
- dt_expected.value = 1;
- }
- else if (int interval = std::abs(SDL_GL_GetSwapInterval()); s.vsync && interval > 0)
- {
- int hz = get_window_refresh_rate(window()) / interval;
- if (!(flags & SDL_WINDOW_INPUT_FOCUS))
- {
- dt_expected.value = 2.f / hz;
- dt_expected.has_focus = false;
- }
- else
- dt_expected.value = 1.f/hz;
- }
- else
- {
- dt_expected.do_sleep = false;
- dt_expected.value = 1e-1f;
- }
-#endif
-}
-
auto main_impl::meshes() noexcept -> struct meshes
{
return { _ground_mesh, _wall_mesh, _anim_mesh, };