smk::Texture Struct Reference

#include <Texture.hpp>

Description

A texture loaded from a file into the GPU. This class support the move and copy operators. Its underlying GPU texture is refcounted and released when then last smk::Texture is deleted.

Example:

auto texture = smk::Texture("./ball.png");

Supported image encoding:

The SMK library uses the stb_image. So it supports the same image types:

  • JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
  • PNG 1/2/4/8/16-bit-per-channel
  • TGA
  • BMP non-1bpp, non-RLE
  • PSD (composited view only, no extra channels, 8/16 bit-per-channel)
  • GIF
  • HDR (radiance rgbE format)
  • PIC (Softimage PIC)
  • PNM (PPM and PGM binary only)
Examples
shape_3d.cpp, sprite.cpp, sprite_move.cpp, and texture_subrectangle.cpp.

Definition at line 38 of file Texture.hpp.

Classes

struct  Option
 

Public Member Functions

 Texture ()
 The null texture. More...
 
 Texture (const std::string &filename)
 Load a texture from a file. More...
 
 Texture (const std::string &filename, const Option &option)
 Load a texture from a file. More...
 
 Texture (const uint8_t *data, int width, int height)
 Load a texture from memory (RAM) More...
 
 Texture (const uint8_t *data, int width, int height, const Option &option)
 Load a texture from memory (RAM) More...
 
 Texture (GLuint id, int width, int height)
 Import an already loaded texture. Useful. More...
 
void Bind (GLuint active_texture=GL_TEXTURE0) const
 
int width () const
 Access the width of the texture. More...
 
int height () const
 Access the height of the texture. More...
 
GLuint id () const
 Access the ID of the texture. More...
 
 operator bool () const
 
 Texture (Texture &&) noexcept
 
 Texture (const Texture &)
 
Textureoperator= (Texture &&) noexcept
 
Textureoperator= (const Texture &)
 
bool operator== (const Texture &other) const
 
bool operator!= (const Texture &other) const
 

Class Documentation

◆ smk::Texture::Option

struct smk::Texture::Option

Definition at line 40 of file Texture.hpp.

Class Members
GLint format
bool generate_mipmap
GLint internal_format
GLint mag_filter
GLint min_filter
GLint type
GLint wrap_s
GLint wrap_t

Constructor & Destructor Documentation

◆ Texture() [1/8]

smk::Texture::Texture ( )
default

The null texture.

◆ Texture() [2/8]

smk::Texture::Texture ( const std::string &  filename)

Load a texture from a file.

Parameters
filenameThe file name of the image to be loaded

Definition at line 30 of file Texture.cpp.

30: Texture(filename, Option()) {}
Texture()
The null texture.

◆ Texture() [3/8]

smk::Texture::Texture ( const std::string &  filename,
const Option option 
)

Load a texture from a file.

Parameters
filenameThe file name of the image to be loaded.
optionAdditionnal option (texture wrap, min filter, mag filter, ...)

Definition at line 35 of file Texture.cpp.

35 {
36 FILE* file = fopen(filename.c_str(), "rb"); // NOLINT
37 if (!file) {
38 std::cerr << "File " << filename << " not found" << std::endl;
39 fclose(file); // NOLINT
40 return;
41 }
42
43 int comp = -1;
44 unsigned char* data = stbi_load_from_file(file, &width_, &height_, &comp, 0);
45 fclose(file); // NOLINT
46
47 // Canonicalize the image to RGBA(8,8,8,8) and maybe on power_of_2 texture.
48 int width_b = next_power_of_2(width_);
49 int height_b = next_power_of_2(height_);
50 std::vector<uint8_t> transformed(width_b * height_b * 4, 0);
51 for (int y = 0; y < height_; ++y) {
52 for (int x = 0; x < width_; ++x) {
53 for (int c = 0; c < 4; ++c) {
54 // NOLINTNEXTLINE
55 transformed[c + 4 * (x + width_b * y)] =
56 // NOLINTNEXTLINE
57 (c == 3 && comp != 4) ? 255 : data[c + comp * (x + width_ * y)];
58 }
59 }
60 }
61 Load(transformed.data(), width_b, height_b, option);
62 stbi_image_free(data);
63}

◆ Texture() [4/8]

