LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2026 ETMSoftware
Loading...
Searching...
No Matches
lg_wins.c File Reference

Functions

void lg_info_win_no_block (const char *text, zboolean with_frame, zboolean swap_fb)
 
void lg_info_win (const char *text, zboolean with_frame, zboolean swap_fb)
 
void lg_info_win_s (const char *text, LG_Win_Style *style)
 
int lg_question_win (const char *text)
 
int lg_question_win_s (const char *text, LG_Win_Style *style)
 
void lg_warning_win (const char *text)
 
void lg_error_win (const char *text)
 
char * lg_entry_win (const char *label, int x, int y)
 
int lg_win_open_centered (LG_Window *win)
 
LG_Window lg_win (const char *text, LG_Color_u text_color, LG_Color_u bg_color, zboolean with_frame, LG_Color_u border_color, TTF_Font *font, int index, LG_Color_u hightlight_text_color, LG_Color_u hightlight_bg_color)
 
LG_MenuWinParamslg_get_menu_wins_params ()
 
int lg_menu_win (const char *text, LG_Color_u text_c, LG_Color_u bg_c, LG_Color_u h_text_c, LG_Color_u h_bg_c, TTF_Font *font, int starting_index)
 
void lg_win_show_annoying_errors ()
 
void lg_win_no_annoying_errors ()
 
int lg_win_open (LG_Window *win, int x, int y)
 
int lg_win_close (LG_Window *win)
 
void lg_win_free_tex (LG_Window *win)
 
zboolean is_lg_win (LG_Window *win)
 
int lg_win_get_w (LG_Window *win)
 
int lg_win_get_h (LG_Window *win)
 
LG_WinColorslg_win_get_colors ()
 
void lg_win_set_default_colors ()
 
char * lg_wrap_lines (const char *txt, TTF_Font *font, int max_width)
 
void lg_fonts_list_init ()
 
zboolean lg_fonts_list_is_initialized ()
 
zboolean lg_font_add_to_list (const char *name, const char *relative_path, int size)
 
LG_Fontlg_font_get (const char *name)
 
TTF_Font * lg_font_get_ttf (const char *name)
 
void lg_font_free (const char *name)
 
void lg_font_free_all ()
 
void lg_font_list_all ()
 
void lg_font_info (LG_Font *font)
 
zboolean lg_font_load_array (LG_Font_Des fonts[], int n_fonts)
 
void lg_font_list_array (LG_Font_Des fonts[], int n_fonts)
 

Detailed Description

 === GUI windows implementation ===

 You may use helper macros:

 - WITH_FRAME / FRAMELESS
 - BLOCK / NO_BLOCK
 - SWAP_FB / NO_SWAP

Function Documentation

◆ lg_info_win_no_block()

void lg_info_win_no_block ( const char *  text,
zboolean  with_frame,
zboolean  swap_fb 
)

Window at screen bottom, horiz. centered - Free but don't close win

Parameters
text
with_frame
swap_fb

◆ lg_info_win()

void lg_info_win ( const char *  text,
zboolean  with_frame,
zboolean  swap_fb 
)

Window is centered

TODO: Which font size on Android ? TODO #2: Should warn if win text is over 12 lines

Parameters
text
with_frame
swap_fb

◆ lg_info_win_s()

void lg_info_win_s ( const char *  text,
LG_Win_Style style 
)

Window is centered

Use LG_Win_Style

=== WARNING: Also swap framebuffer ===

=== WARNING2: This one uses WAIT_FOR_PRESS_OR_CLICK_OR_TOUCH === Also don't output "Press any key ..."

You should always clear or render to bg just before calling this func (with lg_clear_bg() or lg_read_and_render_screen_back() or ...)

Parameters
text
style

◆ lg_question_win()

int lg_question_win ( const char *  text)

Window is centered

On Android: left tap = no / right tap = yes

=== WARNING: Also swap framebuffer ===

TODO: Which font size on Android ?

You should always clear or render to bg just before calling this func (with lg_clear_bg() or lg_read_and_render_screen_back() or ...)

Parameters
text
Returns
YES or NO

◆ lg_question_win_s()

int lg_question_win_s ( const char *  text,
LG_Win_Style style 
)

Window is centered

Use LG_Win_Style

On Android: left tap = no / right tap = yes

