Skip to main content

Program Structure

Fract’ol is built around a core event-driven architecture using the MiniLibX graphics library. The program follows a clear initialization, rendering, and event loop pattern.

Main Components

The application consists of four primary modules:
  1. Initialization - Sets up MLX connection, window, and image buffer
  2. Rendering - Calculates and draws fractal pixels
  3. Event System - Handles keyboard and mouse interactions
  4. Mathematical Engine - Complex number operations and coordinate mapping

Program Flow

The execution flow from main.c follows this sequence:
main.c:15-39
int main(int argc, char **argv)
{
    t_fractol fractol;

    if ((argc == 2 && !ft_strncmp(argv[1], "Mandelbrot"))
        || (argc == 4 && !ft_strncmp(argv[1], "Julia")))
    {
        fractol.name = argv[1];
        if (!ft_strncmp(fractol.name, "Julia"))
        {
            fractol.julia_x = atoi_plus(argv[2]);
            fractol.julia_y = atoi_plus(argv[3]);
        }
        fractol_init(&fractol);
        fractol_render(&fractol);
        mlx_loop(fractol.mlx_connection);
    }
    else
    {
        write (1, "Please enter: \n\t\"./fractol Mandelbrot\"", 39);
        write (1, "\n\t or\n\t", 7);
        write (1, "\"./fractol Julia <value_1> <value_2>\"\n", 39);
        exit(EXIT_FAILURE);
    }
}

Execution Phases

Module Relationships

Data Flow

  1. main.c creates the t_fractol structure and passes it to all subsystems
  2. fractol_init.c populates the structure with MLX pointers and initial values
  3. fractol_render.c reads the structure to render pixels, using math_utils.c for calculations
  4. events.c modifies structure fields (zoom, shift, iterations) and triggers re-renders

Rendering Pipeline

For each pixel at screen coordinates (x, y):
  1. Map to complex plane: Convert pixel coordinates to complex numbers using map()
  2. Apply transformations: Account for zoom and pan (shift_x, shift_y)
  3. Iterate fractal formula: Use sqare_complex() and sum_complex() up to iterations_definition
  4. Check escape: If magnitude exceeds escape_value, pixel escapes
  5. Color assignment: Map iteration count to psychedelic color palette
  6. Write pixel: Store color value in image buffer at calculated offset
See handle_pixel implementation for details.

Memory Management

The program manages several heap-allocated resources:
  • MLX connection: Freed on exit via close_handler()
  • MLX window: Destroyed before freeing connection
  • Image buffer: Destroyed before window destruction
  • Display connection: Destroyed after window cleanup
The cleanup sequence in close_handler() (events.c:15-24) ensures proper resource deallocation:
mlx_destroy_image(fractol->mlx_connection, fractol->img.img_ptr);
mlx_destroy_window(fractol->mlx_connection, fractol->mlx_window);
mlx_destroy_display(fractol->mlx_connection);
free(fractol->mlx_connection);

Configuration Constants

Defined in fractol.h:24-25:
  • WIDTH: 2000 pixels
  • HEIGHT: 1500 pixels
Color palette includes 16 psychedelic colors (lines 27-44), with primary rendering using:
  • PSYCHEDELIC_LIME (0x00FF00) to PSYCHEDELIC_MINT (0x98FF98) gradient
  • WHITE (0xFFFFFF) for points in the set

Build docs developers (and LLMs) love