MLX42 1.0
MLX42
Loading...
Searching...
No Matches
mlx_loop.c File Reference
#include "MLX42/MLX42_Int.h"
Include dependency graph for mlx_loop.c:

Go to the source code of this file.

Functions

static void mlx_exec_loop_hooks (mlx_t *mlx)
 
static void mlx_render_images (mlx_t *mlx)
 
bool mlx_loop_hook (mlx_t *mlx, void(*f)(void *), void *param)
 
void mlx_loop (mlx_t *mlx)
 

Function Documentation

◆ mlx_exec_loop_hooks()

static void mlx_exec_loop_hooks ( mlx_t mlx)
static

Definition at line 17 of file mlx_loop.c.

18{
19 const mlx_ctx_t* mlxctx = mlx->context;
20
21 mlx_list_t* lstcpy = mlxctx->hooks;
23 {
24 mlx_hook_t* hook = ((mlx_hook_t*)lstcpy->content);
25 hook->func(hook->param);
26 lstcpy = lstcpy->next;
27 }
28}
GLuint GLsizei GLsizei * length
Definition glad.h:3372
void(* func)(void *)
Definition MLX42_Int.h:153
Definition MLX42.h:361
void * context
Definition MLX42.h:363
void * window
Definition MLX42.h:362
Here is the caller graph for this function:

◆ mlx_loop()

void mlx_loop ( mlx_t mlx)

In Emscripten the lood is defined differently, there the this function is passed to the while loop instead

Definition at line 97 of file mlx_loop.c.

98{
100
101#ifdef EMSCRIPTEN
102 static double start, oldstart = 0;
103#else
104 double start, oldstart = 0;
106 {
107#endif
108 start = glfwGetTime();
110 oldstart = start;
111
112 glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
115
116 if ((mlx->width > 1 || mlx->height > 1))
118
122
125#ifndef EMSCRIPTEN
126 }
127#endif
128}
#define MLX_NONNULL(var)
Definition MLX42_Int.h:46
void mlx_flush_batch(mlx_ctx_t *mlx)
Definition mlx_images.c:17
void mlx_update_matrix(const mlx_t *mlx)
Definition mlx_window.c:21
#define glClearColor
Definition glad.h:1994
#define GL_COLOR_BUFFER_BIT
Definition glad.h:138
GLuint start
Definition glad.h:2963
#define GL_DEPTH_BUFFER_BIT
Definition glad.h:136
#define glClear
Definition glad.h:1991
static void mlx_render_images(mlx_t *mlx)
Definition mlx_loop.c:30
static void mlx_exec_loop_hooks(mlx_t *mlx)
Definition mlx_loop.c:17
double delta_time
Definition MLX42.h:366
int32_t width
Definition MLX42.h:364
int32_t height
Definition MLX42.h:365
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mlx_loop_hook()

bool mlx_loop_hook ( mlx_t mlx,
void(*)(void *)  f,
void param 
)

Generic loop hook for any custom hooks to add to the main loop. Executes a function per frame, so be careful.

Parameters
[in]mlxThe MLX instance handle.
[in]fThe function.
[in]paramThe parameter to pass on to the function.
Returns
Whether or not the hook was added successfully.

Definition at line 70 of file mlx_loop.c.

71{
74
76 if (!(hook = malloc(sizeof(mlx_hook_t))))
77 return (mlx_error(MLX_MEMFAIL));
78
80 if (!(lst = mlx_lstnew(hook)))
81 {
82 free(hook);
83 return (mlx_error(MLX_MEMFAIL));
84 }
85 hook->func = f;
86 hook->param = param;
87 const mlx_ctx_t *mlxctx = mlx->context;
88 mlx_lstadd_back((mlx_list_t**)(&mlxctx->hooks), lst);
89 return (true);
90}
@ MLX_MEMFAIL
Definition MLX42.h:383
bool mlx_error(mlx_errno_t val)
Definition mlx_error.c:43
mlx_list_t * mlx_lstnew(void *content)
Definition mlx_list.c:45
void mlx_lstadd_back(mlx_list_t **lst, mlx_list_t *new)
Definition mlx_list.c:67
GLdouble f
Definition glad.h:2085
GLenum GLfloat param
Definition glad.h:1968
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mlx_render_images()

static void mlx_render_images ( mlx_t mlx)
static

Definition at line 30 of file mlx_loop.c.

31{
33 mlx_list_t* imglst = mlxctx->images;
34
35 if (sort_queue)
36 {
37 sort_queue = false;
38 mlx_sort_renderqueue(&mlxctx->render_queue);
39 }
40
41 // Upload image textures to GPU
42 while (imglst)
43 {
45 if (!(image = imglst->content)) {
47 return;
48 }
49
50 glBindTexture(GL_TEXTURE_2D, ((mlx_image_ctx_t*)image->context)->texture);
51 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->width, image->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
52 imglst = imglst->next;
53 }
54
55 // Execute draw calls
56 mlx_list_t* render_queue = mlxctx->render_queue;
57 while (render_queue)
58 {
59 draw_queue_t* drawcall = render_queue->content;
60 mlx_instance_t* instance = &drawcall->image->instances[drawcall->instanceid];
61
62 if (drawcall && drawcall->image->enabled && instance->enabled)
64 render_queue = render_queue->next;
65 }
66}
@ MLX_INVIMG
Definition MLX42.h:379
void mlx_draw_instance(mlx_ctx_t *mlx, mlx_image_t *img, mlx_instance_t *instance)
Definition mlx_images.c:63
void mlx_sort_renderqueue(mlx_list_t **lst)
Definition mlx_list.c:161
bool sort_queue
Definition mlx_init.c:162
#define glTexImage2D
Definition glad.h:1985
#define GL_UNSIGNED_BYTE
Definition glad.h:251
GLenum GLenum GLsizei void * image
Definition glad.h:5132
#define glBindTexture
Definition glad.h:2901
#define GL_RGBA
Definition glad.h:286
#define GL_TEXTURE_2D
Definition glad.h:243
struct mlx_list * next
Definition MLX42_Int.h:88
void * content
Definition MLX42_Int.h:87
Here is the call graph for this function:
Here is the caller graph for this function: