MLX42 1.0
MLX42
Loading...
Searching...
No Matches
MLX42_Int.h File Reference
#include "MLX42/MLX42.h"
#include "lodepng/lodepng.h"
#include "glad/glad.h"
#include "KHR/khrplatform.h"
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
Include dependency graph for MLX42_Int.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  vertex
 
struct  mlx_list
 
struct  mlx_srcoll
 
struct  mlx_mouse
 
struct  mlx_cursor
 
struct  mlx_close
 
struct  mlx_resize
 
struct  mlx_key
 
struct  mlx_hook
 
struct  mlx_ctx
 
struct  draw_queue
 
struct  mlx_image_ctx
 

Macros

#define LODEPNG_NO_COMPILE_ALLOCATORS
 
#define MLX_SWAP_INTERVAL   1
 
#define MLX_BATCH_SIZE   12000
 
#define BPP   sizeof(int32_t) /* Only support RGBA */
 
#define GETLINE_BUFF   1280
 
#define MLX_MAX_STRING   512 /* Arbitrary string limit */
 
#define MLX_ASSERT(cond, msg)   assert(cond && msg);
 
#define MLX_NONNULL(var)   MLX_ASSERT(var, "Value can't be null"); /* Assert instead of attribute */
 

Typedefs

typedef struct vertex vertex_t
 
typedef struct mlx_list mlx_list_t
 
typedef struct mlx_srcoll mlx_scroll_t
 
typedef struct mlx_mouse mlx_mouse_t
 
typedef struct mlx_cursor mlx_cursor_t
 
typedef struct mlx_close mlx_close_t
 
typedef struct mlx_resize mlx_resize_t
 
typedef struct mlx_key mlx_key_t
 
typedef struct mlx_hook mlx_hook_t
 
typedef struct mlx_ctx mlx_ctx_t
 
typedef struct draw_queue draw_queue_t
 
typedef struct mlx_image_ctx mlx_image_ctx_t
 

Functions

mlx_list_tmlx_lstnew (void *content)
 
mlx_list_tmlx_lstlast (mlx_list_t *lst)
 
int32_t mlx_lstsize (mlx_list_t *lst)
 
void mlx_lstclear (mlx_list_t **lst, void(*del)(void *))
 
void mlx_lstadd_back (mlx_list_t **lst, mlx_list_t *new)
 
void mlx_lstadd_front (mlx_list_t **lst, mlx_list_t *new)
 
mlx_list_tmlx_lstremove (mlx_list_t **lst, void *value, bool(*comp)(void *, void *))
 
void mlx_sort_renderqueue (mlx_list_t **lst)
 
bool mlx_equal_image (void *lstcontent, void *value)
 
bool mlx_equal_inst (void *lstcontent, void *value)
 
void mlx_draw_pixel (uint8_t *pixel, uint32_t color)
 
bool mlx_error (mlx_errno_t val)
 
bool mlx_freen (int32_t count,...)
 
void mlx_update_matrix (const mlx_t *mlx)
 
void mlx_draw_instance (mlx_ctx_t *mlx, mlx_image_t *img, mlx_instance_t *instance)
 
void mlx_flush_batch (mlx_ctx_t *mlx)
 
bool mlx_getline (char **out, size_t *out_size, FILE *file)
 
uint32_t mlx_rgba_to_mono (uint32_t color)
 
int32_t mlx_atoi_base (const char *str, int32_t base)
 
uint64_t mlx_fnv_hash (char *str, size_t len)
 

Variables

const charvert_shader
 
const charfrag_shader
 
bool sort_queue
 
int32_t mlx_settings [MLX_SETTINGS_MAX]
 

Macro Definition Documentation

◆ BPP

#define BPP   sizeof(int32_t) /* Only support RGBA */

Definition at line 42 of file MLX42_Int.h.

◆ GETLINE_BUFF

#define GETLINE_BUFF   1280

Definition at line 43 of file MLX42_Int.h.

◆ LODEPNG_NO_COMPILE_ALLOCATORS

#define LODEPNG_NO_COMPILE_ALLOCATORS

Definition at line 15 of file MLX42_Int.h.

◆ MLX_ASSERT

#define MLX_ASSERT (   cond,
  msg 
)    assert(cond && msg);

Definition at line 45 of file MLX42_Int.h.

◆ MLX_BATCH_SIZE

