mirror of
https://github.com/raysan5/raygui.git
synced 2026-02-04 05:09:18 -05:00
Avoid memory leak...
...by allocating style as a global array (on heap memory by default). When trying to access guiStyle, if default style was not previously loaded, it loads automatically (RAII)
This commit is contained in:
83
src/raygui.h
83
src/raygui.h
@ -56,9 +56,6 @@
|
|||||||
* internally in the library and input management and drawing functions must be provided by
|
* internally in the library and input management and drawing functions must be provided by
|
||||||
* the user (check library implementation for further details).
|
* the user (check library implementation for further details).
|
||||||
*
|
*
|
||||||
* #define RAYGUI_STYLE_LOADING
|
|
||||||
* Include style loading functionality, useful to load custom styles.
|
|
||||||
*
|
|
||||||
* VERSIONS HISTORY:
|
* VERSIONS HISTORY:
|
||||||
* 2.0 (xx-Nov-2018) Complete review of new controls, redesigned style system
|
* 2.0 (xx-Nov-2018) Complete review of new controls, redesigned style system
|
||||||
* 1.9 (01-May-2018) Lot of rework and redesign! Lots of new controls!
|
* 1.9 (01-May-2018) Lot of rework and redesign! Lots of new controls!
|
||||||
@ -358,12 +355,17 @@ RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledEl
|
|||||||
RAYGUIDEF Color GuiColorPicker(Rectangle bounds, Color color); // Color Picker control
|
RAYGUIDEF Color GuiColorPicker(Rectangle bounds, Color color); // Color Picker control
|
||||||
RAYGUIDEF bool GuiMessageBox(Rectangle bounds, const char *windowTitle, const char *message); // Message Box control, displays a message
|
RAYGUIDEF bool GuiMessageBox(Rectangle bounds, const char *windowTitle, const char *message); // Message Box control, displays a message
|
||||||
|
|
||||||
#if defined(RAYGUI_STYLE_LOADING)
|
// Styles loading functions
|
||||||
RAYGUIDEF void GuiLoadStyle(const char *fileName); // Load style file (.rgs)
|
RAYGUIDEF void GuiLoadStyle(const char *fileName); // Load style file (.rgs)
|
||||||
RAYGUIDEF void GuiLoadStyleProps(const int *props, int count); // Load style from a color palette array (14 values required)
|
RAYGUIDEF void GuiLoadStyleProps(const int *props, int count); // Load style from a color palette array (14 values required)
|
||||||
|
RAYGUIDEF void GuiLoadStyleDefault(void); // Load style default over global style
|
||||||
RAYGUIDEF void GuiUpdateStyleComplete(void); // Updates full style properties set with default values
|
RAYGUIDEF void GuiUpdateStyleComplete(void); // Updates full style properties set with default values
|
||||||
#endif
|
|
||||||
|
/*
|
||||||
|
typedef GuiStyle (unsigned int *)
|
||||||
|
RAYGUIDEF GuiStyle LoadGuiStyle(const char *fileName); // Load style from file (.rgs)
|
||||||
|
RAYGUIDEF void UnloadGuiStyle(GuiStyle style); // Unload style
|
||||||
|
*/
|
||||||
|
|
||||||
#endif // RAYGUI_H
|
#endif // RAYGUI_H
|
||||||
|
|
||||||
@ -398,10 +400,17 @@ RAYGUIDEF void GuiUpdateStyleComplete(void); // Updates full
|
|||||||
// Global Variables Definition
|
// Global Variables Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
static GuiControlState guiState = GUI_STATE_NORMAL;
|
static GuiControlState guiState = GUI_STATE_NORMAL;
|
||||||
static unsigned int *guiStyle = NULL;
|
|
||||||
|
static Font guiFont = { 0 }; // NOTE: Highly coupled to raylib
|
||||||
static bool guiLocked = false;
|
static bool guiLocked = false;
|
||||||
static float guiAlpha = 1.0f;
|
static float guiAlpha = 1.0f;
|
||||||
static Font guiFont = { 0 }; // NOTE: Highly coupled to raylib
|
|
||||||
|
// Global gui style array (allocated on heap by default)
|
||||||
|
// NOTE: In raygui we manage a single int array with all the possible style properties.
|
||||||
|
// When a new style is loaded, it loads over the global style... but default gui style
|
||||||
|
// could always be recovered with GuiLoadStyleDefault()
|
||||||
|
static unsigned int guiStyle[NUM_CONTROLS*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED)] = { 0 };
|
||||||
|
static bool guiStyleLoaded = false;
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Standalone Mode Functions Declaration
|
// Standalone Mode Functions Declaration
|
||||||
@ -489,9 +498,6 @@ static int GuiTextWidth(const char *text)
|
|||||||
return (int)size.x;
|
return (int)size.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get default style, if not available load LIGHT style (RAII)
|
|
||||||
static unsigned int *GetStyleDefault(void);
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module Functions Definition
|
// Module Functions Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
@ -539,19 +545,15 @@ RAYGUIDEF void GuiFade(float alpha)
|
|||||||
// Set control style property value
|
// Set control style property value
|
||||||
RAYGUIDEF void GuiSetStyle(int control, int property, int value)
|
RAYGUIDEF void GuiSetStyle(int control, int property, int value)
|
||||||
{
|
{
|
||||||
// Get current style, initialized automatically if required (RAII)
|
if (!guiStyleLoaded) GuiLoadStyleDefault();
|
||||||
unsigned int *style = GetStyleDefault();
|
guiStyle[control*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + property] = value;
|
||||||
|
|
||||||
style[control*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + property] = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get control style property value
|
// Get control style property value
|
||||||
RAYGUIDEF int GuiGetStyle(int control, int property)
|
RAYGUIDEF int GuiGetStyle(int control, int property)
|
||||||
{
|
{
|
||||||
// Get current style, initialized automatically if required (RAII)
|
if (!guiStyleLoaded) GuiLoadStyleDefault();
|
||||||
unsigned int *style = GetStyleDefault();
|
return guiStyle[control*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + property];
|
||||||
|
|
||||||
return style[control*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + property];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Window Box control
|
// Window Box control
|
||||||
@ -2789,7 +2791,6 @@ RAYGUIDEF Vector2 GuiGrid(Rectangle bounds, int spacing, int subdivs)
|
|||||||
return currentCell;
|
return currentCell;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(RAYGUI_STYLE_LOADING)
|
|
||||||
// Load raygui style file (.rgs)
|
// Load raygui style file (.rgs)
|
||||||
RAYGUIDEF void GuiLoadStyle(const char *fileName)
|
RAYGUIDEF void GuiLoadStyle(const char *fileName)
|
||||||
{
|
{
|
||||||
@ -2899,28 +2900,12 @@ RAYGUIDEF void GuiLoadStyleProps(const int *props, int count)
|
|||||||
for (int k = 0; k < uncompleteSetProps; k++) GuiSetStyle(completeSets, k, props[completeSets*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + k]);
|
for (int k = 0; k < uncompleteSetProps; k++) GuiSetStyle(completeSets, k, props[completeSets*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates controls style with default values
|
// Load style default over global style
|
||||||
RAYGUIDEF void GuiUpdateStyleComplete(void)
|
RAYGUIDEF void GuiLoadStyleDefault(void)
|
||||||
{
|
{
|
||||||
// Populate all controls with default style
|
// We set this variable first to avoid cyclic function calls
|
||||||
// NOTE: Extended style properties are ignored
|
// when calling GuiSetStyle() and GuiGetStyle()
|
||||||
for (int i = 1; i < NUM_CONTROLS; i++)
|
guiStyleLoaded = true;
|
||||||
{
|
|
||||||
for (int j = 0; j < NUM_PROPS_DEFAULT; j++) GuiSetStyle(i, j, GuiGetStyle(DEFAULT, j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // defined(RAYGUI_STYLE_LOADING)
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
|
||||||
// Module specific Functions Definition
|
|
||||||
//----------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Get default style, if not available load LIGHT style (RAII)
|
|
||||||
static unsigned int *GetStyleDefault(void)
|
|
||||||
{
|
|
||||||
if (guiStyle == NULL)
|
|
||||||
{
|
|
||||||
guiStyle = (unsigned int *)calloc(NUM_CONTROLS*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED), sizeof(unsigned int));
|
|
||||||
|
|
||||||
// Initialize default LIGHT style property values
|
// Initialize default LIGHT style property values
|
||||||
GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, 0x838383ff);
|
GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, 0x838383ff);
|
||||||
@ -2972,12 +2957,22 @@ static unsigned int *GetStyleDefault(void)
|
|||||||
GuiSetStyle(LISTVIEW, ELEMENTS_HEIGHT, 0x1e);
|
GuiSetStyle(LISTVIEW, ELEMENTS_HEIGHT, 0x1e);
|
||||||
GuiSetStyle(LISTVIEW, ELEMENTS_PADDING, 2);
|
GuiSetStyle(LISTVIEW, ELEMENTS_PADDING, 2);
|
||||||
GuiSetStyle(LISTVIEW, SCROLLBAR_WIDTH, 10);
|
GuiSetStyle(LISTVIEW, SCROLLBAR_WIDTH, 10);
|
||||||
|
|
||||||
// TODO: Allocated memory must be freed!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return guiStyle;
|
// Updates controls style with default values
|
||||||
|
RAYGUIDEF void GuiUpdateStyleComplete(void)
|
||||||
|
{
|
||||||
|
// Populate all controls with default style
|
||||||
|
// NOTE: Extended style properties are ignored
|
||||||
|
for (int i = 1; i < NUM_CONTROLS; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < NUM_PROPS_DEFAULT; j++) GuiSetStyle(i, j, GuiGetStyle(DEFAULT, j));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Module specific Functions Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Convert color data from RGB to HSV
|
// Convert color data from RGB to HSV
|
||||||
// NOTE: Color data should be passed normalized
|
// NOTE: Color data should be passed normalized
|
||||||
|
|||||||
Reference in New Issue
Block a user