spacebox/src/Texture.hpp

94 lines
3.2 KiB
C++

/* /\ +--------------------------------------------------------------+
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - originally created at [http://nugget.fun] |
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+--------------+
[Texture.hpp]
The Texture class abstracts the file opening, data loading and binding steps of Open GL
texture creation. Currently it only supports loading GL_TEXTURE_2D with GL_RGB8 pixels
and automatic GL_NEAREST mipmapping. Support may be added for users to pass in their own
pixel data, to customize mipmapping, and more. The class can also be used in a custom
way by passing the Texture ID to GL functions instead of using the class's member
functions.
*/
#ifndef SB_TEXTURE_H_
#define SB_TEXTURE_H_
#include <sstream>
#include <stdexcept>
#include "glm/vec2.hpp"
#include "SDL.h"
#include "SDL_image.h"
#include "sdl2-gfx/SDL2_rotozoom.h"
#include "filesystem.hpp"
#include "GLObject.hpp"
#include "Log.hpp"
namespace sb
{
class Texture : public GLObject
{
private:
fs::path path = "";
public:
Texture();
Texture(fs::path);
void associate(fs::path);
void generate();
void generate(glm::vec2);
void load();
void load(fs::path);
void load(SDL_RWops*);
void load(SDL_Surface*);
void load(void* pixels, glm::vec2 size, GLenum format = GL_RGBA, GLenum type = GL_UNSIGNED_BYTE);
#ifndef __EMSCRIPTEN__
/*!
* @overload load(void* pixels, glm::vec2 size, GLenum format, GLenum type)
*
* The texture must have been previously generated with a size to use this generic pixel data load function. The size is
* determined with `glGetTexLevelParameter`, which is only available to OpenGL ES 3.1+, so this overload is not available
* for Emscripten builds.
*
* see OpenGL's `glTexSubImage2D`
*
* @param pixels pointer to pixel data
* @param format GL format of pixel data (for example, GL_RGBA)
* @param type data type of the pixel data (for example, GL_UNSIGNED_BYTE if each byte is one of the RGBA values)
*/
void load(void* pixels, GLenum format = GL_RGBA, GLenum type = GL_UNSIGNED_BYTE);
/*!
* Return the size in pixels of mipmap level 0 (the only mipmap level supported by this class). If the texture hasn't been,
* generated, return {0, 0}. `glGetTexLevelParameter` is only available in OpenGL ES 3.1+, so this function is removed from
* Emscripten builds.
*
* @return glm::vec2 A vector consisting of {TEXTURE_MIPMAP_WIDTH, TEXTURE_MIPMAP_HEIGHT}
*/
glm::vec2 size() const;
#endif
void bind() const override;
bool operator==(const Texture&) const;
};
void texture_deleter(GLuint*);
}
#endif