Improve memory safety.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m44s

Also fixed a case where we didn't reset the GL state correctly 🤷
This commit is contained in:
2024-10-06 23:03:50 -04:00
parent 5f367efc28
commit 308b0dc854
3 changed files with 16 additions and 8 deletions

View File

@@ -23,8 +23,6 @@ namespace JGL {
CLAMP_TO_BORDER = 3 //Effectively the same as clamp_to_edge
};
//TODO copy constructor for this. Copying this as it is and then that copy going out of scope will crash the program as it sits.
/// Represents texture data loaded on the GPU. Contains a handle that can be passed to OpenGL draw calls.
class Texture {
private:
@@ -38,7 +36,6 @@ namespace JGL {
TextureWrappingMode texture_wrapping_mode;
void load(SoftwareTexture* software_texture, const Vector2& size, const TextureFormat& format, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode);
public:
Texture() = default;
/// Load a texture from a file,
explicit Texture(const std::string& file, TextureFilteringMode filtering_mode = TextureFilteringMode::BILINEAR, TextureWrappingMode wrapping_mode = TextureWrappingMode::CLAMP_TO_EDGE);
Texture(const std::string& file, const TextureFlag& flags, TextureFilteringMode filtering_mode = TextureFilteringMode::BILINEAR, TextureWrappingMode wrapping_mode = TextureWrappingMode::CLAMP_TO_EDGE);
@@ -46,6 +43,7 @@ namespace JGL {
/* Initialize a texture filled with trash data
this is primarily for the RenderTarget */
explicit Texture(const Vector2& size);
Texture(const Texture& rhs);
~Texture();
public:
[[nodiscard]] GLuint GetGLTextureHandle() const;
@@ -55,7 +53,6 @@ namespace JGL {
[[nodiscard]] TextureFlag GetFlags() const;
[[nodiscard]] TextureFormat GetFormat() const;
[[nodiscard]] std::vector<Color4> GetPixelData() const;
void SetTextureHandle(GLuint handle);
};

View File

@@ -259,6 +259,7 @@ int main(int argc, char** argv) {
if (j2d_render_target->GetDimensions().x <= 1)
increasing = true;
}
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
window->pollEvents();
window->refresh();

View File

@@ -7,8 +7,7 @@ namespace JGL
{
Texture::Texture(const std::string& file, const ReTexture::TextureFlag& flags, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode)
{
auto *t = new ReTexture::SoftwareTexture(file, flags);
auto* t = new ReTexture::SoftwareTexture(file, flags);
GLuint previous_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
@@ -16,8 +15,6 @@ namespace JGL
texture_flags = flags;
delete t;
glBindTexture(GL_TEXTURE_2D, previous_texture);
}
Texture::Texture(const std::string& file, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode) {
@@ -59,6 +56,10 @@ namespace JGL
void Texture::load(SoftwareTexture* software_texture, const Vector2& size, const TextureFormat& format,
TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode) {
GLuint previous_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
glGenTextures(1, &texture_handle);
glBindTexture(GL_TEXTURE_2D, texture_handle);
@@ -139,6 +140,7 @@ namespace JGL
texture_size = size;
texture_format = format;
texture_filtering_mode = filtering_mode;
glBindTexture(GL_TEXTURE_2D, previous_texture);
}
std::vector<Color4> JGL::Texture::GetPixelData() const {
@@ -206,4 +208,12 @@ namespace JGL
Texture::~Texture() {
Erase();
}
Texture::Texture(const Texture& rhs) {
auto pixels = rhs.GetPixelData();
std::vector<unsigned char> pixels_correct(pixels.size() * sizeof(Color4));
memcpy(pixels_correct.data(), pixels.data(), pixels.size() * sizeof(Color4));
auto software_texture = SoftwareTexture(pixels_correct, rhs.texture_format, rhs.GetDimensions().x, rhs.GetDimensions().y);
this->load(&software_texture, rhs.GetDimensions(), rhs.texture_format, rhs.texture_filtering_mode, rhs.texture_wrapping_mode);
}
}