> ## 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.

# How I run my cub3d in the web
- URL: https://evil-penguin.com/how-i-run-my-cub3d-in-the-web/
- Published: 2025-09-05T03:34:11.000Z
- Updated: 2026-11-16T23:01:26.000Z
- Description: This walk-through will show you how I managed to run my cub3d (in C with mlx) into the web.
- Author: evilpenguin

This is how my cub3d structure looks like. This is more less how I delivered the project.

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-04_12-51-34.png)

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

The cube executable must be provided a map which is not ideal. We will fix this later, we can simply add a small menu screen where you can choose a level. But for now I will hard code the map, so instead of passing argv\[1\] wherever in you code, just pass a hardcoded string. And prevent the program from displaying an error when no arguments are passed.

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-04_12-56-37.png)

Here is how I did it:

![](https://evil-penguin.com/content/images/2026/11/cube-side-to-sidecf.png)

Now I can launch the game simply with:./cube3d  
Fortunately I didn't include <mlx.h> in all my source files, instead I was using a centralized header "cub3d.h", so I can simlpy switch from ***mlx.h to MLX42.h***.  
  
The next step is to clone the MLX42 library and compile it:  
  
git clone https://github.com/codam-coding-college/MLX42  
cd MLX42  
mkdir build  
cmake -B build  
Then you can run the Makefile in the build directory to make the library   
  
cd build  
make  
Now we can try to compile our project with MLX42 to see how many errors we get:  
  
I had to make sure my makefile included the following:  
  
\-LMLX42/build -lmlx42 -ldl -lglfw -lm -pthread # linking mlx42 lib  
\-IMLX42/include # linking MLX42 headers  
And I also made sure to replace "mlx.h" by "MLX42/MLX42.h"

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-05_03-03-57.png)

We see: *mlx\_destroy\_image & mlx\_put\_image\_to\_window* which are supported by mlx42 but not by MLX42.Instead MLX42 provides:  
  
  
mlx\_delete\_imagemlx\_image\_to\_window  
But of course these functions must receive the mlx\_t\* mlxwhich is not present in our current setup, although mlx.h had something similar, which is a **void \*** when you call *mlx\_init().*  
  
Another small detail, the origina mlx.h normally requires to track two(2) pointers. After calling mlx\_init and also after calling mlx\_new\_window. With mlx42.h we don't need to care about generating a new window, since this is an abstraction in the library code, and the window is not needed explicitly, we only heve the mlx\_t \* pointer.  
  
You can read more about it here:  
[https://github.com/codam-coding-college/MLX42/wiki/Basics#main](https://github.com/codam-coding-college/MLX42/wiki/Basics?ref=evil-penguin.com#main)  
  
For this reason I will modify my structure, which I used to store the mlx\_ptr...  
 typedef struct s\_window  
{  
 void \*mlx\_ptr;  
 void \*win\_ptr;  
 int height;  
 int width;  
} t\_window;Becomes:  
  
typedef struct s\_window  
{  
 mlx\_t \*mlx;  
 int height;  
 int width;  
} t\_window;  
  
I had a function which created my *t\_window*:  
  
win = new\_window(WIN\_H, WIN\_W, "cub3D");  
Let's modify this function to make sure it complies with the new data structure.

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

Let's just continue with the main to clean the main and go back to the compilation errors.

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-05_03-21-41.png)

We changed *new\_window,* so it will properly setup our structure, but we are using the **win->win\_ptr & win->mlx\_ptr** which are not present anymore.  
Instead we will implement the following functions:  
  
bool mlx\_loop\_hook(mlx\_t\* mlx, void (\*f)(void\*), void\* param);  
void mlx\_key\_hook(mlx\_t\* mlx, mlx\_keyfunc func, void\* param);  
void mlx\_close\_hook(mlx\_t\* mlx, mlx\_closefunc func, void\* param);  
\*Note: (I know my code is not pretty, I was doing this project while learning as a noob, which is a great example to show any project can be easily converted to run on the web!)Now our main looks compatible with MLX42, *init\_game* which I call in the main is not related to anything with mlx, on the other hand *init\_game\_window* does, so we will have a look at that function.

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-05_03-30-38.png)

MLX42 provides already an out of the box mlx\_image\_t structure:  
See more here: [https://github.com/codam-coding-college/MLX42/wiki/Images#images-how-do-they-work](https://github.com/codam-coding-college/MLX42/wiki/Images?ref=evil-penguin.com#images-how-do-they-work)  
  
So we need to modify however we were doing the image management. We NO longer will need the addr and to be honest we don't need actually anything besides the mlx\_image\_t, but the idea here is just to make the mlx code work, so we can just remove the addr and img\_ptr, even if the other members become redundant.  
  
And later we will modify the pieces of code where we were using the addr, for example to get a pixel and it's color.  
  
The mlx\_new\_image function is the same on mlx.h and MLX42.h so no need to change anything there except for the pointer, which now is ***mlx***.  
  
![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-05_03-36-34.png)

Finally *mlx\_put\_image\_to\_window* doesn't exist in MLX42 but a very similar one does:  
  
int32\_t mlx\_image\_to\_window(mlx\_t\* mlx, mlx\_image\_t\* img, int32\_t x, int32\_t y);This function is actually way more simpler than mlx.h one, we just need to remove all pointers we were passing and only provide the mlx\_image\_t pointer.

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-05_04-03-03.png)

Finally we can return to the compilation errors we saw earlier...  
  
mlx\_destroy\_image(conf->img\_game->win->mlx\_ptr, conf->img\_game->img\_ptr);  
mlx\_put\_image\_to\_window(conf->img\_game->win->mlx\_ptr,  
 conf->img\_game->win->win\_ptr, img\_game->img\_ptr, 0, 0);  
We have similar functions in MLX42.h  
  
void mlx\_delete\_image(mlx\* mlx, mlx\_image\_t\* image)  
  
int32\_t mlx\_image\_to\_window(mlx\_t\* mlx, mlx\_image\_t\* img, int32\_t x, int32\_t y);After applying all fixes we can recompile to continue fixing more errors:

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-05_04-03-54.png)

Now it complains about the return type of the callback functions, let's have a look:  
  
int handle\_key\_up(int key\_code, void \*params);  
int handle\_key\_down(int key\_code, void \*params);  
int exit\_game(t\_config \*conf);  
int update(t\_config \*conf);  
As you might already know, MLX42.h callbacks should return void. Read more about it here: [https://github.com/codam-coding-college/MLX42/wiki/Hooks](https://github.com/codam-coding-college/MLX42/wiki/Hooks?ref=evil-penguin.com)  
  
Let's change then the return type annotations to void.  
  
Also the key hook receives a custom type instead of just an int, we must also change this.mlx\_key\_data\_t keydataAlso MLX42.h doesn't allow void \* type shadowing, and you must declare your callbacks data pointer as void \*, but this is OK you can just simply convert the void\* back to the type you need.  
  
For example if my update function was this:  
  
void update(t\_config \*conf);  
It becomes:  
  
void update(void \*ptr);  
// Inside the function you can retrieve the pointer with a casting  
{  
 t\_config \*conf = (t\_config\*)ptr;  
 if (!ptr) return;  
 ...  
}  
  
  
Now to get an image color I had to customise the following functions, instead of using addr, bpp and lin length to get the pixel color, we do the following:  
  
static inline bool in\_bounds(const mlx\_image\_t\* img, uint32\_t x, uint32\_t y) {  
 return x < img->width && y < img->height;  
}  
  
unsigned int get\_texture\_color(t\_image \*texture, int x, int y)  
{  
 if (!in\_bounds(texture->img\_ptr, x, y)) return 0; // or handle error  
 const uint8\_t\* p = &texture->img\_ptr->pixels\[(y \* texture->img\_ptr->width + x) \* 4\];  
 const uint32\_t b = p\[0\];  
 const uint32\_t g = p\[1\];  
 const uint32\_t r = p\[2\];  
 const uint32\_t a = p\[3\];  
 return (a << 24) | (r << 16) | (g << 8) | b;  
}  
Also what used to be my *put\_pixel\_img:*  
  
void put\_pixel\_img(t\_image \*data, int x, int y, int color)  
{  
char \*dst;  
  
dst = data->addr + (y \* data->line\_len + x \* (data->bpp / 8));  
\*(unsigned int \*) dst = color;  
*}*  
became:  
  
void put\_pixel\_img(mlx\_image\_t\* img, int x, int y, uint32\_t color)  
{  
 if (x < 0 || y < 0 || x >= (int)img->width || y >= (int)img->height)  
 return;  
  
 uint8\_t\* p = &img->pixels\[(y \* img->width + x) \* 4\];  
  
 // unpack TRGB (0xAARRGGBB) into BGRA little-endian bytes  
 p\[0\] = (uint8\_t)(color & 0xFF); // B  
 p\[1\] = (uint8\_t)((color >> 8) & 0xFF); // G  
 p\[2\] = (uint8\_t)((color >> 16) & 0xFF);// R  
 p\[3\] = (uint8\_t)((color >> 24) & 0xFF);// A  
}Finally just fix small compilation errors and you will be able to run the program! :)  
  
  
I prepared a raycasting interactive website for you to try out!  
Reach out if you have issues setting up your cub3d! :D GL and hf!

# [https://pulgamecanica.github.io/raycasting\_mlx42/web/demo.html](https://pulgamecanica.github.io/raycasting%5Fmlx42/web/demo.html?ref=evil-penguin.com)

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-09-05_05-33-57-1.png)