-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTexture.cpp
More file actions
155 lines (135 loc) · 4.59 KB
/
Copy pathTexture.cpp
File metadata and controls
155 lines (135 loc) · 4.59 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (C) 2026 - 2026 Settlers Freaks <sf-team at siedler25.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "Texture.h"
#include <glad/glad.h>
#include <utility>
#include <vector>
Texture::~Texture()
{
if(texture_)
glDeleteTextures(1, &texture_);
}
Texture::Texture(Texture&& other) noexcept
: texture_(std::exchange(other.texture_, 0)), size_(std::exchange(other.size_, {0, 0}))
{}
Texture& Texture::operator=(Texture&& other) noexcept
{
if(this != &other)
{
if(texture_)
glDeleteTextures(1, &texture_);
texture_ = std::exchange(other.texture_, 0);
size_ = std::exchange(other.size_, {0, 0});
}
return *this;
}
void Texture::load(const void* bgraPixels, Extent size, bool filterLinear)
{
if(texture_)
glDeleteTextures(1, &texture_);
texture_ = 0;
size_ = {0, 0};
glGenTextures(1, &texture_);
glBindTexture(GL_TEXTURE_2D, texture_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterLinear ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterLinear ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_BGRA, GL_UNSIGNED_BYTE, bgraPixels);
size_ = size;
}
void Texture::createEmpty(Extent size, bool filterLinear)
{
load(nullptr, size, filterLinear);
}
void Texture::upload(const void* bgraPixels)
{
if(!texture_)
return;
glBindTexture(GL_TEXTURE_2D, texture_);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size_.x, size_.y, GL_BGRA, GL_UNSIGNED_BYTE, bgraPixels);
}
bool Texture::load(SDL_Surface* surface, bool filterLinear)
{
if(!surface)
return false;
// For 8-bit paletted surfaces, honour colorkey if set.
if(surface->format->palette)
{
const int w = surface->w, h = surface->h;
std::vector<Uint32> pixels(static_cast<size_t>(w) * h);
SDL_Palette* pal = surface->format->palette;
Uint32 ck;
const bool hasCK = SDL_GetColorKey(surface, &ck) == 0;
const Uint8 ckIdx = hasCK ? static_cast<Uint8>(ck & 0xFF) : 0;
SDL_LockSurface(surface);
for(int row = 0; row < h; row++)
{
const auto* src = (const Uint8*)surface->pixels + row * surface->pitch;
for(int col = 0; col < w; col++)
{
const Uint8 idx = src[col];
if(hasCK && idx == ckIdx)
pixels[row * w + col] = 0; // transparent
else
{
const SDL_Color& c = pal->colors[idx];
// BGRA layout: A<<24 | R<<16 | G<<8 | B (little-endian GL_BGRA)
pixels[row * w + col] = (0xFFu << 24) | (Uint32(c.r) << 16) | (Uint32(c.g) << 8) | Uint32(c.b);
}
}
}
SDL_UnlockSurface(surface);
load(pixels.data(), Extent(w, h), filterLinear);
return true;
}
// 32-bit surface: convert to destination format (BGRA), force full opacity
SDL_Surface* converted = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
if(!converted)
return false;
// Force alpha to opaque (LBM palette entries often have alpha=0)
SDL_LockSurface(converted);
for(int y = 0; y < converted->h; y++)
{
auto* row = (Uint32*)((Uint8*)converted->pixels + y * converted->pitch);
for(int x = 0; x < converted->w; x++)
row[x] |= 0xFF000000u; // set alpha bits
}
SDL_UnlockSurface(converted);
load(converted->pixels, Extent(converted->w, converted->h), filterLinear);
SDL_FreeSurface(converted);
return true;
}
void Texture::Draw(const Rect& destRect) const
{
if(!texture_)
return;
glBindTexture(GL_TEXTURE_2D, texture_);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(destRect.left, destRect.top);
glTexCoord2f(1, 0);
glVertex2i(destRect.right, destRect.top);
glTexCoord2f(1, 1);
glVertex2i(destRect.right, destRect.bottom);
glTexCoord2f(0, 1);
glVertex2i(destRect.left, destRect.bottom);
glEnd();
}
void Texture::Draw(Position pos) const
{
if(!texture_)
return;
glBindTexture(GL_TEXTURE_2D, texture_);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(pos.x, pos.y);
glTexCoord2f(1, 0);
glVertex2i(pos.x + size_.x, pos.y);
glTexCoord2f(1, 1);
glVertex2i(pos.x + size_.x, pos.y + size_.y);
glTexCoord2f(0, 1);
glVertex2i(pos.x, pos.y + size_.y);
glEnd();
}