summary refs log tree commit diff stats
path: root/src/renderer/texture.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderer/texture.cpp')
-rw-r--r--src/renderer/texture.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp new file mode 100644 index 0000000..04d5cf4 --- /dev/null +++ b/src/renderer/texture.cpp
@@ -0,0 +1,120 @@
1#include "texture.h"
2#include <stdexcept>
3#include <stb_image.h>
4#include "renderer.h"
5#include "util.h"
6
7Texture::Texture(
8 int width,
9 int height) :
10 width_(width),
11 height_(height)
12{
13 if (!Renderer::isSingletonInitialized())
14 {
15 throw std::logic_error("Renderer needs to be initialized");
16 }
17
18 glBindTexture(GL_TEXTURE_2D, texture_.getId());
19 glTexImage2D(
20 GL_TEXTURE_2D,
21 0,
22 GL_RGBA,
23 width_,
24 height_,
25 0,
26 GL_RGBA,
27 GL_UNSIGNED_BYTE,
28 0);
29
30 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
31 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
32 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
34}
35
36Texture::Texture(const char* filename)
37{
38 if (!Renderer::isSingletonInitialized())
39 {
40 throw std::logic_error("Renderer needs to be initialized");
41 }
42
43 glBindTexture(GL_TEXTURE_2D, texture_.getId());
44 unsigned char* data = stbi_load(filename, &width_, &height_, 0, 4);
45 flipImageData(data, width_, height_, 4);
46 glTexImage2D(
47 GL_TEXTURE_2D,
48 0,
49 GL_RGBA,
50 width_,
51 height_,
52 0,
53 GL_RGBA,
54 GL_UNSIGNED_BYTE,
55 data);
56
57 stbi_image_free(data);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
62}
63
64Texture::Texture(
65 const Texture& tex) :
66 width_(tex.width_),
67 height_(tex.height_)
68{
69 if (!Renderer::isSingletonInitialized())
70 {
71 throw std::logic_error("Renderer needs to be initialized");
72 }
73
74 unsigned char* data = new unsigned char[4 * width_ * height_];
75 glBindTexture(GL_TEXTURE_2D, tex.getId());
76 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
77
78 glBindTexture(GL_TEXTURE_2D, texture_.getId());
79 glTexImage2D(
80 GL_TEXTURE_2D,
81 0,
82 GL_RGBA,
83 width_,
84 height_,
85 0,
86 GL_RGBA,
87 GL_UNSIGNED_BYTE,
88 data);
89
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
94
95 delete[] data;
96}
97
98Texture::Texture(Texture&& tex) : Texture(0, 0)
99{
100 swap(*this, tex);
101}
102
103Texture& Texture::operator= (Texture tex)
104{
105 swap(*this, tex);
106
107 return *this;
108}
109
110void swap(Texture& tex1, Texture& tex2)
111{
112 std::swap(tex1.width_, tex2.width_);
113 std::swap(tex1.height_, tex2.height_);
114 std::swap(tex1.texture_, tex2.texture_);
115}
116
117Rectangle Texture::entirety() const
118{
119 return {0, 0, width_, height_};
120}