diff --git a/Makefile b/Makefile index 462e1ec..cc12032 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,10 @@ SOURCEFILES=src/main.c \ src/map/checkers.c \ src/map/setters.c \ src/utils/frees.c \ - src/map/forbidden_characters.c + src/map/forbidden_characters.c \ + src/utils/hooks.c \ + src/draw/drawutils.c \ + src/draw/map.c OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES)) OBJDIR=objects NAME=cub3d diff --git a/includes/cub3d.h b/includes/cub3d.h new file mode 100644 index 0000000..43f0c4c --- /dev/null +++ b/includes/cub3d.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cub3d.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/21 19:34:43 by tchampio #+# #+# */ -/* Updated: 2025/06/25 17:57:24 by tchampio ### ########.fr */ +/* Updated: 2025/07/15 10:32:24 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,6 @@ # define CYAN "\033[36m" # define BOLD "\033[1m" - bool check_cubfile(char *filename, t_mapdata *map); bool check_filename(t_mapdata *map, char *file); void populate_maps(t_mapdata *map, int fd); diff --git a/includes/structs.h b/includes/structs.h index faeb374..d475695 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* structs.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tchampio +#+ +:+ +#+ */ +/* By: tchampio addr + (y * data->line_length + x * (data->bits_per_pixel / 8)); + *(unsigned int *)dst = color; +} diff --git a/src/draw/map.c b/src/draw/map.c new file mode 100644 index 0000000..b39b0a7 --- /dev/null +++ b/src/draw/map.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio map[i]) + { + j = 0; + while (map->map[i][j]) + { + if (map->map[i][j] == '1') + draw_2d_wall(map->f_color, data, 10 * j, 10 * i); + else if (map->map[i][j] == 'Z' || map->map[i][j] == 'z') + draw_2d_wall(0x0008D9D6, data, 10 * j, 10 * i); + else if (map->map[i][j] == 'D' || map->map[i][j] == 'd') + draw_2d_wall(0x00FF2E63, data, 10 * j, 10 * i); + else if (map->map[i][j] == 's') + draw_2d_wall(0x00E84545, data, 10 * j, 10 * i); + else if (map->map[i][j] == 'M') + draw_2d_wall(0x00F4CE14, data, 10 * j, 10 * i); + j++; + } + i++; + } + draw_2d_wall(0x00FF0000, data, 10 * player->x, 10 * player->y); +} diff --git a/src/main.c b/src/main.c index e3c5989..c509c1a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: tchampio #include #include #include #include -void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color) -{ - char *dst; - - dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8)); - *(unsigned int *)dst = color; -} - -void draw_2d_wall(unsigned int color, t_mlx_data *data, - int x, int y) -{ - int i; - int j; - static int size = 10; - - i = 0; - while (i < size) - { - j = 0; - while (j < size) - { - my_mlx_pixel_put(data, x + i, y + j, color); - j++; - } - i++; - } -} - -int destroy(t_cub3d_data *data) -{ - free_map(data->map); - if (data->mlx_win) - mlx_destroy_window(data->mlx, data->mlx_win); - if (data->mlx_data) - mlx_destroy_image(data->mlx, data->mlx_data->img); - free(data->mlx_data); - if (data->mlx) - mlx_destroy_display(data->mlx); - free(data->mlx); - exit(0); - return 0; -} - -int keypress_handler(int keycode, t_cub3d_data *data) -{ - if (keycode == XK_Escape) - destroy(data); - else if (keycode == XK_a) - data->player.movement.x = -MOVEMENT_SPEED; - else if (keycode == XK_d) - data->player.movement.x = MOVEMENT_SPEED; - else if (keycode == XK_w) - data->player.movement.y = -MOVEMENT_SPEED; - else if (keycode == XK_s) - data->player.movement.y = MOVEMENT_SPEED; - return (0); -} - -int keyrelease_handler(int keycode, t_cub3d_data *data) -{ - if (keycode == XK_Escape) - destroy(data); - else if (keycode == XK_a) - data->player.movement.x = 0; - else if (keycode == XK_d) - data->player.movement.x = 0; - else if (keycode == XK_w) - data->player.movement.y = 0; - else if (keycode == XK_s) - data->player.movement.y = 0; - return (0); -} - -void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data) -{ - int i; - int j; - - i = 0; - while (map->map[i]) - { - j = 0; - while (map->map[i][j]) - { - if (map->map[i][j] == '1') - draw_2d_wall(map->f_color, data, 10 * j, 10 * i); - else if (map->map[i][j] == 'Z' || map->map[i][j] == 'z') - draw_2d_wall(0x0008D9D6, data, 10 * j, 10 * i); - else if (map->map[i][j] == 'D' || map->map[i][j] == 'd') - draw_2d_wall(0x00FF2E63, data, 10 * j, 10 * i); - else if (map->map[i][j] == 's') - draw_2d_wall(0x00E84545, data, 10 * j, 10 * i); - else if (map->map[i][j] == 'M') - draw_2d_wall(0x00F4CE14, data, 10 * j, 10 * i); - /* else if (ft_strchr("NSEW", map->map[i][j])) - draw_2d_wall(0x00FF0000, data, 10 * j, 10 * i); - */ - j++; - } - i++; - } - draw_2d_wall(0x00FF0000, data, 10 * player->x, 10 * player->y); -} - void init_player(t_mapdata *mapdata, t_player *player) { player->health = 100; @@ -139,30 +36,33 @@ int game_loop(t_cub3d_data *data) mlx_destroy_image(data->mlx, data->mlx_data->img); data->mlx_data->img = mlx_new_image(data->mlx, 800, 600); draw_map(data->map, &data->player, data->mlx_data); - mlx_put_image_to_window(data->mlx, data->mlx_win, data->mlx_data->img, 0, 0); + mlx_put_image_to_window(data->mlx, data->mlx_win, + data->mlx_data->img, 0, 0); + mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT); return (0); } int main(int argc, char **argv) { - t_cub3d_data data; + t_cub3d_data data; if (argc < 2) return (ft_printf("Error: Missing cub3d file\n"), 1); data.map = ft_calloc(sizeof(t_mapdata), 1); if (!check_cubfile(argv[1], data.map)) - return (ft_printf("Error: Wrong map file. Reason: %s\n", data.map->error), 1); + return (ft_printf("Error: Wrong map file. Reason: %s\n", + data.map->error), 1); data.mlx = mlx_init(); data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d"); data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1); data.mlx_data->img = mlx_new_image(data.mlx, 800, 600); - data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img, &data.mlx_data->bits_per_pixel, &data.mlx_data->line_length, &data.mlx_data->endian); + data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img, + &data.mlx_data->bits_per_pixel, &data.mlx_data->line_length, + &data.mlx_data->endian); init_player(data.map, &(data.player)); mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data); - mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask, keyrelease_handler, &data); - #ifdef BONUS - mlx_string_put(data.mlx, data.mlx_win, 10, 10, 0x00FFFFFF, "compiled with bonuses"); - #endif + mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask, + keyrelease_handler, &data); mlx_loop_hook(data.mlx, game_loop, &data); mlx_loop(data.mlx); } diff --git a/src/utils/frees.c b/src/utils/frees.c index 9480722..7290b09 100644 --- a/src/utils/frees.c +++ b/src/utils/frees.c @@ -6,12 +6,13 @@ /* By: tchampio void free_tab(char **tab) @@ -40,3 +41,18 @@ void free_map(t_mapdata *map) free(map->filename); free(map); } + +int destroy(t_cub3d_data *data) +{ + free_map(data->map); + if (data->mlx_win) + mlx_destroy_window(data->mlx, data->mlx_win); + if (data->mlx_data) + mlx_destroy_image(data->mlx, data->mlx_data->img); + free(data->mlx_data); + if (data->mlx) + mlx_destroy_display(data->mlx); + free(data->mlx); + exit(0); + return (0); +} diff --git a/src/utils/hooks.c b/src/utils/hooks.c new file mode 100644 index 0000000..609a837 --- /dev/null +++ b/src/utils/hooks.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hooks.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#include + +int keypress_handler(int keycode, t_cub3d_data *data) +{ + if (keycode == XK_Escape) + destroy(data); + if (keycode == XK_a) + data->player.movement.x = -MOVEMENT_SPEED; + else if (keycode == XK_d) + data->player.movement.x = MOVEMENT_SPEED; + if (keycode == XK_w) + data->player.movement.y = -MOVEMENT_SPEED; + else if (keycode == XK_s) + data->player.movement.y = MOVEMENT_SPEED; + return (0); +} + +int keyrelease_handler(int keycode, t_cub3d_data *data) +{ + if (keycode == XK_Escape) + destroy(data); + if (keycode == XK_a) + data->player.movement.x = 0; + if (keycode == XK_d) + data->player.movement.x = 0; + if (keycode == XK_w) + data->player.movement.y = 0; + if (keycode == XK_s) + data->player.movement.y = 0; + return (0); +}