diff --git a/src/bcfg.c b/src/bcfg.c new file mode 100644 index 0000000..d589282 --- /dev/null +++ b/src/bcfg.c @@ -0,0 +1,19 @@ +#include "include/sie/config_loader.h" + +char BCFG_PATH[64] = "0:\\zbin\\etc\\SieLibrary.bcfg"; + +void Sie_BCFG_Load() { + __CONFIG_EXTERN(0, cfghdr_fonts_b); + if (Sie_Config_Load(BCFG_PATH, __CONFIG(0, cfghdr_fonts_b))) { + return; + } + BCFG_PATH[0] = '4'; + if (Sie_Config_Load(BCFG_PATH, __CONFIG(0, cfghdr_fonts_b))) { + return; + } + BCFG_PATH[0] = '\0'; +} + +const char *Sie_BCFG_GetPath() { + return BCFG_PATH; +} diff --git a/src/config_data.c b/src/config_data.c new file mode 100644 index 0000000..ec74f00 --- /dev/null +++ b/src/config_data.c @@ -0,0 +1,13 @@ +#include + +__CFG_BEGIN(0) + +__root CFG_HDR cfghdr_fonts_b = {CFG_LEVEL, "Fonts", 1, 0}; + +__root CFG_HDR cfghdr_fonts_0 = {CFG_STR_UTF8, "Font regular path", 3, 127}; +__root char CFG_FONT_REGULAR_PATH[128] = "0:\\zbin\\fonts\\Roboto-Regular.ttf"; + +__root CFG_HDR cfghdr_fonts_e = {CFG_LEVEL, "", 0, 0}; + + +__CFG_END(0) diff --git a/src/config_loader.c b/src/config_loader.c new file mode 100644 index 0000000..7642030 --- /dev/null +++ b/src/config_loader.c @@ -0,0 +1,50 @@ +#include +#include +#include + +static size_t GetSize(CFG_HDR *cfghdr0, const int *config_begin, const int *config_end) { + volatile unsigned int segb = (volatile unsigned int)config_begin; + volatile unsigned int sege = (volatile unsigned int)config_end; + return (sege - segb) - 4; +} + +int Sie_Config_Save(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end) { + unsigned int err; + int fp = _open(path, A_ReadWrite + A_Create + A_Truncate, + P_READ + P_WRITE, &err); + if (fp != -1) { + size_t size = GetSize(cfghdr0, config_begin, config_end); + int write_size = _write(fp, cfghdr0, (int)size, &err); + _close(fp, &err); + if (size == write_size) { + return 1; + } + } + return 0; +} + +int Sie_Config_Load(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end) { + int result = 0; + size_t size = GetSize(cfghdr0, config_begin, config_end); + + char *buf = malloc(size); + if (buf) { + unsigned int err; + int fp = _open(path, A_ReadOnly + A_BIN, 0, &err); + if (fp != -1) { + if (_read(fp, buf, (int)size, &err) == size) { + memcpy(cfghdr0, buf, size); + _close(fp, &err); + result = 2; + } else { + _close(fp, &err); + goto SAVE_NEW_CFG; + } + } else { + SAVE_NEW_CFG: + result = Sie_Config_Save(path, cfghdr0, config_begin, config_end); + } + mfree(buf); + } + return result; +} diff --git a/src/freetype/draw_string.c b/src/freetype/draw_string.c index 9161396..7024ff0 100644 --- a/src/freetype/draw_string.c +++ b/src/freetype/draw_string.c @@ -72,8 +72,8 @@ static void DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2, unsigned int w = 0, h = 0; WSHDR *copy_ws = AllocWS(len); wstrcpy(copy_ws, ws); + Sie_FT_GetStringSize(copy_ws, font_size, &w, &h); if (attr & SIE_FT_TEXT_ALIGN_CENTER) { - Sie_FT_GetStringSize(copy_ws, font_size, &w, &h); if (w <= x2 - x) { x_text = x + (x2 - x - (int)w) / 2; } else { @@ -85,7 +85,6 @@ static void DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2, if (len <= 1) { break; } else { - Sie_FT_GetStringSize(copy_ws, font_size, &w, &h); x_text = x2 - (int)w; if (w > x2 -x) { wsRemoveChars(copy_ws, 1, 2); @@ -101,7 +100,6 @@ static void DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2, if (len <= 1) { break; } else { - Sie_FT_GetStringSize(copy_ws, font_size, &w, &h); if (w > x2 - x) { wsRemoveChars(copy_ws, len - 1, len); len -= 1; diff --git a/src/freetype/freetype.c b/src/freetype/freetype.c index 0de1437..3c1c4df 100644 --- a/src/freetype/freetype.c +++ b/src/freetype/freetype.c @@ -4,19 +4,17 @@ #include "../include/sie/gui/gui.h" #include "../include/sie/freetype/freetype_cache.h" -FT_Library *FT_LIBRARY = NULL; -FT_Face *FT_FACE_REGULAR = NULL; -unsigned int FT_CLIENTS_COUNT = 0; +extern char CFG_FONT_REGULAR_PATH[]; + +FT_Library *FT_LIBRARY; +FT_Face *FT_FACE_REGULAR; +unsigned int FT_CLIENTS_COUNT; void Sie_FT_Init() { if (!FT_LIBRARY) { FT_LIBRARY = malloc(sizeof(FT_Library)); FT_Init_FreeType(FT_LIBRARY); - if (!FT_FACE_REGULAR) { - FT_FACE_REGULAR = malloc(sizeof(FT_Face)); - FT_New_Face(*FT_LIBRARY, "0:\\ZBin\\fonts\\Roboto-Regular.ttf", - 0, FT_FACE_REGULAR); - } + Sie_FT_LoadFaces(); } FT_CLIENTS_COUNT++; } @@ -27,11 +25,7 @@ void Sie_FT_Destroy() { } if (!FT_CLIENTS_COUNT) { if (FT_LIBRARY) { - if (FT_FACE_REGULAR) { - FT_Done_Face(*FT_FACE_REGULAR); - mfree(FT_FACE_REGULAR); - FT_FACE_REGULAR = NULL; - } + Sie_FT_UnloadFaces(); FT_Done_FreeType(*FT_LIBRARY); mfree(FT_LIBRARY); FT_LIBRARY = NULL; @@ -40,6 +34,20 @@ void Sie_FT_Destroy() { } } +void Sie_FT_UnloadFaces() { + if (FT_FACE_REGULAR) { + FT_Done_Face(*FT_FACE_REGULAR); + mfree(FT_FACE_REGULAR); + FT_FACE_REGULAR = NULL; + } +} + +void Sie_FT_LoadFaces() { + FT_FACE_REGULAR = malloc(sizeof(FT_Face)); + FT_New_Face(*FT_LIBRARY, CFG_FONT_REGULAR_PATH, + SIE_FT_FACE_REGULAR_ID, FT_FACE_REGULAR); +} + int Sie_FT_GetMaxHeight(int font_size) { FT_Face *face = FT_FACE_REGULAR; FT_Set_Pixel_Sizes(*face, 0, font_size); diff --git a/src/include/sie/bcfg.h b/src/include/sie/bcfg.h new file mode 100644 index 0000000..f254c34 --- /dev/null +++ b/src/include/sie/bcfg.h @@ -0,0 +1,7 @@ +#ifndef __SIE_BCFG_H__ +#define __SIE_BCFG_H__ + +void Sie_BCFG_Load(); +const char *Sie_BCFG_GetPath(); + +#endif diff --git a/src/include/sie/config_loader.h b/src/include/sie/config_loader.h new file mode 100644 index 0000000..92b73e0 --- /dev/null +++ b/src/include/sie/config_loader.h @@ -0,0 +1,9 @@ +#ifndef __SIE_CONFIG_LOADER_H__ +#define __SIE_CONFIG_LOADER_H__ + +#include + +int Sie_Config_Save(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end); +int Sie_Config_Load(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end); + +#endif diff --git a/src/include/sie/freetype/freetype.h b/src/include/sie/freetype/freetype.h index 0646e67..948231a 100644 --- a/src/include/sie/freetype/freetype.h +++ b/src/include/sie/freetype/freetype.h @@ -6,6 +6,8 @@ #include "draw_text.h" #include "draw_string.h" +#define SIE_FT_FACE_REGULAR_ID 0x00 + #define SIE_FT_TEXT_ALIGN_LEFT TEXT_ALIGNLEFT #define SIE_FT_TEXT_ALIGN_CENTER TEXT_ALIGNMIDDLE #define SIE_FT_TEXT_ALIGN_RIGHT TEXT_ALIGNRIGHT @@ -27,10 +29,12 @@ typedef struct { void (*OnBeforeDraw)(int x, int y, int x2, int y2); } SIE_FT_SCROLL_STRING; +void Sie_FT_UnloadFaces(); +void Sie_FT_LoadFaces(); + int Sie_FT_GetMaxHeight(int font_size); void Sie_FT_GetStringSize(WSHDR *ws, int font_size, unsigned int *w, unsigned int *h); - void Sie_FT_DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2, int font_size, int attr, const char *color); void Sie_FT_DrawBoundingScrollString(SIE_FT_SCROLL_STRING *ss, GBSTMR *tmr); diff --git a/src/include/sie/sie.h b/src/include/sie/sie.h index c1463c9..42767fa 100644 --- a/src/include/sie/sie.h +++ b/src/include/sie/sie.h @@ -3,10 +3,12 @@ #include "ext.h" #include "exec.h" +#include "bcfg.h" #include "strings.h" #include "subproc.h" #include "settings.h" #include "resources.h" +#include "config_loader.h" #include "fs/fs.h" #include "gui/gui.h" #include "freetype/freetype.h" diff --git a/src/init.c b/src/init.c index ce3da37..735b223 100644 --- a/src/init.c +++ b/src/init.c @@ -1,10 +1,11 @@ -#include +#include "include/sie/bcfg.h" __attribute__((constructor)) void InitLibrary() { extern void Sie_FT_Init(); extern void Sie_SubProc_Init(); extern void Sie_Resources_Init(); + Sie_BCFG_Load(); Sie_FT_Init(); Sie_SubProc_Init(); Sie_Resources_Init();