#define MLX_BATCH_SIZE   12000

Definition at line 40 of file MLX42_Int.h.

◆ MLX_MAX_STRING

#define MLX_MAX_STRING   512 /* Arbitrary string limit */

Definition at line 44 of file MLX42_Int.h.

◆ MLX_NONNULL

#define MLX_NONNULL (   var)    MLX_ASSERT(var, "Value can't be null"); /* Assert instead of attribute */

Definition at line 46 of file MLX42_Int.h.

◆ MLX_SWAP_INTERVAL

#define MLX_SWAP_INTERVAL   1

Definition at line 37 of file MLX42_Int.h.

Typedef Documentation

◆ draw_queue_t

◆ mlx_close_t

◆ mlx_ctx_t

For rendering we need to store most of OpenGL's stuff such as the vertex array object, vertex buffer object & the shader program as well as hooks and the zdepth level.

Additionally we represent draw calls with a linked list queue that points to the image and the index of its instance. Again, instances only carry XYZ data, so coupled with the image it lets us know where to draw a copy of the image.

Texture contexts are kept in a struct alongside the capacity of the array of instances, since the array is realloced like a vector.

◆ mlx_cursor_t

◆ mlx_hook_t

◆ mlx_image_ctx_t

◆ mlx_key_t

◆ mlx_list_t

◆ mlx_mouse_t

◆ mlx_resize_t

◆ mlx_scroll_t

There are 2 types of hooks, special and generics.

Specials: Specials are specific callback functions to a specific action such as window resizing or key presses. These are attached to the callbacks of glfw. In case MLX itself needs the callback we call the specials in that callback since there can only ever be a single callback.

Generics: Generics are MLX42 specific hooks and can have multiple hooks at the same time, these are executed every frame and can be used as an alternative for keypresses or animations for instance.

NOTE: Hooks could be achieved with va_args to have any amount of args sized functor but we can't/don't want to let the user deal with va_args and having to look up what args are what, etc...

We want to keep it straightforward with functors already describing what params they have.

◆ vertex_t

Function Documentation

◆ mlx_atoi_base()

int32_t mlx_atoi_base ( const char str,
int32_t  base 
)

◆ mlx_draw_instance()

void mlx_draw_instance ( mlx_ctx_t mlx,
mlx_image_t img,
mlx_instance_t instance 
)

Internal function to draw a single instance of an image to the screen.

Definition at line 63 of file mlx_images.c.

64{
65 float w = (float) img->width;
66 float h = (float) img->height;
67 float x = (float) instance->x;
68 float y = (float) instance->y;
69 float z = (float) instance->z;
71
72 vertex_t vertices[6] = {
73 (vertex_t){x, y, z, 0.f, 0.f, tex},
74 (vertex_t){x + w, y + h, z, 1.f, 1.f, tex},
75 (vertex_t){x + w, y, z, 1.f, 0.f, tex},
76 (vertex_t){x, y, z, 0.f, 0.f, tex},
77 (vertex_t){x, y + h, z, 0.f, 1.f, tex},
78 (vertex_t){x + w, y + h, z, 1.f, 1.f, tex},
79 };
80 memmove(mlx->batch_vertices + mlx->batch_size, vertices, sizeof(vertices));
81 mlx->batch_size += 6;
82
83 if (mlx->batch_size >= MLX_BATCH_SIZE)
85}
struct vertex vertex_t
#define MLX_BATCH_SIZE
Definition MLX42_Int.h:40
GLdouble GLdouble z
Definition glad.h:2301
GLint y
Definition glad.h:1965
GLdouble x
Definition glad.h:2847
GLuint GLsizei GLsizei * length
Definition glad.h:3372
GLint void * img
Definition glad.h:3003
GLfloat GLfloat GLfloat GLfloat h
Definition glad.h:4546
GLdouble GLdouble GLdouble w
Definition glad.h:2325
static int8_t mlx_bind_texture(mlx_ctx_t *mlx, mlx_image_t *img)
Definition mlx_images.c:30
void mlx_flush_batch(mlx_ctx_t *mlx)
Definition mlx_images.c:17
Definition MLX42.h:361
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mlx_draw_pixel()

void mlx_draw_pixel ( uint8_t pixel,
uint32_t  color 
)

Definition at line 16 of file mlx_put_pixel.c.

