LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
lg_gr_func.c File Reference

Functions

void lg_draw_line (int32_t x1, int32_t y1, int32_t x2, int32_t y2, LG_Color_u c)
 
void lg_draw_rect (Rec2Di r, LG_Color_u c)
 
void lg_fill_rect (Rec2Di r, LG_Color_u c)
 
void lg_clear_rect (Rec2Di r, LG_Color_u c)
 
zboolean lg_clip_rect (Rec2Di clip, Rec2Di rect, Rec2Di *clipped_rect)
 
zboolean lg_recs_overlap (Rec2Df *r1, Rec2Df *r2)
 
void lg_copy_rect (Rec2Di src, Rec2Di *dest)
 
void lg_rec_i_copy (Rec2Di *dest, Rec2Di src)
 
void lg_rec_f_copy (Rec2Df *dest, Rec2Df src)
 
Rec2Di lg_rec_i (int x, int y, int w, int h)
 
Rec2Df lg_rec_f (float x, float y, float w, float h)
 
Rec2Df lg_rec_f_from_rec_i (Rec2Di r)
 
Rec2Di lg_rec_i_from_rec_f (Rec2Df r)
 
Point2Di lg_point2d_i (int32_t x, int32_t y)
 
Point2Df lg_point2d_f (float x, float y)
 
Point2Di lg_rec_i_center (Rec2Di r)
 
Point2Df lg_rec_f_center (Rec2Df r)
 
void gl_coords_from_screen_coords (float *x2, float *y2, int x1, int y1, int viewport_w, int viewport_h)
 
void logical_coords_from_viewport_coords (int *x2, int *y2, int x1, int y1)
 
void logical_coords_from_viewport_coords_btaskbar_adjustment (int bth_adj)
 
void logical_coords_from_touchscreen_coords (int *x2, int *y2, float x1, float y1)
 
void lg_set_touchscreen_coords_margins (float left, float top)
 
uint32_t lg_point2di_is_inside_rec2di_from_array (Point2Di p, Rec2Di **r, uint32_t n_rect_max)
 
uint32_t lg_point2f_is_inside_rec2df_from_array (Point2Df p, Rec2Df **r, uint32_t n_rect_max)
 
int32_t sdl_to_gl_int_x (int32_t x)
 
int32_t sdl_to_gl_int_y (int32_t y)
 
float sdl_to_gl_float_x (int32_t x)
 
float sdl_to_gl_float_y (int32_t y)
 
LG_Color_u lg_color_u_from_sdl_color (SDL_Color c)
 
SDL_Color lg_sdl_color_from_lg_color_u (LG_Color_u c)
 
SDL_Color * lg_set_sdl_color_from_str (SDL_Color *c, const char *str)
 
SDL_Color lg_get_sdl_color_from_str (const char *str)
 
LG_Color_ulg_set_lg_color_u_from_str (LG_Color_u *c, const char *str)
 
LG_Color_u lg_get_lg_color_u_from_str (const char *str)
 
int lg_get_n_color_str ()
 
const char * lg_get_color_str_from_color_index (int i)
 
void v_flip_sdl_surf (SDL_Surface *surf)
 

Detailed Description

  === A few 2D structs and graphical funcsPlus colors funcs ===

    Plus colors funcs

 Quite messy, also a lot of convenience conversion funcs

Function Documentation

◆ lg_draw_line()

void lg_draw_line ( int32_t  x1,
int32_t  y1,
int32_t  x2,
int32_t  y2,
LG_Color_u  c 
)

Draw a line from (x1, y1) to (x2, y2)

Expect SDL coords sys (logical coords, ie inside [0 -> env->top_win_logical_w] x [0 -> env->top_win_logical_h])

Parameters
x1Origin x coord
y1Origin y coord
x2Dest x coord
y2Dest y coord
cA LG_Color_u

◆ lg_draw_rect()

void lg_draw_rect ( Rec2Di  r,
LG_Color_u  c 
)

Draw a rectangle

Expect SDL coords sys (logical coords, ie inside [0 -> env->top_win_logical_w] x [0 -> env->top_win_logical_h])

Parameters
rA Rec2Di
cA LG_Color_u

◆ lg_fill_rect()

void lg_fill_rect ( Rec2Di  r,
LG_Color_u  c 
)

Fill a rectangle with color - now just call lg_render_rect_2d()

Expect SDL coords sys (logical coords, ie inside [0 -> env->top_win_logical_w] x [0 -> env->top_win_logical_h])

lg_fill_rect() is about three times faster than lg_clear_rect()

 In test/test.c: test_lg_draw_fill_rect()):
 - Time to draw with lg_clear_rect() = 6.582 ms
 - Time to draw with lg_fill_rect() = 1.925 ms
Parameters
rA Rec2Di
cA LG_Color_u

◆ lg_clear_rect()

void lg_clear_rect ( Rec2Di  r,
LG_Color_u  c 
)

Clear a rectangle with color

