feat: finished base framework to test mlx ideas

This commit is contained in:
B.Goulard 2024-11-12 06:22:27 +01:00
parent 7bd843af12
commit 4db733648e
5 changed files with 90 additions and 35 deletions

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/11/11 20:02:50 by rparodi ### ########.fr # # Updated: 2024/11/12 06:12:02 by bgoulard ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -42,14 +42,16 @@ MLXFLAGS = -L$(MLX_DIR) -lmlx -L/opt/X11/lib -lX11 -lXext -lXrender -lXrandr -lX
LDFLAGS += $(MLXFLAGS) LDFLAGS += $(MLXFLAGS)
SRC =\ SRC =\
raycast/mlx_init.c \ raycast/frame_update.c \
mlx_layer/mlx_init.c \
parsing/arguments.c \ parsing/arguments.c \
sources/cleanups.c \
sources/error.c \
sources/main.c \ sources/main.c \
sources/cleanups.c \
sources/options.c \ sources/options.c \
sources/options_impl.c \ sources/rgb_to_color.c \
sources/rgb_to_color.c sources/error.c \
sources/options_impl.c
# Objects # Objects
OBJDIRNAME = ./build OBJDIRNAME = ./build

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */ /* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */
/* Updated: 2024/11/11 21:27:12 by rparodi ### ########.fr */ /* Updated: 2024/11/12 06:19:04 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,23 +14,26 @@
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "mlx_functions.h" #include "mlx_functions.h"
#include "mlx_structs.h" #include "mlx_structs.h"
#include <stdlib.h> #include <stdlib.h>
#include <X11/keysym.h>
int _keyhook(int keycode, t_info *info) int c3_frame_update(void *inf_ptr);
int c3_keyhook(int keycode, t_info *info)
{ {
(void)info; if (keycode == XK_Escape || keycode == 65307)
if (keycode == 53 || keycode == 65307) return (mlx_loop_end(info->mlx_ptr), EXIT_SUCCESS);
exit(EXIT_SUCCESS); // return (cleanup_info(info), EXIT_SUCCESS); /// Remove for segfault /* move player w keys and call to redraw screen */
return (0); return (EXIT_SUCCESS);
} }
int _redcross(t_info *info) int c3_redcross(t_info *info)
{ {
(void) info; // return (cleanup_info(info), EXIT_SUCCESS); /// Remove for segfault return (mlx_loop_end(info->mlx_ptr), EXIT_SUCCESS);
exit(EXIT_SUCCESS);
} }
t_win_list *_init_mlx_window(t_info *info) t_win_list *c3_init_mlx_window(t_info *info)
{ {
int x; int x;
int y; int y;
@ -38,7 +41,7 @@ t_win_list *_init_mlx_window(t_info *info)
x = 0; x = 0;
y = 0; y = 0;
mlx_get_screen_size(info->mlx_ptr, &x, &y); mlx_get_screen_size(info->mlx_ptr, &x, &y);
return (mlx_new_window(info->mlx_ptr, x, y, "Miaou")); return (mlx_new_window(info->mlx_ptr, x, y, "C3D"));
} }
int init_mlx_env(t_info *info) int init_mlx_env(t_info *info)
@ -46,11 +49,11 @@ int init_mlx_env(t_info *info)
info->mlx_ptr = mlx_init(); info->mlx_ptr = mlx_init();
if (!info->mlx_ptr) if (!info->mlx_ptr)
return (MLX_ERROR); return (MLX_ERROR);
info->win_ptr = _init_mlx_window(info); info->win_ptr = c3_init_mlx_window(info);
if (!info->win_ptr) if (!info->win_ptr)
return (MLX_ERROR); return (MLX_ERROR);
mlx_hook(info->win_ptr, 2, 1L << 0, _keyhook, info); mlx_hook(info->win_ptr, KeyPress, KeyPressMask, c3_keyhook, info);
mlx_hook(info->win_ptr, 17, 1L << 17, _redcross, info); mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, info);
mlx_loop(info->mlx_ptr); mlx_loop_hook(info->mlx_ptr, c3_frame_update, info);
return (NO_ERROR); return (NO_ERROR);
} }

41
raycast/frame_update.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* frame_update.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */
/* Updated: 2024/11/12 06:21:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d_struct.h"
#include "mlx_functions.h"
#include "ft_string.h"
#include <unistd.h>
// Return 42 is irrelevant check mlx_loop to see that mlx doesn't care for the
// return of the function...
// (Why 'int (*)(void*)' when you dont use the int)
//
// This function is called each time the mlx loops over the mlx pointer in
// mlx_loop. here we do calc on time since last frame and player position
// to know if we need to re-draw the screen. if yes call raph function for
// calc.
// The need to redraw can also be expressed in the diferent key_pressed
// functions, I would recomend to make a bool field for that in the info
// struct.
// As a pure artefact of using mlx this function will likely be mooved to
// mlx_layer in the final repo.
int c3_frame_update(void *inf_ptr)
{
t_info *info;
info = inf_ptr;
mlx_clear_window(info->mlx_ptr, info->win_ptr);
ft_putendl_fd("update called\n", STDOUT_FILENO);
return (EXIT_SUCCESS);
}

View file

@ -6,12 +6,13 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */ /* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */
/* Updated: 2024/11/11 21:30:19 by bgoulard ### ########.fr */ /* Updated: 2024/11/12 05:49:23 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "ft_string.h" #include "ft_string.h"
#include "mlx_functions.h"
static void cleanup_cli(t_cli *cli_ctx) static void cleanup_cli(t_cli *cli_ctx)
{ {
@ -21,12 +22,16 @@ static void cleanup_cli(t_cli *cli_ctx)
static void cleanup_map(t_map *map) static void cleanup_map(t_map *map)
{ {
(void)map; if (map->fd)
(close(map->fd), map->fd = 0);
} }
static void cleanup_mlx(t_info *info) static void cleanup_mlx(t_info *info)
{ {
(void)info; mlx_destroy_window(info->mlx_ptr, info->win_ptr);
mlx_destroy_display(info->mlx_ptr);
if (info->mlx_ptr)
ft_free((void **)&info->mlx_ptr);
} }
void cleanup_info(t_info *info) void cleanup_info(t_info *info)

View file

@ -6,20 +6,21 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */
/* Updated: 2024/11/11 21:34:07 by bgoulard ### ########.fr */ /* Updated: 2024/11/12 06:20:46 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "mlx_functions.h"
#include "mlx_structs.h"
#include "ft_string.h" #include "ft_string.h"
#include "mlx_functions.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// not normed but we'll take care of this as a niceties at the last
// possible moment :)
void dump_info(t_info *info) void dump_info(t_info *info)
{ {
const char *bool_str[2] = { "True", "False"}; const char *bool_str[2] = { "True", "False"};
@ -52,13 +53,16 @@ void check_err(t_info *info)
void run_cub3d(t_info *info) void run_cub3d(t_info *info)
{ {
// code here // todo: here
// - parse map // - parse map
// - validity check // - validity check
// - mlx inits
// - game loop
init_mlx_env(info); init_mlx_env(info);
// - mlx cleanup mlx_loop(info->mlx_ptr);
// - game loop : already loops over the mlx_ptr
// -> get events if key pressed move player + run math to re-draw screen
// - mlx cleanup : already called in parent function deprecated to call here
// -> previous 'segfault' were due to someone calling cleaup instead of
// mlx_loop_end
} }
int main_cub3d(char *file_arg, t_info *info) int main_cub3d(char *file_arg, t_info *info)
@ -73,7 +77,7 @@ int main_cub3d(char *file_arg, t_info *info)
if (info->cli_ctx.debug) if (info->cli_ctx.debug)
(dump_info(info), printf("file_arg: %s\n", file_arg)); (dump_info(info), printf("file_arg: %s\n", file_arg));
run_cub3d(info); run_cub3d(info);
return (cleanup_info(info), EXIT_SUCCESS); return (cleanup_info(info), info->last_error);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])