First shot on structures, First shot on option
option:
Added options for help, debug, save, file using bgoulard lib
structures:
Moved cub3d.h structures to cub3d_struct.h
Added color, dpoint, ipoint, tile, cli, error
Modified t_map, t_player to use previously mentioned struct :
ipoint, dpoint, tyle
This commit is contained in:
parent
be6038dcc8
commit
8ee1c5f85a
14 changed files with 580 additions and 79 deletions
38
sources/cleanups.c
Normal file
38
sources/cleanups.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cleanups.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/11/09 01:11:45 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d_struct.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static void cleanup_cli(t_cli *cli_ctx)
|
||||
{
|
||||
if (cli_ctx->file)
|
||||
free(cli_ctx->file);
|
||||
}
|
||||
|
||||
static void cleanup_map(t_map *map)
|
||||
{
|
||||
(void)map;
|
||||
}
|
||||
|
||||
static void cleanup_mlx(t_info *info)
|
||||
{
|
||||
(void)info;
|
||||
}
|
||||
|
||||
void cleanup_info(t_info *info)
|
||||
{
|
||||
cleanup_cli(&info->cli_ctx);
|
||||
cleanup_map(&info->map);
|
||||
cleanup_mlx(info);
|
||||
}
|
||||
|
|
@ -6,25 +6,45 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */
|
||||
/* Updated: 2024/10/31 12:01:18 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/11/09 01:43:06 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_string.h"
|
||||
|
||||
#include "cub3d.h"
|
||||
#include "message_error.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @brief Print the message error with red color !
|
||||
*
|
||||
* @param msg The detailled message
|
||||
*/
|
||||
void print_error(char *msg)
|
||||
const char *g_error_message[] = {
|
||||
"no error",
|
||||
"unknown error",
|
||||
|
||||
"could not open file",
|
||||
"could not read file",
|
||||
"bad file extension",
|
||||
"invalid file name",
|
||||
"missing file",
|
||||
"malloc error",
|
||||
"parse error",
|
||||
"cli error",
|
||||
"mlx error",
|
||||
};
|
||||
|
||||
void c3_perror(t_info *info)
|
||||
{
|
||||
write(2, BOLD_RED, strlen(BOLD_RED));
|
||||
write(2, "Error:\n", strlen("Error:\n"));
|
||||
write(2, RESET, strlen(RESET));
|
||||
write(2, RED, strlen(RED));
|
||||
write(2, msg, strlen(msg));
|
||||
write(2, RESET, strlen(RESET));
|
||||
if (info->last_error == NO_ERROR)
|
||||
return ;
|
||||
print_error(g_error_message[info->last_error]);
|
||||
}
|
||||
|
||||
void print_error(const char *msg)
|
||||
{
|
||||
ft_putstr_fd(BOLD_RED, STDERR_FILENO);
|
||||
ft_putstr_fd("Error:\n", STDERR_FILENO);
|
||||
ft_putstr_fd(RESET, STDERR_FILENO);
|
||||
ft_putstr_fd(RED, STDERR_FILENO);
|
||||
ft_putstr_fd(msg, STDERR_FILENO);
|
||||
ft_putstr_fd(RESET, STDERR_FILENO);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,15 +6,71 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */
|
||||
/* Updated: 2024/11/08 11:48:44 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/11/09 01:45:20 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
#include "cub3d_struct.h"
|
||||
#include "ft_string.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void dump_info(t_info *info)
|
||||
{
|
||||
printf("t_info:\n");
|
||||
printf("\tcli_ctx:\n");
|
||||
printf("\t\tfile: %s\n", info->cli_ctx.file);
|
||||
printf("\t\tdebug: %s\n", info->cli_ctx.debug ? "true" : "false");
|
||||
printf("\t\tsave: %s\n", info->cli_ctx.save ? "true" : "false");
|
||||
printf("\t\thelp: %s\n", info->cli_ctx.help ? "true" : "false");
|
||||
}
|
||||
|
||||
void check_err(t_info *info)
|
||||
{
|
||||
if (info->cli_ctx.file == NULL)
|
||||
return (info->last_error = MISSING_FILE_ERROR, (void)0);
|
||||
else if (ft_strlen(info->cli_ctx.file) < 5)
|
||||
return (info->last_error = NAME_FILE_ERROR, (void)0);
|
||||
else if (ft_strend_with(info->cli_ctx.file, FILE_EXTENSION) == false)
|
||||
return (info->last_error = EXTENSION_FILE_ERROR, (void)0);
|
||||
info->map.fd = open(info->cli_ctx.file, O_RDONLY);
|
||||
if (info->map.fd == -1)
|
||||
return (info->last_error = OPEN_FILE_ERROR, (void)0);
|
||||
}
|
||||
|
||||
void run_cub3d(t_info *info)
|
||||
{
|
||||
// code here
|
||||
(void)info;
|
||||
}
|
||||
|
||||
int main_cub3d(char *file_arg, t_info *info)
|
||||
{
|
||||
if (info->cli_ctx.help)
|
||||
return (cleanup_info(info), EXIT_SUCCESS);
|
||||
if (file_arg && info->cli_ctx.file == NULL)
|
||||
info->cli_ctx.file = ft_strdup(file_arg);
|
||||
check_err(info);
|
||||
if (info->last_error != NO_ERROR)
|
||||
return (c3_perror(info), cleanup_info(info), EXIT_FAILURE);
|
||||
if (info->cli_ctx.debug)
|
||||
(dump_info(info), printf("file_arg: %s\n", file_arg));
|
||||
run_cub3d(info);
|
||||
return (cleanup_info(info), EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (!ft_parse_args(argc, argv))
|
||||
return (1);
|
||||
return (0);
|
||||
t_info info;
|
||||
int parsed_args;
|
||||
|
||||
(void)argc;
|
||||
parsed_args = c3_options(&info, argc, argv);
|
||||
if (parsed_args == -1)
|
||||
return (EXIT_FAILURE);
|
||||
return (main_cub3d(argv[parsed_args], &info));
|
||||
}
|
||||
|
|
|
|||
40
sources/options.c
Normal file
40
sources/options.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* options.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/09 01:14:09 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/11/09 01:37:41 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d_struct.h"
|
||||
#include "cub3d_options.h"
|
||||
|
||||
#include "ft_args.h"
|
||||
#include "ft_args_types.h"
|
||||
#include "ft_string.h"
|
||||
|
||||
int c3_options(t_info *info, int argc, char *argv[])
|
||||
{
|
||||
int parsed_args;
|
||||
const t_opt opts[] = {
|
||||
{"file", 'f', c3_set_file, OPT_ARG | OPT_EQSIGN | OPT_OTHER},
|
||||
{"debug", 'd', c3_set_debug, 0},
|
||||
{"save", 's', c3_set_save, 0},
|
||||
{"help", 'h', c3_print_help, 0},
|
||||
// add more options here put the implementation in options_impl.c
|
||||
// if you want custom option see ft_args_types.h for more info
|
||||
// code example in it.
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
(void)argc;
|
||||
ft_bzero(info, sizeof(t_info));
|
||||
ft_setup_prog((const char * const *)argv);
|
||||
ft_set_opt_list(opts);
|
||||
parsed_args = ft_parse_args((const char **)argv, &(info->cli_ctx));
|
||||
return (parsed_args);
|
||||
}
|
||||
52
sources/options_impl.c
Normal file
52
sources/options_impl.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* options_impl.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/09 01:12:16 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/11/09 01:36:19 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d_struct.h"
|
||||
#include "ft_string.h"
|
||||
|
||||
void c3_set_file(void *usr_control_struct, const char *arg)
|
||||
{
|
||||
t_cli *cli_ctx;
|
||||
|
||||
cli_ctx = (t_cli *)usr_control_struct;
|
||||
cli_ctx->file = ft_strdup(arg);
|
||||
}
|
||||
|
||||
void c3_set_debug(void *usr_control_struct)
|
||||
{
|
||||
t_cli *cli_ctx;
|
||||
|
||||
cli_ctx = (t_cli *)usr_control_struct;
|
||||
cli_ctx->debug = true;
|
||||
}
|
||||
|
||||
void c3_set_save(void *usr_control_struct)
|
||||
{
|
||||
t_cli *cli_ctx;
|
||||
|
||||
cli_ctx = (t_cli *)usr_control_struct;
|
||||
cli_ctx->save = true;
|
||||
}
|
||||
|
||||
void c3_print_help(void *usr_control_struct)
|
||||
{
|
||||
t_cli *cli_ctx;
|
||||
|
||||
cli_ctx = (t_cli *)usr_control_struct;
|
||||
cli_ctx->help = true;
|
||||
ft_putstr_fd("Usage: cub3d [options] <file>\n", STDOUT_FILENO);
|
||||
ft_putstr_fd("Options:\n", STDOUT_FILENO);
|
||||
ft_putstr_fd("\t-f, --file <file> : specify the file to load\n", STDOUT_FILENO);
|
||||
ft_putstr_fd("\t-d, --debug : enable debug mode\n", STDOUT_FILENO);
|
||||
ft_putstr_fd("\t-s, --save : save the state of the 'game' when closing\n", STDOUT_FILENO);
|
||||
ft_putstr_fd("\t-h, --help : print this help\n", STDOUT_FILENO);
|
||||
}
|
||||
24
sources/rgb_to_color.c
Normal file
24
sources/rgb_to_color.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rgb_to_color.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/08 21:49:55 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/11/09 00:14:28 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d_struct.h"
|
||||
|
||||
t_color rgb_to_color(int r, int g, int b)
|
||||
{
|
||||
t_color color;
|
||||
|
||||
color.r = r;
|
||||
color.g = g;
|
||||
color.b = b;
|
||||
color.a = 0;
|
||||
return (color);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue