From: Lukas Date: Fri, 3 Nov 2023 10:36:35 +0000 (+0100) Subject: Save work down so far for access from elsewhere X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=f209c637b89e064e7c4fff6048ff5c3f37ad2b6d;p=42%2Ffract-ol.git Save work down so far for access from elsewhere This should not really be a commit, but I can access it from another location this way. The changes will be listed in the following commit hopefully. --- diff --git a/Makefile b/Makefile index 2edfab6..ac61e97 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ MLX := $(MLXDIR)libmlx_Linux.a LFTDIR := Libft/ LFT := $(LFTDIR)libft.a -SRCS := main.c complex.c +SRCS := main.c complex.c color.c OBJS := $(SRCS:%.c=%.o) all : $(NAME) diff --git a/color.c b/color.c new file mode 100644 index 0000000..aa85fee --- /dev/null +++ b/color.c @@ -0,0 +1,44 @@ + +#include "color.h" + +t_color to_color(unsigned int num) +{ + t_color c; + + c.a = (num & 0xFF000000) >> 24; + c.r = (num & 0x00FF0000) >> 16; + c.g = (num & 0x0000FF00) >> 8; + c.b = (num & 0x000000FF); + return (c); +} + +t_color lin_interp_color(t_color zero, t_color one, double norm_par) +{ + t_color res; + + if (norm_par < 0.) + norm_par = 0.; + else if (norm_par > 1.) + norm_par = 1.; + res.a = zero.a + norm_par * one.a; + res.r = zero.r + norm_par * one.r; + res.g = zero.g + norm_par * one.g; + res.b = zero.b + norm_par * one.b; + return (res); +} + +t_color basic_palette(double normalized_par) +{ + if (normalized_par == 0.) + return (to_color(0x00000000)); + return (lin_interp_color(to_color(0x000000FF), to_color(0x00FFFF00), normalized_par)); +} + +t_color double_grad(double normalized_par) +{ + const double breakpoint = 0.1; + + if (normalized_par < breakpoint) + return (lin_interp_color(to_color(0x00000000), to_color(0x000000FF), normalized_par / breakpoint)); + return (lin_interp_color(to_color(0x000000FF), to_color(0x00FFFF00), (normalized_par - breakpoint) / breakpoint)); +} diff --git a/color.h b/color.h new file mode 100644 index 0000000..bc2efd3 --- /dev/null +++ b/color.h @@ -0,0 +1,18 @@ + +#ifndef COLOR_H +# define COLOR_H + +struct s_color +{ + unsigned char a; + unsigned char r; + unsigned char g; + unsigned char b; +}; +typedef struct s_color t_color; + +t_color lin_interp_color(t_color zero, t_color one, double norm_par); +t_color basic_palette(double normalized_par); +t_color double_grad(double normalized_par); + +#endif diff --git a/fractol.h b/fractol.h index 14669c5..00dfdf1 100644 --- a/fractol.h +++ b/fractol.h @@ -2,6 +2,8 @@ #ifndef FRACTOL_H # define FRACTOL_H +# include "libft.h" + struct s_img { void *img; @@ -10,15 +12,27 @@ struct s_img int bpl; int endian; }; -typedef struct s_img t_img; - -typedef unsigned int color; +typedef struct s_img t_img; struct s_session { void *mlx; - void *win; + t_list *windows; + size_t win_count; +}; +typedef struct s_session t_session; + +struct s_window +{ + t_session *s; + size_t id; +}; +typedef struct s_window t_window; + +enum mlx_status +{ + mlx_success, + mlx_window_error, }; -typedef struct s_session t_session; #endif diff --git a/main.c b/main.c index 38766f4..9afdb6f 100644 --- a/main.c +++ b/main.c @@ -13,41 +13,85 @@ #include #include #include +#include #include #include "libft.h" #include "fractol.h" #include "complex.h" +#include "color.h" -color *to_color_ptr(char *addr) +void ft_putpx_img(t_img *img, int x, int y, t_color c) { - return ((color *)addr); + char *px_addr; + + px_addr = img->addr + y * img->bpl + x * img->bpp / CHAR_BIT; + if (img->endian) + { + px_addr[0] = c.a; + px_addr[1] = c.r; + px_addr[2] = c.g; + px_addr[3] = c.b; + } + else + { + px_addr[3] = c.a; + px_addr[2] = c.r; + px_addr[1] = c.g; + px_addr[0] = c.b; + } + return ; +} + +int handle_key_press(int keycode, t_window *w) +{ + if (keycode == XK_Escape) + mlx_destroy_window(w->s, w->s->windows[w->index]); + return (0); +} + +int handle_button_rel(int button_code, __attribute__((unused)) t_session *s) +{ + ft_printf("Button_code:\t%i\n", button_code); + return (0); +} + +void free_session(t_session *s) +{ + size_t i; + + i = 0; + while (i < s->win_count) + mlx_destroy_window(s->mlx, s->windows[i]); + free(s->windows); + free(s->mlx); + return ; } -void ft_putpx_img(t_img *img, int x, int y, color color) +void free_img(t_img *i) { - *to_color_ptr(img->addr + y * img->bpl + x * img->bpp / CHAR_BIT) = color; + free(i->img); + free(i->addr); return ; } -color basic_palette(double normalized_par) +enum mlx_status add_mlx_window(t_session *s, int x, int y, char *name) { - unsigned char a; - unsigned char r; - unsigned char g; - unsigned char b; - color res; - - if (normalized_par == 0.) - return (0x00000000); - a = 0x00; - r = 0x00 + normalized_par * 0xFF; - g = 0x00 + normalized_par * 0xFF; - b = 0xFF - normalized_par * 0xFF; - res = (a << 24) | (r << 16) | (g << 8) | b; - return (res); + void **tmp; + + tmp = malloc((s->win_count + 1) * sizeof(s->windows)); + if (!tmp) + return (mlx_window_error); + ft_memcpy(tmp, s->windows, s->win_count); + free(s->windows); + s->windows = tmp; + (s->windows)[s->win_count] = mlx_new_window(s->mlx, x, y, name); + if (!(s->windows)[s->win_count]) + return (mlx_window_error); + ++(s->win_count); + return (mlx_success); } -color mandelbrot(t_complex c, double threshold, int max_count, color (*palette)(double)) +t_color mandelbrot(t_complex c, double threshold, int max_count, t_color (*palette)(double)) { const double min_threshold = 2.5; int count; @@ -67,54 +111,58 @@ color mandelbrot(t_complex c, double threshold, int max_count, color (*palette)( count = 0; return (palette((double)count / max_count)); } - -void close_session(t_session *s) +/* +int main(void) { - mlx_destroy_window(s->mlx, s->win); - mlx_loop_end(s->mlx); - return ; + t_session s; + t_img img; + + s.win_count = 0; + s.windows = NULL; + s.mlx = mlx_init(); + add_mlx_window(&s, 1920, 1080, "Hello world!"); + img.img = mlx_new_image(s.mlx, 1920, 1080); + img.addr = mlx_get_data_addr(img.img, &img.bpp, &img.bpl, &img.endian); + mlx_hook(s.windows[0], KeyPress, KeyPressMask, handle_key_press, &(t_window){.s=&s, .index=0}); + mlx_hook(s.windows[0], ButtonRelease, ButtonRelease, handle_button_rel, &s); + mlx_loop(s.mlx); + mlx_destroy_image(s.mlx, img.img); + mlx_destroy_display(s.mlx); + free_img(&img); + free_session(&s); + return (0); } +*/ + -int handle_key_press(int keycode, t_session *s) +typedef struct +{ + void *mlx; + void *win; +} t_test; + +int esc_close(int keycode, t_test *t) { if (keycode == XK_Escape) - close_session(s); + mlx_destroy_window(t->mlx, t->win); return (0); } -int handle_button_rel(int button_code, __attribute__((unused)) t_session *s) +int no_event(__attribute__((unused)) t_test *t) { - ft_printf("Button_code:\t%i\n", button_code); return (0); } int main(void) { - t_session s; - int x; - int y; - t_img img; + t_test t; - s.mlx = mlx_init(); - s.win = mlx_new_window(s.mlx, 1920, 1080, "Hello world!"); - img.img = mlx_new_image(s.mlx, 1920, 1080); - img.addr = mlx_get_data_addr(img.img, &img.bpp, &img.bpl, &img.endian); - y = 0; - while (y <= 1080) - { - x = 0; - while (x <= 1920) - { - //ft_putpx_img(&img, x, y, basic_palette(x/1920.*y/1080.)); - ft_putpx_img(&img, x, y, mandelbrot(to_complex(5*(x/1920. - 0.5), 5*1080/1920*(y/1080. - 0.5)), 1000000, 100, basic_palette)); - ++x; - } - mlx_put_image_to_window(s.mlx, s.win, img.img, 0, 0); - ++y; - } - mlx_put_image_to_window(s.mlx, s.win, img.img, 0, 0); - mlx_hook(s.win, KeyPress, KeyPressMask, handle_key_press, &s); - mlx_hook(s.win, ButtonRelease, ButtonRelease, handle_button_rel, &s); - mlx_loop(s.mlx); + t.mlx = mlx_init(); + t.win = mlx_new_window(t.mlx, 1920, 1080, "TEST"); + mlx_hook(t.win, KeyPress, KeyPressMask, esc_close, &t); + mlx_loop_hook(t.mlx, no_event, &t); + mlx_loop(t.mlx); + mlx_destroy_display(t.mlx); + free(t.mlx); return (0); }