=== WARNING: Also swap framebuffer ===

You should always clear or render to bg just before calling this func (with lg_clear_bg() or lg_read_and_render_screen_back() or ...)

Parameters
text
style
Returns
YES or NO

◆ lg_warning_win()

void lg_warning_win ( const char *  text)

=== WARNING: Also swap framebuffer ===

You should always clear or render to bg just before calling this func

TODO: Which font size on Android ?

Parameters
text

◆ lg_error_win()

void lg_error_win ( const char *  text)

=== WARNING: Also swap framebuffer ===

You should always clear or render to bg just before calling this func

TODO: Which font size on Android ?

Parameters
text

◆ lg_entry_win()

char * lg_entry_win ( const char *  label,
int  x,
int  y 
)

Text entry win

(-1, -1) for centered win, same colors as lg_question_win()

If not NULL (<esc> pressed), returned str must be freed

=== WARNING: Also swap framebuffer ===

You should always clear or render to bg just before calling this func

Parameters
label
x
y
Returns
Entered text

◆ lg_win_open_centered()

int lg_win_open_centered ( LG_Window win)
Parameters
winA LG_Window instance
Returns
LG_OK if OK

◆ lg_win()

LG_Window lg_win ( const char *  text,
LG_Color_u  text_color,
LG_Color_u  bg_color,
zboolean  with_frame,
LG_Color_u  border_color,
TTF_Font *  font,
int  index,
LG_Color_u  hightlight_text_color,
LG_Color_u  hightlight_bg_color 
)

Create a new LG_Window

|------------win_rect------------|
| |--------padding_rect--------| |
| | |-------layout_rect------| | |
| | | layout_texture | | |
| | |------------------------| | |
| |----------------------------| |
|--------------------------------|

WARNING: for frameless wins, the background/border colors are still used, so you MUST set them (ie, DO use either a fully/partially transparent color or an opaque color)

For instance, use lg_color_u(0, 0, 0, 0) or UNUSED_LG_COLOR_U for a fully transparent bg

Max lines num inside window is LG_WIN_N_LINES_MAX, defined in lg_win.h

H_PADDING, V_PADDING, and BORDER_WIDTH are also defined in lg_win.h

TODO: add text overflow check

Params are self-explanatory

Parameters
textA string of lines separated by newlines ('\n')
text_color
with_frame
bg_color
border_color
fontA TTF_Font
indexIf in range [0, LG_WIN_N_LINES_MAX - 1], will (eventually) hightlight (index + 1)th line
hightlight_text_color
hightlight_bg_color
Returns
A new LG_Window, with id set to 0 -> OK / -1 -> invalid win (TODO: need to find sth better)

◆ lg_get_menu_wins_params()

LG_MenuWinParams * lg_get_menu_wins_params ( )

Get pointer to global params for all lg_menu_win() instances

Returns
A pointer to static globale LG_MenuWinParams

◆ lg_menu_win()

int lg_menu_win ( const char *  text,
LG_Color_u  text_c,
LG_Color_u  bg_c,
LG_Color_u  h_text_c,
LG_Color_u  h_bg_c,
TTF_Font *  font,
int  starting_index 
)

Create and open a centered LG_Window with a menu (line highlighting enabled) on a bg_c fullscreen background

Take a string of lines separated by newlines ('\n')

Each line must be short enough as to not trigger wrapping which would break functionnality, as the number of lines is "guessed" from the number of newlines in the string

Return the index (in range [0, n_lines - 1]) of the selected line (ENTER) or -1 if none is selected (ESC), -2 for LEFT, -3 for RIGHT

Usage example from LibGame scene_editor/main_menu.c:

typedef struct {
const char *str;
void (*func)();
} LG_ActionFunc;
static Level *lev;
static void slow_motion()
{
// ...
}
static void next_predef_cam_pos()
{
// ...
}
static void orbit_cam2()
{
// ...
}
// ...
void pick_action(Level *lev2)
{
lev = lev2;
LG_ActionFunc actions_funcs[] = {
{"Slow motion mode <F2>", &slow_motion},
{"Next predef cam position <F3>", &next_predef_cam_pos},
{"Orbit cam <F4>", &orbit_cam2},
{"Output nodes_ntos[] & scene graph <F5>", &output_nodes_ntos_and_scene_graph},
{"New scene <F6>", &new_scene2},
{"Load scene <F7>", &load_scene},
{"Save scene <F8>", &save_scene},
{"New node from OBJ file <F9>", &new_node_from_objet},
{"Copy selected node at scene center <F10>", &copy_selected_node_at_center},
{"Delete selected node <F11>", &delete_selected_node2},
{"Debug info <F12>", &debug_info},
{"Reset cam <enter>", &reset_cam2},
{NULL, NULL}};
int i = 0;
char *str = l_str_new(NULL);
while (actions_funcs[i].str != NULL) {
str = l_str_cat2(str, 2, actions_funcs[i++].str, "\n");
}
if (strlen(str) > 0) {
str[strlen(str) - 1] = 0;
}
LG_Color_u text_c = lg_color_u(192, 192, 192, 255);
LG_Color_u bg_c = lg_color_u(0, 0, 0, 127);
LG_Color_u h_text_c = lg_get_lg_color_u_from_str("orange");
LG_Color_u h_bg_c = lg_get_lg_color_u_from_str("light-purple");
TTF_Font *font = lg_font_get_ttf("intelone_mono_bold_15");
mwp->with_frame = TRUE;
mwp->loop = TRUE;
mwp->orig_bg = lg_take_screenshot();
mwp->disable_h_arrows = TRUE;
i = 0;
while (1) {
i = lg_menu_win(str,
text_c, bg_c, h_text_c, h_bg_c, font,
i > 0 ? i : 0);
if (i >= 0) {
actions_funcs[i].func();
} else if (i == -1) { // -1 if none is selected (ESC), -2 for LEFT, -3 for RIGHT (both disabled)
break;
}
}
l_str_free(str);
if (mwp->orig_bg != NULL) {
lg_texture_remove(mwp->orig_bg);
}
}
LG_Texture * lg_take_screenshot()
Definition lg_background.c:220
char * l_str_cat2(char *str, int n,...)
Definition lg_goodies.c:44
LG_Color_u lg_get_lg_color_u_from_str(const char *str)
Definition lg_gr_func.c:688
void lg_flush_keyb_events()
Definition lg_keyboard.c:253
void lg_texture_remove(LG_Texture *node)
Definition lg_textures.c:803
LG_Color_u lg_color_u(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition lg_vertex.c:208
LG_MenuWinParams * lg_get_menu_wins_params()
Definition lg_wins.c:856
int lg_menu_win(const char *text, LG_Color_u text_c, LG_Color_u bg_c, LG_Color_u h_text_c, LG_Color_u h_bg_c, TTF_Font *font, int starting_index)
Definition lg_wins.c:972
TTF_Font * lg_font_get_ttf(const char *name)
Definition lg_wins.c:1478
Definition lg_vertex.h:111
Definition lg_wins.h:133

=== WARNING: Also swap framebuffer ===

Parameters
textA string of lines separated by newlines ('\n')
text_cText color
bg_cBackground color
h_text_cHightlight text color
h_bg_cHightlight bg color
fontA TTF_Font
starting_index(self-explanatory enough)
Returns
Index Index (in range [0, n_lines - 1]) of the selected line (ENTER) or -1 if none is selected (ESC), -2 for LEFT, -3 for RIGHT

◆ lg_win_show_annoying_errors()

void lg_win_show_annoying_errors ( )

Enable output of annoying, not-critical errors (default)

◆ lg_win_no_annoying_errors()

void lg_win_no_annoying_errors ( )

Disable output of annoying, not-critical errors

◆ lg_win_open()

int lg_win_open ( LG_Window win,
int  x,
int  y 
)

Open a LG_Window instance

Set: x = -1, y = -1 for a centered window

Parameters
win
x
y
Returns
LG_OK if OK

◆ lg_win_close()

int lg_win_close ( LG_Window win)

Close a LG_Window instance

=== This func is DEPRECATED and now a NO-OP ===

Parameters
win
Returns
LG_OK if OK

◆ lg_win_free_tex()

void lg_win_free_tex ( LG_Window win)

Free resources (texture atlas actually) of a LG_Window instance

Parameters
win

◆ is_lg_win()

zboolean is_lg_win ( LG_Window win)

Check win is valid

Parameters
win

◆ lg_win_get_w()

int lg_win_get_w ( LG_Window win)
Parameters
win
Returns
win->tex_rec.w; or -1 on error

◆ lg_win_get_h()

int lg_win_get_h ( LG_Window win)
Parameters
win
Returns
win->tex_rec.h or -1 on error

◆ lg_win_get_colors()

LG_WinColors * lg_win_get_colors ( )

Get win colors

Returns
Addr of a static filled LG_WinColors

◆ lg_win_set_default_colors()

void lg_win_set_default_colors ( )

Set default win colors

◆ lg_wrap_lines()

char * lg_wrap_lines ( const char *  txt,
TTF_Font *  font,
int  max_width 
)

Line and word wrapping: Insert newline(s) if one string (ie between 2 newlines) is too long (ie > max_line_len)

max_line_len is set (very roughly) depending on font size and viewport width

Returned string must be freed afterwards

TODO: keep improving the algo

Parameters
txt
font
max_widthIn pixels
Returns
New string

◆ lg_fonts_list_init()

void lg_fonts_list_init ( )

Initialize the global fonts list

We use a simple list at the moment

TODO: should use a doubly-linked one

◆ lg_fonts_list_is_initialized()

zboolean lg_fonts_list_is_initialized ( )

Check if the global fonts list has been initialized

Returns
TRUE if the global fonts list has been initialized, FALSE otherwise

◆ lg_font_add_to_list()

zboolean lg_font_add_to_list ( const char *  name,
const char *  relative_path,
int  size 
)

Add a font to the global fonts list

Load a font once from file and register it with a user-defined name, so that it's easy later on to use it just by name

Max number of fonts is N_FONTS_MAX, defined in lg_wins.h

To check out game engine preloaded fonts, see: lg_load_engine_fonts()

=== TODO: using a simple list, should use a doubly-linked one to remove some stupid limitations ===

#define N_FONTS_MAX 64
typedef struct {
LG_String *name;
LG_String *relative_path; // Path inside the fonts folder, not relative if symlink
int size;
TTF_Font *f;
Definition lg_wins.h:108
Definition lg_string.h:17
Parameters
nameA user-defined font name - whatever you want but you should pick sth short but descriptive and not confusing
relative_pathPath inside the fonts folder, not relative if symlink
sizeFont size
Returns
TRUE if OK, FALSE otherwise

◆ lg_font_get()

LG_Font * lg_font_get ( const char *  name)

Retrieve a LG_Font from the global fonts list - search by name

Parameters
nameA user-defined font name
Returns
A pointer to a LG_Font, NULL on error

◆ lg_font_get_ttf()

TTF_Font * lg_font_get_ttf ( const char *  name)

Retrieve a TTF_Font from the global fonts list - search by name

Parameters
nameA user-defined font name
Returns
A pointer to a TTF_Font, NULL on error

◆ lg_font_free()

void lg_font_free ( const char *  name)

Find font by name and free all associated resources

You should probably not use this func because:

WARNING1: Removing a TTF font will likely remove all registered sizes

WARNING2: Doesn't enable to add extra fonts, only free all font resources (because the fonts list is not a doubly-linked one)

Parameters
nameFont user-defined name

◆ lg_font_free_all()

void lg_font_free_all ( )

Free all fonts and associated resources from the list

To use the list again, you should first initialize it

WARNING:

  • This func should probably never be called directly, unless you know exactly what you are doing
  • This func is always called by lg_quit()

◆ lg_font_list_all()

void lg_font_list_all ( )

List all loaded fonts

◆ lg_font_info()

void lg_font_info ( LG_Font font)

Print out LG_Font info

◆ lg_font_load_array()

zboolean lg_font_load_array ( LG_Font_Des  fonts[],
int  n_fonts 
)

Load an array of LG_Font_Des and add them to the global fonts list

// Font description
typedef struct {
const char *name;
const char *relative_path; // Path inside the fonts folder, not relative if symlink
int size;
Definition lg_wins.h:116
Parameters
fontsAn array of LG_Font_Des
n_fontsNum of fonts
Returns
TRUE if OK, FALSE otherwise

◆ lg_font_list_array()

void lg_font_list_array ( LG_Font_Des  fonts[],
int  n_fonts 
)

Print out nicely an array of LG_Font_Des

Parameters
fontsAn array of LG_Font_Des
n_fontsNum of fonts