Fractol 1.0
A fractal visualization project implemented in C.
Loading...
Searching...
No Matches
main.c File Reference

Entry point for the Fractol application. More...

#include "fractol.h"
Include dependency graph for main.c:

Go to the source code of this file.

Functions

static int is_valid_number (const char *str)
 Validates if a string represents a valid number.
 
static void exit_with_error (const char *message, char *argv[])
 Prints an error message and terminates the program.
 
static void validate_arguments (int argc, char *argv[])
 Validates the command-line arguments for fractal rendering.
 
static void initialize_window (t_data *data, char *argv[])
 Initializes the MLX42 rendering environment.
 
int main (int argc, char *argv[])
 Main entry point for the fractal rendering program.
 

Detailed Description

Entry point for the Fractol application.

This file contains the main function and orchestrates the entire fractal rendering process. It handles:

  • Command-line argument validation to ensure correct input.
  • Initialization of the fractal type and parameters (Julia or Mandelbrot)
  • Setuup of the rendering environment using the MLX42 graphics liibrary, including the creation of the window and image bufffer.
  • Fractal rendering based on the user-specified parameters.
  • Configuration of interactivee event hooks for keyboard and scroll inputs.
  • Continuous updates through the main event loop.

The program terminates gracefully on completion or reports error for invalid inputs.

Definition in file main.c.

Function Documentation

◆ exit_with_error()

static void exit_with_error ( const char *  message,
char *  argv[] 
)
static

Prints an error message and terminates the program.

This function displays an error, the correct usage off the program, and list of available fractal types with examples. It then terminates the program with a failure status.

Parameters
messageError message to display.
argvCommand-line arguments, used to show the program name in usage examples.

Definition at line 87 of file main.c.

88{
89 fr_putstr_fd(LIGHT_RED "Error: ", 2);
90 fr_putstr_fd((char *)message, 2);
91 fr_putstr_fd("\n" RESET, 2);
93 fr_putstr_fd(argv[0], 2);
94 fr_putstr_fd(" <fractal_type> [parameters]\n" RESET, 2);
95 fr_putstr_fd(LIGHT_RED "Available fractal types:\n" RESET, 2);
96 fr_putstr_fd(LIGHT_GOLD "\tmandelbrot" RESET, 2);
97 fr_putstr_fd("\t\tDisplays the Mandelbrot set.\n", 2);
98 fr_putstr_fd(LIGHT_GOLD "\tjulia [c_re c_im]" RESET, 2);
99 fr_putstr_fd("\tDisplays the Julia set.\n", 2);
100 fr_putstr_fd(LIGHT_RED "Example: " LIGHT_GREEN, 2);
101 fr_putstr_fd(argv[0], 2);
102 fr_putstr_fd(" julia 0.285 -0.01\n" RESET, 2);
103 exit(EXIT_FAILURE);
104}
int fr_putstr_fd(char *s, int fd)
Writes a string to a file descriptor.
Definition utils.c:109
#define LIGHT_GREEN
Definition fractol.h:197
#define LIGHT_RED
Definition fractol.h:195
#define RESET
Definition fractol.h:194
#define LIGHT_GOLD
Definition fractol.h:196
Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialize_window()

static void initialize_window ( t_data data,
char *  argv[] 
)
static

Initializes the MLX42 rendering environment.

This function sets up the environment for rendering fractals by:

  • Creating an MLX42 window with thee given title.
  • Allocating an image buffer for rendering.
  • Setting initial offsets and zoom level for the fractal.

If Julia set parameters (c[COMPLEX_RE] or c[COMPLEX_IM]) are outside the range [-2.0, 2.0], it issues a warning about potential visual effects. The function terminates the program in case of initialization failures.

Parameters
dataPointer to the data structure containing fractal information, offsets, zoom, and MLX42 resources.
argvArray of argument strings, where argv[1] is used as the window title.

Definition at line 167 of file main.c.

