diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-05 14:44:21 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-05 14:44:21 +0100 |
commit | ccb3e58071b3e807109918184727b305df8b96a0 (patch) | |
tree | d8830f9a3ec84ea17fea064b3ff27b2112c4ca74 /src | |
parent | 7dd8b0cc0561c5fa0c9096b79fb7f0647b3470b4 (diff) |
Fix font being corrupt with certain sizes, fix image being corrupt if its rgb, fix window resize incorrect size
Diffstat (limited to 'src')
-rw-r--r-- | src/gl.c | 1 | ||||
-rw-r--r-- | src/graphics/font.c | 14 | ||||
-rw-r--r-- | src/graphics/image.c | 15 | ||||
-rw-r--r-- | src/graphics/texture.c | 4 | ||||
-rw-r--r-- | src/window/window.c | 2 |
5 files changed, 26 insertions, 10 deletions
@@ -42,7 +42,6 @@ int mgl_gl_load(mgl_gl *self) { { &self->glGenTextures, "glGenTextures" }, { &self->glDeleteTextures, "glDeleteTextures" }, { &self->glTexImage2D, "glTexImage2D" }, - { &self->glCompressedTexImage2D, "glCompressedTexImage2D" }, { &self->glBindTexture, "glBindTexture" }, { &self->glTexParameteri, "glTexParameteri" }, { &self->glHint, "glHint" }, 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; } diff --git a/src/window/window.c b/src/window/window.c index f975c1e..30abebb 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -158,6 +158,7 @@ static mgl_key x11_keysym_to_mgl_key(KeySym key_sym) { case XK_BackSpace: return MGL_KEY_BACKSPACE; case XK_Tab: return MGL_KEY_TAB; case XK_Return: return MGL_KEY_ENTER; + case XK_Escape: return MGL_KEY_ESCAPE; case XK_Delete: return MGL_KEY_DELETE; case XK_Home: return MGL_KEY_HOME; case XK_Left: return MGL_KEY_LEFT; @@ -243,7 +244,6 @@ static bool mgl_window_on_receive_event(mgl_window *self, XEvent *xev, mgl_event event->type = MGL_EVENT_RESIZED; event->size.width = self->size.x; event->size.height = self->size.y; - event->mouse_move.y = self->cursor_position.y; return true; } return false; |