diff --git a/Makefile b/Makefile index 93e6b9b..48bdf14 100644 --- a/Makefile +++ b/Makefile @@ -69,7 +69,7 @@ $(SB_SRC_DIR)Box.o : $(addprefix $(SB_SRC_DIR),extension.hpp Segment.hpp) $(SB_SRC_DIR)Segment.o : $(addprefix $(SB_SRC_DIR),extension.hpp Box.hpp) $(SB_SRC_DIR)Pixels.o : $(addprefix $(SB_SRC_DIR),Box.hpp extension.hpp) $(SB_SRC_DIR)Audio.o : $(addprefix $(SB_SRC_DIR),Node.hpp Display.hpp Configuration.hpp Box.hpp filesystem.hpp extension.hpp) -$(SB_SRC_DIR)Texture.o : $(addprefix $(SB_SRC_DIR),GLObject.cpp filesystem.hpp Game.hpp) +$(SB_SRC_DIR)Texture.o : $(addprefix $(SB_SRC_DIR),GLObject.hpp filesystem.hpp Game.hpp) $(SRC_DIR)Item.o : $(addprefix $(SB_SRC_DIR),extension.hpp Node.hpp Texture.hpp) $(SRC_DIR)Pudding.o : $(SRC_H_FILES) $(SB_H_FILES) %.o : %.cpp %.hpp diff --git a/lib/sb b/lib/sb index 6adac98..f03d58d 160000 --- a/lib/sb +++ b/lib/sb @@ -1 +1 @@ -Subproject commit 6adac9806faba1483da80895de4d9edb783cdb1f +Subproject commit f03d58dae13d673f4db26af88710e541a892cb77 diff --git a/resource/tile/puddingtile.jpg b/resource/tile/puddingtile.jpg new file mode 100644 index 0000000..edc333e Binary files /dev/null and b/resource/tile/puddingtile.jpg differ diff --git a/resource/tile/rat1tile.jpg b/resource/tile/rat1tile.jpg new file mode 100644 index 0000000..62cbd6e Binary files /dev/null and b/resource/tile/rat1tile.jpg differ diff --git a/src/Pudding.cpp b/src/Pudding.cpp index 844051d..fe66502 100644 --- a/src/Pudding.cpp +++ b/src/Pudding.cpp @@ -161,10 +161,9 @@ void Pudding::load_gl_context() { log("could not create capture frame thread context"); } - /* Allocate a vertex array object, bind it as current, doesn't need to be a member var because the same one is always bound */ - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + /* Generate a vertex array object ID, bind it as current */ + vao.generate(); + vao.bind(); /* 2D vertices for any texture that is a plane spanning the screen */ std::array rectangle_vertices = {{ {-1.0f, 1.0f}, {1.0f, 1.0f}, {-1.0f, -1.0f}, @@ -178,9 +177,8 @@ void Pudding::load_gl_context() /* Generate one vertex buffer object to hold all vertices and rectangle UV map. Since we're using one buffer, data * will be copied in one after the other, offset to after the previous data location. The same buffer offset will * be passed to the vertex attributes for each data. */ - GLuint vbo; - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); + vbo.generate(); + vbo.bind(); /* allocate space for vertices, UV and colors, and copy rectangle vertices in at initialization */ GLsizeiptr vbo_size = (rectangle_vertices.size() + rectangle_uv.size() + pudding_uv.size()) * sizeof(glm::vec2) + (pudding_vertices.size() + pudding_colors.size()) * sizeof(glm::vec3); @@ -855,3 +853,49 @@ void Pudding::update() previous_barcode = current_barcode; } } + +VAO::VAO() : GLObject(vao_deleter) {} + +/* Bind this VAO to the current GL context */ +void VAO::bind() const +{ + glBindVertexArray(id()); +} + +/* Forward the GL VAO generate function to the base class */ +void VAO::generate() +{ + GLObject::generate(glGenVertexArrays); +} + +/* This function gets passed to the abstract base class for deleting the VAO data when the ID + * pointer goes out of scope (when all instances of this VAO and its copies are out of scope) */ +void vao_deleter(GLuint* id) +{ + /* not sure why SDL_Log works here on program exit but SDL_LogDebug and SDL_LogInfo don't */ + SDL_Log("destroying VAO ID %i", *id); + glDeleteVertexArrays(1, id); +} + +VBO::VBO() : GLObject(vbo_deleter) {} + +/* Bind this VBO to the current GL context */ +void VBO::bind() const +{ + glBindBuffer(GL_ARRAY_BUFFER, id()); +} + +/* Forward the GL buffer generate function to the base class */ +void VBO::generate() +{ + GLObject::generate(glGenBuffers); +} + +/* This function gets passed to the abstract base class for deleting the VBO data when the ID + * pointer goes out of scope (when all instances of this VBO and its copies are out of scope) */ +void vbo_deleter(GLuint* id) +{ + /* not sure why SDL_Log works here on program exit but SDL_LogDebug and SDL_LogInfo don't */ + SDL_Log("destroying VBO ID %i", *id); + glDeleteBuffers(1, id); +} diff --git a/src/Pudding.hpp b/src/Pudding.hpp index 5e7f078..215e69a 100644 --- a/src/Pudding.hpp +++ b/src/Pudding.hpp @@ -26,6 +26,33 @@ #include "Item.hpp" #include "Animation.hpp" #include "Texture.hpp" +#include "GLObject.hpp" + +class VAO : public GLObject +{ + +public: + + VAO(); + void generate(); + void bind() const; + +}; + +void vao_deleter(GLuint*); + +class VBO : public GLObject +{ + +public: + + VBO(); + void generate(); + void bind() const; + +}; + +void vbo_deleter(GLuint*); class Pudding : public Game { @@ -79,6 +106,8 @@ private: std::vector tiles; Texture capture_texture_front_buffer, capture_texture_back_buffer; Texture& capture_texture = capture_texture_front_buffer; + VAO vao; + VBO vbo; void set_pudding_model(float, float, int, int = 1, float = -1, float = 1, float = 0.3f); void load_gl_context(); @@ -109,43 +138,6 @@ public: }; -class VAO -{ - -private: - - std::shared_ptr array_id = nullptr; - -public: - - VAO() {}; - void generate(); - void id(GLuint); - GLuint id() const; - void bind() const; - static void destroy(GLuint*); - -}; - -class Buffer -{ - -private: - - std::shared_ptr buffer_id = nullptr; - -public: - - Buffer() {}; - void generate(); - void id(GLuint); - -}; - -class VBO : public Buffer -{ -}; - class Attributes { };