From efa286a550115ebcb8dfb4e84ea6a719d110fd27 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 9 Oct 2016 13:09:08 +0200 Subject: [PATCH] Allow no default font loading Useful if text module is not required... --- src/core.c | 21 ++++++++++++++++----- src/text.c | 28 ++++++++++++++++++---------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/core.c b/src/core.c index 0db1c573f..a35dbae05 100644 --- a/src/core.c +++ b/src/core.c @@ -18,6 +18,10 @@ * * On PLATFORM_RPI, graphic device is managed by EGL and input system is coded in raw mode. * +* Module Configuration Flags: +* +* RL_LOAD_DEFAULT_FONT - Use external module functions to load default raylib font (module: text) +* * Copyright (c) 2014 Ramon Santamaria (@raysan5) * * This software is provided "as-is", without any express or implied warranty. In no event @@ -130,6 +134,8 @@ #define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad) #endif +#define RL_LOAD_DEFAULT_FONT // Load default font on window initialization (module: text) + //---------------------------------------------------------------------------------- // Types and Structures Definition //---------------------------------------------------------------------------------- @@ -238,11 +244,10 @@ static bool showLogo = false; // Track if showing logo at init is //---------------------------------------------------------------------------------- // Other Modules Functions Declaration (required by core) //---------------------------------------------------------------------------------- +#if defined(RL_LOAD_DEFAULT_FONT) extern void LoadDefaultFont(void); // [Module: text] Loads default font on InitWindow() extern void UnloadDefaultFont(void); // [Module: text] Unloads default font from GPU memory - -extern void ProcessGestureEvent(GestureEvent event); // [Module: gestures] Process gesture event and translate it into gestures -extern void UpdateGestures(void); // [Module: gestures] Update gestures detected (called in PollInputEvents()) +#endif //---------------------------------------------------------------------------------- // Module specific Functions Declaration @@ -311,9 +316,11 @@ void InitWindow(int width, int height, const char *title) // Init graphics device (display device and OpenGL context) InitGraphicsDevice(width, height); - // Load default font for convenience +#if defined(RL_LOAD_DEFAULT_FONT) + // Load default font // NOTE: External function (defined in module: text) LoadDefaultFont(); +#endif // Init hi-res timer InitTimer(); @@ -420,7 +427,9 @@ void InitWindow(int width, int height, struct android_app *state) // Close Window and Terminate Context void CloseWindow(void) { +#if defined(RL_LOAD_DEFAULT_FONT) UnloadDefaultFont(); +#endif rlglClose(); // De-init rlgl @@ -2245,9 +2254,11 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd) // Init graphics device (display device and OpenGL context) InitGraphicsDevice(screenWidth, screenHeight); - // Load default font for convenience + #if defined(RL_LOAD_DEFAULT_FONT) + // Load default font // NOTE: External function (defined in module: text) LoadDefaultFont(); + #endif // TODO: GPU assets reload in case of lost focus (lost context) // NOTE: This problem has been solved just unbinding and rebinding context from display diff --git a/src/text.c b/src/text.c index c538ea56b..d1d7602ff 100644 --- a/src/text.c +++ b/src/text.c @@ -293,13 +293,17 @@ void UnloadSpriteFont(SpriteFont spriteFont) // NOTE: chars spacing is proportional to fontSize void DrawText(const char *text, int posX, int posY, int fontSize, Color color) { - Vector2 position = { (float)posX, (float)posY }; + // Check if default font has been loaded + if (defaultFont.texture.id != 0) + { + Vector2 position = { (float)posX, (float)posY }; - int defaultFontSize = 10; // Default Font chars height in pixel - if (fontSize < defaultFontSize) fontSize = defaultFontSize; - int spacing = fontSize/defaultFontSize; + int defaultFontSize = 10; // Default Font chars height in pixel + if (fontSize < defaultFontSize) fontSize = defaultFontSize; + int spacing = fontSize/defaultFontSize; - DrawTextEx(defaultFont, text, position, (float)fontSize, spacing, color); + DrawTextEx(GetDefaultFont(), text, position, (float)fontSize, spacing, color); + } } // Draw text using SpriteFont @@ -404,13 +408,17 @@ const char *SubText(const char *text, int position, int length) // Measure string width for default font int MeasureText(const char *text, int fontSize) { - Vector2 vec; + Vector2 vec = { 0.0f, 0.0f }; - int defaultFontSize = 10; // Default Font chars height in pixel - if (fontSize < defaultFontSize) fontSize = defaultFontSize; - int spacing = fontSize/defaultFontSize; + // Check if default font has been loaded + if (defaultFont.texture.id != 0) + { + int defaultFontSize = 10; // Default Font chars height in pixel + if (fontSize < defaultFontSize) fontSize = defaultFontSize; + int spacing = fontSize/defaultFontSize; - vec = MeasureTextEx(defaultFont, text, fontSize, spacing); + vec = MeasureTextEx(GetDefaultFont(), text, fontSize, spacing); + } return (int)vec.x; }