GameKit  0.0.1a
C++ gamedev tools
Texture.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Texture.cpp
5  *
6  * Description:
7  *
8  * Created: 20/12/2014 01:15:43
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/gl/GLCheck.hpp"
15 #include "gk/gl/Texture.hpp"
16 #include "gk/core/Exception.hpp"
17 
18 namespace gk {
19 
20 Texture::Texture(const std::string &filename) {
21  loadFromFile(filename);
22 }
23 
24 Texture::Texture(SDL_Surface *surface) {
25  loadFromSurface(surface);
26 }
27 
29  m_filename = texture.m_filename;
30 
31  m_width = texture.m_width;
32  m_height = texture.m_height;
33 
34  m_texture = texture.m_texture;
35  texture.m_texture = 0;
36 }
37 
38 Texture::~Texture() noexcept {
39  glCheck(glDeleteTextures(1, &m_texture));
40 }
41 
43  m_filename = texture.m_filename;
44 
45  m_width = texture.m_width;
46  m_height = texture.m_height;
47 
48  m_texture = texture.m_texture;
49  texture.m_texture = 0;
50 
51  return *this;
52 }
53 
54 void Texture::loadFromFile(const std::string &filename) {
56 
57  SDL_Surface *surface = IMG_Load(m_filename.c_str());
58  if(!surface) {
59  throw EXCEPTION("Failed to load texture:", filename);
60  }
61 
62  loadFromSurface(surface);
63 
64  SDL_FreeSurface(surface);
65 }
66 
67 void Texture::loadFromSurface(SDL_Surface *surface) {
68  m_width = surface->w;
69  m_height = surface->h;
70 
71  if (m_texture == 0)
72  glCheck(glGenTextures(1, &m_texture));
73 
74  bind(this);
75 
76  // GLenum format = (surface->format->BytesPerPixel == 4) ? GL_RGBA : GL_RGB;
77  // glTexImage2D(GL_TEXTURE_2D, 0, format, m_width, m_height, 0, format, GL_UNSIGNED_BYTE, surface->pixels);
78 
79  GLenum format;
80  if (surface->format->BytesPerPixel == 4) {
81  format = (surface->format->Rmask == 0x000000ff) ? GL_RGBA : GL_BGRA;
82  } else {
83  format = (surface->format->Rmask == 0x000000ff) ? GL_RGB : GL_BGR;
84  }
85 
86  glCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, format, GL_UNSIGNED_BYTE, surface->pixels));
87 
88  glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
89  glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
90 
91  bind(nullptr);
92 }
93 
94 void Texture::bind(const Texture *texture) {
95  if(texture) {
96  glCheck(glBindTexture(GL_TEXTURE_2D, texture->m_texture));
97  } else {
98  glCheck(glBindTexture(GL_TEXTURE_2D, 0));
99  }
100 }
101 
102 } // namespace gk
103 
void loadFromFile(const std::string &filename)
Load the texture from a file on the disk.
Definition: Texture.cpp:54
static void bind(const Texture *texture)
Bind a texture for rendering.
Definition: Texture.cpp:94
void loadFromSurface(SDL_Surface *surface)
Load the texture from a SDL_Surface.
Definition: Texture.cpp:67
GLuint m_texture
Internal OpenGL texture ID.
Definition: Texture.hpp:135
#define EXCEPTION(args...)
Definition: Exception.hpp:22
u16 m_width
Width of the texture.
Definition: Texture.hpp:132
Texture & operator=(Texture &&texture)
Move assignement operator.
Definition: Texture.cpp:42
Image living on the graphics card that can be used for drawing.
Definition: Texture.hpp:30
const std::string & filename() const
Return the filename of the texture.
Definition: Texture.hpp:108
~Texture() noexcept
Destructor.
Definition: Texture.cpp:38
#define glCheck(expr)
Definition: GLCheck.hpp:18
std::string m_filename
Texture filename.
Definition: Texture.hpp:130
u16 m_height
Height of the texture.
Definition: Texture.hpp:133
Texture()=default
Default constructor.