From 63e46d7bb42b0339af7b076b0e4e1500dcdb1fac Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 15 May 2021 02:27:13 -0500 Subject: [PATCH] [nostalgia] Move glutils into separate library --- src/nostalgia/CMakeLists.txt | 1 + src/nostalgia/core/glfw/CMakeLists.txt | 1 + src/nostalgia/core/userland/CMakeLists.txt | 9 +----- src/nostalgia/core/userland/gfx_opengl.cpp | 28 +++++++++---------- src/nostalgia/glutils/CMakeLists.txt | 27 ++++++++++++++++++ .../{core/userland => glutils}/glutils.cpp | 2 +- .../{core/userland => glutils}/glutils.hpp | 4 ++- 7 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 src/nostalgia/glutils/CMakeLists.txt rename src/nostalgia/{core/userland => glutils}/glutils.cpp (97%) rename src/nostalgia/{core/userland => glutils}/glutils.hpp (97%) diff --git a/src/nostalgia/CMakeLists.txt b/src/nostalgia/CMakeLists.txt index 9389cc04f..ce3b0e403 100644 --- a/src/nostalgia/CMakeLists.txt +++ b/src/nostalgia/CMakeLists.txt @@ -20,6 +20,7 @@ if(NOSTALGIA_BUILD_PLAYER) endif() if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") + add_subdirectory(glutils) add_subdirectory(tools) if(NOSTALGIA_BUILD_STUDIO) add_subdirectory(studio) diff --git a/src/nostalgia/core/glfw/CMakeLists.txt b/src/nostalgia/core/glfw/CMakeLists.txt index 70bb2ae1c..802c7af7c 100644 --- a/src/nostalgia/core/glfw/CMakeLists.txt +++ b/src/nostalgia/core/glfw/CMakeLists.txt @@ -9,6 +9,7 @@ find_package(glfw3 REQUIRED) target_link_libraries( NostalgiaCore-GLFW PUBLIC glfw::glfw + NostalgiaGlUtils NostalgiaCore-Userspace ) diff --git a/src/nostalgia/core/userland/CMakeLists.txt b/src/nostalgia/core/userland/CMakeLists.txt index f1d628d7e..21fc4ae98 100644 --- a/src/nostalgia/core/userland/CMakeLists.txt +++ b/src/nostalgia/core/userland/CMakeLists.txt @@ -2,7 +2,6 @@ add_library( NostalgiaCore-Userspace OBJECT gfx.cpp gfx_opengl.cpp - glutils.cpp media.cpp ) @@ -10,19 +9,13 @@ if(NOT MSVC) target_compile_options(NostalgiaCore-Userspace PRIVATE -Wsign-conversion) endif() -if(APPLE) - find_package(OpenGL REQUIRED) -else() - set(OPENGL_gl_LIBRARY GL) -endif() - target_link_libraries( NostalgiaCore-Userspace PUBLIC OxClaw OxFS OxStd NostalgiaCore - ${OPENGL_gl_LIBRARY} + NostalgiaGlUtils ) install( diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index 1d39ffe1a..bb606fb48 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -11,11 +11,11 @@ #include #include +#include + #include #include -#include "glutils.hpp" - namespace nostalgia::core { @@ -30,10 +30,10 @@ constexpr auto BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength; constexpr auto BgVertexEboLength = 6; struct Background { - GLVertexArray vao; - GLBuffer vbo; - GLBuffer ebo; - GLTexture tex; + glutils::GLVertexArray vao; + glutils::GLBuffer vbo; + glutils::GLBuffer ebo; + glutils::GLTexture tex; bool enabled = false; bool updated = false; std::array bgVertices = {}; @@ -41,7 +41,7 @@ struct Background { }; struct GlImplData { - GLProgram bgShader; + glutils::GLProgram bgShader; int64_t prevFpsCheckTime = 0; uint64_t draws = 0; std::array backgrounds; @@ -119,16 +119,16 @@ static void initBackgroundBufferObjects(Context *ctx, Background *bg) noexcept { } } -static GLVertexArray genVertexArrayObject() noexcept { +static glutils::GLVertexArray genVertexArrayObject() noexcept { GLuint vao = 0; glGenVertexArrays(1, &vao); - return GLVertexArray(vao); + return glutils::GLVertexArray(vao); } -static GLBuffer genBuffer() noexcept { +static glutils::GLBuffer genBuffer() noexcept { GLuint buff = 0; glGenBuffers(1, &buff); - return GLBuffer(buff); + return glutils::GLBuffer(buff); } static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) noexcept { @@ -151,10 +151,10 @@ static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) ox::bit_cast(2 * sizeof(float))); } -static GLTexture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept { +static glutils::GLTexture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept { GLuint texId = 0; glGenTextures(1, &texId); - GLTexture tex(texId); + glutils::GLTexture tex(texId); tex.width = w; tex.height = h; glActiveTexture(GL_TEXTURE0); @@ -208,7 +208,7 @@ static void drawBackgrounds(GlImplData *id) noexcept { ox::Error init(Context *ctx) noexcept { const auto id = new GlImplData; ctx->setRendererData(id); - oxReturnError(buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader)); + oxReturnError(glutils::buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader)); for (auto &bg : id->backgrounds) { initBackgroundBufferset(ctx, id->bgShader, &bg); } diff --git a/src/nostalgia/glutils/CMakeLists.txt b/src/nostalgia/glutils/CMakeLists.txt new file mode 100644 index 000000000..d24acfc86 --- /dev/null +++ b/src/nostalgia/glutils/CMakeLists.txt @@ -0,0 +1,27 @@ +add_library( + NostalgiaGlUtils OBJECT + glutils.cpp +) + +if(NOT MSVC) + target_compile_options(NostalgiaGlUtils PRIVATE -Wsign-conversion) +endif() + +if(APPLE) + find_package(OpenGL REQUIRED) +else() + set(OPENGL_gl_LIBRARY GL) +endif() + +target_link_libraries( + NostalgiaGlUtils PUBLIC + OxStd + ${OPENGL_gl_LIBRARY} +) + +install( + TARGETS + NostalgiaGlUtils + DESTINATION + include/nostalgia/glutils +) diff --git a/src/nostalgia/core/userland/glutils.cpp b/src/nostalgia/glutils/glutils.cpp similarity index 97% rename from src/nostalgia/core/userland/glutils.cpp rename to src/nostalgia/glutils/glutils.cpp index 940f21cef..b566f456d 100644 --- a/src/nostalgia/core/userland/glutils.cpp +++ b/src/nostalgia/glutils/glutils.cpp @@ -11,7 +11,7 @@ #include "glutils.hpp" -namespace nostalgia::core::renderer { +namespace nostalgia::glutils { void deleteBuffer(GLuint b) noexcept { glDeleteBuffers(1, &b); diff --git a/src/nostalgia/core/userland/glutils.hpp b/src/nostalgia/glutils/glutils.hpp similarity index 97% rename from src/nostalgia/core/userland/glutils.hpp rename to src/nostalgia/glutils/glutils.hpp index 21910de0d..32a11f11a 100644 --- a/src/nostalgia/core/userland/glutils.hpp +++ b/src/nostalgia/glutils/glutils.hpp @@ -6,6 +6,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include + #define GL_GLEXT_PROTOTYPES 1 #ifdef OX_OS_Darwin #define GL_SILENCE_DEPRECATION @@ -14,7 +16,7 @@ #include #endif -namespace nostalgia::core::renderer { +namespace nostalgia::glutils { struct Empty {};