aboutsummaryrefslogtreecommitdiff
path: root/src/graphics
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-05 14:44:21 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-05 14:44:21 +0100
commitccb3e58071b3e807109918184727b305df8b96a0 (patch)
treed8830f9a3ec84ea17fea064b3ff27b2112c4ca74 /src/graphics
parent7dd8b0cc0561c5fa0c9096b79fb7f0647b3470b4 (diff)
Fix font being corrupt with certain sizes, fix image being corrupt if its rgb, fix window resize incorrect size
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/font.c14
-rw-r--r--src/graphics/image.c15
-rw-r--r--src/graphics/texture.c4
3 files changed, 25 insertions, 8 deletions
diff --git a/src/graphics/font.c b/src/graphics/font.c
index 2ed54d2..c017755 100644
--- a/src/graphics/font.c
+++ b/src/graphics/font.c
@@ -10,6 +10,11 @@
/* TODO: Test and fix .tcc files */
+static unsigned int to_div2_ceil(unsigned int value) {
+ const uint32_t v = value;
+ return v + (v & 1);
+}
+
int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int character_size) {
self->texture.id = 0;
@@ -47,8 +52,13 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c
/* TODO: Optimize */
/* Find optimal size for atlas, starting from small to large */
for(int i = 0; i < 4; ++i) {
- self->font_atlas.width = (14 + (8 * i)) * self->character_size;
- self->font_atlas.height = (14 + (8 * i)) * self->character_size;
+ /*
+ This to_div2_ceil is needed because otherwise for character sizes such as 33 which are not divisable by 2
+ causes the font texture to be skewed. I dont know why this happens. Maybe a bug in stbtt?
+ TODO: Figure out why it happens.
+ */
+ self->font_atlas.width = (14 + (8 * i)) * to_div2_ceil(self->character_size);
+ self->font_atlas.height = (14 + (8 * i)) * to_div2_ceil(self->character_size);
unsigned char *new_atlas = realloc(self->font_atlas.atlas, self->font_atlas.width * self->font_atlas.height);
if(!new_atlas) {
fprintf(stderr, "Error: failed to load font %s, error: out of memory\n", filepath);
diff --git a/src/graphics/image.c b/src/graphics/image.c
index 593a3d2..9934b42 100644
--- a/src/graphics/image.c
+++ b/src/graphics/image.c
@@ -1,4 +1,5 @@
#include "../../include/mgl/graphics/image.h"
+#include <assert.h>
#define STBI_NO_PSD
#define STBI_NO_TGA
@@ -19,7 +20,8 @@ static mgl_image_format stbi_format_to_mgl_image_format(int stbi_format) {
case STBI_rgb_alpha:
return MGL_IMAGE_FORMAT_RGBA;
}
- return 0;
+ assert(0);
+ return MGL_IMAGE_FORMAT_RGBA;
}
static size_t mgl_image_format_num_channels(mgl_image_format image_format) {
@@ -30,7 +32,8 @@ static size_t mgl_image_format_num_channels(mgl_image_format image_format) {
case MGL_IMAGE_FORMAT_RGB: return 3;
case MGL_IMAGE_FORMAT_RGBA: return 4;
}
- return 0;
+ assert(0);
+ return 4;
}
/* TODO: Ensure texture is power of 2 if the hardware doesn't support non power of two textures */
@@ -41,12 +44,14 @@ int mgl_image_load_from_file(mgl_image *self, const char *filepath) {
self->height = 0;
int format;
- self->data = stbi_load(filepath, &self->width, &self->height, &format, 0);
+ /* TODO: format is forced to rgba right now because rgb images cause the image to be skewed (bug) */
+ self->data = stbi_load(filepath, &self->width, &self->height, &format, 4);
if(!self->data) {
fprintf(stderr, "Error: failed to load image %s, error: %s\n", filepath, stbi_failure_reason());
mgl_image_unload(self);
return -1;
}
+ format = 4;
self->format = stbi_format_to_mgl_image_format(format);
return 0;
@@ -60,12 +65,14 @@ int mgl_image_load_from_memory(mgl_image *self, const unsigned char *data, size_
self->height = 0;
int format;
- self->data = stbi_load_from_memory(data, size, &self->width, &self->height, &format, 0);
+ /* TODO: format is forced to rgba right now because rgb images cause the image to be skewed (bug) */
+ self->data = stbi_load_from_memory(data, size, &self->width, &self->height, &format, 4);
if(!self->data) {
fprintf(stderr, "Error: failed to load image from memory, error: %s\n", stbi_failure_reason());
mgl_image_unload(self);
return -1;
}
+ format = 4;
self->format = stbi_format_to_mgl_image_format(format);
return 0;
diff --git a/src/graphics/texture.c b/src/graphics/texture.c
index c70c0f9..f6549f1 100644
--- a/src/graphics/texture.c
+++ b/src/graphics/texture.c
@@ -30,8 +30,8 @@ static int mgl_texture_format_to_compressed_opengl_format(mgl_texture_format for
static int mgl_texture_format_to_source_opengl_format(mgl_texture_format format) {
switch(format) {
case MGL_TEXTURE_FORMAT_ALPHA: return GL_ALPHA;
- case MGL_TEXTURE_FORMAT_GRAY: return GL_LUMINANCE8;
- case MGL_TEXTURE_FORMAT_GRAY_ALPHA: return GL_LUMINANCE8_ALPHA8;
+ case MGL_TEXTURE_FORMAT_GRAY: return GL_LUMINANCE;
+ case MGL_TEXTURE_FORMAT_GRAY_ALPHA: return GL_LUMINANCE_ALPHA;
case MGL_TEXTURE_FORMAT_RGB: return GL_RGB;
case MGL_TEXTURE_FORMAT_RGBA: return GL_RGBA;
}