From 4b2076ce7b93578210867a622aba9907b79a5198 Mon Sep 17 00:00:00 2001
From: Relintai <relintai@protonmail.com>
Date: Sun, 31 Mar 2024 10:21:41 +0200
Subject: [PATCH] Various small improvements.

---
 sfw/render_core/color_material.cpp               |  1 +
 sfw/render_core/color_material_2d.cpp            |  1 +
 sfw/render_core/colored_material.cpp             |  1 +
 sfw/render_core/font_material.cpp                |  1 +
 sfw/render_core/frame_buffer.cpp                 | 12 ++++++------
 sfw/render_core/material.cpp                     |  6 ++++++
 sfw/render_core/mesh.cpp                         |  4 ++--
 sfw/render_core/shader.cpp                       |  4 +++-
 sfw/render_core/shader.h                         |  1 -
 sfw/render_core/texture.cpp                      |  1 +
 sfw/render_core/texture.h                        |  1 -
 sfw/render_core/texture_material.cpp             |  1 +
 sfw/render_core/texture_material_2d.cpp          |  1 +
 sfw/render_core/transparent_texture_material.cpp |  1 +
 14 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/sfw/render_core/color_material.cpp b/sfw/render_core/color_material.cpp
index ec2691a..f0d1e39 100644
--- a/sfw/render_core/color_material.cpp
+++ b/sfw/render_core/color_material.cpp
@@ -1,6 +1,7 @@
 
 //--STRIP
 #include "color_material.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void ColorMaterial::bind_uniforms() {
diff --git a/sfw/render_core/color_material_2d.cpp b/sfw/render_core/color_material_2d.cpp
index ec8f54a..79ed8ad 100644
--- a/sfw/render_core/color_material_2d.cpp
+++ b/sfw/render_core/color_material_2d.cpp
@@ -1,6 +1,7 @@
 
 //--STRIP
 #include "color_material_2d.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void ColorMaterial2D::bind_uniforms() {
diff --git a/sfw/render_core/colored_material.cpp b/sfw/render_core/colored_material.cpp
index a1067b7..b90958d 100644
--- a/sfw/render_core/colored_material.cpp
+++ b/sfw/render_core/colored_material.cpp
@@ -1,5 +1,6 @@
 //--STRIP
 #include "colored_material.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void ColoredMaterial::bind_uniforms() {
diff --git a/sfw/render_core/font_material.cpp b/sfw/render_core/font_material.cpp
index fb8a13b..acfa6c8 100644
--- a/sfw/render_core/font_material.cpp
+++ b/sfw/render_core/font_material.cpp
@@ -1,5 +1,6 @@
 //--STRIP
 #include "font_material.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void FontMaterial::bind_uniforms() {
