From 6bdee3fc1cdbe9a15cdb08160f8fbed52548b791 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 20 Mar 2021 15:47:32 -0500 Subject: [PATCH] [nostalgia/core/userland] Add texture deletion --- src/nostalgia/core/userland/gfx_opengl.cpp | 8 ++--- src/nostalgia/core/userland/glutils.cpp | 10 +++++-- src/nostalgia/core/userland/glutils.hpp | 34 ++++++++++------------ 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index 6cc0a1234..170811398 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -137,11 +137,11 @@ static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) } static ox::Error loadTexture(Texture *tex, void *pixels) { - if (tex->texId == 0) { - glGenTextures(1, &tex->texId); + if (tex->id == 0) { + glGenTextures(1, &tex->id); } glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, tex->texId); + glBindTexture(GL_TEXTURE_2D, tex->id); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -258,7 +258,7 @@ void draw(Context *ctx) { void clearTileLayer(Context *ctx, int layer) { const auto id = ctx->rendererData(); - auto bg = id->backgrounds[static_cast(layer)]; + auto &bg = id->backgrounds[static_cast(layer)]; initBackgroundBufferObjects(ctx, &bg); bg.updated = true; } diff --git a/src/nostalgia/core/userland/glutils.cpp b/src/nostalgia/core/userland/glutils.cpp index 134c5b91e..04b51c4ea 100644 --- a/src/nostalgia/core/userland/glutils.cpp +++ b/src/nostalgia/core/userland/glutils.cpp @@ -15,12 +15,17 @@ namespace nostalgia::core::renderer { +void deleteTexture(GLuint t) { + glDeleteTextures(1, &t); +} + template struct GLobject; template struct GLobject; +template struct GLobject; [[nodiscard]] static ox::Result buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) { - const auto shader = glCreateShader(shaderType); + Shader shader(glCreateShader(shaderType)); glShaderSource(shader, 1, &src, nullptr); glCompileShader(shader); GLint status; @@ -29,10 +34,9 @@ static ox::Result buildShader(GLuint shaderType, const GLchar *src, cons std::array errMsg; glGetShaderInfoLog(shader, errMsg.size(), nullptr, errMsg.data()); oxErrorf("shader compile error in {}: {}", shaderName, errMsg.data()); - glDeleteShader(shader); return OxError(1, "shader compile error"); } - return Shader(shader); + return ox::move(shader); } [[nodiscard]] diff --git a/src/nostalgia/core/userland/glutils.hpp b/src/nostalgia/core/userland/glutils.hpp index 5f3384beb..3b2d19232 100644 --- a/src/nostalgia/core/userland/glutils.hpp +++ b/src/nostalgia/core/userland/glutils.hpp @@ -19,8 +19,18 @@ namespace nostalgia::core::renderer { -template -struct GLobject { +struct Empty {}; + +struct TextureBase { + + GLsizei width = 0; + GLsizei height = 0; + +}; + + +template +struct GLobject: public Base { GLuint id = 0; @@ -61,26 +71,14 @@ struct GLobject { }; +void deleteTexture(GLuint t); + extern template struct GLobject; extern template struct GLobject; +extern template struct GLobject; using Shader = GLobject; using Program = GLobject; - -struct Texture { - - GLuint texId = 0; - GLsizei width = 0; - GLsizei height = 0; - - constexpr operator GLuint&() noexcept { - return texId; - } - - constexpr operator const GLuint&() const noexcept { - return texId; - } - -}; +using Texture = GLobject; struct Bufferset { GLuint vao = 0;