diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-10-19 22:12:52 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-10-19 22:12:52 +0200 |
commit | 9da3c2188060dc982412d7a6e1cd2051b9ddb6a6 (patch) | |
tree | 18d7cd9ec63c1f2e42dcda3941907f32e34ac241 /src/graphics | |
parent | 3bdf82eec2c915e91ae487e29d72639f9efcad67 (diff) |
Change from callback to window poll
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/font.c | 12 | ||||
-rw-r--r-- | src/graphics/image.c | 67 | ||||
-rw-r--r-- | src/graphics/text.c | 4 | ||||
-rw-r--r-- | src/graphics/texture.c | 103 |
4 files changed, 115 insertions, 71 deletions
diff --git a/src/graphics/font.c b/src/graphics/font.c index 20d9dfa..f55144a 100644 --- a/src/graphics/font.c +++ b/src/graphics/font.c @@ -10,14 +10,14 @@ /* TODO: Test and fix .tcc files */ -int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int font_size) { +int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int character_size) { self->texture.id = 0; self->font_atlas.atlas = NULL; self->font_atlas.width = 0; self->font_atlas.height = 0; - self->size = font_size; + self->character_size = character_size; self->packed_chars = NULL; self->num_packed_chars = 0; @@ -47,8 +47,8 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int f /* TODO: Optimize */ /* Find optimal size for atlas, starting from small to large */ for(int i = 0; i < 4; ++i) { - self->font_atlas.width = (8 + (8 * i)) * self->size; - self->font_atlas.height = (8 + (8 * i)) * self->size; + self->font_atlas.width = (8 + (8 * i)) * self->character_size; + self->font_atlas.height = (8 + (8 * i)) * 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); @@ -67,7 +67,7 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int f return -1; } - if(!stbtt_PackFontRange(&pc, filedata.data, 0, self->size, 0, self->num_packed_chars, self->packed_chars)) { + if(!stbtt_PackFontRange(&pc, filedata.data, 0, self->character_size, 0, self->num_packed_chars, self->packed_chars)) { stbtt_PackEnd(&pc); continue; } @@ -84,7 +84,7 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int f return -1; } - if(mgl_texture_load_from_memory(&self->texture, self->font_atlas.atlas, self->font_atlas.width, self->font_atlas.height, MGL_TEXTURE_ALPHA, NULL) != 0) { + if(mgl_texture_load_from_memory(&self->texture, self->font_atlas.atlas, self->font_atlas.width, self->font_atlas.height, MGL_IMAGE_FORMAT_ALPHA, NULL) != 0) { fprintf(stderr, "Error: failed to load font %s, error: mgl_texture_load_from_memory failed\n", filepath); mgl_filedata_free(&filedata); mgl_font_unload(self); diff --git a/src/graphics/image.c b/src/graphics/image.c new file mode 100644 index 0000000..430f03a --- /dev/null +++ b/src/graphics/image.c @@ -0,0 +1,67 @@ +#include "../../include/mgl/graphics/image.h" + +#define STBI_NO_PSD +#define STBI_NO_TGA +#define STBI_NO_HDR +#define STBI_NO_PIC +#define STBI_NO_PNM +#define STB_IMAGE_IMPLEMENTATION +#include "../../external/stb_image.h" + +static mgl_image_format stbi_format_to_mgl_image_format(int stbi_format) { + switch(stbi_format) { + case STBI_grey: + return MGL_IMAGE_FORMAT_GRAY; + case STBI_grey_alpha: + return MGL_IMAGE_FORMAT_GRAY_ALPHA; + case STBI_rgb: + return MGL_IMAGE_FORMAT_RGB; + case STBI_rgb_alpha: + return MGL_IMAGE_FORMAT_RGBA; + } + return 0; +} + +static size_t mgl_image_format_num_channels(mgl_image_format image_format) { + switch(image_format) { + case MGL_IMAGE_FORMAT_ALPHA: return 1; + case MGL_IMAGE_FORMAT_GRAY: return 1; + case MGL_IMAGE_FORMAT_GRAY_ALPHA: return 2; + case MGL_IMAGE_FORMAT_RGB: return 3; + case MGL_IMAGE_FORMAT_RGBA: return 4; + } + return 0; +} + +/* TODO: Ensure texture is power of 2 if the hardware doesn't support non power of two textures */ +/* TODO: Verify if source format should always be 4 components (RGBA) because apparently if its another format then opengl will internally convert it to RGBA */ +int mgl_image_load_from_file(mgl_image *self, const char *filepath) { + self->data = NULL; + self->width = 0; + self->height = 0; + + int format; + self->data = stbi_load(filepath, &self->width, &self->height, &format, 0); + if(!self->data) { + fprintf(stderr, "Error: failed to load image %s, error: %s\n", filepath, stbi_failure_reason()); + mgl_image_unload(self); + return -1; + } + self->format = stbi_format_to_mgl_image_format(format); + + return 0; +} + +void mgl_image_unload(mgl_image *self) { + if(self->data) { + stbi_image_free(self->data); + self->data = NULL; + } + self->width = 0; + self->height = 0; + self->format = 0; +} + +size_t mgl_image_get_size(mgl_image *self) { + return (size_t)self->width * (size_t)self->height * mgl_image_format_num_channels(self->format); +} diff --git a/src/graphics/text.c b/src/graphics/text.c index 0aa33cf..7f90216 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -42,7 +42,7 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) { const char *str = text->text; mgl_font_glyph glyph; mgl_vec2f position = text->position; - position.y += text->font->size; + position.y += text->font->character_size; context->gl.glColor4ub(text->color.r, text->color.g, text->color.b, text->color.a); context->gl.glBindTexture(GL_TEXTURE_2D, text->font->texture.id); @@ -64,7 +64,7 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) { } } else if(c == '\n') { position.x = text->position.x; - position.y += text->font->size; + position.y += text->font->character_size; } ++str; } diff --git a/src/graphics/texture.c b/src/graphics/texture.c index 4c7f68c..1427e6b 100644 --- a/src/graphics/texture.c +++ b/src/graphics/texture.c @@ -1,108 +1,85 @@ #include "../../include/mgl/graphics/texture.h" +#include "../../include/mgl/graphics/image.h" #include "../../include/mgl/mgl.h" - -#define STBI_NO_PSD -#define STBI_NO_TGA -#define STBI_NO_HDR -#define STBI_NO_PIC -#define STBI_NO_PNM -#define STB_IMAGE_IMPLEMENTATION -#include "../../external/stb_image.h" +#include <stdio.h> /* TODO: Check for glTexImage2D failure */ static int mgl_texture_format_to_opengl_format(mgl_texture_format format) { switch(format) { - case MGL_TEXTURE_ALPHA: return GL_ALPHA8; - case MGL_TEXTURE_GRAY: return GL_LUMINANCE8; - case MGL_TEXTURE_GRAY_ALPHA: return GL_LUMINANCE8_ALPHA8; - case MGL_TEXTURE_RGB: return GL_RGB8; - case MGL_TEXTURE_RGBA: return GL_RGBA8; + case MGL_TEXTURE_FORMAT_ALPHA: return GL_ALPHA8; + case MGL_TEXTURE_FORMAT_GRAY: return GL_LUMINANCE8; + case MGL_TEXTURE_FORMAT_GRAY_ALPHA: return GL_LUMINANCE8_ALPHA8; + case MGL_TEXTURE_FORMAT_RGB: return GL_RGB8; + case MGL_TEXTURE_FORMAT_RGBA: return GL_RGBA8; } return 0; } static int mgl_texture_format_to_compressed_opengl_format(mgl_texture_format format) { switch(format) { - case MGL_TEXTURE_ALPHA: return GL_ALPHA8; - case MGL_TEXTURE_GRAY: return GL_LUMINANCE8; - case MGL_TEXTURE_GRAY_ALPHA: return GL_LUMINANCE8_ALPHA8; - case MGL_TEXTURE_RGB: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; - case MGL_TEXTURE_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + case MGL_TEXTURE_FORMAT_ALPHA: return GL_ALPHA8; + case MGL_TEXTURE_FORMAT_GRAY: return GL_LUMINANCE8; + case MGL_TEXTURE_FORMAT_GRAY_ALPHA: return GL_LUMINANCE8_ALPHA8; + case MGL_TEXTURE_FORMAT_RGB: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + case MGL_TEXTURE_FORMAT_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; } return 0; } static int mgl_texture_format_to_source_opengl_format(mgl_texture_format format) { switch(format) { - case MGL_TEXTURE_ALPHA: return GL_ALPHA; - case MGL_TEXTURE_GRAY: return GL_LUMINANCE8; - case MGL_TEXTURE_GRAY_ALPHA: return GL_LUMINANCE8_ALPHA8; - case MGL_TEXTURE_RGB: return GL_RGB; - case MGL_TEXTURE_RGBA: return GL_RGBA; + 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_RGB: return GL_RGB; + case MGL_TEXTURE_FORMAT_RGBA: return GL_RGBA; } return 0; } -static mgl_texture_format stbi_format_to_mgl_texture_format(int stbi_format) { - switch(stbi_format) { - case STBI_grey: - return MGL_TEXTURE_GRAY; - case STBI_grey_alpha: - return MGL_TEXTURE_GRAY_ALPHA; - case STBI_rgb: - return MGL_TEXTURE_RGB; - case STBI_rgb_alpha: - return MGL_TEXTURE_RGBA; +static mgl_texture_format mgl_image_format_to_mgl_texture_format(mgl_image_format image_format) { + switch(image_format) { + case MGL_IMAGE_FORMAT_ALPHA: return MGL_TEXTURE_FORMAT_ALPHA; + case MGL_IMAGE_FORMAT_GRAY: return MGL_TEXTURE_FORMAT_GRAY; + case MGL_IMAGE_FORMAT_GRAY_ALPHA: return MGL_TEXTURE_FORMAT_GRAY_ALPHA; + case MGL_IMAGE_FORMAT_RGB: return MGL_TEXTURE_FORMAT_RGB; + case MGL_IMAGE_FORMAT_RGBA: return MGL_TEXTURE_FORMAT_RGBA; } return 0; } -/* TODO: Ensure texture is power of 2 if the hardware doesn't support non power of two textures */ -/* TODO: Verify if source format should always be 4 components (RGBA) because apparently if its another format then opengl will internally convert it to RGBA */ int mgl_texture_load_from_file(mgl_texture *self, const char *filepath, mgl_texture_load_options *load_options) { self->id = 0; + self->width = 0; + self->height = 0; - int format; - stbi_uc *image_data = stbi_load(filepath, &self->width, &self->height, &format, 0); - if(!image_data) { - fprintf(stderr, "Error: failed to load image %s, error: %s\n", filepath, stbi_failure_reason()); + mgl_image image; + if(mgl_image_load_from_file(&image, filepath) != 0) return -1; - } - self->format = stbi_format_to_mgl_texture_format(format); - - mgl_context *context = mgl_get_context(); - context->gl.glGenTextures(1, &self->id); - if(self->id == 0) { - fprintf(stderr, "Error: failed to load image %s", filepath); - stbi_image_free(image_data); - return -1; - } - - const int opengl_texture_format = load_options && load_options->compressed ? mgl_texture_format_to_compressed_opengl_format(self->format) : mgl_texture_format_to_opengl_format(self->format); - context->gl.glBindTexture(GL_TEXTURE_2D, self->id); - context->gl.glTexImage2D(GL_TEXTURE_2D, 0, opengl_texture_format, self->width, self->height, 0, mgl_texture_format_to_source_opengl_format(self->format), GL_UNSIGNED_BYTE, image_data); - context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - context->gl.glBindTexture(GL_TEXTURE_2D, 0); + int result = mgl_texture_load_from_image(self, &image, load_options); + mgl_image_unload(&image); + return result; +} - stbi_image_free(image_data); - return 0; +int mgl_texture_load_from_image(mgl_texture *self, mgl_image *image, mgl_texture_load_options *load_options) { + return mgl_texture_load_from_memory(self, image->data, image->width, image->height, image->format, load_options); } -int mgl_texture_load_from_memory(mgl_texture *self, const unsigned char *data, int width, int height, mgl_texture_format format, mgl_texture_load_options *load_options) { +int mgl_texture_load_from_memory(mgl_texture *self, const unsigned char *data, int width, int height, mgl_image_format format, mgl_texture_load_options *load_options) { self->id = 0; self->width = width; self->height = height; - self->format = format; + self->format = mgl_image_format_to_mgl_texture_format(format); mgl_context *context = mgl_get_context(); context->gl.glGenTextures(1, &self->id); - if(self->id == 0) + if(self->id == 0) { + fprintf(stderr, "Error: failed to load image from memory (glGenTextures failed)\n"); + mgl_texture_unload(self); return -1; + } const int opengl_texture_format = load_options && load_options->compressed ? mgl_texture_format_to_compressed_opengl_format(self->format) : mgl_texture_format_to_opengl_format(self->format); |