diff --git a/engine/gf_draw_common.c b/engine/gf_draw_common.c index 0f65efa..51ebde3 100644 --- a/engine/gf_draw_common.c +++ b/engine/gf_draw_common.c @@ -39,6 +39,7 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) { draw->height = 480; draw->running = 0; draw->draw_3d = 0; + draw->font = NULL; strcpy(draw->title, title); draw->platform = gf_draw_platform_create(engine, draw); if(draw->platform != NULL) { @@ -61,6 +62,8 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) { draw->lookat[1] = 0; draw->lookat[2] = 0; + draw->font = gf_font_create("font/helvR24.bdf"); + draw->gui = gf_gui_create(engine, draw); if(1) { int w, h, c; @@ -68,29 +71,6 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) { test_texture = gf_texture_create(draw, w, h, d); free(d); } - - regcount = 0; - for(i = 0; i < 128; i++) { - unsigned char* font = malloc(8 * 8 * 4); - int j; - /* TODO: check font here */ - draw->font[i] = 0; - if(1) { - free(font); - continue; - } - regcount++; - for(j = 0; j < 8 * 8; j++) { - unsigned char val = 255; - font[j * 4 + 0] = val; - font[j * 4 + 1] = val; - font[j * 4 + 2] = val; - font[j * 4 + 3] = val; - } - draw->font[i] = gf_texture_create(draw, 8, 8, font); - free(font); - } - gf_log_function(engine, "Registered %d glyphs", regcount); } else { gf_draw_destroy(draw); draw = NULL; @@ -138,12 +118,6 @@ int gf_draw_step(gf_draw_t* draw) { } void gf_draw_destroy(gf_draw_t* draw) { - if(draw->running) { - int i; - for(i = 0; i < 128; i++) { - if(draw->font[i] != NULL) gf_texture_destroy(draw->font[i]); - } - } if(draw->driver != NULL) gf_draw_driver_destroy(draw->driver); if(draw->platform != NULL) gf_draw_platform_destroy(draw->platform); gf_log_function(draw->engine, "Destroyed drawing interface", ""); diff --git a/engine/gf_font.c b/engine/gf_font.c index e1a55ef..88d0f5b 100644 --- a/engine/gf_font.c +++ b/engine/gf_font.c @@ -1,3 +1,5 @@ +#define GF_EXPOSE_FONT + #include /* External library */ @@ -6,5 +8,38 @@ #include /* Engine */ +#include /* Standard */ +#include +#include +#include + +gf_font_glyph_t* gf_font_get(gf_font_t* font, int code) { + int i; + if(code < 0x20) return NULL; + for(i = 0; i < font->count; i++) { + if(font->glyph[i]->code == code) { + return font->glyph[i]; + } + } + return NULL; +} + +#ifdef _MSC_VER +#define gf_stat _stat +#else +#define gf_stat stat +#endif + +gf_font_t* gf_font_create(const char* path) { + gf_font_t* font = malloc(sizeof(*font)); + struct gf_stat s; + memset(font, 0, sizeof(*font)); + if(gf_stat(path, &s) != 0) { + free(font); + return NULL; + } + gf_log_function(NULL, "%s: %lu bytes", path, (unsigned long)s.st_size); + return font; +} diff --git a/engine/gf_graphic_common.c b/engine/gf_graphic_common.c index 8088c03..59a1d54 100644 --- a/engine/gf_graphic_common.c +++ b/engine/gf_graphic_common.c @@ -1,5 +1,6 @@ #define GF_EXPOSE_DRAW #define GF_EXPOSE_TEXTURE +#define GF_EXPOSE_FONT #include @@ -10,6 +11,7 @@ /* Engine */ #include +#include /* Standard */ #include @@ -17,13 +19,20 @@ void gf_graphic_fill_rect(gf_draw_t* draw, float x, float y, float w, float h, gf_graphic_color_t color) { gf_graphic_fill_polygon(draw, color, GF_GRAPHIC_2D, 4, x, y, x, y + h, x + w, y + h, x + w, y); } void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* text, gf_graphic_color_t color) { - int i; + int i; + double mx = 0; + gf_font_glyph_t* glyph; for(i = 0; text[i] != 0; i++) { - if(draw->font[text[i]] != NULL) gf_graphic_draw_texture_2d(draw, x + i * (size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y), y, size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y, size, draw->font[text[i]], color); + if(draw->font != NULL && (glyph = gf_font_get(draw->font, text[i])) != NULL) { + double fax = 64; + double fay = 64; + gf_graphic_draw_texture_2d(draw, x + mx, y, size * fax / fay, size, glyph->texture, color); + mx += (size * glyph->dwidth[0]); + } } } -float gf_graphic_text_width(gf_draw_t* draw, float size, const char* text) { return (float)strlen(text) * (size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y); } +float gf_graphic_text_width(gf_draw_t* draw, float size, const char* text) { return 0; } void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_graphic_color_t color) { if(texture != NULL) gf_graphic_draw_texture_polygon(draw, texture, color, GF_GRAPHIC_2D, 4, 0.0, 0.0, x, y, 0.0, 1.0, x, y + h, 1.0, 1.0, x + w, y + h, 1.0, 0.0, x + w, y); diff --git a/engine/include/gf_font.h b/engine/include/gf_font.h index 6576720..21595cf 100644 --- a/engine/include/gf_font.h +++ b/engine/include/gf_font.h @@ -11,9 +11,27 @@ #include /* Type */ +#include /* Engine */ /* Standard */ +/** + * @~english + * @brief Get glyph + * @param font Font + * @param code Character code + * @return Glyph + */ +GF_EXPORT gf_font_glyph_t* gf_font_get(gf_font_t* font, int code); + +/** + * @~english + * @brief Load font + * @param path Path + * @return Font + */ +GF_EXPORT gf_font_t* gf_font_create(const char* path); + #endif diff --git a/engine/include/gf_graphic.h b/engine/include/gf_graphic.h index 14e5b6b..7e79222 100644 --- a/engine/include/gf_graphic.h +++ b/engine/include/gf_graphic.h @@ -19,18 +19,6 @@ /* Standard */ -/** - * @~english - * @brief Aspect of font width - */ -#define GF_GRAPHIC_FONT_ASPECT_X 1 - -/** - * @~english - * @brief Aspect of font height - */ -#define GF_GRAPHIC_FONT_ASPECT_Y 2 - /** * @~english * @brief Dimension parameter for 2D diff --git a/engine/include/gf_macro.h b/engine/include/gf_macro.h index ac07b0f..bc46a79 100644 --- a/engine/include/gf_macro.h +++ b/engine/include/gf_macro.h @@ -105,6 +105,14 @@ #define GF_EXPOSE_SERVER #endif +#ifndef GF_EXPOSE_FONT +/** + * @~english + * @brief Expose font properties + */ +#define GF_EXPOSE_FONT +#endif + #ifndef GF_EXPOSE_GRAPHIC /** * @~english diff --git a/engine/include/gf_type/draw.h b/engine/include/gf_type/draw.h index 6694cc2..4326c3d 100644 --- a/engine/include/gf_type/draw.h +++ b/engine/include/gf_type/draw.h @@ -20,6 +20,7 @@ #include #include #include +#include /* Standard */ @@ -65,7 +66,7 @@ * @brief Window title * * @var gf_draw_t::font - * @brief Array of texture of glyphs + * @brief Current font * * @var gf_draw_t::light * @brief Light location @@ -93,7 +94,7 @@ GF_DECLARE_TYPE(draw, { int running; int draw_3d; char title[128]; - gf_texture_t* font[128]; + gf_font_t* font; gf_math_vector_t light; gf_math_vector_t lookat; gf_math_vector_t camera; diff --git a/engine/include/gf_type/font.h b/engine/include/gf_type/font.h new file mode 100644 index 0000000..0d160c1 --- /dev/null +++ b/engine/include/gf_type/font.h @@ -0,0 +1,98 @@ +/** + * @file gf_type/font.h + * @~english + * @brief Type definitions related to font + */ + +#ifndef __GF_TYPE_FONT_H__ +#define __GF_TYPE_FONT_H__ + +#include +#include + +#ifdef GF_EXPOSE_FONT +/* External library */ + +/* Engine */ +#include + +/* Standard */ + +/** + * @struct gf_font_bbox_t + * @~english + * @brief Bounding box + * + * @var gf_font_bbox_t::width + * @brief Width + * + * @var gf_font_bbox_t::height + * @brief Height + * + * @var gf_font_bbox_t::x + * @brief X coord + * + * @var gf_font_bbox_t::y + * @brief Y coord + */ +GF_DECLARE_TYPE(font_bbox, { + int width; + int height; + int x; + int y; +}); + +/** + * @struct gf_font_glyph_t + * @~english + * @brief Glyph + * + * @var gf_font_glyph_t::code + * @brief Character code + * + * @var gf_font_glyph_t::texture + * @brief Texture + * + * @var gf_font_glyph_t::bpl + * @brief Bytes per line + * + * @var gf_font_glyph_t::bbox + * @brief Bounding box + * + * @var gf_font_glyph_t::dwidth + * @brief Device width + */ +GF_DECLARE_TYPE(font_glyph, { + int code; + gf_texture_t* texture; + int bpl; + gf_font_bbox_t bbox; + int dwidth[2]; +}); + +/** + * @struct gf_font_t + * @~english + * @brief Font + * + * @var gf_font_t::glyph + * @brief Glyph data + * + * @var gf_font_t::count + * @brief Glyph count + * + * @var gf_font_t::bbox + * @brief Bounding box + */ +GF_DECLARE_TYPE(font, { + gf_font_glyph_t** glyph; + int count; + gf_font_bbox_t bbox; +}); +#else +typedef void gf_font_glyph_t; +typedef void gf_font_bbox_t; +typedef void gf_font_t; +#endif + +#endif