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 | |
parent | 7dd8b0cc0561c5fa0c9096b79fb7f0647b3470b4 (diff) |
Fix font being corrupt with certain sizes, fix image being corrupt if its rgb, fix window resize incorrect size
-rw-r--r-- | include/mgl/gl.h | 1 | ||||
-rw-r--r-- | include/mgl/gl_macro.h | 2 | ||||
-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 | ||||
-rw-r--r-- | tests/X11.jpg | bin | 0 -> 19567 bytes | |||
-rw-r--r-- | tests/main.c | 4 |
9 files changed, 30 insertions, 13 deletions
diff --git a/include/mgl/gl.h b/include/mgl/gl.h index c9a709d..1d19339 100644 --- a/include/mgl/gl.h +++ b/include/mgl/gl.h @@ -27,7 +27,6 @@ typedef struct { void (*glGenTextures)(int n, unsigned int *textures); void (*glDeleteTextures)(int n, const unsigned int *textures); void (*glTexImage2D)(unsigned int target, int level, int internalFormat, int width, int height, int border, unsigned int format, unsigned int type, const void *pixels); - void (*glCompressedTexImage2D)(unsigned int target, int level, unsigned int internalformat, int width, int height, int border, int imageSize, const void *data); void (*glBindTexture)(unsigned int target, unsigned int texture); void (*glTexParameteri)(unsigned int target, unsigned int pname, int param); void (*glHint)(unsigned int target, unsigned int mode); diff --git a/include/mgl/gl_macro.h b/include/mgl/gl_macro.h index c47c8fb..aa264ee 100644 --- a/include/mgl/gl_macro.h +++ b/include/mgl/gl_macro.h @@ -31,6 +31,8 @@ #define GL_UNSIGNED_BYTE 0x1401 #define GL_ALPHA 0x1906 +#define GL_LUMINANCE 0x1909 +#define GL_LUMINANCE_ALPHA 0x190A #define GL_RGB 0x1907 #define GL_RGBA 0x1908 @@ -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; diff --git a/tests/X11.jpg b/tests/X11.jpg Binary files differnew file mode 100644 index 0000000..95e4793 --- /dev/null +++ b/tests/X11.jpg diff --git a/tests/main.c b/tests/main.c index 878f8ff..d430ab2 100644 --- a/tests/main.c +++ b/tests/main.c @@ -32,7 +32,7 @@ static void draw(mgl_window *window, void *userdata) { }; mgl_rectangle_draw(context, &rect); - mgl_shader_program_set_uniform_vec2f(u->shader_program, "resolution", (mgl_vec2f){ window->size.x, window->size.y }); + mgl_shader_program_set_uniform_vec2f(u->shader_program, "resolution", (mgl_vec2f){ u->texture->width, u->texture->height }); mgl_sprite sprite; mgl_sprite_init(&sprite, u->texture, 100.0f - 10.0f, 0.0f); @@ -117,7 +117,7 @@ int main(int argc, char **argv) { if(mgl_window_create(&window, "mgl", 1280, 720) != 0) return 1; - if(mgl_texture_load_from_file(&texture, "tests/X11.png", NULL) != 0) + if(mgl_texture_load_from_file(&texture, "tests/X11.jpg", NULL) != 0) return 1; if(mgl_font_load_from_file(&font, "/usr/share/fonts/noto/NotoSans-Regular.ttf", 32) != 0) |