diff --git a/sfw/render_core/frame_buffer.cpp b/sfw/render_core/frame_buffer.cpp
index 970544e..ac402a5 100644
--- a/sfw/render_core/frame_buffer.cpp
+++ b/sfw/render_core/frame_buffer.cpp
@@ -29,13 +29,13 @@ int FrameBuffer::create(const int p_width, const int p_height, const int p_msaa_
 	if (msaa % 2 == 0) {
 		--msaa;
 	}
+	
+	destroy();
 
 	_fbo_width = p_width;
 	_fbo_height = p_height;
 	_fbo_msaa_count = msaa;
 
-	destroy();
-
 	glGenFramebuffers(1, &_fbo);
 	glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
 
@@ -262,8 +262,8 @@ Vector2i FrameBuffer::get_size() const {
 void FrameBuffer::blit_color_to(const uint32_t p_destination_framebuffer, const Rect2i &p_rect) {
 	ERR_FAIL_COND(!p_destination_framebuffer);
 
-	int width = p_rect.size.width == 0 ? p_rect.size.width : _fbo_width;
-	int height = p_rect.size.height == 0 ? p_rect.size.height : _fbo_height;
+	int width = p_rect.size.width == 0 ?_fbo_width : p_rect.size.width;
+	int height = p_rect.size.height == 0 ? _fbo_height : p_rect.size.height;
 
 	uint32_t fbo = get_gl_fbo();
 
@@ -275,8 +275,8 @@ void FrameBuffer::blit_color_to(const uint32_t p_destination_framebuffer, const
 void FrameBuffer::blit_depth_to(const uint32_t p_destination_framebuffer, const Rect2i &p_rect) {
 	ERR_FAIL_COND(!p_destination_framebuffer);
 
-	int width = p_rect.size.width == 0 ? p_rect.size.width : _fbo_width;
-	int height = p_rect.size.height == 0 ? p_rect.size.height : _fbo_height;
+	int width = p_rect.size.width == 0 ?_fbo_width : p_rect.size.width;
+	int height = p_rect.size.height == 0 ? _fbo_height : p_rect.size.height;
 
 	uint32_t fbo = get_gl_fbo();
 
diff --git a/sfw/render_core/material.cpp b/sfw/render_core/material.cpp
index 148e9b3..493d688 100644
--- a/sfw/render_core/material.cpp
+++ b/sfw/render_core/material.cpp
@@ -2,6 +2,8 @@
 #include "render_core/material.h"
 
 #include <stdio.h>
+
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void Material::bind() {
@@ -58,6 +60,10 @@ Material::Material() {
 	shader = NULL;
 }
 Material::~Material() {
+	if (current_material == this) {
+		unbind();
+		current_material = NULL;
+	}
 }
 
 void Material::set_uniform(int32_t p_uniform, const Transform &p_transform) {
diff --git a/sfw/render_core/mesh.cpp b/sfw/render_core/mesh.cpp
index 6535983..c6b916e 100644
--- a/sfw/render_core/mesh.cpp
+++ b/sfw/render_core/mesh.cpp
@@ -224,7 +224,7 @@ void Mesh::upload() {
 	normals_vbo_size = sizeof(float) * normals.size();
 	colors_vbo_size = sizeof(float) * colors.size();
 	uvs_vbo_size = sizeof(float) * uvs.size();
-	indices_vbo_size = sizeof(float) * indices.size();
+	indices_vbo_size = sizeof(uint32_t) * indices.size();
 
 	glBindBuffer(GL_ARRAY_BUFFER, VBO);
 	glBufferData(GL_ARRAY_BUFFER, vertices_vbo_size + normals_vbo_size + colors_vbo_size + uvs_vbo_size, NULL, GL_STATIC_DRAW);
@@ -268,7 +268,7 @@ void Mesh::destroy() {
 	}
 }
 void Mesh::render() {
-	if (vertices.size() == 0) {
+	if (!vertices_vbo_size) {
 		return;
 	}
 
diff --git a/sfw/render_core/shader.cpp b/sfw/render_core/shader.cpp
index 8de3b4a..1a36f38 100644
--- a/sfw/render_core/shader.cpp
+++ b/sfw/render_core/shader.cpp
@@ -2,6 +2,8 @@
 #include "render_core/shader.h"
 
 #include <stdio.h>
+
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 bool Shader::bind() {
@@ -124,7 +126,7 @@ void Shader::print_program_errors(const uint32_t p_program) {
 		glGetProgramInfoLog(p_program, max_length, &info_length, info_log);
 
 		if (info_length > 0) {
-			ERR_PRINT(String::utf8(info_log, max_length));
+			ERR_PRINT(String::utf8(info_log, info_length));
 		}
 	} else {
 		ERR_PRINT("print_program_errors: Not a program!\n");
diff --git a/sfw/render_core/shader.h b/sfw/render_core/shader.h
index d4a981b..9c7896e 100644
--- a/sfw/render_core/shader.h
+++ b/sfw/render_core/shader.h
@@ -5,7 +5,6 @@
 
 //--STRIP
 #include "core/hash_map.h"
-#include "render_core/3rd_glad.h"
 //--STRIP
 
 class Shader {
diff --git a/sfw/render_core/texture.cpp b/sfw/render_core/texture.cpp
index ca52d6a..2f517ed 100644
--- a/sfw/render_core/texture.cpp
+++ b/sfw/render_core/texture.cpp
@@ -7,6 +7,7 @@
 #include "render_core/app_window.h"
 
 #include "render_core/frame_buffer.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void Texture::create_from_image(const Ref<Image> &img) {
diff --git a/sfw/render_core/texture.h b/sfw/render_core/texture.h
index cb5ddf0..4fcf76f 100644
--- a/sfw/render_core/texture.h
+++ b/sfw/render_core/texture.h
@@ -7,7 +7,6 @@
 #include "core/vector2i.h"
 
 #include "object/resource.h"
-#include "render_core/3rd_glad.h"
 #include "render_core/image.h"
 //--STRIP
 
diff --git a/sfw/render_core/texture_material.cpp b/sfw/render_core/texture_material.cpp
index 2e6a84b..f448b2d 100644
--- a/sfw/render_core/texture_material.cpp
+++ b/sfw/render_core/texture_material.cpp
@@ -1,6 +1,7 @@
 
 //--STRIP
 #include "texture_material.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void TextureMaterial::bind_uniforms() {
diff --git a/sfw/render_core/texture_material_2d.cpp b/sfw/render_core/texture_material_2d.cpp
index 30bd371..10bbbde 100644
--- a/sfw/render_core/texture_material_2d.cpp
+++ b/sfw/render_core/texture_material_2d.cpp
@@ -1,5 +1,6 @@
 //--STRIP
 #include "texture_material_2d.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void TextureMaterial2D::bind_uniforms() {
diff --git a/sfw/render_core/transparent_texture_material.cpp b/sfw/render_core/transparent_texture_material.cpp
index 017bb4b..d40cb26 100644
--- a/sfw/render_core/transparent_texture_material.cpp
+++ b/sfw/render_core/transparent_texture_material.cpp
@@ -1,5 +1,6 @@
 //--STRIP
 #include "transparent_texture_material.h"
+#include "render_core/3rd_glad.h"
 //--STRIP
 
 void TransparentTextureMaterial::bind_uniforms() {