2D_Game_Engine
Loading...
Searching...
No Matches
texture.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2#include <texture.hpp>
3
4#include <stb_image.h>
5
6Texture::Texture(const std::string &path,int mag_filter,int min_filter,uint32_t tex_id): m_ID(0),m_TexID(tex_id),m_MagFilter(mag_filter),m_MinFilter(min_filter),m_LocalBuffer(nullptr),m_Width(0),m_Height(0),m_BPP(0){
7 m_FilePath=path;
9
10 m_FilePath.resize(100);
11 m_LoadedFilePath.resize(100);
12
13 m_LoadedMagFilter=mag_filter;
14 m_LoadedMinFilter=min_filter;
15
16 stbi_set_flip_vertically_on_load(1);
17 m_LocalBuffer=stbi_load(path.c_str(),&m_Width,&m_Height,&m_BPP,4);
18 if(!m_LocalBuffer){
19 fprintf(stderr,"Failed to load texture %s: %s\n",path.c_str(),stbi_failure_reason());
20 return;
21 }
22
23 glGenTextures(1,&m_ID);
24 glBindTexture(GL_TEXTURE_2D,m_ID);
25
26 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,min_filter);
27 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mag_filter);
28 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
29 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
30
31 glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,m_Width,m_Height,0,GL_RGBA,GL_UNSIGNED_BYTE,m_LocalBuffer);
32
33 stbi_image_free(m_LocalBuffer);
34 m_LocalBuffer=nullptr;
35}
36
38 m_ID=other.m_ID;
39 other.m_ID=std::numeric_limits<unsigned int>::max();
47 m_Width=other.m_Width;
48 m_Height=other.m_Height;
49 m_BPP=other.m_BPP;
50}
51
53 if(m_ID!=std::numeric_limits<unsigned int>::max()){
54 glDeleteTextures(1,&m_ID);
55 if(m_LocalBuffer!=nullptr)
56 free(m_LocalBuffer);
57 }
58}
59
60
61
62void Texture::Bind(unsigned int slot) const{
63 glActiveTexture(GL_TEXTURE0+slot);
64 glBindTexture(GL_TEXTURE_2D,m_ID);
65}
66
67void Texture::Unbind() const{
68 glBindTexture(GL_TEXTURE_2D,0);
69}
70
72 m_ID=other.m_ID;
73 other.m_ID=std::numeric_limits<unsigned int>::max();
74 m_TexID=other.m_TexID;
82 m_Width=other.m_Width;
83 m_Height=other.m_Height;
84 m_BPP=other.m_BPP;
85 m_TileWidth=other.m_TileWidth;
86 m_TileHeight=other.m_TileHeight;
87}
88
89std::array<Vertex,4> SpriteSheet::CreateQuadSpriteSheet(float x,float y,float width,float height,float row,float col,int layer,float texID){
90 Vertex v1;
91 v1.position={x,y};
92 v1.texcoords={(col*m_TileWidth)/m_Width,(row*m_TileHeight)/m_Height};
93 v1.layer=layer;
94 v1.texID=texID;
95
96 Vertex v2;
97 v2.position={x,y+height};
98 v2.texcoords={(col*m_TileWidth)/m_Width,((row+1)*m_TileHeight)/m_Height};
99 v2.layer=layer;
100 v2.texID=texID;
101
102 Vertex v3;
103 v3.position={x+width,y+height};
104 v3.texcoords={((col+1)*m_TileWidth)/m_Width,((row+1)*m_TileHeight)/m_Height};
105 v3.layer=layer;
106 v3.texID=texID;
107
108 Vertex v4;
109 v4.position={x+width,y};
110 v4.texcoords={((col+1)*m_TileWidth)/m_Width,(row*m_TileHeight)/m_Height};
111 v4.layer=layer;
112 v4.texID=texID;
113
114 return {v1,v2,v3,v4};
115}
std::array< Vertex, 4 > CreateQuadSpriteSheet(float x, float y, float width, float height, float row, float col, int layer, float texID)
Definition texture.cpp:89
~Texture()
Definition texture.cpp:52
void Unbind() const
Definition texture.cpp:67
void Bind(unsigned int slot=0) const
Definition texture.cpp:62
int m_LoadedMinFilter
Definition texture.hpp:79
uint32_t m_TexID
Definition texture.hpp:77
std::string m_LoadedFilePath
Definition texture.hpp:78
int m_LoadedMagFilter
Definition texture.hpp:79
Texture()
Definition texture.hpp:11
int m_BPP
Definition texture.hpp:81
int m_MinFilter
Definition texture.hpp:79
int m_Height
Definition texture.hpp:81
std::string m_FilePath
Definition texture.hpp:78
int m_MagFilter
Definition texture.hpp:79
unsigned char * m_LocalBuffer
Definition texture.hpp:80
int m_Width
Definition texture.hpp:81
unsigned int m_ID
Definition texture.hpp:76
float texID
Definition structs.hpp:32
Vec2 position
Definition structs.hpp:29
Vec2 texcoords
Definition structs.hpp:30
int layer
Definition structs.hpp:31