aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-16 19:36:53 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-16 19:46:05 +0200
commit046b2b7a38ec66208c96be59c030294b6d10351b (patch)
tree5858b113d026c47cdae0c47033eaa0bbe9f0d113 /include
parent5cbff06ff9153f7a7958202a777d98ebeae59393 (diff)
Add font rendering
Diffstat (limited to 'include')
-rw-r--r--include/mgl/gl.h50
-rw-r--r--include/mgl/gl_macro.h56
-rw-r--r--include/mgl/graphics/font.h37
-rw-r--r--include/mgl/graphics/text.h23
-rw-r--r--include/mgl/graphics/texture.h13
-rw-r--r--include/mgl/system/fileutils.h14
6 files changed, 139 insertions, 54 deletions
diff --git a/include/mgl/gl.h b/include/mgl/gl.h
index 823e6af..36b2743 100644
--- a/include/mgl/gl.h
+++ b/include/mgl/gl.h
@@ -1,55 +1,7 @@
#ifndef MGL_GL_H
#define MGL_GL_H
-/* Copied from GL/glx.h */
-#define GLX_USE_GL 1
-#define GLX_BUFFER_SIZE 2
-#define GLX_LEVEL 3
-#define GLX_RGBA 4
-#define GLX_DOUBLEBUFFER 5
-#define GLX_STEREO 6
-#define GLX_AUX_BUFFERS 7
-#define GLX_RED_SIZE 8
-#define GLX_GREEN_SIZE 9
-#define GLX_BLUE_SIZE 10
-#define GLX_ALPHA_SIZE 11
-#define GLX_DEPTH_SIZE 12
-#define GLX_STENCIL_SIZE 13
-#define GLX_ACCUM_RED_SIZE 14
-#define GLX_ACCUM_GREEN_SIZE 15
-#define GLX_ACCUM_BLUE_SIZE 16
-#define GLX_ACCUM_ALPHA_SIZE 17
-
-/* Copied from GL/gl.h */
-#define GL_COLOR_BUFFER_BIT 0x00004000
-#define GL_BLEND 0x0BE2
-#define GL_SRC_ALPHA 0x0302
-#define GL_ONE_MINUS_SRC_ALPHA 0x0303
-#define GL_TEXTURE_2D 0x0DE1
-#define GL_TEXTURE_WRAP_S 0x2802
-#define GL_TEXTURE_WRAP_T 0x2803
-#define GL_TEXTURE_MAG_FILTER 0x2800
-#define GL_TEXTURE_MIN_FILTER 0x2801
-#define GL_UNSIGNED_BYTE 0x1401
-
-#define GL_RGB 0x1907
-#define GL_RGBA 0x1908
-
-#define GL_LUMINANCE8 0x8040
-#define GL_LUMINANCE8_ALPHA8 0x8045
-#define GL_RGB8 0x8051
-#define GL_RGBA8 0x8058
-#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
-#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
-#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
-#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
-
-#define GL_CLAMP_TO_EDGE 0x812F
-#define GL_LINEAR 0x2601
-
-#define GL_QUADS 0x0007
-
-#define GL_PROJECTION 0x1701
+#include "gl_macro.h"
typedef struct _XVisualInfo _XVisualInfo;
typedef struct _XDisplay Display;
diff --git a/include/mgl/gl_macro.h b/include/mgl/gl_macro.h
new file mode 100644
index 0000000..a0bb16c
--- /dev/null
+++ b/include/mgl/gl_macro.h
@@ -0,0 +1,56 @@
+#ifndef MGL_GL_MACRO_H
+#define MGL_GL_MACRO_H
+
+/* Copied from GL/glx.h */
+#define GLX_USE_GL 1
+#define GLX_BUFFER_SIZE 2
+#define GLX_LEVEL 3
+#define GLX_RGBA 4
+#define GLX_DOUBLEBUFFER 5
+#define GLX_STEREO 6
+#define GLX_AUX_BUFFERS 7
+#define GLX_RED_SIZE 8
+#define GLX_GREEN_SIZE 9
+#define GLX_BLUE_SIZE 10
+#define GLX_ALPHA_SIZE 11
+#define GLX_DEPTH_SIZE 12
+#define GLX_STENCIL_SIZE 13
+#define GLX_ACCUM_RED_SIZE 14
+#define GLX_ACCUM_GREEN_SIZE 15
+#define GLX_ACCUM_BLUE_SIZE 16
+#define GLX_ACCUM_ALPHA_SIZE 17
+
+/* Copied from GL/gl.h */
+#define GL_COLOR_BUFFER_BIT 0x00004000
+#define GL_BLEND 0x0BE2
+#define GL_SRC_ALPHA 0x0302
+#define GL_ONE_MINUS_SRC_ALPHA 0x0303
+#define GL_TEXTURE_2D 0x0DE1
+#define GL_TEXTURE_WRAP_S 0x2802
+#define GL_TEXTURE_WRAP_T 0x2803
+#define GL_TEXTURE_MAG_FILTER 0x2800
+#define GL_TEXTURE_MIN_FILTER 0x2801
+#define GL_UNSIGNED_BYTE 0x1401
+
+#define GL_ALPHA 0x1906
+#define GL_RGB 0x1907
+#define GL_RGBA 0x1908
+
+#define GL_ALPHA8 0x803C
+#define GL_LUMINANCE8 0x8040
+#define GL_LUMINANCE8_ALPHA8 0x8045
+#define GL_RGB8 0x8051
+#define GL_RGBA8 0x8058
+#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
+#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
+#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
+#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
+
+#define GL_CLAMP_TO_EDGE 0x812F
+#define GL_LINEAR 0x2601
+
+#define GL_QUADS 0x0007
+
+#define GL_PROJECTION 0x1701
+
+#endif /* MGL_GL_MACRO_H */
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 */
diff --git a/include/mgl/system/fileutils.h b/include/mgl/system/fileutils.h
new file mode 100644
index 0000000..71af147
--- /dev/null
+++ b/include/mgl/system/fileutils.h
@@ -0,0 +1,14 @@
+#ifndef MGL_FILEUTILS_H
+#define MGL_FILEUTILS_H
+
+#include <stddef.h>
+
+typedef struct {
+ unsigned char *data;
+ size_t size;
+} mgl_filedata;
+
+int mgl_load_file(const char *filepath, mgl_filedata *filedata);
+void mgl_filedata_free(mgl_filedata *self);
+
+#endif /* MGL_FILEUTILS_H */