Skip to content
W2.Wizard edited this page Feb 1, 2022 · 24 revisions

Images

Images are essentially just OpenGL textures with a simple to use interface. They store a buffer of pixels and are then displayed to the window.

How do they work?

In MLX, the behaviour of the image functions work almost similarly to minilibx.

An image on its own is very simple:

typedef struct s_mlx_image
{
    const uint16_t    width;
    const uint16_t    height;
    uint8_t	      *pixels;
    t_mlx_instance    *instances;
    uint16_t	      count;
    void	      *context;
}   t_mlx_image;

It holds the width, height and pixels. However images can have instances, that is, copies of itself that share the same pixel buffer. This makes it easy to make for instance, a row of trees. They all hold the same pixel information, just different locations.

instances is simply an array of each individual instance, they hold the x, y and z positions of the instance,count tells us the element count of this array.

All that is needed is to call the mlx_image_to_window function to create a new copy/instance. However its also possible to move the location of individual images quite easily:

// Modify the x & y position of an already existing instance.
img->instances[0].x += 5;
img->instances[0].y += 5;

Extra

All that is needed is to simply change the values and MLX will handle the rest of updating the position. Internally this is via a render queue, anytime the mlx_image_to_window function is used, a new entry is added into a linked list. Every frame MLX will iterate over this linked list and execute a drawcall to draw that image onto the window.

An improvement to this rendering could be to add batched rendering.

Functions

// Creates a whole new image.
t_mlx_image	*mlx_new_image(t_mlx *mlx, uint16_t width, uint16_t height)
// Creates a new instance/copy of an already existing image.
void	mlx_image_to_window(t_mlx_image *img, int32_t x, int32_t y, int32_t z)
// Deletes an image and removes it from the render queue.
void	mlx_delete_image(t_mlx *mlx, t_mlx_image *image)

MLX42 works similarly to minilibx in regards to how images are drawn onto the window.

Example

TODO

Clone this wiki locally