Skip to content

Commit 7f2c4b3

Browse files
committed
Add Texture class
1 parent c2787bb commit 7f2c4b3

File tree

6 files changed

+197
-0
lines changed

6 files changed

+197
-0
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ qt_add_qml_module(scratchcpp-render
3131
listmonitorlistmodel.cpp
3232
listmonitorlistmodel.h
3333
irenderedtarget.h
34+
texture.cpp
35+
texture.h
3436
renderedtarget.cpp
3537
renderedtarget.h
3638
targetpainter.cpp

src/texture.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include "texture.h"
4+
5+
using namespace scratchcpprender;
6+
7+
Texture::Texture() :
8+
m_size(QSize(0, 0))
9+
{
10+
}
11+
12+
Texture::Texture(GLuint texture, const QSize &size) :
13+
m_handle(texture),
14+
m_isValid(true),
15+
m_size(size)
16+
{
17+
}
18+
19+
Texture::Texture(GLuint texture, int width, int height) :
20+
Texture(texture, QSize(width, height))
21+
{
22+
}
23+
24+
GLuint Texture::handle() const
25+
{
26+
return m_handle;
27+
}
28+
29+
bool Texture::isValid() const
30+
{
31+
return m_isValid;
32+
}
33+
34+
const QSize &Texture::size() const
35+
{
36+
return m_size;
37+
}
38+
39+
int Texture::width() const
40+
{
41+
return m_size.width();
42+
}
43+
44+
int Texture::height() const
45+
{
46+
return m_size.height();
47+
}
48+
49+
void Texture::release()
50+
{
51+
if (m_isValid)
52+
glDeleteTextures(1, &m_handle);
53+
}
54+
55+
bool Texture::operator==(const Texture &texture) const
56+
{
57+
return (!m_isValid && !texture.m_isValid) || (m_isValid && texture.m_isValid && m_handle == texture.m_handle);
58+
}
59+
60+
bool scratchcpprender::Texture::operator!=(const Texture &texture) const
61+
{
62+
return !(*this == texture);
63+
}

src/texture.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <QtOpenGL>
6+
7+
namespace scratchcpprender
8+
{
9+
10+
class Texture
11+
{
12+
public:
13+
Texture();
14+
Texture(GLuint texture, const QSize &size);
15+
Texture(GLuint texture, int width, int height);
16+
17+
GLuint handle() const;
18+
bool isValid() const;
19+
const QSize &size() const;
20+
int width() const;
21+
int height() const;
22+
23+
void release();
24+
25+
bool operator==(const Texture &texture) const;
26+
bool operator!=(const Texture &texture) const;
27+
28+
private:
29+
GLuint m_handle = 0;
30+
bool m_isValid = false;
31+
QSize m_size;
32+
};
33+
34+
} // namespace scratchcpprender

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ add_subdirectory(keyeventhandler)
2929
add_subdirectory(mouseeventhandler)
3030
add_subdirectory(scenemousearea)
3131
add_subdirectory(monitor_models)
32+
add_subdirectory(texture)

test/texture/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_executable(
2+
texture_test
3+
texture_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
texture_test
8+
GTest::gtest_main
9+
scratchcpp-render
10+
${QT_LIBS}
11+
)
12+
13+
add_test(texture_test)
14+
gtest_discover_tests(texture_test)

test/texture/texture_test.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <texture.h>
2+
3+
#include "../common.h"
4+
5+
using namespace scratchcpprender;
6+
7+
TEST(TextureTest, Constructors)
8+
{
9+
{
10+
Texture tex;
11+
ASSERT_EQ(tex.handle(), 0);
12+
ASSERT_FALSE(tex.isValid());
13+
ASSERT_EQ(tex.size().width(), 0);
14+
ASSERT_EQ(tex.size().height(), 0);
15+
ASSERT_EQ(tex.width(), 0);
16+
ASSERT_EQ(tex.height(), 0);
17+
}
18+
19+
{
20+
Texture tex(2, QSize(4, 2));
21+
ASSERT_EQ(tex.handle(), 2);
22+
ASSERT_TRUE(tex.isValid());
23+
ASSERT_EQ(tex.size().width(), 4);
24+
ASSERT_EQ(tex.size().height(), 2);
25+
ASSERT_EQ(tex.width(), 4);
26+
ASSERT_EQ(tex.height(), 2);
27+
}
28+
29+
{
30+
Texture tex(2, 5, 8);
31+
ASSERT_EQ(tex.handle(), 2);
32+
ASSERT_TRUE(tex.isValid());
33+
ASSERT_EQ(tex.size().width(), 5);
34+
ASSERT_EQ(tex.size().height(), 8);
35+
ASSERT_EQ(tex.width(), 5);
36+
ASSERT_EQ(tex.height(), 8);
37+
}
38+
}
39+
40+
TEST(TextureTest, Release)
41+
{
42+
QOpenGLContext context;
43+
context.create();
44+
ASSERT_TRUE(context.isValid());
45+
46+
QOffscreenSurface surface;
47+
surface.setFormat(context.format());
48+
surface.create();
49+
Q_ASSERT(surface.isValid());
50+
context.makeCurrent(&surface);
51+
52+
QOpenGLFramebufferObject fbo(1, 1);
53+
GLuint handle = fbo.takeTexture();
54+
ASSERT_TRUE(glIsTexture(handle));
55+
56+
Texture tex(handle, fbo.width(), fbo.height());
57+
ASSERT_TRUE(glIsTexture(handle));
58+
59+
tex.release();
60+
ASSERT_FALSE(glIsTexture(handle));
61+
62+
context.doneCurrent();
63+
}
64+
65+
TEST(TextureTest, Operators)
66+
{
67+
Texture t1;
68+
Texture t2;
69+
ASSERT_TRUE(t1 == t2);
70+
ASSERT_FALSE(t1 != t2);
71+
72+
Texture t3(3, 10, 10);
73+
ASSERT_FALSE(t1 == t3);
74+
ASSERT_TRUE(t1 != t3);
75+
76+
Texture t4(3, 10, 10);
77+
ASSERT_TRUE(t3 == t4);
78+
ASSERT_FALSE(t3 != t4);
79+
80+
Texture t5(2, 10, 10);
81+
ASSERT_FALSE(t4 == t5);
82+
ASSERT_TRUE(t4 != t5);
83+
}

0 commit comments

Comments
 (0)