AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
TextureImpl.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include "Texture.h"
15#include "gl.h"
16#include "ResourcePool.h"
17#include <AUI/GL/State.h>
18
19
20template<unsigned int TEXTURE_TARGET>
21gl::Texture<TEXTURE_TARGET>::Texture() {
22 mTexture = gl::ResourcePool<gl::ResourceKind::TEXTURE>::get();
23 gl::State::bindTexture(TEXTURE_TARGET, mTexture);
24 setupLinear();
25 setupMirroredRepeat();
26}
27
28template<unsigned int TEXTURE_TARGET>
29gl::Texture<TEXTURE_TARGET>::~Texture() {
30 gl::ResourcePool<gl::ResourceKind::TEXTURE>::put(mTexture);
31 gl::State::bindTexture(TEXTURE_TARGET, 0);
32}
33
34template<unsigned int TEXTURE_TARGET>
35void gl::Texture<TEXTURE_TARGET>::setupNearest() {
36 if (mFiltering == Filtering::NEAREST) {
37 return;
38 }
39 bind();
40 mFiltering = Filtering::NEAREST;
41 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
42 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
43}
44template<unsigned int TEXTURE_TARGET>
45void gl::Texture<TEXTURE_TARGET>::setupLinear() {
46 if (mFiltering == Filtering::LINEAR) {
47 return;
48 }
49 bind();
50 mFiltering = Filtering::LINEAR;
51 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
52 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53}
54
55template<unsigned int TEXTURE_TARGET>
56void gl::Texture<TEXTURE_TARGET>::setupRepeat() {
57 if (mWrapping == Wrapping::REPEAT) {
58 return;
59 }
60 bind();
61 mWrapping = Wrapping::REPEAT;
62 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_WRAP_S, GL_REPEAT);
63 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_WRAP_T, GL_REPEAT);
64}
65
66template<unsigned int TEXTURE_TARGET>
67void gl::Texture<TEXTURE_TARGET>::setupMirroredRepeat() {
68 if (mWrapping == Wrapping::MIRRORED_REPEAT) {
69 return;
70 }
71 bind();
72 mWrapping = Wrapping::MIRRORED_REPEAT;
73 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
74 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
75}
76
77template<unsigned int TEXTURE_TARGET>
78void gl::Texture<TEXTURE_TARGET>::setupClampToEdge() {
79 if (mWrapping == Wrapping::CLAMP_TO_EDGE) {
80 return;
81 }
82 bind();
83 mWrapping = Wrapping::CLAMP_TO_EDGE;
84 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
85 glTexParameteri(TEXTURE_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
86}
87
88template<unsigned int TEXTURE_TARGET>
89void gl::Texture<TEXTURE_TARGET>::bind(uint8_t index) {
90 AUI_ASSERTX(getHandle() != 0, "gl::Texture::bind: mTexture == 0");
91 gl::State::activeTexture(index);
92 gl::State::bindTexture(TEXTURE_TARGET, mTexture);
93}
94
95template<unsigned int TEXTURE_TARGET>
96void gl::Texture<TEXTURE_TARGET>::unbind(uint8_t index) {
97 gl::State::activeTexture(index);
98 gl::State::bindTexture(TEXTURE_TARGET, 0);
99}
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74