-
Notifications
You must be signed in to change notification settings - Fork 66
Basics
Starting off MLX is very straight forward, once you followed the instructions on the README.
In this section we will gover basic window creation and making an image move, which could serve as a basis for making a very simple 2D game!
To make a window there are two components, initilization and the loop.
Initilization leads to the creation of the mlx handle which contains relevant information regarding the graphics interface.
typedef struct s_mlx
{
void *window; // Window pointer, only really relevant to MLX itself.
void *context; // Abstracted data only relevant to MLX & OpenGL.
int32_t width; // The current width of the window, gets updated on resize.
int32_t height; // The current height of the window, gets updated on resize.
double delta_time; // The time it took to draw the last frame, useful to interpolations.
} t_mlx;
Internally mlx_init()
does a whole bunch of stuff, mainly it creates the OpenGL window hints, creates a window context and also compiles the shaders. It also declares the vertexattribute layout (Describes what kind of data vertices store) in the case of MLX only the vertex position
and uv
coordinates matter. Vertex color information is just redundant in this case. Finally it also creates the vertex buffer object and array.
The reason the window pointer exists outside of the context object is mainly for convinience of the developer of MLX, most other stuff like the VBO
, VAO
& Shaderprogram
are stored in the context object.
#include "MLX42/MLX42.h"
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 256
#define HEIGHT 256
int32_t main(void)
{
t_mlx *mlx;
mlx = mlx_init(WIDTH, HEIGHT, "MLX42", true);
if (!mlx)
exit(EXIT_FAILURE);
mlx_loop(mlx);
mlx_terminate(mlx);
return (EXIT_SUCCESS);
}
To learn more about images go to the image section.
#include "MLX42/MLX42.h"
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 256
#define HEIGHT 256
static t_mlx_image *g_img;
int32_t main(void)
{
t_mlx *mlx;
mlx = mlx_init(WIDTH, HEIGHT, "MLX42", true);
if (!mlx)
exit(EXIT_FAILURE);
g_img = mlx_new_image(mlx, 128, 128) // Creates a new image.
mlx_image_to_window(mlx, g_img, 0, 0); // Adds an image to the render queue.
mlx_loop(mlx);
mlx_delete_image(mlx, g_img); // Once the application request an exit, cleanup.
mlx_terminate(mlx);
return (EXIT_SUCCESS);
}
#include "MLX42/MLX42.h"
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 256
#define HEIGHT 256
static t_mlx_image *g_img;
int32_t main(void)
{
t_mlx *mlx;
mlx = mlx_init(WIDTH, HEIGHT, "MLX42", true);
if (!mlx)
exit(EXIT_FAILURE);
g_img = mlx_new_image(mlx, 128, 128) // Creates a new image.
mlx_image_to_window(mlx, g_img, 0, 0); // Adds an image to the render queue.
mlx_putpixel(g_img, 64, 64, 0xFFFFFF00); // Single white pixel in the middle.
mlx_loop(mlx);
mlx_delete_image(mlx, g_img); // Once the application request an exit, cleanup.
mlx_terminate(mlx);
return (EXIT_SUCCESS);
}