From 3e555d38e5e82eff882b142f59bd2d43405a1307 Mon Sep 17 00:00:00 2001
From: Gary Talent <gary@drinkingtea.net>
Date: Tue, 6 Jul 2021 20:32:54 -0500
Subject: [PATCH] [nostalgia/core] Fix imgui initialization

---
 src/nostalgia/core/glfw/gfx.cpp            |  7 +++----
 src/nostalgia/core/userland/gfx_opengl.cpp | 23 ++++++++++++++--------
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/src/nostalgia/core/glfw/gfx.cpp b/src/nostalgia/core/glfw/gfx.cpp
index 68ec9f3e..f384dafa 100644
--- a/src/nostalgia/core/glfw/gfx.cpp
+++ b/src/nostalgia/core/glfw/gfx.cpp
@@ -8,7 +8,6 @@
 
 #include <GLFW/glfw3.h>
 #include <imgui.h>
-#include <imgui_impl_glfw.h>
 
 #include <nostalgia/core/userland/gfx.hpp>
 
@@ -38,10 +37,10 @@ ox::Error initGfx(Context *ctx) noexcept {
 	}
 	glfwSetWindowUserPointer(id->window, ctx);
 	glfwMakeContextCurrent(id->window);
-	oxReturnError(renderer::init(ctx));
-	oxReturnError(OxError(glfwInit() != 0));
+	IMGUI_CHECKVERSION();
 	ImGui::CreateContext();
-	ImGui_ImplGlfw_InitForOpenGL(id->window, true);
+	ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
+	oxReturnError(renderer::init(ctx));
 	return OxError(0);
 }
 
diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp
index bb606fb4..5c2ccc99 100644
--- a/src/nostalgia/core/userland/gfx_opengl.cpp
+++ b/src/nostalgia/core/userland/gfx_opengl.cpp
@@ -8,11 +8,14 @@
 
 #include <array>
 
+#include <nostalgia/glutils/glutils.hpp>
+
+#define IMGUI_IMPL_OPENGL_ES3
+#include <imgui_impl_opengl3.h>
+
 #include <ox/std/bit.hpp>
 #include <ox/std/fmt.hpp>
 
-#include <nostalgia/glutils/glutils.hpp>
-
 #include <nostalgia/core/config.hpp>
 #include <nostalgia/core/gfx.hpp>
 
@@ -48,7 +51,7 @@ struct GlImplData {
 };
 
 constexpr const GLchar *bgvshad = R"(
-	#version 150
+	{}
 	in vec2 vTexCoord;
 	in vec2 position;
 	out vec2 fTexCoord;
@@ -59,7 +62,7 @@ constexpr const GLchar *bgvshad = R"(
 	})";
 
 constexpr const GLchar *bgfshad = R"(
-	#version 150
+	{}
 	out vec4 outColor;
 	in vec2 fTexCoord;
 	uniform sampler2D image;
@@ -114,7 +117,7 @@ static void initBackgroundBufferObjects(Context *ctx, Background *bg) noexcept {
 			const auto i = bgVertexRow(x, y);
 			auto vbo = &bg->bgVertices[i * BgVertexVboLength];
 			auto ebo = &bg->bgElements[i * BgVertexEboLength];
-			setTileBufferObject(ctx, i * BgVertexVboRows, x, y, 0, vbo, ebo);
+			setTileBufferObject(ctx, i * BgVertexVboRows, static_cast<float>(x), static_cast<float>(y), 0, vbo, ebo);
 		}
 	}
 }
@@ -191,7 +194,7 @@ static void drawBackground(Background *bg) noexcept {
 			renderer::sendVbo(*bg);
 		}
 		glBindTexture(GL_TEXTURE_2D, bg->tex);
-		glDrawElements(GL_TRIANGLES, bg->bgElements.size(), GL_UNSIGNED_INT, 0);
+		glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(bg->bgElements.size()), GL_UNSIGNED_INT, nullptr);
 	}
 }
 
@@ -206,12 +209,16 @@ static void drawBackgrounds(GlImplData *id) noexcept {
 }
 
 ox::Error init(Context *ctx) noexcept {
+	constexpr auto GlslVersion = "#version 150";
 	const auto id = new GlImplData;
 	ctx->setRendererData(id);
-	oxReturnError(glutils::buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader));
+	const auto vshad = ox::sfmt(bgvshad, GlslVersion);
+	const auto fshad = ox::sfmt(bgfshad, GlslVersion);
+	oxReturnError(glutils::buildShaderProgram(vshad.c_str(), fshad.c_str()).moveTo(&id->bgShader));
 	for (auto &bg : id->backgrounds) {
 		initBackgroundBufferset(ctx, id->bgShader, &bg);
 	}
+	ImGui_ImplOpenGL3_Init(GlslVersion);
 	return OxError(0);
 }
 
@@ -298,7 +305,7 @@ void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) noexcep
 	auto &bg = id->backgrounds[z];
 	auto vbo = &bg.bgVertices[i * renderer::BgVertexVboLength];
 	auto ebo = &bg.bgElements[i * renderer::BgVertexEboLength];
-	renderer::setTileBufferObject(ctx, i * renderer::BgVertexVboRows, x, y, tile, vbo, ebo);
+	renderer::setTileBufferObject(ctx, i * renderer::BgVertexVboRows, static_cast<float>(x), static_cast<float>(y), tile, vbo, ebo);
 	bg.updated = true;
 }