> ## Content Index
> Fetch the complete content index at: https://evil-penguin.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# MLX42 - Intro
- URL: https://evil-penguin.com/mlx42-intro/
- Published: 2024-01-30T23:10:28.000Z
- Updated: 2026-11-16T23:03:28.000Z
- Description: Learn how to use the brand new mlx library, baked in CODAM just for you!
- Author: evilpenguin
- Tags: 42, MLX, C

## MLX42 is simple and gives you more freedom!

A few useful things before you get started:  
  
MLX42 is the new un-official version of mlx, make sure this tutorial is appropiate for your project  
MLX42 has an official documentation which you can visit here: [https://github.com/codam-coding-college/MLX42/wiki](https://github.com/codam-coding-college/MLX42/wiki?ref=evil-penguin.com)  
MLX42 functions can be found [here](https://bit.ly/3aWZL7C?ref=evil-penguin.com)   
  
Alright, that last one was a troll... you can find them (all the functions) on the headers.

# Let's get started!

First you will need to download the library, for that you can use the following commands:  
  
git clone https://github.com/codam-coding-college/MLX42  
cd MLX42  
cmake -B build  
make -C build/Now you should be able to locate the libmlx42.a file.You can use mlx42 library by linking the library

> \-L<path-to-library> -lmlx42  
>  

MLX42 is dependent on a few other libraries, when you are using Linux you should also link the libraries required. With the following command.-ldl -lglfw -lm -lpthreaddl -> **Dynamically loaded** (DL)  
glfw -> **Graphics Library Framework**  
m -> **Math library**  
  
I hope this helps demystify what all those weird symbols are when you are compiling! It should all make sense now.  
  
Make sure to run the example which is on the MLX42 GitHub [README](https://github.com/codam-coding-college/MLX42?ref=evil-penguin.com) to make sure that you have installed the library with success!\* Note:   
You can go ahead and copy-paste the example from the GitHub and compile with the following:  
  
gcc -o test main.c -ldl -lglfw -lm -lpthread -LMLX42/build -lmlx42 -IMLX42/include  
  
./testNow that everything is setup, we can begin creating a program.  
For this tutorial we are going to build a color game!  
It's simple, the game consist on choosing the same color as the one displayed by the program, trust your eyes!  
  
It will include:  
Animations  
Menu  
Game dynamics

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2024-01-30_16-21-49.png)

Let's start by creating the menu. The menu is the first thing that your user will see. The user will be able to choose between a set of options in order to use your program.  
  
For this tutorial we will keep it simple, a button to start the game and another to configure the difficulty level.  
  
The menu will have a fixed image as a background, and we will create a selector for the user to choose the options. (No mouse, the user selects with the keys)  
Download the menu image bellow and save it in a folder accessible to your project.  
  
\*Note: I created the background image with [Tiled](https://www.mapeditor.org/?ref=evil-penguin.com) and [this](https://crusenho.itch.io/complete-gui-essential-pack?ref=evil-penguin.com) sprite pack.

![](https://evil-penguin.com/content/images/2026/11/menu_bg.png)

As you might have noticed, the second button is empty, that's because the text that will be on top of that button might change depending on the option that the player chooses. So I've prepared all the images with the possible text.  
"Easy", "Medium" & "Hard"

![](https://evil-penguin.com/content/images/2026/11/menu_easy.png)

![](https://evil-penguin.com/content/images/2026/11/menu_medium.png)

![](https://evil-penguin.com/content/images/2026/11/menu_hard.png)

Let's start simple, create a main, load our texture (the menu background image) convert it to an image and place the image into the window.  
  
> "in order to display a sprite image onto our window we would first load the texture from disk into our memory and store the information in a mlx\_texture\_t\*. After that we create a new mlx\_image\_t\* based on the information given by the texture and then we can display our image onto the window" - from the [wiki](https://github.com/codam-coding-college/MLX42/wiki/Textures?ref=evil-penguin.com)

#include <stdlib.h>  
#include <stdio.h>  
#include "MLX42/MLX42.h"  
  
#define WIDTH 640  
#define HEIGHT 360  
  
static void error(void)  
{  
 puts(mlx\_strerror(mlx\_errno));  
 exit(EXIT\_FAILURE);  
}  
  
int32\_t main(void)  
{  
 // Start mlx  
 mlx\_t\* mlx = mlx\_init(WIDTH, HEIGHT, "Color Game", false);  
 if (!mlx)  
 error();  
  
 // Try to load the file  
 mlx\_texture\_t\* texture = mlx\_load\_png("./images/menu\_bg.png");  
 if (!texture)  
 error();  
  
 // Convert texture to a displayable image  
 mlx\_image\_t\* img = mlx\_texture\_to\_image(mlx, texture);  
 if (!img)  
 error();  
  
 // Display the image  
 if (mlx\_image\_to\_window(mlx, img, 0, 0) < 0)  
 error();  
  
 mlx\_loop(mlx);  
  
 mlx\_delete\_image(mlx, img);  
 mlx\_delete\_texture(texture);  
  
 // Optional, terminate will clean up any leftover images (not textures!)  
 mlx\_terminate(mlx);  
 return (EXIT\_SUCCESS);  
}  
We initialize mlx with mlx\_init (the last parameter is fasle, indicating we don't want this to be a resizable window).  
Then you should be able to visualize the image in your mlx window.

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2024-01-30_17-27-42.png)

# Animations

Now let's create an animation.  
We will animate our selector (to choose the options). We will use a linked list to iterate through each of the images.  
If you don't understand the code bellow, go to my animations [tutorial](http://pulgamecanica.herokuapp.com/posts/mlx-animations/?ref=evil-penguin.com) where everything is explained.  
  
I've create a sprite-sheet with the animation using [Pixilart](https://www.pixilart.com/draw?ref=evil-penguin.com).  
Download the sheet so we can cut it to create our animation.

![](https://evil-penguin.com/content/images/2026/11/select_sprite_sheet.png)

# Slices

Let's create the functions which will allow us to cut in slices our sprite-sheet.  
I will be using my libft in order to use the linked lists (t\_list).  
Let's start by defining the header  
  
***animate.h***  
#ifndef \_\_ANIMATIONS\_H\_\_  
\# define \_\_ANIMATIONS\_H\_\_  
  
\# include "libft.h"  
\# include "MLX42/MLX42.h"  
  
typedef struct s\_animation {  
 t\_list\* frames;  
 int frame\_speed; // The speed of the animation in miliseconds  
 double accum; // The accumulator to controll the speed  
 int current\_frame\_num; // Which frame is selected  
 int mirrored;  
 long int frame\_count; // The frame count  
} t\_animation;  
  
typedef struct sprite\_slice {  
 int x;  
 int y;  
 int width;  
 int height;  
 int padding\_x;  
 int padding\_y;  
} sprite\_slice;  
  
typedef struct s\_sprite {  
 mlx\_image\_t\* sprite\_img;  
 mlx\_t\* mlx;  
} t\_sprite;  
  
/\* SPRITES \*/  
t\_sprite new\_sprite(char\* file\_name, mlx\_t\* mlx);  
t\_animation\* slice\_sprite(t\_sprite\* s, sprite\_slice slice, int mirrored, int frames, int delay);  
void destroy\_sprite(t\_sprite\* s);  
  
#endif  
The t\_sprite is really just to help us convert the PNG, then we have a sprite\_slice which helps us cut the sprite.  
In the case of the sprite-sheet above each frame is **128x32 px**, so the slice will need to cut those dimensions.  
  
Now let's create the code:  
(I've included the declaration of the **error** function on the top (the **error** function is in the ***main.c*** file)  
  
***animate.c***  
#include "animate.h"  
  
void error(void);  
  
t\_sprite new\_sprite(char\* file\_path, mlx\_t\* mlx) {  
 mlx\_image\_t\* img;  
 mlx\_texture\_t\* texture;  
  
 // Load png  
 texture = mlx\_load\_png(file\_path);  
 if (!texture)  
 error();  
  
 // Create image from png texture  
 img = mlx\_texture\_to\_image(mlx, texture);  
 if (!img)  
 error();  
  
 mlx\_delete\_texture(texture);  
  
 return (t\_sprite){img, mlx};  
}  
  
void destroy\_sprite(t\_sprite\* s) {  
 if (!s)  
 error();  
 mlx\_delete\_image(s->mlx, s->sprite\_img);  
}  
  
static void add\_frame(t\_animation\* a, t\_sprite\* s, sprite\_slice slice, int mirrored) {  
 mlx\_image\_t\* frame;  
  
 frame = mlx\_new\_image(s->mlx, slice.width - (slice.padding\_x \* 2), slice.height - (slice.padding\_y \* 2));  
 if (!frame)  
 error();  
 for (int i = 0; i < slice.height - (slice.padding\_y \* 2); i++) {  
 for (int j = 0; j < slice.width - (slice.padding\_x \* 2); j++) {  
 if (mirrored)  
 mlx\_put\_pixel(frame, (slice.height - (slice.padding\_y \* 2)) - j - 1, i, mlx\_get\_pixel(s->sprite\_img, slice.x + j + slice.padding\_x, slice.y + i + slice.padding\_y));  
 else  
 mlx\_put\_pixel(frame, j, i, mlx\_get\_pixel(s->sprite\_img, slice.x + j + slice.padding\_x, slice.y + i + slice.padding\_y));  
 }  
 }  
 ft\_lstadd\_back(&a->frames, ft\_lstnew(frame));  
}  
  
t\_animation\* slice\_sprite(t\_sprite\* s, sprite\_slice slice, int mirrored, int frames, int delay) {  
 t\_animation \* a;  
  
 a = (t\_animation\*)calloc(sizeof(t\_animation), 1);  
 if (!a)  
 error();  
 \*a = (t\_animation){NULL, delay, 0, 0, mirrored, 0, 0};  
 for (int i = 0; i < frames; i++) {  
 add\_frame(a, s, slice, mirrored);  
 slice.x += slice.width;  
 if ((uint32\_t)slice.x >= s->sprite\_img->width) {  
 slice.x = 0;  
 slice.y += slice.height;  
 }  
 }  
 return (a);  
}  
Now you can compile your code with this:  
(We must add the linking of our libft, I usually create a soft-link **ln -s <path\_to\_libft> libft**)  
  
gcc -o test animate.c main.c -ldl -lglfw -lm -lpthread -LMLX42/build -lmlx42 -Llibft/ -lft -IMLX42/include -Ilibft/includes/  
After you compile this you will see an error.  
  
> **animate.c:39:89: warning: implicit declaration of function ‘mlx\_get\_pixel’; did you mean ‘mlx\_put\_pixel’?**

The MLX42 library has a function **mlx\_put\_pixel** but it lacks the function **mlx\_get\_pixel**.  
  
We can create this function easily...  
  
***image\_utils.c***  
#include "MLX42/MLX42.h"  
  
#define BPP sizeof(int32\_t)  
  
static int get\_rgba(int r, int g, int b, int a) {  
 return (r << 24 | g << 16 | b << 8 | a);  
}  
  
/\*\*  
 \* In MLX42 Colors are as follows:  
 \* 0000 0000 A (1st byte) -> uint8\_t because it's 8 bits  
 \* 0000 0000 R (2nd byte)  
 \* 0000 0000 G (3rd byte)  
 \* 0000 0000 B (4th byte)  
 \*\*/  
int32\_t mlx\_get\_pixel(mlx\_image\_t\* image, uint32\_t x, uint32\_t y) {  
 if (x > image->width || y > image->height)  
 return 0xFF000000;  
 uint8\_t\* pixelstart = image->pixels + (y \* image->width + x) \* BPP;  
 return get\_rgba(\*(pixelstart), \*(pixelstart + 1),  
 \* (pixelstart + 2), \*(pixelstart + 3));  
}  
Add the declaration of the ***mlx\_get\_pixel*** on top of your ***animate.c*** file.  
  
Now we can add the file **image\_utils.c** to our compilation and it should compile without any issues.  
  
Let's test then to see if the code works! Let's put it to practice.  
  
As I mentioned before, the slice will be something like this:(sprite\_slice){0, 0, 128, 32, 0, 0}We can create a structure to hold our game components, this way we can control all the game elements through one single pointer.  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 t\_animation \* select\_animation;  
} t\_color\_game;  
Now your ***main.c*** should look like this:  
  
#include <stdlib.h>  
#include <stdio.h>  
#include "MLX42/MLX42.h"  
#include "animate.h"  
  
#define WIDTH 640  
#define HEIGHT 360  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 t\_animation \* select\_animation;  
} t\_color\_game;  
  
void error(void) {  
 puts(mlx\_strerror(mlx\_errno));  
 exit(EXIT\_FAILURE);  
}  
  
void bait(void\*ptr){(void)ptr;};  
  
t\_color\_game init\_game() {  
 mlx\_t\* mlx;  
 mlx\_texture\_t\* texture;  
 mlx\_image\_t\* img;  
 t\_animation\* anim;  
 t\_sprite sprite;  
  
 // Start mlx  
 mlx = mlx\_init(WIDTH, HEIGHT, "Color Game", false);  
 if (!mlx)  
 error();  
 // Try to load the file  
 texture = mlx\_load\_png("./images/menu\_bg.png");  
 if (!texture)  
 error();  
 // Convert texture to a displayable image  
 img = mlx\_texture\_to\_image(mlx, texture);  
 if (!img)  
 error();  
 mlx\_delete\_texture(texture);  
 // Sprite & Animation  
 sprite = new\_sprite("./images/select\_sprite\_sheet.png", mlx);  
 anim = slice\_sprite(&sprite, (sprite\_slice){0, 0, 128, 32, 0, 0}, false, 5, 120);  
 destroy\_sprite(&sprite);  
  
 return (t\_color\_game){mlx, img, anim};  
}  
  
int32\_t main(void)  
{  
 t\_color\_game cg;  
  
 cg = init\_game();  
  
 if (mlx\_image\_to\_window(cg.mlx, cg.menu\_bg, 0, 0) == -1)  
 error();  
  
 mlx\_loop(cg.mlx);  
  
 ft\_lstclear(&cg.select\_animation->frames, bait);  
 free(cg.select\_animation);  
 mlx\_terminate(cg.mlx);  
 return (EXIT\_SUCCESS);  
}  
\*Note: I've set up the frame\_speed to 120ms, later we will use this to update the animation.  
  
Awesome! Finally we can use our animation. Let's implement a hook to display our image.  
  
In order to display animations, we need to draw the **current** frame to an image and display the image, when the animation is supposed to change, the **frame** should be repainted, and so on and so on.  
  
We need a "***foreground***" image which will hold the foreground of our game, that is the outer-most layer. We will paint the frames of the animation in the foreground layer. It will look something like this:  
  
![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-01-3021-31-26-ezgif.com-video-to-gif-converter.gif)

# Image Utils

In order to paint the frames of the animation to the **foreground** we need to create a function which can copy an image to another image (we must copy the frame to the foreground).  
  
That function can look something like this:  
(The *put\_pixel\_valid* function is just a protection in case the dst image is smaller)  
  
***image\_utils.c***  
  
static int put\_pixel\_valid(mlx\_image\_t\* img, uint32\_t x, uint32\_t y) {  
 return (x < img->width && y < img->height && mlx\_get\_pixel(img, x, y) != 0);  
}  
  
void put\_img\_to\_img(mlx\_image\_t\* dst, mlx\_image\_t\* src, int x, int y) {  
 int i;  
 int j;  
  
 i = 0;  
 while(i < src->width) {  
 j = 0;  
 while (j < src->height) {  
 if (put\_pixel\_valid(src, i, j))  
 mlx\_put\_pixel(dst, x + i, y + j, mlx\_get\_pixel(src, i, j));  
 j++;  
 }  
 i++;  
 }  
}  
Now let's add the foreground to our game structure:  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 mlx\_image\_t \* foreground;  
 t\_animation \* select\_animation;  
 int fps;  
} t\_color\_game;  
In the ***game\_init*** function let's add the foreground creation:  
  
...  
**mlx\_image\_t\* foreground\_img;**  
...  
 // Foreground  
 foreground\_img = mlx\_new\_image(mlx, WIDTH, HEIGHT);  
 if (!foreground\_img)  
 **error();**  
 return (t\_color\_game){mlx, img, **foreground\_img**, anim};  
}  
And finally let's create an update function for the loop hook!  
  
void update(void \* ptr) {  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(cg->select\_animation->frames, cg->select\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 put\_img\_to\_img(cg->foreground, frame, 256, 160);  
 update\_animation(cg->select\_animation, cg->mlx->delta\_time);  
}  
We use ***ft\_lstget*** to retrieve the current frame of the animation. Then we place the animation at coordinates \[256, 160\]. (This is the coordinates of the button, later we will implement something more dynamic)  
  
You can compile this with the following command:gcc -fsanitize=address -o test image\_utils.c animate.c main.c -ldl -lglfw -lm -lpthread -LMLX42/build -lmlx42 -Llibft/ -lft -IMLX42/include -Ilibft/includes   
You will see an error:

> ***main.c:71:3: warning: implicit declaration of function ‘update\_animation’***

Of course we are missing this fun function!  
  
# **Update Animation**

This function is super important to understand.  
Basically the delta time will tell up how much time has passed between the last frame and the current frame.  
Accumulating the time that passes between each frame will let us know when we are supposed to change the animation.  
This method is very good, because the animation speed is not dependant on the fps (or the performance) of the program.  
The animation will have a fixed frame speed and will change it's frame at that speed.  
  
So we can implement the function finally:  
  
***animate.h***  
  
void update\_animation(t\_animation \* a, double dt);  
***animate.c***  
  
void update\_animation(t\_animation \* a, double dt) {  
 if (a) {  
 a->accum += dt \* 1000;  
 if (a->accum > a->frame\_speed) {  
 a->accum -= a->frame\_speed;  
 a->current\_frame\_num++;  
 a->current\_frame\_num %= ft\_lstsize(a->frames);  
 }  
 }  
}  
The accumulation is counted in miliseconds, just as our frame speed.  
We multiply in order to be in the same order of magnitude (miliseconds), since the delta time is given in seconds.  
Remember 1 seconds = 1000 miliseconds.  
  
Finally implement the hook, on the ***main*** function and we will be able to see an animation! :D  
  
mlx\_loop\_hook(cg.mlx, update, &cg);

## Game Dynamics

We've reached a very important part of the tutorial, the game dynamics. I always recommend not to hesitate on having as many useful data types as you can, but also be sure not to overwhelm so much the program to a point where you don't understand why you need that data type.  
  
That is why I suggest only the following data types to control the menu:  
  
#define SELECTION\_LEN 2  
  
enum game\_status {  
 MENU,  
 PLAYING  
};  
  
enum menu\_selection {  
 SELECT\_PLAY,  
 SELECT\_DIFFICULTY  
};  
The **game status** will allow us to control the input when the player is playing and when the player is selecting stuff in the menu.  
  
The **menu selection** helps keeping track of our selection, we only have two options in this case, that's why I've set the define **SELECTION\_LEN** to 2.  
  
Finally let's add this to out color game struct:  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 mlx\_image\_t \* foreground;  
 t\_animation \* select\_animation;  
 enum menu\_selection menu\_selection;  
 **enum game\_status game\_status;**  
} t\_color\_game;  
And of course, add this to the **game\_init** return:  
  
return (t\_color\_game){mlx, img, foreground\_img, anim, **SELECT\_PLAY**, **MENU**};  
Now we can add a static member to our ***update*** function, a matrix with the exact coordinates of our buttons.  
  
static int menu\_selection\_coords\[SELECTION\_LEN\]\[SELECTION\_LEN\] = {{256, 160}, {256, 256}};  
Now, we can modify the ***put\_img\_to\_img*** function so it will choose the appropriate coordinate depending on the ***menu\_selection***.  
  
put\_img\_to\_img(cg->foreground, frame,  
 menu\_selection\_coords\[cg->menu\_selection\]\[0\],  
 menu\_selection\_coords\[cg->menu\_selection\]\[1\]);  
And finally we can add the logic to our menu!  
  
In the update function add the logic for the menu, when the user presses the down key, move the selection down, when the user press up, choose the selection which is above.  
  
 // Logic for the menu  
 if (cg->game\_status == MENU) {  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_DOWN)) {  
 cg->menu\_selection = SELECT\_DIFFICULTY;  
 }  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_UP)) {  
 cg->menu\_selection = SELECT\_PLAY;  
 }  
 }  
Finally if you compile this, you should be able to change between selection, there is just one problem...  
When we change the selection, the other selection is not erased from the **foreground**!  
  
![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-01-3022-17-27-ezgif.com-video-to-gif-converter.gif)

This can be fixed very easily, just clean the foreground before you paint the animation frame!  
  
On the first line of execution of out update function add the following line:  
  
memset(cg->foreground->pixels, 0xFF000000, cg->foreground->width \* cg->foreground->height \* BPP);  
It's simple, just set all the pixels to transparent. Ta-Da it's clean! :D

## Menu Multiple Options

As you would have guessed from the title, now we are going to create the difficulty selection.  
We are not going to implement this one in the update function just yet... That is because the update function can only receive input when the key is pressed, and this is repeated each frame, it would be very complicated to implement a multiple options when you press a key, if they key is pressed 1000 times in one second.  
  
We have 3 options **EASY, MEDIUM & HARD,** when the user selects the difficulty button, they should be able to change the difficulty options with the **right & left** keys, but only when the key is pressed the first time.  
  
So we can implement that with a key hook!  
  
First let's modify our game structure:  
  
#define DIFFICULTY\_LEN 3  
  
enum game\_difficulty {  
 EASY,  
 MEDIUM,  
 HARD  
**};**  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 mlx\_image\_t \* foreground;  
 t\_animation \* select\_animation;  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
**enum game\_difficulty game\_difficulty;**  
} t\_color\_game;  
And in the ***init\_game*** function remember to modify the return as well.  
  
return (t\_color\_game){mlx, img, foreground\_img, anim, SELECT\_PLAY, MENU, **EASY**};  
Now we can implement the key\_hook:  
  
void key\_update(mlx\_key\_data\_t keydata, void\* ptr) {  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 if (cg->game\_status == MENU && cg->menu\_selection == SELECT\_DIFFICULTY && keydata.action == MLX\_PRESS) {  
 if (keydata.key == MLX\_KEY\_LEFT) {  
 cg->game\_difficulty--;  
 if (cg->game\_difficulty == -1)  
 cg->game\_difficulty = DIFFICULTY\_LEN - 1;  
 } else if (keydata.key == MLX\_KEY\_RIGHT) {  
 cg->game\_difficulty++;  
 cg->game\_difficulty %= DIFFICULTY\_LEN;  
 }  
 }  
}  
So if they game status is menu, and we are choosing the difficulty and the input is a key pressed, then:  
When the key pressed is the **left** key, then subtract one value, unless the value is lower negative, then go to the last value (**DIFICULTY\_LEN - 1**)  
When the key pressed is the **right** key, then increment one value, and do the modulus with the **DIFICULTY\_LEN** to make sure that it will never surpass the options we have.  
  
Now this code will make sure that the input get's handled, but we still need to show the user the level which he is choosing, to do this we will need to create 3 images, one for each level.   
  
Modify, again, the color\_game structure.  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 mlx\_image\_t \* foreground;  
 **mlx\_image\_t \* difficulty\_imgs\[DIFFICULTY\_LEN\];**  
 t\_animation \* select\_animation;  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
} t\_color\_game;  
And of course, we must also edit the **init\_game** function.  
  
Because we would need to create a texture and an image for each difficulty image, it would be so repetitive to put this as it is. That is why I will create a function ***my\_load\_png*** and in this function I will take advantage and will also setup the image to the window, so it will enable **1** instance for each image. (I can also take advantage and replace the code from the menu background)  
  
The functions would look something like this:  
  
mlx\_image\_t \* my\_load\_png( char\* file\_path, mlx\_t \* mlx) {  
 mlx\_image\_t\* img;  
 mlx\_texture\_t\* texture;  
 // Try to load the file  
 texture = mlx\_load\_png(file\_path);  
 if (!texture)  
 error();  
 // Convert texture to a displayable image  
 img = mlx\_texture\_to\_image(mlx, texture);  
 if (!img)  
 error();  
 if (mlx\_image\_to\_window(mlx, img, 0, 0) == -1)  
 error();  
 mlx\_delete\_texture(texture);  
 return (img);  
**}**  
  
t\_color\_game init\_game() {  
 mlx\_t\* mlx;  
 mlx\_image\_t\* img;  
 mlx\_image\_t\* foreground\_img;  
 mlx\_image\_t\* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 t\_animation\* anim;  
 t\_sprite sprite;  
  
 // Start mlx  
 mlx = mlx\_init(WIDTH, HEIGHT, "Color Game", false);  
 if (!mlx)  
 error();  
 // Load the Background and Menu images  
 img = my\_load\_png("./images/menu\_bg.png", mlx);  
 difficulty\_imgs\[0\] = my\_load\_png("./images/menu\_easy.png", mlx);  
 difficulty\_imgs\[1\] = my\_load\_png("./images/menu\_medium.png", mlx);  
 **difficulty\_imgs\[2\] = my\_load\_png("./images/menu\_hard.png", mlx);**  
 // Sprite & Animation  
 sprite = new\_sprite("./images/select\_sprite\_sheet.png", mlx);  
 anim = slice\_sprite(&sprite, (sprite\_slice){0, 0, 128, 32, 0, 0}, false, 5, 120);  
 destroy\_sprite(&sprite);  
 // Foreground  
 foreground\_img = mlx\_new\_image(mlx, WIDTH, HEIGHT);  
 if (!foreground\_img)  
 error();  
 return (t\_color\_game){mlx, img, foreground\_img,  
**{difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},**  
 anim, SELECT\_PLAY, MENU, EASY};  
}  
Also make sure to remove from the main, the mlx\_image\_to\_window of the background. But leave the **foreground** one, or else you wont be able to see the animation anymore :( .  
  
Now, if we start the program we will see the 3 images stack one after another, we don't want that, we only want to see the dificulty level which is selected. For this we can play with the instance **enabled** boolean.  
  
Set all the dificulty images **enabled** to false on the update function, and only activate the one which is selected, and Voila!  
  
***update***  
  
 // Logic for the menu  
 if (cg->game\_status == MENU) {   
 for (int i = 0; i < DIFFICULTY\_LEN; i++) {  
 cg->difficulty\_imgs\[i\]->instances\[0\].enabled = false;  
 **}**  
**cg->difficulty\_imgs\[cg->game\_difficulty\]->instances\[0\].enabled = true;**  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_DOWN)) {  
 cg->menu\_selection = SELECT\_DIFFICULTY;  
 }  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_UP)) {  
 cg->menu\_selection = SELECT\_PLAY;  
 }  
 }

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-01-3023-08-48-ezgif.com-video-to-gif-converter.gif)

## Start the game!

Now we can start the game!  
  
We want to start the game when the use releases the "Enter"  
And so we can create this logic on the key\_hook function. And also with a few modification on the update function.  
The game will need to have a background image as well.  
We are going to add an extra attribute to the color game, the **game\_bg**  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 **mlx\_image\_t \* game\_bg;**  
 mlx\_image\_t \* foreground;  
 mlx\_image\_t \* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 t\_animation \* select\_animation;  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
} t\_color\_game;  
Make sure to create this image in the **init\_game**, we can just use the new function we created to load PNG images.  
**init\_game**  
  
init\_game()  
...  
**mlx\_image\_t\* img2;**  
...  
**img2 = my\_load\_png("./images/game\_bg.png", mlx);**  
...  
return (t\_color\_game){mlx, img, **img2**, foreground\_img,  
 {difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},  
 anim, SELECT\_PLAY, MENU, EASY};  
The image which I will use for the game background is the one bellow.

![](https://evil-penguin.com/content/images/2026/11/game_bg-1.png)

When the user chooses to start playing, we must remove the menu background, the difficulty images, the selection animation, well basically everything which was on the menu.  
  
The foreground by default will be cleaned each frame, so we don't need to worry about the select animation, although we should move the logic to put it in the foreground inside the if **"menu"** .  
  
As for the other components, we can simply set the **enabled** boolean to false and they will forever be gone! :D  
MLX42 is just so simple!  
  
So the code would be something like this:  
  
void update(void \* ptr) {  
 static int menu\_selection\_coords\[SELECTION\_LEN\]\[SELECTION\_LEN\] = {{256, 160}, {256, 256}};  
 t\_color\_game\* cg = (t\_color\_game\*)  
  
 // Clean the foreground  
 memset(cg->foreground->pixels, 0xFF000000, cg->foreground->width \* cg->foreground->height \* BPP);  
**if (cg->game\_status == MENU) {**  
 // Paint the select animation on the foreground  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(cg->select\_animation->frames, cg->select\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 put\_img\_to\_img(cg->foreground, frame,  
 menu\_selection\_coords\[cg->menu\_selection\]\[0\],  
 menu\_selection\_coords\[cg->menu\_selection\]\[1\]);  
 **update\_animation(cg->select\_animation, cg->mlx->delta\_time);**  
 // Logic for the menu  
 for (int i = 0; i < DIFFICULTY\_LEN; i++) {  
 cg->difficulty\_imgs\[i\]->instances\[0\].enabled = false;  
 }  
 cg->difficulty\_imgs\[cg->game\_difficulty\]->instances\[0\].enabled = true;  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_DOWN)) {  
 cg->menu\_selection = SELECT\_DIFFICULTY;  
 }  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_UP)) {  
 cg->menu\_selection = SELECT\_PLAY;  
 }  
 }  
}  
  
void key\_update(mlx\_key\_data\_t keydata, void\* ptr) {  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 if (cg->game\_status == MENU && keydata.action == MLX\_RELEASE) {  
 if (keydata.key == MLX\_KEY\_ENTER) {  
 // Disable the menu components  
 for (int i = 0; i < DIFFICULTY\_LEN; i++)  
 cg->difficulty\_imgs\[i\]->instances\[0\].enabled = false;  
 cg->menu\_bg->instances\[0\].enabled = false;  
 cg->game\_status = PLAYING;  
 **}**  
**} else** if (cg->game\_status == MENU && cg->menu\_selection == SELECT\_DIFFICULTY && keydata.action == MLX\_PRESS) {  
 if (keydata.key == MLX\_KEY\_LEFT) {  
 cg->game\_difficulty--;  
 if (cg->game\_difficulty == -1)  
 cg->game\_difficulty = DIFFICULTY\_LEN - 1;  
 } else if (keydata.key == MLX\_KEY\_RIGHT) {  
 cg->game\_difficulty++;  
 cg->game\_difficulty %= DIFFICULTY\_LEN;  
 }  
 }  
}

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-01-3023-40-52-ezgif.com-video-to-gif-converter.gif)

Now because the tutorial is getting very very long, the actual game I will publish in another blog post. I hope you enjoyed so far!  
  
Thanks a lot to CODAM for creating this awesome library!

[⬇ images.zip](https://evil-penguin.com/content/files/2026/11/images.zip)

[⬇ mlx42\_intro.zip](https://evil-penguin.com/content/files/2026/11/mlx42%5Fintro.zip)