168{
169 if ((data->c[COMPLEX_RE] > 2.0 || data->c[COMPLEX_RE] < -2
170 || data->c[COMPLEX_IM] > 2.0 || data->c[COMPLEX_IM] < -2))
171 {
172 fr_putstr_fd(LIGHT_GOLD "Warning: The values for Julia are", 1);
173 fr_putstr_fd(" outside the common range [-2.0, 2.0]\n", 1);
174 fr_putstr_fd(RESET "This may produce unusual or", 1);
175 fr_putstr_fd(" less interesting fractals.\n", 1);
176 }
177 data->mlx = mlx_init(WIDTH, HEIGHT, argv[1], false);
178 if (!data->mlx)
179 exit (EXIT_FAILURE);
180 data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
181 if (!data->img)
182 {
183 mlx_terminate(data->mlx);
184 exit (EXIT_FAILURE);
185 }
186 if (mlx_image_to_window(data->mlx, data->img, 0, 0) == -1)
187 {
188 mlx_terminate(data->mlx);
189 exit (EXIT_FAILURE);
190 }
191 data->offset[OFFSET_X] = 0.0;
192 data->offset[OFFSET_Y] = 0.0;
193 data->zoom = 1.0;
194}
#define WIDTH
Definition fractol.h:188
@ OFFSET_Y
Definition fractol.h:35
@ OFFSET_X
Definition fractol.h:34
@ COMPLEX_IM
Definition fractol.h:53
@ COMPLEX_RE
Definition fractol.h:52
#define HEIGHT
Definition fractol.h:189
double offset[2]
Definition fractol.h:95
mlx_image_t * img
Definition fractol.h:97
mlx_t * mlx
Definition fractol.h:96
double c[2]
Definition fractol.h:94
double zoom
Definition fractol.h:93
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_valid_number()

static int is_valid_number ( const char *  str)
static

Validates if a string represents a valid number.

This function checks iff the input string is a valid number, considering:

  • Optional leading spaces or tabs.
  • An optional sign (+/-).
  • At least one digit.
  • At most one decimal point with digits before or after it. Invalid cases include multiple decimal points, invalid characters, or empty strings.
Parameters
strInput string to validate
Returns
1 if the string is a valid number, 0 otherwise.

Definition at line 47 of file main.c.

48{
49 int i;
50 int valid_status[2];
51
52 i = 0;
53 valid_status[HAS_DIGIT] = 0;
54 valid_status[HAS_POINT] = 0;
55 while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
56 i++;
57 if (str[i] == '-' || str[i] == '+')
58 i++;
59 while (str[i] >= '0' && str[i] <= '9')
60 {
61 valid_status[HAS_DIGIT] = 1;
62 i++;
63 }
64 if (str[i] == '.')
65 {
66 valid_status[HAS_POINT] = 1;
67 i++;
68 }
69 if (valid_status[HAS_POINT] && (str[i] < '0' || str[i] > '9'))
70 return (0);
71 while (str[i] >= '0' && str[i] <= '9')
72 i++;
73 return (valid_status[HAS_DIGIT] && str[i] == '\0');
74}
@ HAS_POINT
Definition fractol.h:62
@ HAS_DIGIT
Definition fractol.h:61
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main entry point for the fractal rendering program.

This function orchestrates the entire fractal rendering process by:

  • Validating the command-line arguments to ensure correct fractal type and parameters.
  • Initializing the fractal type, including Julia parameters if applicable.
  • Setting up the MLX42 rendering environment, including the window and image buffer.
  • Rendering the chosen fractal based on the input parameters.
  • Configuring event hooks for used interaction (keyboard and scroll events).
  • Starting the main event loop for continuous interaction and updates.
Parameters
argcNumber of arguments passed to the program.
argvArray of argument strings, including the program name and input parameters.
Returns
Returns EXIT_SUCCESS on successful execution, or terminates on error.

Definition at line 215 of file main.c.

