summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-11-12 11:38:34 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-11-14 23:46:14 +0000
commit82bfbf1f459d540b0e204bb873ca5786997b24da (patch)
tree3b409db10a77d10f52a3ce45f135a423d8ff2da1
parentFix type of SceneShader (diff)
downloadilt-82bfbf1f459d540b0e204bb873ca5786997b24da.tar.bz2
ilt-82bfbf1f459d540b0e204bb873ca5786997b24da.tar.xz
ilt-82bfbf1f459d540b0e204bb873ca5786997b24da.zip
Refactor for per window context and more setup to the right places
-rw-r--r--gfx/models/texture.cpp1
-rw-r--r--ui/sceneRenderer.cpp3
-rw-r--r--ui/window.cpp40
-rw-r--r--ui/window.h8
4 files changed, 22 insertions, 30 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp
index ff38d0f..ab1a5fe 100644
--- a/gfx/models/texture.cpp
+++ b/gfx/models/texture.cpp
@@ -23,6 +23,7 @@ Texture::Texture(const Image & tex) :
Texture::Texture(GLsizei width, GLsizei height, const void * data)
{
glBindTexture(GL_TEXTURE_2D, m_texture);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
diff --git a/ui/sceneRenderer.cpp b/ui/sceneRenderer.cpp
index 6e75dec..5ef0379 100644
--- a/ui/sceneRenderer.cpp
+++ b/ui/sceneRenderer.cpp
@@ -51,6 +51,9 @@ SceneRenderer::render(std::function<void()> content) const
{
// Geometry pass
glEnable(GL_BLEND);
+ glEnable(GL_CULL_FACE);
+ glCullFace(GL_BACK);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
diff --git a/ui/window.cpp b/ui/window.cpp
index 82f4d40..0d4c401 100644
--- a/ui/window.cpp
+++ b/ui/window.cpp
@@ -7,35 +7,24 @@
#include <tuple>
#include <type_traits>
-static SDL_GLContext
-SDL_GL_CreateContextAndGlewInit(SDL_Window * w)
+Window::GlewInitHelper::GlewInitHelper()
{
- auto ctx = SDL_GL_CreateContext(w);
- if (glewInit() != GLEW_OK) {
- throw std::runtime_error {"Glew failed to initialize!"};
- }
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- return ctx;
+ [[maybe_unused]] static auto init = []() {
+ if (const auto r = glewInit(); r != GLEW_OK) {
+ throw std::runtime_error {reinterpret_cast<const char *>(glewGetErrorString(r))};
+ }
+ else {
+ return r;
+ }
+ }();
}
-using GL_Context = std::remove_pointer_t<SDL_GLContext>;
-using SDL_GLContextPtr = wrapped_ptrt<GL_Context, SDL_GL_CreateContextAndGlewInit, SDL_GL_DeleteContext>;
-
Window::Window(size_t width, size_t height, const std::string & title) :
size {static_cast<int>(width), static_cast<int>(height)}, m_window {title.c_str(),
static_cast<int>(SDL_WINDOWPOS_CENTERED),
static_cast<int>(SDL_WINDOWPOS_CENTERED), size.x,
size.y, static_cast<Uint32>(SDL_WINDOW_OPENGL)},
- uiShader {[this](auto w) {
- // must call glContent before creating the shader
- std::ignore = glContext();
- return w;
- }(width),
- height}
+ glContext {m_window}, uiShader {width, height}
{
}
@@ -73,17 +62,10 @@ Window::handleInput(const SDL_Event & e)
return false;
}
-SDL_GLContext
-Window::glContext() const
-{
- static SDL_GLContextPtr m_glContext {m_window};
- return m_glContext;
-}
-
void
Window::refresh() const
{
- SDL_GL_MakeCurrent(m_window, glContext());
+ SDL_GL_MakeCurrent(m_window, glContext);
clear(0.0F, 0.0F, 0.0F, 1.0F);
render();
diff --git a/ui/window.h b/ui/window.h
index 0661915..d618976 100644
--- a/ui/window.h
+++ b/ui/window.h
@@ -26,12 +26,18 @@ public:
void swapBuffers() const;
protected:
- [[nodiscard]] SDL_GLContext glContext() const;
virtual void render() const;
+ struct GlewInitHelper {
+ GlewInitHelper();
+ };
using SDL_WindowPtr = wrapped_ptrt<SDL_Window, SDL_CreateWindow, SDL_DestroyWindow>;
+ using GL_Context = std::remove_pointer_t<SDL_GLContext>;
+ using SDL_GLContextPtr = wrapped_ptrt<GL_Context, SDL_GL_CreateContext, SDL_GL_DeleteContext>;
const glm::ivec2 size;
SDL_WindowPtr m_window;
+ SDL_GLContextPtr glContext;
+ GlewInitHelper glewinithelper;
Collection<UIComponent> uiComponents;
UIShader uiShader;
};