aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-27 14:47:23 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-27 14:47:23 +0100
commitdab9efb7b745fe884fb72017591dce95978c19c4 (patch)
tree67de56f79c67a29f3b916593c8f2ee4ebd5df45d
parent7a5632ab67f95c561ce22a19352e963af3077a5b (diff)
downloadfractol-dab9efb7b745fe884fb72017591dce95978c19c4.tar.gz
fractol-dab9efb7b745fe884fb72017591dce95978c19c4.tar.bz2
fractol-dab9efb7b745fe884fb72017591dce95978c19c4.zip
Optimization and norming a bitHEADmaster
-rw-r--r--Makefile4
-rw-r--r--include/fractol.h39
-rw-r--r--src/capture.c25
-rw-r--r--src/color.c2
-rw-r--r--src/event.c6
-rw-r--r--src/fractals/burningship.c32
-rw-r--r--src/fractals/julia.c38
-rw-r--r--src/fractals/mandelbrot.c34
-rw-r--r--src/fractals/tricorn.c32
-rw-r--r--src/helper.c13
-rw-r--r--src/main.c2
-rw-r--r--src/render.c120
-rw-r--r--src/state.c30
-rw-r--r--test.bmpbin5760054 -> 5760054 bytes
14 files changed, 184 insertions, 193 deletions
diff --git a/Makefile b/Makefile
index 199d32f..1e9eccc 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/02/24 09:19:49 by cacharle #+# #+# #
-# Updated: 2020/02/26 18:04:14 by cacharle ### ########.fr #
+# Updated: 2020/02/27 13:11:14 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -24,7 +24,7 @@ OBJ_DIRS = $(shell find $(SRC_DIR) -type d | sed 's/src/obj/')
CC = gcc
CCFLAGS = -I$(LIBFT_DIR)/include -I$(MINILIBX_DIR) -I$(INCLUDE_DIR) \
- -O3 -Wall -Wextra #-Werror
+ -Ofast -Wall -Wextra #-Werror
LDFLAGS = -L$(LIBFT_DIR) -lft \
-L$(MINILIBX_DIR) -lmlx \
-framework OpenGL -framework AppKit -lm -lpthread
diff --git a/include/fractol.h b/include/fractol.h
index b1e22dc..99d350c 100644
--- a/include/fractol.h
+++ b/include/fractol.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 09:28:16 by cacharle #+# #+# */
-/* Updated: 2020/02/26 18:15:38 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 13:39:08 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,18 +26,24 @@
# define WINDOW_TITLE "fractol"
-# define FRACTOL_RESOLUTION_HIGH
+# define FRACTOL_RESOLUTION_LOW
# ifdef FRACTOL_RESOLUTION_HIGH
# define WINDOW_WIDTH 1600
# define WINDOW_HEIGHT 1200
+# define FWINDOW_WIDTH 1600.0
+# define FWINDOW_HEIGHT 1200.0
# elif defined(FRACTOL_RESOLUTION_MEDIUM)
# define WINDOW_WIDTH 1024
# define WINDOW_HEIGHT 768
+# define FWINDOW_WIDTH 1024.0
+# define FWINDOW_HEIGHT 768.0
# else
# define FRACTOL_RESOLUTION_LOW
# define WINDOW_WIDTH 800
# define WINDOW_HEIGHT 600
+# define FWINDOW_WIDTH 800.0
+# define FWINDOW_HEIGHT 600.0
# endif
// # define WINDOW_WIDTH_DOUBLE 640.0
// # define WINDOW_HEIGHT_DOUBLE 480.0
@@ -92,7 +98,7 @@ typedef struct
int width;
int height;
void *id;
- char *data;
+ t_color *data;
int depth;
int size_line;
int endian;
@@ -110,16 +116,16 @@ typedef int (*t_func_fractal)(struct s_state *state, t_complex z);
typedef struct s_state
{
+ t_func_fractal func;
+ t_color *palette;
+ t_image window;
+ t_complex c;
bool running;
bool updated;
void *mlx_ptr;
void *window_ptr;
- t_image window;
- t_color *palette;
- t_func_fractal func;
t_complex center;
t_complex plane;
- t_complex c;
int iterations;
int offsets[WINDOW_HEIGHT];
double samples;
@@ -127,10 +133,10 @@ typedef struct s_state
typedef struct
{
- t_state *state;
- int offset;
- t_complex z;
+ double z_i;
double step_i;
+ int offset;
+ t_state *state;
} t_render_routine_arg;
/*
@@ -161,10 +167,10 @@ int event_mouse_motion(int x, int y, t_state *state);
** fractals/
*/
-int mandelbrot(t_state *state, t_complex z);
-int julia(t_state *state, t_complex z);
-int tricorn(t_state *state, t_complex z);
-int burningship(t_state *state, t_complex z);
+int mandelbrot(t_state *state, t_complex c);
+int julia(t_state *state, t_complex c);
+int tricorn(t_state *state, t_complex c);
+int burningship(t_state *state, t_complex c);
/*
** helper.c
@@ -180,5 +186,10 @@ void h_zoom_out(t_state *state);
t_color_hsl color_rgb_to_hsl(t_color color_rgb);
t_color color_hsl_to_rgb(t_color_hsl color_hsl);
+/*
+** capture.c
+*/
+
+int capture(t_state *state, char *filename);
#endif
diff --git a/src/capture.c b/src/capture.c
index ade572d..8945593 100644
--- a/src/capture.c
+++ b/src/capture.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/11 13:15:11 by cacharle #+# #+# */
-/* Updated: 2020/02/26 18:13:10 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:46:01 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -57,17 +57,19 @@ static void bmp_write_pixels(int fd, t_image *image, uint8_t *bmp_data)
j = -1;
while (++j < image->width)
{
- bmp_data[3 * j + 0] = image->data[4 * (i * image->width + j) + 0];
- bmp_data[3 * j + 1] = image->data[4 * (i * image->width + j) + 1];
- bmp_data[3 * j + 2] = image->data[4 * (i * image->width + j) + 2];
+ bmp_data[3 * j + 0] = image->data[4 * (i * image->width + j)].rgb.r;
+ bmp_data[3 * j + 1] = image->data[4 * (i * image->width + j)].rgb.g;
+ bmp_data[3 * j + 2] = image->data[4 * (i * image->width + j)].rgb.b;
}
write(fd, bmp_data, image->width * 3);
write(fd, padding, padding_size);
}
}
-static void bmp_fill_header(t_image *image, uint8_t file_header[FILE_HEADER_SIZE],
- uint8_t info_header[INFO_HEADER_SIZE])
+static void bmp_fill_header(
+ t_image *image,
+ uint8_t file_header[FILE_HEADER_SIZE],
+ uint8_t info_header[INFO_HEADER_SIZE])
{
int file_size;
@@ -95,7 +97,6 @@ static void bmp_fill_header(t_image *image, uint8_t file_header[FILE_HEADER_SIZE
info_header[14] = (uint8_t)(IMG_DEPTH * 8);
}
-
static bool bmp_write(t_image *image, uint8_t file_header[FILE_HEADER_SIZE],
uint8_t info_header[INFO_HEADER_SIZE], char *filename)
{
@@ -117,17 +118,11 @@ static bool bmp_write(t_image *image, uint8_t file_header[FILE_HEADER_SIZE],
return (true);
}
-int capture(t_state *state, char *filename)
+int capture(t_state *state, char *filename)
{
uint8_t file_header[FILE_HEADER_SIZE];
uint8_t info_header[INFO_HEADER_SIZE];
bmp_fill_header(&state->window, file_header, info_header);
- if (!bmp_write(&state->window, file_header, info_header, filename))
- {
- /* state_destroy(state); */
- return (1);
- }
- /* state_destroy(state); */
- return (0);
+ return (bmp_write(&state->window, file_header, info_header, filename));
}
diff --git a/src/color.c b/src/color.c
index 7fabc9c..103ed68 100644
--- a/src/color.c
+++ b/src/color.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/25 14:36:12 by cacharle #+# #+# */
-/* Updated: 2020/02/25 15:23:44 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:46:32 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/event.c b/src/event.c
index 59378b0..668a599 100644
--- a/src/event.c
+++ b/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 09:52:35 by cacharle #+# #+# */
-/* Updated: 2020/02/26 18:20:48 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:47:00 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,12 +35,12 @@ int event_keydown(int key, t_state *state)
state->center.r += state->plane.r * MOVE_SPEED;
else if (key == MLXK_PLUS)
{
- state->iterations++;
+ state->iterations += 5;
state_update_palette(state);
}
else if (key == MLXK_MINUS)
{
- state->iterations--;
+ state->iterations -= 5;
if (state->iterations < 1)
state->iterations = 1;
state_update_palette(state);
diff --git a/src/fractals/burningship.c b/src/fractals/burningship.c
index fb950ed..07baf38 100644
--- a/src/fractals/burningship.c
+++ b/src/fractals/burningship.c
@@ -6,37 +6,33 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 16:15:49 by cacharle #+# #+# */
-/* Updated: 2020/02/26 13:20:36 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 12:25:24 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
-#define BURNING_SHIP_MAX_ITERATION 20
-#define BURNING_SHIP_ESCAPE_RADIUS_SQUARED 4
+#define BURNING_SHIP_ESCAPE_RADIUS_SQUARED 4.0
-int burningship(t_state *state, t_complex z)
+int burningship(t_state *state, t_complex c)
{
- int n;
- double zr;
- double zi;
- double zr_square;
- double zi_square;
- double tmp;
+ int n;
+ t_complex z;
+ t_complex z_square;
+ double tmp;
(void)state;
- zr = z.r;
- zi = z.i;
+ z = c;
n = -1;
while (++n < state->iterations)
{
- zi_square = zi * zi;
- zr_square = zr * zr;
- if (zr_square + zi_square > BURNING_SHIP_ESCAPE_RADIUS_SQUARED)
+ z_square.i = z.i * z.i;
+ z_square.r = z.r * z.r;
+ if (z_square.r + z_square.i > BURNING_SHIP_ESCAPE_RADIUS_SQUARED)
break;
- tmp = zr_square - zi_square + z.r;
- zi = fabs(2.0 * zr * zi + z.i);
- zr = fabs(tmp);
+ tmp = z_square.r - z_square.i + z.r;
+ z.i = fabs(2.0 * z.r * z.i + c.i);
+ z.r = fabs(tmp);
}
return (n);
}
diff --git a/src/fractals/julia.c b/src/fractals/julia.c
index 8474bb8..a91731c 100644
--- a/src/fractals/julia.c
+++ b/src/fractals/julia.c
@@ -6,36 +6,34 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 15:17:38 by cacharle #+# #+# */
-/* Updated: 2020/02/25 07:33:43 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 13:44:21 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
-#define JULIA_MAX_ITERATION 20
-#define JULIA_ESCAPE_RADIUS_SQUARED 100
+#define JULIA_ESCAPE_RADIUS_SQUARED 4.0
-int julia(t_state *state, t_complex z)
+int julia(t_state *state, t_complex origin_c)
{
- int n;
- double zr;
- double zi;
- double zr_square;
- double zi_square;
+ int n;
+ t_complex z;
+ t_complex z_square;
+ t_complex c;
- zr = z.r;
- zi = z.i;
+ z = origin_c;
+ c = state->c;
+ z_square.r = 0.0;
+ z_square.i = 0.0;
n = -1;
- while (++n < state->iterations)
+ while (z_square.r + z_square.i <= JULIA_ESCAPE_RADIUS_SQUARED && ++n < state->iterations)
{
- zi_square = zi * zi;
- zr_square = zr * zr;
- if (zr_square + zi_square > JULIA_ESCAPE_RADIUS_SQUARED)
- break;
- zi = 2.0 * zr * zi;
- zr = zr_square - zi_square;
- zi += state->c.i;
- zr += state->c.r;
+ z_square.i = z.i * z.i;
+ z_square.r = z.r * z.r;
+ z.i = 2.0 * z.r * z.i;
+ z.r = z_square.r - z_square.i;
+ z.i += c.i;
+ z.r += c.r;
}
return (n);
}
diff --git a/src/fractals/mandelbrot.c b/src/fractals/mandelbrot.c
index 662ea25..312fc71 100644
--- a/src/fractals/mandelbrot.c
+++ b/src/fractals/mandelbrot.c
@@ -6,37 +6,33 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 11:07:41 by cacharle #+# #+# */
-/* Updated: 2020/02/26 13:12:55 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:18:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
-#define MANDEL_MAX_ITERATION 20
-#define MANDEL_ESCAPE_RADIUS_SQUARED 4
+#define MANDEL_ESCAPE_RADIUS_SQUARED 4.0
int mandelbrot(t_state *state, t_complex c)
{
- int n;
- double zr;
- double zi;
- double zr_square;
- double zi_square;
+ int n;
+ t_complex z;
+ t_complex z_square;
(void)state;
- zr = c.r;
- zi = c.i;
+ z = c;
n = -1;
- while (++n < state->iterations)
+ z_square.r = 0.0;
+ z_square.i = 0.0;
+ while (z_square.r + z_square.i <= MANDEL_ESCAPE_RADIUS_SQUARED && ++n < state->iterations)
{
- zi_square = zi * zi;
- zr_square = zr * zr;
- if (zr_square + zi_square > MANDEL_ESCAPE_RADIUS_SQUARED)
- break;
- zi = 2.0 * zr * zi;
- zr = zr_square - zi_square;
- zi += c.i;
- zr += c.r;
+ z_square.i = z.i * z.i;
+ z_square.r = z.r * z.r;
+ z.i = 2.0 * z.r * z.i;
+ z.r = z_square.r - z_square.i;
+ z.i += c.i;
+ z.r += c.r;
}
return (n);
}
diff --git a/src/fractals/tricorn.c b/src/fractals/tricorn.c
index 052cc2f..2abe01b 100644
--- a/src/fractals/tricorn.c
+++ b/src/fractals/tricorn.c
@@ -6,37 +6,33 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 16:00:46 by cacharle #+# #+# */
-/* Updated: 2020/02/25 07:34:01 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 12:24:35 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
-#define TRICORN_MAX_ITERATION 20
-#define TRICORN_ESCAPE_RADIUS_SQUARED 100
+#define TRICORN_ESCAPE_RADIUS_SQUARED 4.0
-int tricorn(t_state *state, t_complex z)
+int tricorn(t_state *state, t_complex c)
{
- int n;
- double zr;
- double zi;
- double zr_square;
- double zi_square;
- double tmp;
+ int n;
+ t_complex z;
+ t_complex z_square;
+ double tmp;
(void)state;
- zr = z.r;
- zi = z.i;
+ z = c;
n = -1;
while (++n < state->iterations)
{
- zi_square = zi * zi;
- zr_square = zr * zr;
- if (zr_square + zi_square > TRICORN_ESCAPE_RADIUS_SQUARED)
+ z_square.i = z.i * z.i;
+ z_square.r = z.r * z.r;
+ if (z_square.r + z_square.i > TRICORN_ESCAPE_RADIUS_SQUARED)
break;
- tmp = zr_square - zi_square + z.r;
- zi = -2.0 * zr * zi + z.i;
- zr = tmp;
+ tmp = z_square.r - z_square.i + z.r;
+ z.i = -2.0 * z.r * z.i + c.i;
+ z.r = tmp;
}
return (n);
}
diff --git a/src/helper.c b/src/helper.c
index fb55a8b..b78669c 100644
--- a/src/helper.c
+++ b/src/helper.c
@@ -6,23 +6,12 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 11:54:57 by cacharle #+# #+# */
-/* Updated: 2020/02/25 16:21:13 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:46:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
-/* void h_offset_to_complex(t_state *state, t_complex *z, int offset) */
-/* { */
-/* int y; // unoptimized */
-/* int x; */
-/* */
-/* y = offset / state->window.width; */
-/* x = offset % state->window.width; */
-/* z->a = y / state->window.height * state->plane.a; */
-/* z->b = x / state->window.width * state->plane.b; */
-/* } */
-
void h_zoom_in(t_state *state)
{
state->plane.r /= ZOOM_SPEED;
diff --git a/src/main.c b/src/main.c
index 2d897e1..22e8c84 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 09:27:44 by cacharle #+# #+# */
-/* Updated: 2020/02/24 16:20:48 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:47:05 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/render.c b/src/render.c
index 16bb920..a57e3f7 100644
--- a/src/render.c
+++ b/src/render.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */
-/* Updated: 2020/02/26 18:21:33 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:40:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,73 +14,77 @@
#define SUPERSAMPLE_SIZE 3
-static t_color st_take_sample(t_state *state, t_complex z, double step_r, double step_i)
+static t_color st_take_sample(t_state *state, t_complex z,
+ double step_r, double step_i)
{
- double u;
- double v;
- t_complex sz;
- t_color color;
- t_color tmp;
- int r;
- int g;
- int b;
+ t_complex epsilon;
+ t_complex sample;
+ t_color color;
+ t_color tmp;
+ uint32_t r;
+ uint32_t g;
+ uint32_t b;
+ int s;
color.hexcode = 0x0;
r = 0;
g = 0;
b = 0;
- u = 0.0;
- while (u < state->samples)
+ epsilon.i = 0.0;
+ while (epsilon.i < state->samples)
{
- v = 0.0;
- while (v < state->samples)
+ epsilon.r = 0.0;
+ while (epsilon.r < state->samples)
{
- sz.i = z.i + step_i * (u / state->samples);
- sz.r = z.r + step_r * (v / state->samples);
- tmp = state->palette[state->func(state, sz)];
-
+ sample.i = z.i + step_i * (epsilon.i / state->samples);
+ sample.r = z.r + step_r * (epsilon.r / state->samples);
+ tmp = state->palette[state->func(state, sample)];
r += tmp.rgb.r;
g += tmp.rgb.g;
b += tmp.rgb.b;
- v += 1.0;
+ epsilon.r += 1.0;
}
- u += 1.0;
+ epsilon.i += 1.0;
}
- int s = (int)(state->samples * state->samples);
+ s = (int)(state->samples * state->samples);
color.rgb.r = r / s;
color.rgb.g = g / s;
color.rgb.b = b / s;
-
return (color);
}
-static void *st_render_routine(void *void_arg)
+static void *st_render_routine(void *arg)
{
- int j;
- t_complex z;
- t_state *state;
- int offset;
- double step_r;
- t_render_routine_arg *arg;
+ int j;
+ t_complex z;
+ t_state *state;
+ double step_r;
- arg = void_arg;
- state = arg->state;
- z = arg->z;
- offset = arg->offset;
- j = -1;
- step_r = state->plane.r / (double)WINDOW_WIDTH;
+ state = ((t_render_routine_arg*)arg)->state;
+ z.i = ((t_render_routine_arg*)arg)->z_i;
+ step_r = state->plane.r / FWINDOW_WIDTH;
z.r = state->center.r - state->plane.r / 2.0;
- while (++j < WINDOW_WIDTH)
- {
- ((t_color*)state->window.data)[offset] = state->samples == 1.0 ?
- state->palette[state->func(state, z)] : st_take_sample(state, z, step_r, arg->step_i);
- offset++;
- z.r += step_r;
- }
+ j = -1;
+ if (state->samples == 1.0)
+ while (++j < WINDOW_WIDTH)
+ {
+ state->window.data[((t_render_routine_arg*)arg)->offset] =
+ state->palette[state->func(state, z)];
+ z.r += step_r;
+ ((t_render_routine_arg*)arg)->offset++;
+ }
+ else
+ while (++j < WINDOW_WIDTH)
+ {
+ state->window.data[((t_render_routine_arg*)arg)->offset] =
+ st_take_sample(state, z, step_r, ((t_render_routine_arg*)arg)->step_i);
+ z.r += step_r;
+ ((t_render_routine_arg*)arg)->offset++;
+ }
return (NULL);
}
-static void st_render_fractal(t_state *state)
+static void st_render_fractal(t_state *state)
{
int i;
pthread_t threads[WINDOW_HEIGHT];
@@ -88,19 +92,20 @@ static void st_render_fractal(t_state *state)
double step_i;
double z_i;
- step_i = state->plane.i / (double)WINDOW_HEIGHT;
+ step_i = state->plane.i / FWINDOW_HEIGHT;
z_i = state->center.i - state->plane.i / 2.0;
i = -1;
while (++i < WINDOW_HEIGHT)
{
routine_args[i].state = state;
routine_args[i].offset = i * WINDOW_WIDTH;
- routine_args[i].z.i = z_i;
+ routine_args[i].z_i = z_i;
routine_args[i].step_i = step_i;
- if (pthread_create(threads + i, NULL, st_render_routine, routine_args + i) < 0)
+ if (pthread_create(threads + i, NULL,
+ st_render_routine, routine_args + i) < 0)
{
state->running = false;
- break;
+ break ;
}
z_i += step_i;
}
@@ -108,8 +113,11 @@ static void st_render_fractal(t_state *state)
pthread_join(threads[i], NULL);
}
-int render_update(t_state *state)
+int render_update(t_state *state)
{
+ char *iterations_str;
+ char *samples_str;
+
if (!state->running)
{
state_destroy(state);
@@ -118,14 +126,14 @@ int render_update(t_state *state)
if (state->updated)
return (0);
st_render_fractal(state);
- mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0);
- char *iterations_str = ft_itoa(state->iterations);
- char *samples_str = ft_itoa((int)state->samples);
- char center_buf[1024];
- sprintf(center_buf, "%f + %fi", state->center.r, state->center.i); // no
- mlx_string_put (state->mlx_ptr, state->window_ptr, 10, 20, 0x000000, iterations_str);
- mlx_string_put (state->mlx_ptr, state->window_ptr, 10, 30, 0x000000, samples_str);
- mlx_string_put (state->mlx_ptr, state->window_ptr, 10, 40, 0x000000, center_buf);
+ mlx_put_image_to_window(state->mlx_ptr, state->window_ptr,
+ state->window.id, 0, 0);
+ iterations_str = ft_itoa(state->iterations);
+ samples_str = ft_itoa((int)state->samples * (int)state->samples);
+ mlx_string_put(state->mlx_ptr, state->window_ptr, 10, 20,
+ 0xeeeeee, iterations_str);
+ mlx_string_put(state->mlx_ptr, state->window_ptr, 10, 30,
+ 0xeeeeee, samples_str);
free(iterations_str);
free(samples_str);
state->updated = true;
diff --git a/src/state.c b/src/state.c
index a02547b..4e6ee09 100644
--- a/src/state.c
+++ b/src/state.c
@@ -6,19 +6,20 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 09:58:01 by cacharle #+# #+# */
-/* Updated: 2020/02/26 18:08:49 by cacharle ### ########.fr */
+/* Updated: 2020/02/27 14:43:39 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
-void state_update_palette(t_state *state)
+void state_update_palette(t_state *state)
{
int i;
t_color_hsl hsl;
free(state->palette);
- if ((state->palette = malloc(sizeof(t_color) * (state->iterations + 1))) == NULL)
+ if ((state->palette = malloc(
+ sizeof(t_color) * (state->iterations + 1))) == NULL)
{
state->running = false;
return ;
@@ -40,7 +41,6 @@ void state_shift_palette(t_state *state)
t_color_hsl hsl;
int shift_size;
- /* printf("yo\n"); */
i = -1;
shift_size = 255 / state->iterations;
while (++i < state->iterations)
@@ -74,26 +74,28 @@ static int st_state_dispatch_func(t_state *state, char *fractal_name)
return (0);
}
-int state_init(t_state *state, char *fractal_name)
+int state_init(t_state *state, char *fractal_name)
{
if (st_state_dispatch_func(state, fractal_name) < 0)
return (-1);
if ((state->mlx_ptr = mlx_init()) == NULL)
return (-1);
- if ((state->window_ptr = mlx_new_window(state->mlx_ptr, WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)) == NULL)
+ if ((state->window_ptr = mlx_new_window(state->mlx_ptr,
+ WINDOW_WIDTH, WINDOW_HEIGHT, 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)
+ 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->window.data = (t_color*)mlx_get_data_addr(state->window.id,
+ &state->window.depth, &state->window.size_line, &state->window.endian);
state->running = true;
state->center.r = 0.0;
state->center.i = 0.0;
state->plane.r = 4.0;
state->plane.i = 4.0;
- state->iterations = 10;
+ state->iterations = 30;
state->palette = NULL;
state->samples = 1.0;
state_update_palette(state);
@@ -101,13 +103,13 @@ int state_init(t_state *state, char *fractal_name)
return (0);
}
-int state_destroy(t_state *state)
+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);
+ 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);
+ mlx_destroy_window(state->mlx_ptr, state->window_ptr);
+ return (-1);
}
diff --git a/test.bmp b/test.bmp
index 508839d..cb7e36c 100644
--- a/test.bmp
+++ b/test.bmp
Binary files differ