216{
217 t_data data;
218 char *error;
219
220 validate_arguments(argc, argv);
221 if (ft_strcmp(argv[1], "julia") == 0)
222 {
223 error = "Julia parameters must be valid numbers.";
224 data.fractal_type = JULIA;
225 data.c[COMPLEX_RE] = ft_atod(argv[2]);
226 data.c[COMPLEX_IM] = ft_atod(argv[3]);
227 if (data.c[COMPLEX_RE] > INT_MAX || data.c[COMPLEX_RE] < INT_MIN)
228 return (exit_with_error(error, argv), 1);
229 if (data.c[COMPLEX_IM] > INT_MAX || data.c[COMPLEX_IM] < INT_MIN)
230 return (exit_with_error(error, argv), 1);
231 }
232 else if (ft_strcmp(argv[1], "mandelbrot") == 0)
234 initialize_window(&data, argv);
235 render_fractal(&data);
236 mlx_key_hook(data.mlx, handle_key, &data);
237 mlx_scroll_hook(data.mlx, handle_scroll, &data);
238 mlx_loop(data.mlx);
239 mlx_terminate(data.mlx);
240 return (EXIT_SUCCESS);
241}
void render_fractal(t_data *data)
Renders the chosen fractal on the screen.
Definition calcs.c:108
@ MANDELBROT
Definition fractol.h:25
@ JULIA
Definition fractol.h:26
int ft_strcmp(const char *s1, const char *s2)
Compares two strings.
Definition utils.c:51
void handle_key(mlx_key_data_t keydata, void *param)
Handles key inputs.
Definition utils.c:64
double ft_atod(const char *str)
Converts a string to a double.
Definition utils.c:19
void handle_scroll(double xdelta, double ydelta, void *param)
Handles scroll inputs.
Definition utils.c:91
static void exit_with_error(const char *message, char *argv[])
Prints an error message and terminates the program.
Definition main.c:87
static void validate_arguments(int argc, char *argv[])
Validates the command-line arguments for fractal rendering.
Definition main.c:122
static void initialize_window(t_data *data, char *argv[])
Initializes the MLX42 rendering environment.
Definition main.c:167
Data structure for fractal rendering.
Definition fractol.h:92
t_fractal_type fractal_type
Definition fractol.h:98
Here is the call graph for this function:

◆ validate_arguments()

static void validate_arguments ( int  argc,
char *  argv[] 
)
static

Validates the command-line arguments for fractal rendering.

This function checks that the user has specified a valid fractal type and the correct parameters. The supported fractals and their requirements are:

  • mandelbrot: No additional arguments.
  • julia: Requires exactly two valid numbers representing the real and imaginary part of the parameter.

If the arguments are invalid, the function calls exit_with_error() to display an appropiate error message and terminate the program.

Parameters
argcNumber of command-line arguments passed to the program.
argvArray of command-line argument strings.

Definition at line 122 of file main.c.

123{
124 char *e_msg[5];
125
126 e_msg[NO_TYPE] = "No fractal type specified.";
127 e_msg[INVALID_TYPE] = "Invalid fractal type. Use 'julia' or 'mandelbrot'.";
128 e_msg[JULIA_ARGS] = "Julia needs: 'julia <c_re> <c_im>'.";
129 e_msg[JULIA_NUM] = "Julia parameters must be valid numbers.";
130 e_msg[MANDELBROT_ARGS] = "Mandelbrot needs: 'mandelbrot'.";
131 if (argc < 2)
132 exit_with_error(e_msg[NO_TYPE], argv);
133 if (ft_strcmp(argv[1], "mandelbrot") == 0)
134 {
135 if (argc != 2)
136 exit_with_error(e_msg[MANDELBROT_ARGS], argv);
137 return ;
138 }
139 if (ft_strcmp(argv[1], "julia") == 0)
140 {
141 if (argc != 4)
142 exit_with_error(e_msg[JULIA_ARGS], argv);
143 if (!is_valid_number(argv[2]) || !is_valid_number(argv[3]))
144 exit_with_error(e_msg[JULIA_NUM], argv);
145 }
146 else
147 exit_with_error(e_msg[INVALID_TYPE], argv);
148}
@ JULIA_NUM
Definition fractol.h:73
@ INVALID_TYPE
Definition fractol.h:71
@ NO_TYPE
Definition fractol.h:70
@ JULIA_ARGS
Definition fractol.h:72
@ MANDELBROT_ARGS
Definition fractol.h:74
static int is_valid_number(const char *str)
Validates if a string represents a valid number.
Definition main.c:47
Here is the call graph for this function:
Here is the caller graph for this function: