MLX42 1.0
MLX42
Loading...
Searching...
No Matches
MLX42_Int.h
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* :::::::: */
4/* MLX42_Int.h :+: :+: */
5/* +:+ */
6/* By: W2Wizard <main@w2wizard.dev> +#+ */
7/* +#+ */
8/* Created: 2021/12/27 23:55:34 by W2Wizard #+# #+# */
9/* Updated: 2022/07/21 10:46:43 by sbos ######## odam.nl */
10/* */
11/* ************************************************************************** */
12
13#ifndef MLX42_INT_H
14# define MLX42_INT_H
15# define LODEPNG_NO_COMPILE_ALLOCATORS
16# include "MLX42/MLX42.h"
17# include "lodepng/lodepng.h"
18# include "glad/glad.h"
19# include "KHR/khrplatform.h"
20# if defined(__APPLE__)
21# define GL_SILENCE_DEPRECATION
22# endif
23# include <GLFW/glfw3.h>
24# include <stdlib.h>
25# include <memory.h>
26# include <stdio.h>
27# if defined(__linux__)
28# include <linux/limits.h>
29# else
30# include <limits.h>
31# endif
32# include <ctype.h> /* isspace, isprint, ... */
33# include <string.h> /* strlen, memmove, ... */
34# include <stdarg.h> /* va_arg, va_end, ... */
35# include <assert.h> /* assert, static_assert, ... */
36# ifndef MLX_SWAP_INTERVAL
37# define MLX_SWAP_INTERVAL 1
38# endif
39# ifndef MLX_BATCH_SIZE
40# define MLX_BATCH_SIZE 12000
41# endif
42# define BPP sizeof(int32_t) /* Only support RGBA */
43# define GETLINE_BUFF 1280
44# define MLX_MAX_STRING 512 /* Arbitrary string limit */
45# define MLX_ASSERT(cond, msg) assert(cond && msg);
46# define MLX_NONNULL(var) MLX_ASSERT(var, "Value can't be null"); /* Assert instead of attribute */
47
48/**
49 * The shader code is extracted from the shader files
50 * and converted to a .c file as a single string at
51 * compile time. This keeps shader files external but
52 * still integrated into the program letting you use
53 * the executable anywhere without having to take the
54 * shaders with you.
55 *
56 * Most modern frameworks like .NET do this by having resource files
57 * instead.
58 *
59 * See: https://bit.ly/3LJYG0r
60 */
61
62extern const char* vert_shader;
63extern const char* frag_shader;
64
65// Flag to indicate if the render queue has to be sorted.
66extern bool sort_queue;
67
68// Settings array, use the enum 'key' to get the value.
69extern int32_t mlx_settings[MLX_SETTINGS_MAX];
70
71//= Types =//
72
73// A single vertex, identical to the layout in the shader.
74typedef struct vertex
75{
76 float x;
77 float y;
78 float z;
79 float u;
80 float v;
81 int8_t tex;
83
84// Layout for linked list.
85typedef struct mlx_list
86{
87 void* content;
88 struct mlx_list* next;
89 struct mlx_list* prev;
91
92//= Hook structs =//
93/**
94 * There are 2 types of hooks, special and generics.
95 *
96 * Specials: Specials are specific callback functions to a specific action
97 * such as window resizing or key presses. These are attached to the
98 * callbacks of glfw. In case MLX itself needs the callback we call
99 * the specials in that callback since there can only ever be a single
100 * callback.
101 *
102 * Generics: Generics are MLX42 specific hooks and can have multiple
103 * hooks at the same time, these are executed every frame and can be
104 * used as an alternative for keypresses or animations for instance.
105 *
106 * NOTE: Hooks could be achieved with va_args to have any amount
107 * of args sized functor but we can't/don't want to let the user
108 * deal with va_args and having to look up what args are what, etc...
109 *
110 * We want to keep it straightforward with functors already describing
111 * what params they have.
112 */
113
119
125
131
137
143
144typedef struct mlx_key
145{
146 void* param;
149
150typedef struct mlx_hook
151{
152 void* param;
153 void (*func)(void*);
155
156//= Rendering =//
157/**
158 * For rendering we need to store most of OpenGL's stuff
159 * such as the vertex array object, vertex buffer object &
160 * the shader program as well as hooks and the zdepth level.
161 *
162 * Additionally we represent draw calls with a linked list
163 * queue that points to the image and the index of its instance.
164 * Again, instances only carry XYZ data, so coupled with the image it
165 * lets us know where to draw a copy of the image.
166 *
167 * Texture contexts are kept in a struct alongside the capacity
168 * of the array of instances, since the array is realloced like a vector.
169 */
170
171// MLX instance context.
197
198// Draw call queue entry.
204
205// Image context.
211
212//= Functions =//
213/**
214 * All sorts of internal functions shared in the library that
215 * should not be accessible to the user! No touch!
216 */
217
218//= Linked List Functions =//
219
220mlx_list_t* mlx_lstnew(void* content);
222int32_t mlx_lstsize(mlx_list_t* lst);
223void mlx_lstclear(mlx_list_t** lst, void (*del)(void*));
224void mlx_lstadd_back(mlx_list_t** lst, mlx_list_t* new);
225void mlx_lstadd_front(mlx_list_t** lst, mlx_list_t* new);
226mlx_list_t* mlx_lstremove(mlx_list_t** lst, void* value, bool (*comp)(void*, void*));
228
229//= Misc functions =//
230
231bool mlx_equal_image(void* lstcontent, void* value);
232bool mlx_equal_inst(void* lstcontent, void* value);
233void mlx_draw_pixel(uint8_t* pixel, uint32_t color);
234
235//= Error/log Handling Functions =//
236
238bool mlx_freen(int32_t count, ...);
239
240//= OpenGL Functions =//
241
242void mlx_update_matrix(const mlx_t* mlx);
245
246// Utils Functions =//
247
248bool mlx_getline(char** out, size_t* out_size, FILE* file);
249uint32_t mlx_rgba_to_mono(uint32_t color);
250int32_t mlx_atoi_base(const char* str, int32_t base);
251uint64_t mlx_fnv_hash(char* str, size_t len);
252#endif
enum mlx_errno mlx_errno_t
void(* mlx_closefunc)(void *param)
Definition MLX42.h:462
mlx_settings
Definition MLX42.h:398
@ MLX_SETTINGS_MAX
Definition MLX42.h:404
void(* mlx_mousefunc)(mouse_key_t button, action_t action, modifier_key_t mods, void *param)
Definition MLX42.h:424
void(* mlx_resizefunc)(int32_t width, int32_t height, void *param)
Definition MLX42.h:453
void(* mlx_scrollfunc)(double xdelta, double ydelta, void *param)
Definition MLX42.h:414
void(* mlx_cursorfunc)(double xpos, double ypos, void *param)
Definition MLX42.h:433
void(* mlx_keyfunc)(mlx_key_data_t keydata, void *param)
Definition MLX42.h:441
mlx_list_t * mlx_lstlast(mlx_list_t *lst)
Definition mlx_list.c:58
struct mlx_key mlx_key_t
struct mlx_close mlx_close_t
void mlx_flush_batch(mlx_ctx_t *mlx)
Definition mlx_images.c:17
void mlx_draw_instance(mlx_ctx_t *mlx, mlx_image_t *img, mlx_instance_t *instance)
Definition mlx_images.c:63
struct mlx_cursor mlx_cursor_t
void mlx_lstadd_front(mlx_list_t **lst, mlx_list_t *new)
Definition mlx_list.c:81
void mlx_sort_renderqueue(mlx_list_t **lst)
Definition mlx_list.c:161
bool mlx_error(mlx_errno_t val)
Definition mlx_error.c:43
struct mlx_image_ctx mlx_image_ctx_t
int32_t mlx_atoi_base(const char *str, int32_t base)
struct mlx_resize mlx_resize_t
struct mlx_srcoll mlx_scroll_t
mlx_list_t * mlx_lstremove(mlx_list_t **lst, void *value, bool(*comp)(void *, void *))
Definition mlx_list.c:100
struct vertex vertex_t
struct mlx_ctx mlx_ctx_t
bool mlx_equal_image(void *lstcontent, void *value)
Definition mlx_compare.c:17
void mlx_lstclear(mlx_list_t **lst, void(*del)(void *))
Definition mlx_list.c:33
int32_t mlx_lstsize(mlx_list_t *lst)
Definition mlx_list.c:17
void mlx_draw_pixel(uint8_t *pixel, uint32_t color)
bool mlx_freen(int32_t count,...)
Definition mlx_utils.c:89
struct mlx_mouse mlx_mouse_t
bool mlx_equal_inst(void *lstcontent, void *value)
Definition mlx_compare.c:25
#define MLX_BATCH_SIZE
Definition MLX42_Int.h:40
bool sort_queue
Definition mlx_init.c:162
uint64_t mlx_fnv_hash(char *str, size_t len)
Definition mlx_utils.c:68
const char * vert_shader
struct draw_queue draw_queue_t
const char * frag_shader
mlx_list_t * mlx_lstnew(void *content)
Definition mlx_list.c:45
uint32_t mlx_rgba_to_mono(uint32_t color)
Definition mlx_utils.c:109
void mlx_update_matrix(const mlx_t *mlx)
Definition mlx_window.c:21
struct mlx_hook mlx_hook_t
void mlx_lstadd_back(mlx_list_t **lst, mlx_list_t *new)
Definition mlx_list.c:67
struct mlx_list mlx_list_t
bool mlx_getline(char **out, size_t *out_size, FILE *file)
Definition mlx_utils.c:28
GLfloat value
Definition glad.h:2667
GLenum func
Definition glad.h:3336
GLint GLsizei count
Definition glad.h:2869
unsigned int GLuint
Definition glad.h:99
GLuint color
Definition glad.h:3749
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLuint GLfloat * val
Definition glad.h:3979
GLint void * img
Definition glad.h:3003
mlx_image_t * image
Definition MLX42_Int.h:201
int32_t instanceid
Definition MLX42_Int.h:202
mlx_closefunc func
Definition MLX42_Int.h:135
void * param
Definition MLX42_Int.h:134
int32_t bound_textures[16]
Definition MLX42_Int.h:193
int32_t initialWidth
Definition MLX42_Int.h:178
mlx_key_t key_hook
Definition MLX42_Int.h:188
mlx_cursor_t cursor_hook
Definition MLX42_Int.h:187
GLuint shaderprogram
Definition MLX42_Int.h:176
int32_t zdepth
Definition MLX42_Int.h:192
mlx_list_t * images
Definition MLX42_Int.h:182
GLuint vbo
Definition MLX42_Int.h:175
GLuint vao
Definition MLX42_Int.h:174
mlx_list_t * render_queue
Definition MLX42_Int.h:183
mlx_scroll_t scroll_hook
Definition MLX42_Int.h:185
vertex_t batch_vertices[MLX_BATCH_SIZE]
Definition MLX42_Int.h:195
mlx_list_t * hooks
Definition MLX42_Int.h:181
int32_t batch_size
Definition MLX42_Int.h:194
int32_t initialHeight
Definition MLX42_Int.h:179
mlx_mouse_t mouse_hook
Definition MLX42_Int.h:186
mlx_close_t close_hook
Definition MLX42_Int.h:190
mlx_resize_t resize_hook
Definition MLX42_Int.h:189
void * param
Definition MLX42_Int.h:128
mlx_cursorfunc func
Definition MLX42_Int.h:129
void * param
Definition MLX42_Int.h:152
size_t instances_capacity
Definition MLX42_Int.h:209
GLuint texture
Definition MLX42_Int.h:208
void * param
Definition MLX42_Int.h:146
mlx_keyfunc func
Definition MLX42_Int.h:147
struct mlx_list * next
Definition MLX42_Int.h:88
void * content
Definition MLX42_Int.h:87
struct mlx_list * prev
Definition MLX42_Int.h:89
mlx_mousefunc func
Definition MLX42_Int.h:123
void * param
Definition MLX42_Int.h:122
void * param
Definition MLX42_Int.h:140
mlx_resizefunc func
Definition MLX42_Int.h:141
void * param
Definition MLX42_Int.h:116
mlx_scrollfunc func
Definition MLX42_Int.h:117
Definition MLX42.h:361
float x
Definition MLX42_Int.h:76
float v
Definition MLX42_Int.h:80
float u
Definition MLX42_Int.h:79
int8_t tex
Definition MLX42_Int.h:81
float y
Definition MLX42_Int.h:77
float z
Definition MLX42_Int.h:78