From ee4c9685acd70b9b12b22d967c2a45e9a63a714c Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 24 Feb 2020 10:45:32 +0100 Subject: Boilerplate minilibx --- Makefile | 7 ++-- author | 1 + include/fractol.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++- src/event.c | 26 +++++++++++++++ src/fractals/julia.c | 0 src/fractals/mandelbrot.c | 0 src/helper.c | 0 src/main.c | 16 +++++++-- src/render.c | 50 ++++++++++++++++++++++++++++ src/state.c | 48 ++++++++++++++++++++++++++ 10 files changed, 228 insertions(+), 5 deletions(-) create mode 100644 author create mode 100644 src/event.c create mode 100644 src/fractals/julia.c create mode 100644 src/fractals/mandelbrot.c create mode 100644 src/helper.c create mode 100644 src/render.c create mode 100644 src/state.c diff --git a/Makefile b/Makefile index bbc0af6..ca5aa12 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/24 09:19:49 by cacharle #+# #+# # -# Updated: 2020/02/24 09:32:45 by cacharle ### ########.fr # +# Updated: 2020/02/24 09:57:58 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -30,7 +30,10 @@ LDFLAGS = -L$(LIBFT_DIR) -lft \ -framework OpenGL -framework AppKit -lm NAME = fractol -SRC_FILES = main.c +SRC_FILES = main.c \ + render.c \ + event.c \ + state.c SRC = $(addprefix $(SRC_DIR)/,$(SRC_FILES)) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) diff --git a/author b/author new file mode 100644 index 0000000..718becd --- /dev/null +++ b/author @@ -0,0 +1 @@ +cacharle diff --git a/include/fractol.h b/include/fractol.h index 61b8e68..6806c31 100644 --- a/include/fractol.h +++ b/include/fractol.h @@ -6,14 +6,97 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:28:16 by cacharle #+# #+# */ -/* Updated: 2020/02/24 09:31:55 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 10:43:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FRACTOL_H # define FRACTOL_H +# include +# include +# include +# include # include "mlx.h" # include "libft.h" +# define WINDOW_TITLE "fractol" +# define WINDOW_WIDTH 640 +# define WINDOW_HEIGHT 480 + +# define MLXK_ESC 53 +# define MLXK_W 13 +# define MLXK_A 0 +# define MLXK_S 1 +# define MLXK_D 2 +# define MLXK_LEFT 123 +# define MLXK_RIGHT 124 + +# define MLX_LITTLE_ENDIAN 0 +# define MLX_BIG_ENDIAN 1 + +# define PALETTE_SIZE 2048 + +typedef union +{ + unsigned int hexcode; + struct + { + uint8_t b; + uint8_t g; + uint8_t r; + uint8_t alpha; + } rgb; +} t_color; + +typedef struct +{ + int width; + int height; + void *id; + char *data; + int depth; + int size_line; + int endian; +} t_image; + +typedef struct +{ + double a; + double b; +} t_complex; + +typedef int (*t_func_fractal)(t_complex z); + +typedef struct +{ + bool running; + void *mlx_ptr; + void *window_ptr; + t_image window; + t_complex window_complex[WINDOW_HEIGHT * WINDOW_WIDTH]; + t_color palette[PALETTE_SIZE]; + t_func_fractal func; +} t_state; + +/* +** state.c +*/ + +int state_init(t_state *state, char *fractal_name); +int state_destroy(t_state *state); + +/* +** render.c +*/ + +int render_update(t_state *state); + +/* +** event.c +*/ + +int event_quit(t_state *state); +int event_keydown(int key, t_state *state); + #endif diff --git a/src/event.c b/src/event.c new file mode 100644 index 0000000..6aac07b --- /dev/null +++ b/src/event.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* event.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 09:52:35 by cacharle #+# #+# */ +/* Updated: 2020/02/24 10:10:29 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fractol.h" + +int event_quit(t_state *state) +{ + state->running = false; + return (0); +} + +int event_keydown(int key, t_state *state) +{ + if (key == MLXK_ESC) + state->running = false; + return (0); +} diff --git a/src/fractals/julia.c b/src/fractals/julia.c new file mode 100644 index 0000000..e69de29 diff --git a/src/fractals/mandelbrot.c b/src/fractals/mandelbrot.c new file mode 100644 index 0000000..e69de29 diff --git a/src/helper.c b/src/helper.c new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index 087c48e..a0e5a6e 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:27:44 by cacharle #+# #+# */ -/* Updated: 2020/02/24 09:33:03 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 10:17:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,18 @@ int main(int argc, char **argv) { - ft_putendl("hello"); + t_state state; + + if (argc != 2) + { + ft_putstr("mandelbrot\njulia\n"); + return (0); + } + if (state_init(&state, argv[1]) < 0) + return (1); + mlx_hook(state.window_ptr, 17, 0, event_quit, (void*)&state); + mlx_hook(state.window_ptr, 2, 2, event_keydown, (void*)&state); + mlx_loop_hook(state.mlx_ptr, render_update, (void*)&state); + mlx_loop(state.mlx_ptr); return (0); } diff --git a/src/render.c b/src/render.c new file mode 100644 index 0000000..ab4fca9 --- /dev/null +++ b/src/render.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */ +/* Updated: 2020/02/24 10:44:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fractol.h" + +static void st_render_fractal(t_state *state) +{ + int offset; + int i; + int j; + t_color color; + + color.hexcode = 0xffffff; + offset = 0; + i = -1; + while (++i < state->window.height) + { + j = -1; + while (++j < state->window.width) + { + ((t_color*)state->window.data)[offset] = + state->palette[ + state->func(state->window_complex[offset]) + ]; + offset++; + } + } +} + +int render_update(t_state *state) +{ + if (!state->running) + { + state_destroy(state); + exit(EXIT_SUCCESS); + } + /* st_render_fractal(state); */ + mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0); + return (0); +} + diff --git a/src/state.c b/src/state.c new file mode 100644 index 0000000..4f33041 --- /dev/null +++ b/src/state.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 09:58:01 by cacharle #+# #+# */ +/* Updated: 2020/02/24 10:43:08 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fractol.h" + +static void st_state_init_palette(t_state *state) +{ + + +} + +int state_init(t_state *state, char *fractal_name) +{ + (void)fractal_name; + + if ((state->mlx_ptr = mlx_init()) == NULL) + return (-1); + if ((state->window_ptr = mlx_new_window(state->mlx_ptr, 640, 480, WINDOW_TITLE)) == NULL) + return (state_destroy(state)); + state->window.width = WINDOW_WIDTH; + state->window.height = WINDOW_HEIGHT; + if ((state->window.id = mlx_new_image(state->mlx_ptr, state->window.width, state->window.height)) == NULL) + return (state_destroy(state)); + state->window.data = mlx_get_data_addr(state->window.id, &state->window.depth, + &state->window.size_line, &state->window.endian); + state->running = true; + return (0); +} + +int state_destroy(t_state *state) +{ + if (state == NULL) + return (-1); + if (state->mlx_ptr != NULL && state->window.id != NULL) + mlx_destroy_image(state->mlx_ptr, state->window.id); + if (state->mlx_ptr != NULL && state->window_ptr != NULL) + mlx_destroy_window(state->mlx_ptr, state->window_ptr); + return (-1); +} -- cgit