Working on UI part

This commit is contained in:
Vladislav Khorev committed 2026-05-26 22:21:05 +03:00
1 parent 6c8fcf17b6
commit 0da1ae124c
17 files changed
+221 -42

No files matched your search

+2
View File
@@ -459,6 +459,8 @@ void TextRenderer::drawText(const std::string& text, float x, float y, float sca
r->RenderUniform1i("uText", 0);
r->RenderUniform4fv("uColor", color.data());
r->RenderUniform1f("uAlpha", 1.0f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, atlasTexture->getTexID());
+71 -6
View File
@@ -8,9 +8,68 @@
namespace ZL
{
bool IsPowerOf2(size_t n) {
return n > 0 && (n & (n - 1)) == 0;
}
size_t NextPowerOf2(size_t n) {
if (n == 0) return 1;
--n;
n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16;
#if SIZE_MAX > 0xFFFFFFFF
n |= n >> 32;
#endif
return n + 1;
}
TextureDataStruct ResizeTextureDataToPOT(const TextureDataStruct& src) {
size_t newW = IsPowerOf2(src.width) ? src.width : NextPowerOf2(src.width);
size_t newH = IsPowerOf2(src.height) ? src.height : NextPowerOf2(src.height);
if (newW == src.width && newH == src.height) return src;
int channels = (src.format == TextureDataStruct::R8) ? 1
: (src.format == TextureDataStruct::RGB) ? 3 : 4;
TextureDataStruct dst = src;
dst.width = newW;
dst.height = newH;
dst.data.resize(newW * newH * channels);
for (size_t y = 0; y < newH; ++y) {
for (size_t x = 0; x < newW; ++x) {
float fx = (newW > 1) ? (float)x * (float)(src.width - 1) / (float)(newW - 1) : 0.f;
float fy = (newH > 1) ? (float)y * (float)(src.height - 1) / (float)(newH - 1) : 0.f;
size_t x0 = (size_t)fx, y0 = (size_t)fy;
size_t x1 = min(x0 + 1, src.width - 1);
size_t y1 = min(y0 + 1, src.height - 1);
float tx = fx - (float)x0, ty = fy - (float)y0;
for (int c = 0; c < channels; ++c) {
float v00 = (float)(unsigned char)src.data[(y0 * src.width + x0) * channels + c];
float v10 = (float)(unsigned char)src.data[(y0 * src.width + x1) * channels + c];
float v01 = (float)(unsigned char)src.data[(y1 * src.width + x0) * channels + c];
float v11 = (float)(unsigned char)src.data[(y1 * src.width + x1) * channels + c];
float val = v00 * (1.f - tx) * (1.f - ty) + v10 * tx * (1.f - ty)
+ v01 * (1.f - tx) * ty + v11 * tx * ty;
dst.data[(y * newW + x) * channels + c] = (char)(unsigned char)(val + 0.5f);
}
}
}
std::cout << "TextureManager: resized texture from "
<< src.width << "x" << src.height
<< " to " << newW << "x" << newH << " (POT)" << std::endl;
return dst;
}
Texture::Texture(const TextureDataStruct& texData) {
width = texData.width;
height = texData.height;
if (!IsPowerOf2(width) || !IsPowerOf2(height)) {
std::cerr << "TextureManager WARNING: non-POT texture "
<< width << "x" << height
<< " — upload may fail on some platforms." << std::endl;
}
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
@@ -422,36 +481,42 @@ namespace ZL
return zipFile.empty() ? fileName : fileName + "|" + zipFile;
}
std::shared_ptr<Texture> TextureManager::LoadFromBmp24(const std::string& fileName, const std::string& zipFile)
std::shared_ptr<Texture> TextureManager::LoadFromBmp24(const std::string& fileName, const std::string& zipFile, bool resizeToPOT)
{
std::string key = MakeKey(fileName, zipFile);
auto it = textureMap.find(key);
if (it != textureMap.end())
return it->second;
auto tex = std::make_shared<Texture>(CreateTextureDataFromBmp24(fileName, zipFile));
auto data = CreateTextureDataFromBmp24(fileName, zipFile);
if (resizeToPOT) data = ResizeTextureDataToPOT(data);
auto tex = std::make_shared<Texture>(data);
textureMap[key] = tex;
return tex;
}
std::shared_ptr<Texture> TextureManager::LoadFromBmp32(const std::string& fileName, const std::string& zipFile)
std::shared_ptr<Texture> TextureManager::LoadFromBmp32(const std::string& fileName, const std::string& zipFile, bool resizeToPOT)
{
std::string key = MakeKey(fileName, zipFile);
auto it = textureMap.find(key);
if (it != textureMap.end())
return it->second;
auto tex = std::make_shared<Texture>(CreateTextureDataFromBmp32(fileName, zipFile));
auto data = CreateTextureDataFromBmp32(fileName, zipFile);
if (resizeToPOT) data = ResizeTextureDataToPOT(data);
auto tex = std::make_shared<Texture>(data);
textureMap[key] = tex;
return tex;
}
#ifdef PNG_ENABLED
std::shared_ptr<Texture> TextureManager::LoadFromPng(const std::string& fileName, const std::string& zipFile)
std::shared_ptr<Texture> TextureManager::LoadFromPng(const std::string& fileName, const std::string& zipFile, bool resizeToPOT)
{
std::string key = MakeKey(fileName, zipFile);
auto it = textureMap.find(key);
if (it != textureMap.end())
return it->second;
auto tex = std::make_shared<Texture>(CreateTextureDataFromPng(fileName, zipFile));
auto data = CreateTextureDataFromPng(fileName, zipFile);
if (resizeToPOT) data = ResizeTextureDataToPOT(data);
auto tex = std::make_shared<Texture>(data);
textureMap[key] = tex;
return tex;
}
+7 -3
View File
@@ -62,13 +62,17 @@ namespace ZL
TextureDataStruct CreateTextureDataFromPng(const std::string& fullFileName, const std::string& ZIPFileName = "");
#endif
bool IsPowerOf2(size_t n);
size_t NextPowerOf2(size_t n);
TextureDataStruct ResizeTextureDataToPOT(const TextureDataStruct& texData);
class TextureManager
{
public:
std::shared_ptr<Texture> LoadFromBmp24(const std::string& fileName, const std::string& zipFile = "");
std::shared_ptr<Texture> LoadFromBmp32(const std::string& fileName, const std::string& zipFile = "");
std::shared_ptr<Texture> LoadFromBmp24(const std::string& fileName, const std::string& zipFile = "", bool resizeToPOT = false);
std::shared_ptr<Texture> LoadFromBmp32(const std::string& fileName, const std::string& zipFile = "", bool resizeToPOT = false);
#ifdef PNG_ENABLED
std::shared_ptr<Texture> LoadFromPng(const std::string& fileName, const std::string& zipFile = "");
std::shared_ptr<Texture> LoadFromPng(const std::string& fileName, const std::string& zipFile = "", bool resizeToPOT = false);
#endif
void Unload(const std::string& fileName, const std::string& zipFile = "");