MLX42 1.0
MLX42
Loading...
Searching...
No Matches
mlx_utils.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* :::::::: */
4/* mlx_utils.c :+: :+: */
5/* +:+ */
6/* By: W2Wizard <main@w2wizard.dev> +#+ */
7/* +#+ */
8/* Created: 2022/01/03 20:13:17 by W2Wizard #+# #+# */
9/* Updated: 2022/11/22 10:56:09 by jvan-hal ######## odam.nl */
10/* */
11/* ************************************************************************** */
12
13#include "MLX42/MLX42_Int.h"
14
15//= Private =//
16
17/**
18 * Function to read a file stream line by line, reusing the same output pointer.
19 * Since the same output pointer is reused it should only be freed once, either on success or failure.
20 * This function is made to be somewhat similar to getline.
21 * Getline can't be used directly since it's not standard and therefore not available on all platforms.
22 *
23 * @param out Pointer to store output string.
24 * @param out_size Pointer to store output strings length.
25 * @param file File stream to read from.
26 * @return True if line was read, false if EOF was reached or an error occurred.
27 */
28bool mlx_getline(char** out, size_t* out_size, FILE* file)
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}
59
60/**
61 * String hashing algorithm using FNV-1a.
62 * Source: https://bit.ly/3JcRGHa
63 *
64 * @param str The string to hash
65 * @param len The length of the string.
66 * @return The hashed output.
67 */
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}
81
82/**
83 * Utility function that lets you free x amount of pointers.
84 *
85 * @param count The amount of args provided.
86 * @param ... Any form of pointer.
87 * @return False, this is simply for convenience when necessary.
88 */
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}
99
100/**
101 * Converts an RGBA value to a monochrome/grayscale value.
102 * It does so using specific weights for each channel.
103 *
104 * @see https://goodcalculators.com/rgb-to-grayscale-conversion-calculator/
105 *
106 * @param color The input RGBA value.
107 * @return The rgba value converted to a grayscale color.
108 */
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}
118
119//= Public =//
120
121double mlx_get_time(void)
122{
123 return (glfwGetTime());
124}
125
127{
129
131}
#define MLX_NONNULL(var)
Definition MLX42_Int.h:46
#define GETLINE_BUFF
Definition MLX42_Int.h:43
GLint GLsizei count
Definition glad.h:2869
GLint y
Definition glad.h:1965
GLuint color
Definition glad.h:3749
GLboolean GLboolean GLboolean b
Definition glad.h:3632
GLboolean GLboolean g
Definition glad.h:3632
GLuint GLsizei GLsizei * length
Definition glad.h:3372
GLdouble GLdouble r
Definition glad.h:2421
GLsizeiptr size
Definition glad.h:3302
void mlx_focus(mlx_t *mlx)
Definition mlx_utils.c:126
bool mlx_freen(int32_t count,...)
Definition mlx_utils.c:89
uint64_t mlx_fnv_hash(char *str, size_t len)
Definition mlx_utils.c:68
uint32_t mlx_rgba_to_mono(uint32_t color)
Definition mlx_utils.c:109
double mlx_get_time(void)
Definition mlx_utils.c:121
bool mlx_getline(char **out, size_t *out_size, FILE *file)
Definition mlx_utils.c:28
Definition MLX42.h:361
void * window
Definition MLX42.h:362