spacebox/src/GLObject.hpp
Cocktail Frank c1f8edace7 Add functions for reading sprite and text attributes
Add a function for reading a sprite's translation. Add functions for
reading a text object's foreground, background, and font.

Log a warning when the draw function of a sprite containing textures is
called but the currently active texture will not be drawn because it
has not been generated yet.

Line-length linting.
2025-02-18 16:23:59 -05:00

115 lines
2.9 KiB
C++

/* +=======================================================+
____/ \____ /: Open source game framework licensed to freely use, :
\ / / : copy, and modify - created for dank.game :
+==\ ^__^ /==+ : :
: ~/ \~ : : Download at https://open.shampoo.ooo/shampoo/spacebox :
: ~~~~~~~~~~~~ : +=======================================================+
: SPACE ~~~~~ : /
: ~~~~~~~ BOX :/
+==============+
The abstract GLObject class is meant to be inherited and implemented by more specific
types of OpenGL objects, for example Textures or Buffer objects. It stores the object's
ID and declares virtual functions for generating, binding, and destroying the object for
the derivative class to implement.
An appropriate deleter function must be passed in to the constructor from the derived
class. It can be a custom function, or it can be one of the GL deleter functions like
glDeleteTextures.
A VAO class and a general Buffer class are also defined here. The VAO class just fills
in the abstract methods of the GLObject class and doesn't have any further SPACE BOX
specific implementation. The buffer class is probably most useful being inherited by a
derived class that implements a more specific type of buffer that passes its target to
the base class, like the VBO class.
*/
#pragma once
#include <iostream>
#include <sstream>
#include <memory>
#include <functional>
#include <stdexcept>
#include "gl.h"
#include "Log.hpp"
namespace sb
{
/* Calls to the stream operator from within this namespace will search the global namespace as well as the current */
using std::operator<<;
class GLObject;
std::ostream& operator<<(std::ostream&, const GLObject&);
class GLObject
{
private:
/* function types */
typedef std::function<void(GLsizei, GLuint*)> generator_function;
typedef std::function<void(GLuint*)> deleter_function;
std::shared_ptr<GLuint> object_id = nullptr;
deleter_function deleter = nullptr;
protected:
GLObject(deleter_function);
virtual void bind() const = 0;
void generate(generator_function);
public:
virtual void id(GLuint);
virtual GLuint id() const;
virtual bool generated() const;
virtual ~GLObject() = default;
friend std::ostream& operator<<(std::ostream&, const GLObject&);
};
class VAO : public GLObject
{
public:
VAO();
void generate();
void bind() const;
};
void vao_deleter(GLuint*);
class Buffer : public GLObject
{
private:
GLenum buffer_target = GL_INVALID_ENUM;
protected:
GLenum target() const;
public:
Buffer();
Buffer(GLenum);
void target(GLenum);
void generate();
void bind() const;
void bind(GLenum);
};
void buffer_deleter(GLuint*);
}