17{
18 *(pixel++) = (uint8_t)(color >> 24);
19 *(pixel++) = (uint8_t)(color >> 16);
20 *(pixel++) = (uint8_t)(color >> 8);
21 *(pixel++) = (uint8_t)(color & 0xFF);
22}
GLuint color
Definition glad.h:3749
Here is the caller graph for this function:

◆ mlx_equal_image()

bool mlx_equal_image ( void lstcontent,
void value 
)

Definition at line 17 of file mlx_compare.c.

18{
20 const mlx_image_t* lvalue = value;
21
22 return (lcontent == lvalue);
23}
GLfloat value
Definition glad.h:2667
Here is the caller graph for this function:

◆ mlx_equal_inst()

bool mlx_equal_inst ( void lstcontent,
void value 
)

Definition at line 25 of file mlx_compare.c.

26{
28 const mlx_image_t* lvalue = value;
29
30 return (lcontent->image == lvalue);
31}
Here is the caller graph for this function:

◆ mlx_error()

bool mlx_error ( mlx_errno_t  val)

Functions to set the error number, simply for convenience.

Parameters
valThe error value.
Returns
Always false

Definition at line 43 of file mlx_error.c.

44{
45 mlx_errno = val;
46#ifndef NDEBUG
47 fprintf(stderr, "MLX42: %s", mlx_strerror(mlx_errno));
48#endif
49 return (false);
50}
mlx_errno
Definition MLX42.h:371
GLuint GLfloat * val
Definition glad.h:3979
const char * mlx_strerror(mlx_errno_t val)
Definition mlx_error.c:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mlx_flush_batch()

void mlx_flush_batch ( mlx_ctx_t mlx)

Definition at line 17 of file mlx_images.c.

18{
19 if (mlx->batch_size <= 0)
20 return;
21
23 glBufferData(GL_ARRAY_BUFFER, mlx->batch_size * sizeof(vertex_t), mlx->batch_vertices, GL_STATIC_DRAW);
24 glDrawArrays(GL_TRIANGLES, 0, mlx->batch_size);
25
26 mlx->batch_size = 0;
27 memset(mlx->bound_textures, 0, sizeof(mlx->bound_textures));
28}
#define glBindBuffer
Definition glad.h:3292
#define GL_TRIANGLES
Definition glad.h:145
#define GL_ARRAY_BUFFER
Definition glad.h:857
#define glDrawArrays
Definition glad.h:2871
#define glBufferData
Definition glad.h:3304
#define GL_STATIC_DRAW
Definition glad.h:871
Here is the caller graph for this function:

◆ mlx_fnv_hash()

uint64_t mlx_fnv_hash ( char str,
size_t  len 
)

String hashing algorithm using FNV-1a. Source: https://bit.ly/3JcRGHa

Parameters
strThe string to hash
lenThe length of the string.
Returns
The hashed output.

Definition at line 68 of file mlx_utils.c.

69{
70 const uint64_t fnv_prime = 0x100000001b3;
71 const uint64_t fnv_offset = 0xcbf29ce484222325;
73
74 for (size_t i = 0; i < len; i++)
75 {
76 hash ^= str[i];
77 hash *= fnv_prime;
78 }
79 return (hash);
80}
Here is the caller graph for this function:

◆ mlx_freen()

bool mlx_freen ( int32_t  count,
  ... 
)

Utility function that lets you free x amount of pointers.

Parameters
countThe amount of args provided.
...Any form of pointer.
Returns
False, this is simply for convenience when necessary.

Definition at line 89 of file mlx_utils.c.

90{
92
94 for (int32_t i = 0; i < count; i++)
95 free(va_arg(args, void*));
96 va_end(args);
97 return (false);
98}
GLint GLsizei count
Definition glad.h:2869
Here is the caller graph for this function:

◆ mlx_getline()

bool mlx_getline ( char **  out,
size_t out_size,
FILE file 
)

Function to read a file stream line by line, reusing the same output pointer. Since the same output pointer is reused it should only be freed once, either on success or failure. This function is made to be somewhat similar to getline. Getline can't be used directly since it's not standard and therefore not available on all platforms.

Parameters
outPointer to store output string.
out_sizePointer to store output strings length.
fileFile stream to read from.
Returns
True if line was read, false if EOF was reached or an error occurred.

Definition at line 28 of file mlx_utils.c.

