Texture class
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m25s

This commit is contained in:
2024-08-02 20:03:32 -04:00
parent 0005c036b4
commit 9688854533
9 changed files with 128 additions and 36 deletions

View File

@@ -126,7 +126,7 @@ namespace JGL {
}
//TODO rotation, I'm unsure if @josh wants degrees or radians.
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size, u8 opacity, Inversion::Inversion inversion) {
void J2D::DrawSprite(const GLuint& texture, const Vector2& pos, const Vector2& size, u8 opacity, Inversion::Inversion inversion) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
@@ -180,7 +180,7 @@ namespace JGL {
J2D::OutlineQuad(Color4(color), v1, v2, v3, v4);
}
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity, Inversion::Inversion inversion) {
void J2D::DrawSprite(const GLuint& texture, float x, float y, float w, float h, u8 opacity, Inversion::Inversion inversion) {
J2D::DrawSprite(texture, {x, y}, {w, h}, opacity, inversion);
}

View File

@@ -97,13 +97,6 @@ namespace JGL
}
Vector2 Font::MeasureString(const std::string &text, unsigned int ptSize) {
// TODO: Check if a font of that size is in the cache and if so use the info from that because this is sloooooooooooooow.
// That'll make it go vroom vroom.
// TODO: Work in-progress implementation unfortunately.
// This is likely returning slightly incorrect results for likely several reasons.
// Half-ass solution for now ~ dawsh.
Vector2 extents = Vector2(0,0);
bool font_of_size_in_cache = false;
@@ -131,11 +124,11 @@ namespace JGL
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
for (const char& c : text) {
FT_GlyphSlot slot = face->glyph;
auto glyph_index = FT_Get_Char_Index(this->face, c);
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
if (error)
continue;
@@ -147,15 +140,12 @@ namespace JGL
extents += advance;
// Gives smaller results than we'd want.
if (extents.y < slot->metrics.height / 64) {
if (extents.y < slot->metrics.height / 64)
extents.y = slot->metrics.height / 64;
}
// Just fucking hardcode it, we know the glyph height is roughly always the ptSize anyway.
if (extents.y < ptSize)
{
extents.y = ptSize;
}
}
return extents;

85
src/JGL/Texture.cpp Normal file
View File

@@ -0,0 +1,85 @@
#include <JGL/Texture.h>
#include <iostream>
JGL::Texture::Texture(const std::string &file, const ReTexture::TextureFlag &flags) {
auto* t = new ReTexture::Texture(file, flags);
*this = Texture(t->pixelData, {(float) t->getWidth(), (float) t->getHeight()}, t->getTextureFormat());
texture_flags = flags;
}
JGL::Texture::Texture(const std::string& file) {
auto* t = new ReTexture::Texture(file);
*this = Texture(t->pixelData, {(float) t->getWidth(), (float) t->getHeight()}, t->getTextureFormat());
delete t;
}
JGL::Texture::Texture(const std::vector<Color4>& pixel_data, const Vector2 &size, const ReTexture::TextureFormat &format) {
std::vector<unsigned char> pixels(pixel_data.size());
memcpy(pixels.data(), pixel_data.data(), pixels.size());
*this = Texture(pixels, size, format);
}
JGL::Texture::Texture(const std::vector<unsigned char>& pixel_data, const Vector2& size, const ReTexture::TextureFormat& format) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
if (format == ReTexture::TextureFormat::RGBA)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int) size.x, (int) size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data.data());
else if (format == ReTexture::TextureFormat::RGB)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (int) size.x, (int) size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, pixel_data.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
texture_size = size;
texture_format = format;
}
std::vector<JGL::Color4> JGL::Texture::getPixelData() {
std::vector<JGL::Color4> result((size_t) (texture_size.x * texture_size.y));
glBindTexture(GL_TEXTURE_2D, texture);
if (texture_format == ReTexture::TextureFormat::RGBA) {
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, result.data());
return result;
}
std::vector<JGL::Color3> color3((size_t) (texture_size.x * texture_size.y));
if (texture_format == ReTexture::TextureFormat::RGB)
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, color3.data());
for (const auto& c : color3)
result.emplace_back(c);
return result;
}
void JGL::Texture::eraseTexture() {
if (texture != 0)
glDeleteTextures(1, &texture);
}
GLuint JGL::Texture::getTexture() {
return texture;
}
Vector2 JGL::Texture::getSize() {
return texture_size;
}
ReTexture::TextureFlag JGL::Texture::getFlags() {
return texture_flags;
}
ReTexture::TextureFormat JGL::Texture::getFormat() {
return texture_format;
}