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

Go to the source code of this file.

Functions

bool mlx_getline (char **out, size_t *out_size, FILE *file)
 
uint64_t mlx_fnv_hash (char *str, size_t len)
 
bool mlx_freen (int32_t count,...)
 
uint32_t mlx_rgba_to_mono (uint32_t color)
 
double mlx_get_time (void)
 
void mlx_focus (mlx_t *mlx)
 

Function Documentation

◆ 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}
GLuint GLsizei GLsizei * length
Definition glad.h:3372
Here is the caller graph for this function:

◆ mlx_focus()

void mlx_focus ( mlx_t mlx)

This function brings the specified window to front and sets input focus.

Do not use this function to steal focus from other applications unless you are certain that is what the user wants. Focus stealing can be extremely disruptive.

Parameters
[in]mlxThe MLX instance handle.

Definition at line 126 of file mlx_utils.c.

127{
129
131}
#define MLX_NONNULL(var)
Definition MLX42_Int.h:46
Definition MLX42.h:361
void * window
Definition MLX42.h:362

◆ 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_get_time()

double mlx_get_time ( void  )

Gets the elapsed time since MLX was initialized.

Returns
The amount of time elapsed in seconds.

Definition at line 121 of file mlx_utils.c.

122{
123 return (glfwGetTime());
124}

◆ 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 GETLINE_BUFF
Definition MLX42_Int.h:43
GLsizeiptr size
Definition glad.h:3302
Here is the caller graph for this function:

◆ 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}
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
GLdouble GLdouble r
Definition glad.h:2421
Here is the caller graph for this function: