Images management
Images are a very important tool in MacroLibX in order to embrace its full potential. These functions will allow you to read files directly into a image object. This is very useful for textures or sprites of course.
We’ve already seen here how to load images from disk and display them. Now we’ll see how to create empty images and modify/read pixels from any mlx images.
😶 Making an empty image
The MacroLibX gives a function to create a new image with a custom size.
It is called mlx_new_image
and is pretty fast forward :
But creating an empty image is useless if we cannot modify it, that’s what we are going to see right now.
✍️ Modify an image
To modify any image there is mlx_set_image_pixel
which is fairly simple to use :
If you try to modify a pixel that is not in the image (coordinates bellow 0 or outside the image) it will do nothing.
🔎 Reading an image
To read any image there is mlx_get_image_pixel
which is as simple to use as mlx_set_image_pixel
:
If you try to modify a pixel that is not in the image (coordinates bellow 0 or outside the image) it will return 0
.
⚠️ Troubleshooting ⚠️
If you run into glitches when writing or reading pixels from images you can turn off images optimisations by using make IMAGES_OPTIMIZED=false
when compiling the MacroLibX.