29{
33
34 size_t size = 0;
35 char* temp = NULL;
36 static char BUFF[GETLINE_BUFF + 1]; // Add space for '\0'
37
38 if (*out) *out[0] = '\0';
39
40 while (fgets(BUFF, sizeof(BUFF), file))
41 {
42 size += strlen(BUFF);
43 if (!(temp = realloc(*out, sizeof(char) * size + 1)))
44 return (false);
45 if (*out == NULL)
46 memset(temp, '\0', size);
47 temp[size] = '\0';
48
49 *out = temp;
50 *out_size = size;
51
52 strncat(*out, BUFF, size);
53 if (strrchr(BUFF, '\n'))
54 return (true);
55 memset(BUFF, '\0', sizeof(BUFF));
56 }
57 return (size);
58}
#define MLX_NONNULL(var)
Definition MLX42_Int.h:46
#define GETLINE_BUFF
Definition MLX42_Int.h:43
GLsizeiptr size
Definition glad.h:3302
Here is the caller graph for this function:

◆ mlx_lstadd_back()

void mlx_lstadd_back ( mlx_list_t **  lst,
mlx_list_t new 
)

Definition at line 67 of file mlx_list.c.

68{
69 if (!lst || !new)
70 return;
71 if (!*lst)
72 *lst = new;
73 else
74 {
76 new->prev = temp;
77 temp->next = new;
78 }
79}
mlx_list_t * mlx_lstlast(mlx_list_t *lst)
Definition mlx_list.c:58
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mlx_lstadd_front()

void mlx_lstadd_front ( mlx_list_t **  lst,
mlx_list_t new 
)

Definition at line 81 of file mlx_list.c.

82{
83 if (!lst || !new)
84 return;
85 if ((*lst) != NULL)
86 (*lst)->prev = new;
87 new->next = *lst;
88 new->prev = NULL;
89 *lst = new;
90}
Here is the caller graph for this function:

◆ mlx_lstclear()

void mlx_lstclear ( mlx_list_t **  lst,
void(*)(void *)  del 
)

Definition at line 33 of file mlx_list.c.

34{
36
37 while (*lst != NULL)
38 {
39 next_lst = (*lst)->next;
41 *lst = next_lst;
42 }
43}
static void mlx_lstdelone(mlx_list_t *lst, void(*del)(void *))
Definition mlx_list.c:26
struct mlx_list * next
Definition MLX42_Int.h:88
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mlx_lstlast()

mlx_list_t * mlx_lstlast ( mlx_list_t lst)

Definition at line 58 of file mlx_list.c.

59{
60 if (!lst)
61 return (NULL);
62 while (lst->next)
63 lst = lst->next;
64 return (lst);
65}
Here is the caller graph for this function:

◆ mlx_lstnew()

mlx_list_t * mlx_lstnew ( void content)

All sorts of internal functions shared in the library that should not be accessible to the user! No touch!

Definition at line 45 of file mlx_list.c.

46{
48
49 if ((out = malloc(sizeof(mlx_list_t))))
50 {
51 out->content = content;
52 out->next = NULL;
53 out->prev = NULL;
54 }
55 return (out);
56}
void * content
Definition MLX42_Int.h:87
Here is the caller graph for this function:

◆ mlx_lstremove()

mlx_list_t * mlx_lstremove ( mlx_list_t **  lst,
void value,
bool(*)(void *, void *)  comp 
)

Removes the specified content from the list, if found. Also fixes any relinking that might be needed.

Parameters
[in]lstThe list
[in]compFunction to check if the content and value are the same.
Returns
The removed element, clean up as you wish.

Definition at line 100 of file mlx_list.c.

101{
103
104 while (lstcpy && !comp(lstcpy->content, value))
105 lstcpy = lstcpy->next;
106 if (lstcpy == NULL)
107 return (NULL);
108 if (lstcpy == *lst)
109 *lst = lstcpy->next;
110 if (lstcpy->next != NULL)
112 if (lstcpy->prev != NULL)
114 return (lstcpy);
115}
struct mlx_list * prev
Definition MLX42_Int.h:89
Here is the caller graph for this function:

◆ mlx_lstsize()

int32_t mlx_lstsize ( mlx_list_t lst)

Definition at line 17 of file mlx_list.c.

18{
19 int32_t i;
20
21 for (i = 0; lst != NULL; i++)
22 lst = lst->next;
23 return (i);
24}

◆ mlx_rgba_to_mono()

uint32_t mlx_rgba_to_mono ( uint32_t  color)

Converts an RGBA value to a monochrome/grayscale value. It does so using specific weights for each channel.

See also
https://goodcalculators.com/rgb-to-grayscale-conversion-calculator/
Parameters
colorThe input RGBA value.
Returns
The rgba value converted to a grayscale color.

Definition at line 109 of file mlx_utils.c.

110{
111 const uint8_t r = 0.299f * ((color >> 24) & 0xFF);
112 const uint8_t g = 0.587f * ((color >> 16) & 0xFF);
113 const uint8_t b = 0.114f * ((color >> 8) & 0xFF);
114 const uint8_t y = r + g + b;
115
116 return (y << 24 | y << 16 | y << 8 | (color & 0xFF));
117}
GLboolean GLboolean GLboolean b
Definition glad.h:3632
GLboolean GLboolean g
Definition glad.h:3632
GLdouble GLdouble r
Definition glad.h:2421
Here is the caller graph for this function:

◆ mlx_sort_renderqueue()

void mlx_sort_renderqueue ( mlx_list_t **  lst)

Okay-ish sorting algorithm to sort the render queue / doubly linked list. We need to do this to fix transparency.

Parameters
lstThe render queue.

Definition at line 161 of file mlx_list.c.

162{
165
166 while (lstcpy != NULL)
167 {
168 mlx_list_t* next = lstcpy->next;
169
170 // Separate entry out of list and insert it back but sorted.
171 lstcpy->prev = lstcpy->next = NULL;
173 lstcpy = next;
174 }
175 *lst = sorted;
176}
static void mlx_insertsort(mlx_list_t **head, mlx_list_t *new)
Definition mlx_list.c:126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mlx_update_matrix()

void mlx_update_matrix ( const mlx_t mlx)

Recalculate the view projection matrix, used by images for screen pos Reference: https://bit.ly/3KuHOu1 (Matrix View Projection)

In case the setting to stretch the image is set, we maintain the width and height but not the depth.

Definition at line 21 of file mlx_window.c.

22{
23 const mlx_ctx_t* mlxctx = mlx->context;
24 const float depth = mlxctx->zdepth;
25
26 /**
27 * In case the setting to stretch the image is set, we maintain the width and height but not
28 * the depth.
29 */
30 const float width = mlx_settings[MLX_STRETCH_IMAGE] ? mlxctx->initialWidth : mlx->width;
31 const float height = mlx_settings[MLX_STRETCH_IMAGE] ? mlxctx->initialHeight : mlx->height;
32
33 const float matrix[16] = {
34 2.f / width, 0, 0, 0,
35 0, 2.f / -(height), 0, 0,
36 0, 0, -2.f / (depth - -depth), 0,
37 -1, -(height / -height),
38 -((depth + -depth) / (depth - -depth)), 1
39 };
40
41 glUniformMatrix4fv(glGetUniformLocation(mlxctx->shaderprogram, "ProjMatrix"), 1, GL_FALSE, matrix);
42}
mlx_settings
Definition MLX42.h:398
@ MLX_STRETCH_IMAGE
Definition MLX42.h:399
#define glGetUniformLocation
Definition glad.h:3401
#define glUniformMatrix4fv
Definition glad.h:3491
GLint GLsizei GLsizei height
Definition glad.h:1965
GLint GLsizei width
Definition glad.h:1965
GLint GLint GLsizei GLsizei GLsizei depth
Definition glad.h:2966
#define GL_FALSE
Definition glad.h:139
void * context
Definition MLX42.h:363
int32_t width
Definition MLX42.h:364
int32_t height
Definition MLX42.h:365
Here is the caller graph for this function:

Variable Documentation

◆ frag_shader

const char* frag_shader
extern

◆ mlx_settings

Definition at line 160 of file mlx_init.c.

160{false, false, false, true, false};

◆ sort_queue

bool sort_queue
extern

Definition at line 162 of file mlx_init.c.

◆ vert_shader

const char* vert_shader
extern

The shader code is extracted from the shader files and converted to a .c file as a single string at compile time. This keeps shader files external but still integrated into the program letting you use the executable anywhere without having to take the shaders with you.

Most modern frameworks like .NET do this by having resource files instead.

See: https://bit.ly/3LJYG0r