Skip to main content

Initialization Functions

fractol_init()

Initializes the MLX graphics system, creates the window and image buffer, and sets up event handlers.
fractol.h:86
void fractol_init(t_fractol *fractol);
fractol
t_fractol*
required
Pointer to the fractol structure to initialize. Must have name field set, and for Julia fractals, julia_x and julia_y must be set.

Implementation Details

From fractol_init.c:40-66:
void fractol_init(t_fractol *fractol)
{
    fractol->mlx_connection = mlx_init();
    if (fractol->mlx_connection == NULL)
        malloc_error();
    fractol->mlx_window = mlx_new_window(fractol->mlx_connection,
            WIDTH, HEIGHT, fractol->name);
    if (fractol->mlx_window == NULL)
    {
        mlx_destroy_display(fractol->mlx_connection);
        free(fractol->mlx_connection);
        malloc_error();
    }
    fractol->img.img_ptr = mlx_new_image(fractol->mlx_connection,
            WIDTH, HEIGHT);
    if (fractol->img.img_ptr == NULL)
    {
        mlx_destroy_window(fractol->mlx_connection, fractol->mlx_window);
        mlx_destroy_display(fractol->mlx_connection);
        free(fractol->mlx_connection);
        malloc_error();
    }
    fractol->img.pixel_ptr = mlx_get_data_addr(fractol->img.img_ptr,
            &fractol->img.bpp, &fractol->img.line_length, &fractol->img.endian);
    events_init(fractol);
    data_init(fractol);
}

Behavior

  1. MLX Connection: Calls mlx_init() to establish connection to X server
  2. Window Creation: Creates 2000x1500 window with fractal name as title
  3. Image Buffer: Allocates image buffer matching window dimensions
  4. Pixel Data: Retrieves direct memory address for pixel manipulation
  5. Event Registration: Calls events_init() to hook keyboard/mouse handlers
  6. Data Initialization: Calls data_init() to set default parameters
If any allocation fails, the function performs proper cleanup before calling malloc_error().

data_init()

Initializes fractal rendering parameters to default values.
fractol.h:87
void data_init(t_fractol *fractol);
fractol
t_fractol*
required
Pointer to the fractol structure to populate with default values.

Implementation

From fractol_init.c:21-28:
void data_init(t_fractol *fractol)
{
    fractol->escape_value = 4;
    fractol->iterations_definition = 30;
    fractol->shift_x = 0.0;
    fractol->shift_y = 0.0;
    fractol->zoom = 1;
}

Default Values

escape_value
double
Set to 4.0 - the squared magnitude threshold for escape condition (2² = 4)
iterations_definition
int
Set to 30 - maximum iterations before considering a point in the set
shift_x
double
Set to 0.0 - no horizontal pan offset
shift_y
double
Set to 0.0 - no vertical pan offset
zoom
double
Set to 1.0 - default zoom level (no magnification)

Rendering Functions

fractol_render()

Renders the fractal by calculating and coloring every pixel, then displays the result.
fractol.h:88
void fractol_render(t_fractol *fractol);
fractol
t_fractol*
required
Pointer to fully initialized fractol structure containing rendering parameters and MLX resources.

Implementation

From fractol_render.c:63-81:
void fractol_render(t_fractol *fractol)
{
    int x;
    int y;

    y = 0;
    while (y < HEIGHT)
    {
        x = 0;
        while (x < WIDTH)
        {
            handle_pixel(x, y, fractol);
            x++;
        }
        y++;
    }
    mlx_put_image_to_window(fractol->mlx_connection, fractol->mlx_window,
        fractol->img.img_ptr, 0, 0);
}

Behavior

  1. Iterates through all 2000×1500 = 3,000,000 pixels
  2. Calls handle_pixel() for each coordinate
  3. After all pixels are calculated, displays image buffer to window via mlx_put_image_to_window()
This function is called:
  • Once after initialization in main()
  • After every zoom event (mouse scroll)
  • After every pan event (arrow keys)
  • After iteration count changes (+/- keys)

handle_pixel()

Calculates the color for a single pixel by iterating the fractal formula.
fractol.h:89
void handle_pixel(int x, int y, t_fractol *fractol);
x
int
required
Screen x-coordinate (0 to WIDTH-1)
y
int
required
Screen y-coordinate (0 to HEIGHT-1)
fractol
t_fractol*
required
Pointer to fractol structure containing rendering parameters

Implementation

From fractol_render.c:37-61:
void handle_pixel(int x, int y, t_fractol *fractol)
{
    t_complex z;
    t_complex c;
    int       i;
    int       color;

    i = 0;
    z.x = (map(x, -2, +2, WIDTH) * fractol->zoom) + fractol->shift_x;
    z.y = (map(y, +2, -2, HEIGHT) * fractol->zoom) + fractol->shift_y;
    mandel_vs_julia(&z, &c, fractol);
    while (i < fractol->iterations_definition)
    {
        z = sum_complex(sqare_complex(z), c);
        if ((z.x * z.x) + (z.y * z.y) > fractol->escape_value)
        {
            color = map(i, PSYCHEDELIC_LIME, PSYCHEDELIC_MINT,
                    fractol->iterations_definition);
            my_pixel_put(x, y, &fractol->img, color);
            return;
        }
        i++;
    }
    my_pixel_put(x, y, &fractol->img, WHITE);
}

Algorithm Steps

  1. Coordinate Mapping: Convert screen pixel to complex plane coordinates
    • X maps from [0, WIDTH] to [-2, +2]
    • Y maps from [0, HEIGHT] to [+2, -2] (inverted for screen coordinates)
    • Apply zoom and shift transformations
  2. Fractal Selection: mandel_vs_julia() sets initial conditions
    • Julia: c = constant (julia_x, julia_y), z = pixel coordinate
    • Mandelbrot: c = pixel coordinate, z = (0, 0) implicitly
  3. Iteration Loop: Apply formula z = z² + c up to iterations_definition
  4. Escape Check: If |z|² > escape_value, point escapes
    • Color mapped from iteration count to gradient (LIME → MINT)
    • Early return after pixel write
  5. In-Set Points: If max iterations reached without escape, color WHITE

Event Handlers

These functions are registered as callbacks in events_init() (fractol_init.c:30-38):
static void events_init(t_fractol *fractol)
{
    mlx_hook(fractol->mlx_window, KeyPress, KeyPressMask,
        key_handler, fractol);
    mlx_hook(fractol->mlx_window, ButtonPress,
        ButtonPressMask, mouse_handler, fractol);
    mlx_hook(fractol->mlx_window, DestroyNotify, StructureNotifyMask,
        close_handler, fractol);
}

key_handler()

From events.c:26-44:
fractol.h:80
int key_handler(int keysym, t_fractol *fractol);
Supported Keys:
  • ESC: Exit program via close_handler()
  • Left/A: Pan left (decrease shift_x)
  • Right/D: Pan right (increase shift_x)
  • Up/W: Pan up (increase shift_y)
  • Down/S: Pan down (decrease shift_y)
  • +: Increase iterations by 5
  • -: Decrease iterations by 5
Re-renders after parameter changes.

mouse_handler()

From events.c:46-60:
fractol.h:79
int mouse_handler(int button, int x, int y, t_fractol *fractol);
Supported Buttons:
  • Button4 (scroll up): Zoom in (multiply zoom by 0.95)
  • Button5 (scroll down): Zoom out (multiply zoom by 1.05)
Re-renders after zoom change.

close_handler()

From events.c:15-24:
fractol.h:81
int close_handler(t_fractol *fractol);
Cleanup sequence:
  1. Destroy image buffer
  2. Destroy window
  3. Destroy display connection
  4. Free MLX connection
  5. Exit with EXIT_SUCCESS

Build docs developers (and LLMs) love