Skip to content
W2.Wizard edited this page Jan 30, 2022 · 24 revisions

Images

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

How do they work?

An image on its own 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.

All that is needed it call the mlx_image_to_window function to create a new copy. 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;

All that is needed is to simply change the values and MLX will handle the rest of updating the position.

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)

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

Example

TODO

Clone this wiki locally