My own recreation of the MiniLibX library used by 42, using GLFW & glad, running on OpenGL 3.3. The goal of MLX42 is to replace the outdated and stale MiniLibX library.
For information and documentation about MLX42 check the wiki.
The main idea of MLX42 is to be a cross-platform graphics interface. In 42 everything runs (currently) on MacOS, but it's very useful to be able to work remotely on different machines. With MiniLibX this is not possible.
MLX42 will always adhere fully to the latest norme, if 42 creates a norme, it should also use it.
Almost all functions and types are fully documented giving you a clue as to how to approach and use the library. No more guessing and searching how something functions or is supposed to work.
A custom simple to use XPM-like format which has some minor differences to the XPM3 format.
MLX42 will try to always maintain and use the latest and stable version of OpenGL available.
Switching to MLX42 from MiniLibX is not a lot of work, most features present in MiniLibX are also present in MLX42, albeit with different prototypes.
In the very end a library is generated, compile your program with this library!
- Download MLX42
➜ ~ git clone https://github.com/W2Codam/MLX42.git- Install GLFW
➜ ~ brew update
➜ ~ brew install glfw- Compile MLX42
➜ ~ cd MLX42
➜ ~ make- Compile Program With normal brew you can now simply compile the program with:
➜ ~ gcc main.c mlx42.a -lglfw ...However, with 42Homebrew you have additionally specify the location of the library like here:
➜ ~ gcc main.c mlx42.a -I include -lglfw -L "/Users/$USER/.brew/opt/glfw/lib/"-
Download the binaries directly here.
-
If possible move the contents of
libandincludeof GLFW to/usr/local/liband/usr/local/includerespectively. If not possible, move the lib file to the root of MLX42 and move the GLFW directory in include to the include of MLX42. NOTE: For the lib choose the appropriate.a&.dylibfile depending on your architecture. -
Compile MLX42
➜ ~ cd MLX42
➜ ~ makeWhen compiling with the static library, directly you should compile your program like this:
➜ ~ gcc main.c mlx42.a libglfw3.a ... -framework Cocoa -framework OpenGL -framework IOKitElse, simply compile like this:
➜ ~ gcc main.c mlx42.a -lglfw ...- Run
In case of any security warnings or MacOS telling you it can't verify the author/developer, go to Settings > Security & Privacy.
There will be a pop-up at the bottom telling you that an application tried to run, click the option to let it run.
NOTE: This will not run with Windows Subsystem for Linux (WSL)!!!
- Download GLFW: Click Me!
- Extract GLFW, and to install, run the following commands:
➜ ~ sudo apt update
➜ ~ sudo apt install build-essential
➜ ~ sudo apt install cmake
➜ ~ sudo apt install xorg-devNOTE: For arch-linux you might also have to do sudo apt-get install glfw-x11 if available.
- Navigate to the extracted GLFW directory and run:
➜ ~ cmake -G "Unix Makefiles"
➜ ~ make
➜ ~ sudo make install
➜ ~ sudo apt-get install libx11-dev libglu1-mesa-dev freeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libgl1-mesa-glx libgl1-mesa-dev libglfw3-dev libglfw3- Download MLX42 & Build
➜ ~ git clone https://github.com/W2Codam/MLX42.git
➜ ~ cd MLX42
➜ ~ make- Create a
main.cfile, includeMLX42/MLX42.h, compile with:
-ldl -lglfw -lGL -lX11 -lpthread -lXrandr -lXi, make sure to also do-I <include_path>. At the very least-ldl -lglfware required.
- Run.
The systems below have not been tested yet.
-
Download & Install MinGW: Here!
-
Simply click continue, select whatever your choice is. Once reaching the MinGW Installation Manager select:
- mingw32-base
- mingw32-gcc-g++
-
Apply by going to
Installation > Apply Changes, after it's done, you may close the window. -
Download & Install CMake: Here!, use the installer. Simply select all default options.
-
Download & Install GnuWin: Here!
-
If you used all default options, add these paths to your SYSTEM Environment variables:
- C:\MinGW\bin
- C:\Program Files\CMake\bin
- C:\Program Files (x86)\GnuWin32\bin
-
Download GLFW: https://www.glfw.org/download.html
-
Open the terminal and type
cmake-gui, select the downloaded/extracted source folder, select any place you want the build output to be. -
Click on configure once and select the
MinGW Makefiles, then finish. -
Set the CMAKE_INSTALL_PREFIX to
C:/GLFW -
Click on configure again, and then generate.
-
Go to the build directory and do:
makemake install
- Go to the directory you assigned in Step 10. Copy the GLFW folder in the include folder to
C:\MinGW\include& copy the .a file in the lib folder toC:\MinGW\lib.
NOTE: As of now the build script for windows does not exist, compile by adding every c file manually.
- Compile your program with these flags:
-lglfw3-lopengl32-lgdi32
In the end you should have something like:
➜ ~ gcc main.c <Additional .c Files> mlx42.a -lglfw3 -lopengl32 -lgdi32- Run.
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.c :+: :+: */
/* +:+ */
/* By: W2Wizard <w2.wizzard@gmail.com> +#+ */
/* +#+ */
/* Created: 2022/01/31 00:40:08 by W2Wizard #+# #+# */
/* Updated: 2022/01/31 00:41:56 by W2Wizard ######## odam.nl */
/* */
/* ************************************************************************** */
#include "MLX42/MLX42.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <memory.h>
#define WIDTH 256
#define HEIGHT 256
t_mlx_image *g_img;
void hook(void *param)
{
t_mlx *mlx;
mlx = param;
if (mlx_is_key_down(param, MLX_KEY_ESCAPE))
mlx_quit(param);
if (mlx_is_key_down(param, MLX_KEY_UP))
g_img->instances[0].y -= 5;
if (mlx_is_key_down(param, MLX_KEY_DOWN))
g_img->instances[0].y += 5;
if (mlx_is_key_down(param, MLX_KEY_LEFT))
g_img->instances[0].x -= 5;
if (mlx_is_key_down(param, MLX_KEY_RIGHT))
g_img->instances[0].x += 5;
}
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);
memset(g_img->pixels, 255, g_img->width * g_img->height * sizeof(int));
mlx_image_to_window(mlx, g_img, 0, 0);
mlx_loop_hook(mlx, &hook, mlx);
mlx_loop(mlx);
mlx_terminate(mlx);
return (EXIT_SUCCESS);
}