forked from plastictown/Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.cpp
More file actions
60 lines (51 loc) · 1.35 KB
/
Copy pathimage.cpp
File metadata and controls
60 lines (51 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* https://github.com/plastictown/Engine
* Copyright (C) 2018 Mikhail Domchenkov
*/
#include <image.h>
#include <GL/gl.h>
#include <iostream>
Incrementer<GLuint> Image::id_gen;
std::set<GLuint> Image::textures;
void Image::cleanup()
{
if(textures.empty())
return;
for(auto i:textures)
{
glDeleteTextures(1, &i);
}
}
bool Image::load(const std::string& fname)
{
SDL_RWops *rwop = SDL_RWFromFile(fname.c_str(), "rb");
if(!rwop)
return false;
SDL_Surface* surface = IMG_LoadPNG_RW(rwop);
if(surface == nullptr)
return false;
glGenTextures(1, &m_id);
glBindTexture(GL_TEXTURE_2D, m_id);
int Mode = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, Mode, surface->w, surface->h, 0, Mode, GL_UNSIGNED_BYTE, surface->pixels);
box.right_bottom = Point2f {GLfloat(surface->w), GLfloat(surface->h)};
aspect = GLfloat(surface->w) / GLfloat(surface->h);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
SDL_FreeSurface(surface);
return true;
}
void Image::set() const noexcept
{
glBindTexture(GL_TEXTURE_2D, m_id);
}
Image::~Image()
{
//glDeleteTextures(1,&m_id);
}
const Image& Image::getImage() const
{
return *this;
}