smk::Texture::Texture ( const uint8_t *  data,
int  width,
int  height 
)

Load a texture from memory (RAM)

Parameters
dataThe memory area to read the image from.
widththe image's with.
heightthe image's height.

Definition at line 69 of file Texture.cpp.

70 : Texture(data, width, height, Option()) {}
int height() const
Access the height of the texture.
Definition: Texture.cpp:201
int width() const
Access the width of the texture.
Definition: Texture.cpp:195

◆ Texture() [5/8]

smk::Texture::Texture ( const uint8_t *  data,
int  width,
int  height,
const Option option 
)

Load a texture from memory (RAM)

Parameters
dataThe memory area to read the image from.
widththe image's with.
heightthe image's height.
optionAdditionnal option (texture wrap, min filter, mag filter, ...)

Definition at line 77 of file Texture.cpp.

81 : Texture() {
82 width_ = width;
83 height_ = height;
84 Load(data, width_, height_, option);
85}

◆ Texture() [6/8]

smk::Texture::Texture ( GLuint  id,
int  width,
int  height 
)

Import an already loaded texture. Useful.

Parameters
idThe OpenGL identifier of the loaded texture.
widththe image's with.
heightthe image's height.

Definition at line 110 of file Texture.cpp.

111 : id_(id), width_(width), height_(height) {}

◆ ~Texture()

smk::Texture::~Texture ( )

Definition at line 115 of file Texture.cpp.

115 {
116 Release();
117}

◆ Texture() [7/8]

smk::Texture::Texture ( Texture &&  other)
noexcept

Definition at line 141 of file Texture.cpp.

141 {
142 operator=(std::move(other));
143}

◆ Texture() [8/8]

smk::Texture::Texture ( const Texture other)

Definition at line 145 of file Texture.cpp.

145 {
146 operator=(other);
147}

Member Function Documentation

◆ Bind()

void smk::Texture::Bind ( GLuint  active_texture = GL_TEXTURE0) const

Definition at line 180 of file Texture.cpp.

180 {
181 glActiveTexture(active_texture);
182 glBindTexture(GL_TEXTURE_2D, id_);
183}

◆ height()

int smk::Texture::height ( ) const

Access the height of the texture.

Returns
The texture's height in pixel

Definition at line 201 of file Texture.cpp.

201 {
202 return height_;
203}

◆ id()

GLuint smk::Texture::id ( ) const

Access the ID of the texture.

Returns
The texture's ID.

Definition at line 207 of file Texture.cpp.

207 {
208 return id_;
209}

◆ operator bool()

smk::Texture::operator bool ( ) const
inline

Definition at line 65 of file Texture.hpp.

65{ return id_ != 0; }

◆ operator!=()

bool smk::Texture::operator!= ( const Texture other) const

Definition at line 189 of file Texture.cpp.

189 {
190 return id_ != other.id_;
191}

◆ operator=() [1/2]

Texture & smk::Texture::operator= ( const Texture other)

Definition at line 158 of file Texture.cpp.

158 {
159 if (&other == this) {
160 return *this;
161 }
162 Release();
163 id_ = other.id_;
164 width_ = other.width_;
165 height_ = other.height_;
166
167 if (!other.id_) {
168 return *this;
169 }
170
171 if (!other.ref_count_) {
172 other.ref_count_ = new int(1); // NOLINT
173 }
174
175 ref_count_ = other.ref_count_;
176 (*ref_count_)++;
177 return *this;
178}

◆ operator=() [2/2]

Texture & smk::Texture::operator= ( Texture &&  other)
noexcept

Definition at line 149 of file Texture.cpp.

149 {
150 Release();
151 std::swap(id_, other.id_);
152 std::swap(width_, other.width_);
153 std::swap(height_, other.height_);
154 std::swap(ref_count_, other.ref_count_);
155 return *this;
156}

◆ operator==()

bool smk::Texture::operator== ( const Texture other) const

Definition at line 185 of file Texture.cpp.

185 {
186 return id_ == other.id_;
187}

◆ width()

int smk::Texture::width ( ) const

Access the width of the texture.

Returns
The texture's width in pixel
Examples
sprite_move.cpp.

Definition at line 195 of file Texture.cpp.

195 {
196 return width_;
197}

The documentation for this struct was generated from the following files: