--- /dev/null
+
+#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));
+}
#include <X11/X.h>
#include <X11/keysym.h>
#include <limits.h>
+#include <stdlib.h>
#include <mlx.h>
#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;
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);
}