Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::opengl::Texture Class Reference

An image on the GPU, ready to be drawn. More...

#include <aegis2d/render/opengl/texture.hpp>

Public Member Functions

 Texture (const Image &image)
 Sends an image to the GPU.
 
 Texture (const Texture &)=delete
 
Texture & operator= (const Texture &)=delete
 
 Texture (Texture &&other) noexcept
 Takes over the texture of other, which becomes empty.
 
Texture & operator= (Texture &&other) noexcept
 Deletes this texture and takes over the texture of other.
 
 ~Texture ()
 Deletes the texture.
 
TextureSize size () const noexcept
 Size of the image.
 
std::uint32_t id () const noexcept
 OpenGL name of the texture.
 
void bind (std::uint32_t unit=0) const noexcept
 Binds the texture to a texture unit, for the next draw calls.
 
Image pixels () const
 Reads the texture back from the GPU.
 

Static Public Member Functions

static Texture load (const std::filesystem::path &path)
 Reads an image file and sends it to the GPU.
 

Detailed Description

An image on the GPU, ready to be drawn.

The pixels are stored as RGBA, 8 bits per channel, and sampled with the nearest texel, so pixel art scaled up stays sharp instead of blurred, and the frames of a sprite sheet do not blend into each other; the edges are clamped, so the border of a sprite never picks up texels from the opposite side.

player.bind(0); // for a shader sampling unit 0
An image on the GPU, ready to be drawn.
Definition texture.hpp:26
static Texture load(const std::filesystem::path &path)
Reads an image file and sends it to the GPU.
void bind(std::uint32_t unit=0) const noexcept
Binds the texture to a texture unit, for the next draw calls.
std::filesystem::path asset_path(const std::filesystem::path &relative)
Path of one asset.

The renderer makes one for each IRenderer::create_texture(); games usually go through it or a TextureManager rather than using this class. Needs a current Context; destroy the texture before the context.

Constructor & Destructor Documentation

◆ Texture() [1/3]

aegis2d::render::opengl::Texture::Texture ( const Image &  image)
explicit

Sends an image to the GPU.

Parameters
imageRGBA image, rows from the top.
Exceptions
std::invalid_argumentif image is empty or its pixels do not match its size.

◆ Texture() [2/3]

aegis2d::render::opengl::Texture::Texture ( const Texture &  )
delete

◆ Texture() [3/3]

aegis2d::render::opengl::Texture::Texture ( Texture &&  other)
noexcept

Takes over the texture of other, which becomes empty.

Parameters
otherTexture to move from.

◆ ~Texture()

aegis2d::render::opengl::Texture::~Texture ( )

Deletes the texture.

Member Function Documentation

◆ bind()

void aegis2d::render::opengl::Texture::bind ( std::uint32_t  unit = 0) const
noexcept

Binds the texture to a texture unit, for the next draw calls.

Parameters
unitTexture unit, the value of the shader's sampler uniform.

◆ id()

std::uint32_t aegis2d::render::opengl::Texture::id ( ) const
noexcept

OpenGL name of the texture.

Returns
The texture object, 0 once moved from.

◆ load()

static Texture aegis2d::render::opengl::Texture::load ( const std::filesystem::path &  path)
static

Reads an image file and sends it to the GPU.

Parameters
pathPNG file, or any format of Image::load(); UTF-8 paths work everywhere.
Returns
The texture.
Exceptions
ImageErrorif the file cannot be read or decoded.

◆ operator=() [1/2]

Texture & aegis2d::render::opengl::Texture::operator= ( const Texture &  )
delete

◆ operator=() [2/2]

Texture & aegis2d::render::opengl::Texture::operator= ( Texture &&  other)
noexcept

Deletes this texture and takes over the texture of other.

Parameters
otherTexture to move from.
Returns
This texture.

◆ pixels()

Image aegis2d::render::opengl::Texture::pixels ( ) const

Reads the texture back from the GPU.

For tests and tools: it waits for the GPU.

Returns
The pixels as stored, RGBA rows from the top.

◆ size()

TextureSize aegis2d::render::opengl::Texture::size ( ) const
noexcept

Size of the image.

Returns
Width and height in pixels; zero once moved from.

The documentation for this class was generated from the following file: