MinilibX Linux

repository·master·Indexed 19 days ago

https://github.com/42paris/minilibx-linux

A simple X-Window (X11R6) programming API written in C designed for beginners to learn X11 programming. It provides functions for window management, pixel and string rendering, image manipulation via raw memory access, and event handling through hooks for keyboard, mouse, and expose events.

Tokens
5.9K
Snippets
31
Records
39
Agent score
64%

What's inside minilibx-linux

  1. Install MinilibX on MacOS

    master

    MinilibX requires XQuartz to run on MacOS. After installing XQuartz via Homebrew, you must reboot your system for the changes to take effect. You can verify the X11 environment is working by running xeyes.

    brew install xquartz
    # Reboot your machine after installation
    xeyes
  2. Install MinilibX libraries and headers manually

    master

    MinilibX does not provide an automated installation script. To make the library and headers available globally, you should manually copy the files to standard system paths:

    • Libraries: Copy libmlx.a and/or libmlx_$(HOSTTYPE).a to /usr/X11/lib or /usr/local/lib.
    • Headers: Copy mlx.h to /usr/X11/include or /usr/local/include.
    • Man pages: Copy man/man3/mlx*.1 to /usr/X11/man/man3 or /usr/local/man/man3.
  3. Install MinilibX dependencies on Linux

    master

    MinilibX requires specific system packages to function. On Debian/Ubuntu-based systems, you can install the necessary dependencies using apt-get.

    Requirements:

    • gcc and make for compilation.
    • xorg for X11 include files.
    • libxext-dev for the XShm extension.
    • libbsd-dev for BSD utility functions.
    • A display environment supporting TrueColor (8, 15, 16, 24, or 32 bits depth).
    sudo apt-get install gcc make xorg libxext-dev libbsd-dev
  4. Compile MinilibX

    master

    To build the library, you can run either ./configure or make. Both commands perform the following steps:

    1. Run a few tests.
    2. Generate Makefile.gen.
    3. Automatically run make on the generated Makefile.gen.

    This process produces:

    • libmlx.a and libmlx_$(HOSTTYPE).a (the static libraries).
    • test/mlx-test (a test binary).
    ./configure
    # OR
    make
  5. How the X-Window system works with MiniLibX

    master

    MiniLibX operates on the X-Window model, which is a network-oriented graphical system. The architecture consists of two main entities:

    1. Your Software (Client): Sends drawing orders (e.g., "draw a pixel") and requests input to the X-Server.
    2. X-Server (Display): Manages the physical screen, keyboard, and mouse. It receives drawing orders and sends keyboard/mouse events back to your software.

    A network connection is established between these two to facilitate the exchange of drawing commands and input events.

  6. Encode colors for MiniLibX

    master

    Colors in MiniLibX are represented by a single integer. This integer is composed of three color components: Red (R), Green (G), and Blue (B), each with a value ranging from 0 to 255.

    To avoid endianness issues, ensure that the Blue byte is always the least significant byte. The integer structure is as follows:

    Bit RangeComponent
    31..24Unused/Other
    23..16Red (R)
    15..8Green (G)
    7..0Blue (B)
  7. Create and manage images in MiniLibX

    master

    MiniLibX allows you to create images in memory, manipulate their pixel data, and display them within a window.

    To create a blank image, use mlx_new_image. To load an image from an XPM file, use mlx_xpm_file_to_image. Once an image is created, you can display it in a window using mlx_put_image_to_window.

    Lifecycle:

    1. Create an image identifier using mlx_new_image or mlx_xpm_file_to_image.
    2. (Optional) Access the raw memory address via mlx_get_data_addr to draw custom pixels.
    3. Display the image in a window using mlx_put_image_to_window.
    4. Destroy the image when finished using mlx_destroy_image to free memory.
    void *img_ptr = mlx_new_image(mlx_ptr, width, height);
    // ... manipulate img_ptr ...
    mlx_put_image_to_window(mlx_ptr, win_ptr, img_ptr, x, y);
    mlx_destroy_image(mlx_ptr, img_ptr);
  8. Create and manipulate images in MiniLibX

    master

    To work with images in MiniLibX, you follow a lifecycle of creating an image in memory, accessing its raw memory address to manipulate pixels, and finally 'dumping' it to a window for display.

    Workflow

    1. Create: Use mlx_new_image to allocate an image buffer.
    2. Access Memory: Use mlx_get_data_addr to retrieve the pointer to the raw pixel data and metadata (bits per pixel, line size, and endianness).
    3. Modify: Calculate pixel offsets using the size_line and write color values to the memory address.
    4. Display: Use mlx_put_image_to_window to render the image at specific (x, y) coordinates in a window.
    5. Cleanup: Use mlx_destroy_image to free the image memory.
    /* Conceptual workflow */
    void *img = mlx_new_image(mlx_ptr, width, height);
    char *addr = mlx_get_data_addr(img, &bpp, &line_size, &endian);
    // ... manipulate pixels in addr ...
    mlx_put_image_to_window(mlx_ptr, win_ptr, img, x, y);
    mlx_destroy_image(mlx_ptr, img);
  9. Core MiniLibX functional areas

    master

    MiniLibX provides several categories of functions to manage graphical software:

    • Window Management: Functions like mlx_new_window allow you to create and manage windows.
    • Drawing: Functions like mlx_pixel_put allow you to draw directly inside a window.
    • Image Manipulation: Functions like mlx_new_image allow you to create and manipulate images.
    • Event Handling: Functions like mlx_loop are used to handle user inputs such as keyboard or mouse events.
  10. How the X-Window concept works in MiniLibX

    master

    MiniLibX operates on the X-Window model, which separates your software from the hardware management:

    1. Your Software: Sends drawing orders (e.g., "draw a pixel") and requests input data.
    2. X-Server (Display): Manages the physical screen, keyboard, and mouse.

    A network connection is established between your software and the X-Server. MiniLibX abstracts this connection, allowing you to send drawing commands and receive keyboard/mouse events through the connection identifier obtained via mlx_init().

  11. Link MiniLibX to your project

    master

    When compiling your software, you must link against the MiniLibX library and the necessary X11 libraries. Add the following flags to your linking command:

    -lmlx -lXext -lX11

    If the libraries are not in your standard library path, use the -L flag to specify their location.

    gcc main.c -lmlx -lXext -lX11