[nostalgia] Move glutils into separate library

This commit is contained in:
2021-05-15 02:27:13 -05:00
parent 282bdc0fd7
commit 63e46d7bb4
7 changed files with 48 additions and 24 deletions
+27
View File
@@ -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
)
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright 2016 - 2021 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <ox/std/bstring.hpp>
#include <ox/std/trace.hpp>
#include "glutils.hpp"
namespace nostalgia::glutils {
void deleteBuffer(GLuint b) noexcept {
glDeleteBuffers(1, &b);
}
void deleteTexture(GLuint t) noexcept {
glDeleteTextures(1, &t);
}
void deleteVertexArray(GLuint v) noexcept {
glDeleteVertexArrays(1, &v);
}
template struct GLObject<deleteBuffer>;
template struct GLObject<deleteTexture, TextureBase>;
template struct GLObject<deleteVertexArray>;
template struct GLObject<glDeleteProgram>;
template struct GLObject<glDeleteShader>;
[[nodiscard]]
static ox::Result<GLShader> buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) noexcept {
GLShader shader(glCreateShader(shaderType));
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
ox::BString<1000> errMsg;
glGetShaderInfoLog(shader, errMsg.cap(), nullptr, errMsg.data());
oxErrorf("shader compile error in {}: {}", shaderName, errMsg);
return OxError(1, "shader compile error");
}
return ox::move(shader);
}
[[nodiscard]]
ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept {
oxRequire(vs, buildShader(GL_VERTEX_SHADER, vert, "vshad"));
oxRequire(fs, buildShader(GL_FRAGMENT_SHADER, frag, "fshad"));
GLProgram prgm(glCreateProgram());
glAttachShader(prgm, vs);
glAttachShader(prgm, fs);
glLinkProgram(prgm);
return ox::move(prgm);
}
}
+113
View File
@@ -0,0 +1,113 @@
/*
* Copyright 2016 - 2021 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <ox/std/defines.hpp>
#define GL_GLEXT_PROTOTYPES 1
#ifdef OX_OS_Darwin
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl3.h>
#else
#include <GLES3/gl3.h>
#endif
namespace nostalgia::glutils {
struct Empty {};
struct TextureBase {
GLsizei width = 0;
GLsizei height = 0;
constexpr TextureBase() noexcept = default;
constexpr TextureBase(TextureBase &&tb) noexcept {
width = tb.width;
height = tb.height;
tb.width = 0;
tb.height = 0;
}
constexpr TextureBase &operator=(TextureBase &&tb) noexcept {
width = tb.width;
height = tb.height;
tb.width = 0;
tb.height = 0;
return *this;
}
};
template<auto del, typename Base = Empty>
struct GLObject: public Base {
GLuint id = 0;
constexpr GLObject() noexcept = default;
explicit constexpr GLObject(GLuint id) noexcept {
this->id = id;
}
constexpr GLObject(GLObject &&o) noexcept: Base(ox::move(o)) {
id = o.id;
o.id = 0;
}
~GLObject() {
del(id);
}
GLObject &operator=(GLObject &&o) noexcept {
if (this != &o) {
del(id);
Base::operator=(ox::move(o));
id = o.id;
o.id = 0;
}
return *this;
}
constexpr GLuint release() noexcept {
auto out = id;
id = 0;
return out;
}
constexpr operator GLuint&() noexcept {
return id;
}
constexpr operator const GLuint&() const noexcept {
return id;
}
};
void deleteBuffer(GLuint b) noexcept;
void deleteTexture(GLuint t) noexcept;
void deleteVertexArray(GLuint v) noexcept;
extern template struct GLObject<deleteBuffer>;
extern template struct GLObject<deleteTexture, TextureBase>;
extern template struct GLObject<deleteVertexArray>;
extern template struct GLObject<glDeleteProgram>;
extern template struct GLObject<glDeleteShader>;
using GLBuffer = GLObject<deleteBuffer>;
using GLShader = GLObject<glDeleteShader>;
using GLProgram = GLObject<glDeleteProgram>;
using GLTexture = GLObject<deleteTexture, TextureBase>;
using GLVertexArray = GLObject<deleteVertexArray>;
[[nodiscard]]
ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept;
}