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) nothing will be done.
By using the extended mlx header you can access the mlx_set_image_region
function which allows you to modify a whole region of the image in a single function call.
Note that you have to make sure the pixels buffer you pass to mlx_set_image_region
is big enough for the given region.
🔎 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
.
By using the extended mlx header you can access the mlx_get_image_region
function which allows you to get a whole region of the image in a single function call.
Note that you have to make sure the pixels buffer you pass to mlx_get_image_region
is big enough for the given region.
⚠️ 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.