-
Notifications
You must be signed in to change notification settings - Fork 0
Main
This is the main function, everything else is called from here.
int main (int argc, char **argv)
{
t_var *var;
if (!(argc == 2 || argc == 3))
ft_exit_fail("Wrong number of arguments (main.c)");
var = get_var();
init(argv[1], var);
if (argc == 2)
game_option(var);
else if (argc == 3 && ft_strcmp(argv[2], "--save") == 1)
screenshot_option(var);
else
ft_exit_fail("Invalid argument (main.c)");
}
t_var *var is a pointer to the main struct, it contains pointers to all the other structs and the values for the speed of movement and rotation of the player.
typedef struct s_var
{
t_id *id;
t_file *file;
t_images *images;
t_key *key;
double mov_speed;
double rotate_speed;
t_ray *ray;
t_sprite_ray *spr_ray;
} t_var;The get_var function generates the pointer to var and stores it internally as a static variable, so the function always returns the same pointer. Note that the var function remains allocated in the stack.
t_var *get_var(void)
{
static t_var var;
return (&var);
}
The init function reads and parses the .cub file which should be passed as the first argument (argv1). You can see this function in detail on the init page .
init(argv[1], var);When only one argument, the program runs the game normally
if (argc == 2)
game_option(var);void game_option(t_var *var)
{
var->id->win = mlx_new_window(var->id->mlx,
var->file->params->resolution_x,
var->file->params->resolution_y,
"cub3D");
if (var->id->win == NULL)
ft_exit_fail("Failed to open new window (init.c)");
zero_values(var);
mlx_hook(var->id->win, 33, 1L<<17, &ft_exit_success, var);
mlx_hook(var->id->win, 2, 1L<<0, &key_pressed, var->key);
mlx_hook(var->id->win, 3, 1L<<1, &key_released, var->key);
mlx_loop_hook(var->id->mlx, &actualize, var);
mlx_loop(var->id->mlx);
}First, a window is created. The window’s id, a void* variable, is stored in the id struct. Its dimensions are the ones specified on the .cub file and its title is “cub3D”.
var->id->win = mlx_new_window(var->id->mlx,
var->file->params->resolution_x,
var->file->params->resolution_y,
"cub3D");
if (var->id->win == NULL)
ft_exit_fail("Failed to open new window (init.c)");The zero_values function initializes some variables.
zero_values(var);void zero_values(t_var *var)
{
var->key->a = 0;
var->key->w = 0;
var->key->s = 0;
var->key->d = 0;
var->key->left = 0;
var->key->right = 0;
var->key->esc = 0;
var->mov_speed = 0.05;
var->rotate_speed = 0.02;
ft_bzero(var->spr_ray->sprite_order,
var->file->map->num_sprites);
ft_bzero(var->spr_ray->wall_z, var->file->params->resolution_x);
}This hook manages the destroy notify event, triggered when the window is closed by clicking the ‘X’. When the event is triggered, the ft_exit_success function is called, you can see it in detail on the exit page.
mlx_hook(var->id->win, 33, 1L<<17, &ft_exit_success, var);This hook manages the key pressed event, triggered while a key is being pressed.
mlx_hook(var->id->win, 2, 1L<<0, &key_pressed, var->key);This hook manages the key released event, triggered when a key is released.
mlx_hook(var->id->win, 3, 1L<<1, &key_released, var->key);When any of these two events are triggered, the key_pressed and key_released functions, respectively, are called. You can see them in detail on the input page.
This hook runs constantly and actualizes the rendered image by calling the actualize function, you can see it in detail on the render page.
mlx_loop_hook(var->id->mlx, &actualize, var);Event hooks info here
This loop maintains the connection with the mlx graphic server.
mlx_loop(var->id->mlx);Now, back to the main function. If the program receives two arguments and the last one is the “—save” option, the program takes a screenshot of the first rendered image and saves it on a .bmp file. If the second argument is something other than “—save”, the program gives an error message and finishes.
else if (argc == 3 && ft_strcmp(argv[2], "--save") == 1)
screenshot_option(var);
else
ft_exit_fail("Invalid argument (main.c)");In the screenshot_option function, the first three functions render an image of the game (you can see them in detail on the render and sprites pages), and the last one creates the .bmp file (you can see it in detail on the bmp page).
void screenshot_option(t_var *var)
{
render(var->file, var->ray, var->images, var->spr_ray);
order_sprites(var->spr_ray->sprite_order, var->file->map);
render_sprites(var);
create_bmp(var->file->params, var->images->screen->addr);