Expect SDL coords sys (logical coords, ie inside [0 -> env->top_win_logical_w] x [0 -> env->top_win_logical_h])

lg_clear_rect() is about three times slower than lg_fill_rect() (see above)

Parameters
rA Rec2Di
cA LG_Color_u

◆ lg_clip_rect()

zboolean lg_clip_rect ( Rec2Di  clip,
Rec2Di  rect,
Rec2Di clipped_rect 
)

Return FALSE if rect is fully outside clip. Otherwise, set clipped_rect (rect is unchanged) and return TRUE.

◆ lg_recs_overlap()

zboolean lg_recs_overlap ( Rec2Df r1,
Rec2Df r2 
)

Test if 2 rects (Rec2Df) overlap

Using Rec2Df with OpenGL coords sys orientation

typedef struct {
float x;
float y;
float w;
float h;
^
|
x,y+h |--------
| |
| |
------------->
x,y x+w,y
Parameters
r1Pointer to a Rec2Df
r2Pointer to a Rec2Df
Returns
TRUE if the rects overlap, FALSE otherwise

◆ lg_copy_rect()

void lg_copy_rect ( Rec2Di  src,
Rec2Di dest 
)

TODO: should be lg_copy_rect(dest, src) for consistency sake

DEPRECATED anyways

Parameters
src
dest

◆ lg_rec_i_copy()

void lg_rec_i_copy ( Rec2Di dest,
Rec2Di  src 
)

Copy src Rec2Di to dest Rec2Di

Parameters
destA pointer to a Rec2Di
srcA Rec2Di

◆ lg_rec_f_copy()

void lg_rec_f_copy ( Rec2Df dest,
Rec2Df  src 
)

Copy src Rec2Df to dest Rec2Df

Parameters
destA pointer to a Rec2Df
srcA Rec2Df

◆ lg_rec_i()

Rec2Di lg_rec_i ( int  x,
int  y,
int  w,
int  h 
)

Set a Rec2Di

Parameters
x
y
w
h
Returns
A Rec2Di

◆ lg_rec_f()

Rec2Df lg_rec_f ( float  x,
float  y,
float  w,
float  h 
)

Set a Rec2Df

Parameters
x
y
w
h
Returns
A Rec2Df

◆ lg_rec_f_from_rec_i()

Rec2Df lg_rec_f_from_rec_i ( Rec2Di  r)

Get Rec2Df from Rec2Di

Parameters
rA Rec2Di
Returns
A Rec2Df

◆ lg_rec_i_from_rec_f()

Rec2Di lg_rec_i_from_rec_f ( Rec2Df  r)

Get Rec2Di from Rec2Df

This func uses lroundf()

Parameters
rA Rec2Df
Returns
A Rec2Di

◆ lg_point2d_i()

Point2Di lg_point2d_i ( int32_t  x,
int32_t  y 
)

Create a Point2Di

Parameters
x
y
Returns
A new Point2Di

◆ lg_point2d_f()

Point2Df lg_point2d_f ( float  x,
float  y 
)

Create a Point2Df

Parameters
x
y
Returns
A new Point2Df

◆ lg_rec_i_center()

Point2Di lg_rec_i_center ( Rec2Di  r)

Compute center of rectangle

This func uses lroundf()

Parameters
rA Rec2Di
Returns
The center as a Rec2Di

◆ lg_rec_f_center()

Point2Df lg_rec_f_center ( Rec2Df  r)

Compute center of rectangle

Parameters
rA Rec2Df
Returns
The center as a Point2Df

◆ gl_coords_from_screen_coords()

void gl_coords_from_screen_coords ( float *  x2,
float *  y2,
int  x1,
int  y1,
int  viewport_w,
int  viewport_h 
)

Get GL coords (more exactly NDC) in range [-1, 1] x [-1, 1] with origin at center from some screen coords in range [0, some screen width] x [0, some screen height] with origin at top left corner

On Linux, useful to get mouse pointer NDC coords for ray picking or whatever

Parameters
x2(GL coord)
y2(GL coord)
x1(Screen coord)
y1(Screen coord)
viewport_wUsually env->viewport_rect.w
viewport_hUsually env->viewport_rect.h

◆ logical_coords_from_viewport_coords()

void logical_coords_from_viewport_coords ( int *  x2,
int *  y2,
int  x1,
int  y1 
)

Get logical coords in range [0, env->top_win_logical_w] x [0, env->top_win_logical_h] from viewport coords in range [0, env->viewport_rect.w] x [0, env->viewport_rect.h] from current LG_Env

On Linux, useful to get 'logical' mouse pointer coords (you may need to use logical_coords_from_viewport_coords_btaskbar_adjustment() to get accurate results)

Parameters
x2(Logical coord)
y2(Logical coord)
x1(Viewport coord)
y1(Viewport coord)

◆ logical_coords_from_viewport_coords_btaskbar_adjustment()

void logical_coords_from_viewport_coords_btaskbar_adjustment ( int  bth_adj)

Set (static int) bottom_taskbar_height_adjustment to correct logical_coords_from_viewport_coords() y2 inaccuracy

Linux only - should find how to get bottom taskbar height

Parameters
bth_adjBottom taskbar height adjustment

◆ logical_coords_from_touchscreen_coords()

void logical_coords_from_touchscreen_coords ( int *  x2,
int *  y2,
float  x1,
float  y1 
)

Get logical coords in range [0, env->top_win_logical_w] x [0, env->top_win_logical_h] from touchscreen coords in range [0.0, 1.0] x [0.0, 1.0]

Useful to get 'logical' touch coords on Android

Parameters
x2(Logical coord)
y2(Logical coord)
x1(Viewport coord)
y1(Viewport coord)

◆ lg_set_touchscreen_coords_margins()

void lg_set_touchscreen_coords_margins ( float  left,
float  top 
)

Used by logical_coords_from_touchscreen_coords() to adjust stuff

Use this func only if you know what you're doing

Parameters
leftLeft margin value
topTop margin value

◆ lg_point2di_is_inside_rec2di_from_array()

uint32_t lg_point2di_is_inside_rec2di_from_array ( Point2Di  p,
Rec2Di **  r,
uint32_t  n_rect_max 
)

Test against all rect (Rec2Di) from an array if a point (Point2Di) is inside one of them

NOTE: comparing with >= and <=

Parameters
pA Point2Di
rPointer to an array of Rec2Di
n_rect_maxMax num of rect in the array
Returns
The index + 1 of the rect in the array or 0 if it is in none

◆ lg_point2f_is_inside_rec2df_from_array()

uint32_t lg_point2f_is_inside_rec2df_from_array ( Point2Df  p,
Rec2Df **  r,
uint32_t  n_rect_max 
)

Test against all rect (Rect2Df) from an array if a point (Point2Df) is inside one of them

NOTE: comparing with >= and <=

Parameters
pA Point2Df
rPointer to a NULL-terminated array of Rec2Df
n_rect_maxMax num of rect in the array
Returns
The index + 1 of the rect in the array or 0 if it is in none

◆ sdl_to_gl_int_x()

int32_t sdl_to_gl_int_x ( int32_t  x)

Convert from SDL coords to OpenGL coords

Parameters
x
Returns
New value as an int

◆ sdl_to_gl_int_y()

int32_t sdl_to_gl_int_y ( int32_t  y)

Convert from SDL coords to OpenGL coords

Parameters
y
Returns
New value as an int

◆ sdl_to_gl_float_x()

float sdl_to_gl_float_x ( int32_t  x)

Convert from SDL coords to OpenGL coords

About the 0.375 value, see: https://www.opengl.org/archives/resources/faq/technical/transformations.htm

Parameters
x
Returns
New value as a float

◆ sdl_to_gl_float_y()

float sdl_to_gl_float_y ( int32_t  y)

Convert from SDL coords to OpenGL coords

About the 0.375 value, see: https://www.opengl.org/archives/resources/faq/technical/transformations.htm

Parameters
y
Returns
New value as a float

◆ lg_color_u_from_sdl_color()

LG_Color_u lg_color_u_from_sdl_color ( SDL_Color  c)

Get LG_Color_u from SDL_Color

Parameters
cA SDL_Color
Returns
A LG_Color_u

◆ lg_sdl_color_from_lg_color_u()

SDL_Color lg_sdl_color_from_lg_color_u ( LG_Color_u  c)

Get SDL_Color from LG_Color_u

Parameters
cLG_Color_u
Returns
A SDL_Color

◆ lg_set_sdl_color_from_str()

SDL_Color* lg_set_sdl_color_from_str ( SDL_Color *  c,
const char *  str 
)

Bla bla (self-explanatory)

◆ lg_get_sdl_color_from_str()

SDL_Color lg_get_sdl_color_from_str ( const char *  str)

Bla bla (self-explanatory)

◆ lg_set_lg_color_u_from_str()

LG_Color_u* lg_set_lg_color_u_from_str ( LG_Color_u c,
const char *  str 
)

Bla bla (self-explanatory)

◆ lg_get_lg_color_u_from_str()

LG_Color_u lg_get_lg_color_u_from_str ( const char *  str)

Bla bla (self-explanatory)

◆ lg_get_n_color_str()

int lg_get_n_color_str ( )

Bla bla (self-explanatory)

◆ lg_get_color_str_from_color_index()

const char* lg_get_color_str_from_color_index ( int  i)

Bla bla (self-explanatory)

◆ v_flip_sdl_surf()

void v_flip_sdl_surf ( SDL_Surface *  surf)

Flip (vertical one) SDL surf

TODO: fix the compilation warning

(From gcc doc) === gcc option -Wpointer-arith ===

Warn about anything that depends on the “size of” a function type or of void. GNU C assigns these types a size of 1, for convenience in calculations with void * pointers and pointers to functions.

(WTF ?)

Rec2Df
Definition: lg_gr_func.h:56