diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-10-16 19:36:53 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-10-16 19:46:05 +0200 |
commit | 046b2b7a38ec66208c96be59c030294b6d10351b (patch) | |
tree | 5858b113d026c47cdae0c47033eaa0bbe9f0d113 /include/mgl/graphics | |
parent | 5cbff06ff9153f7a7958202a777d98ebeae59393 (diff) |
Add font rendering
Diffstat (limited to 'include/mgl/graphics')
-rw-r--r-- | include/mgl/graphics/font.h | 37 | ||||
-rw-r--r-- | include/mgl/graphics/text.h | 23 | ||||
-rw-r--r-- | include/mgl/graphics/texture.h | 13 |
3 files changed, 68 insertions, 5 deletions
diff --git a/include/mgl/graphics/font.h b/include/mgl/graphics/font.h new file mode 100644 index 0000000..470662a --- /dev/null +++ b/include/mgl/graphics/font.h @@ -0,0 +1,37 @@ +#ifndef MGL_FONT_H +#define MGL_FONT_H + +#include "../system/vec.h" +#include "texture.h" +#include <stdint.h> + +typedef struct mgl_font mgl_font; + +typedef struct { + mgl_vec2f position; + mgl_vec2f size; + mgl_vec2f texture_position; + mgl_vec2f texture_size; + float advance; +} mgl_font_glyph; + +typedef struct { + unsigned char *atlas; + int width; + int height; +} mgl_font_atlas; + +struct mgl_font { + mgl_texture texture; + mgl_font_atlas font_atlas; + unsigned int size; + void *packed_chars; + uint32_t num_packed_chars; +}; + +int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int font_size); +void mgl_font_unload(mgl_font *self); + +int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph); + +#endif /* MGL_FONT_H */ diff --git a/include/mgl/graphics/text.h b/include/mgl/graphics/text.h new file mode 100644 index 0000000..bb07675 --- /dev/null +++ b/include/mgl/graphics/text.h @@ -0,0 +1,23 @@ +#ifndef MGL_TEXT_H +#define MGL_TEXT_H + +#include "../system/vec.h" +#include "color.h" + +typedef struct mgl_font mgl_font; +typedef struct mgl_context mgl_context; + +typedef struct { + mgl_font *font; + const char *text; + mgl_color color; + mgl_vec2f position; +} mgl_text; + +/* Note: keeps a reference to |text|. |text| needs to be valid as long as |self| is used. */ +int mgl_text_init(mgl_text *self, mgl_font *font, const char *text, float x, float y); +void mgl_text_deinit(mgl_text *self); + +void mgl_text_draw(mgl_context *context, mgl_text *text); + +#endif /* MGL_TEXT_H */ diff --git a/include/mgl/graphics/texture.h b/include/mgl/graphics/texture.h index f859554..e6fc97b 100644 --- a/include/mgl/graphics/texture.h +++ b/include/mgl/graphics/texture.h @@ -6,10 +6,11 @@ typedef struct mgl_texture mgl_texture; typedef enum { - MGL_TEXTURE_GRAY = 1, - MGL_TEXTURE_GRAY_ALPHA = 2, - MGL_TEXTURE_RGB = 3, - MGL_TEXTURE_RGB_ALPHA = 4 + MGL_TEXTURE_ALPHA, + MGL_TEXTURE_GRAY, + MGL_TEXTURE_GRAY_ALPHA, + MGL_TEXTURE_RGB, + MGL_TEXTURE_RGBA } mgl_texture_format; struct mgl_texture { @@ -23,8 +24,10 @@ typedef struct { bool compressed; } mgl_texture_load_options; -/* |load_options| can be null, in which case default options are used */ +/* |load_options| can be null, in which case the default options are used */ int mgl_texture_load_from_file(mgl_texture *self, const char *filepath, mgl_texture_load_options *load_options); +/* |load_options| can be null, in which case the default options are used */ +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); void mgl_texture_unload(mgl_texture *self); #endif /* MGL_TEXTURE_H */ |