aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-26 18:21:47 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-26 18:21:47 +0100
commit7a5632ab67f95c561ce22a19352e963af3077a5b (patch)
treeaac0b07c04d6ac720f83801c7f980f20098a1cfb
parentfcd239d3d5b46d72cf99a7c439a72031a0ef3a9c (diff)
downloadfractol-7a5632ab67f95c561ce22a19352e963af3077a5b.tar.gz
fractol-7a5632ab67f95c561ce22a19352e963af3077a5b.tar.bz2
fractol-7a5632ab67f95c561ce22a19352e963af3077a5b.zip
Added screen capture from cub3d
-rw-r--r--Makefile3
-rw-r--r--include/fractol.h5
-rw-r--r--src/capture.c133
-rw-r--r--src/event.c4
-rw-r--r--src/render.c11
-rw-r--r--src/state.c2
-rw-r--r--test.bmpbin0 -> 5760054 bytes
7 files changed, 152 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index eb45987..199d32f 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 11:17:06 by cacharle ### ########.fr #
+# Updated: 2020/02/26 18:04:14 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -36,6 +36,7 @@ SRC_FILES = main.c \
state.c \
helper.c \
color.c \
+ capture.c \
fractals/mandelbrot.c \
fractals/julia.c \
fractals/tricorn.c \
diff --git a/include/fractol.h b/include/fractol.h
index c46e350..b1e22dc 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 13:23:33 by cacharle ### ########.fr */
+/* Updated: 2020/02/26 18:15:38 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,7 +26,7 @@
# define WINDOW_TITLE "fractol"
-# define FRACTOL_RESOLUTION_MEDIUM
+# define FRACTOL_RESOLUTION_HIGH
# ifdef FRACTOL_RESOLUTION_HIGH
# define WINDOW_WIDTH 1600
@@ -56,6 +56,7 @@
# define MLXK_S 1
# define MLXK_E 14
# define MLXK_R 15
+# define MLXK_C 8
# define MLXK_PLUS 24
# define MLXK_MINUS 27
diff --git a/src/capture.c b/src/capture.c
new file mode 100644
index 0000000..ade572d
--- /dev/null
+++ b/src/capture.c
@@ -0,0 +1,133 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* capture.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/11 13:15:11 by cacharle #+# #+# */
+/* Updated: 2020/02/26 18:13:10 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "fractol.h"
+
+#define IMG_DEPTH 3
+#define FILE_HEADER_SIZE 14
+#define INFO_HEADER_SIZE 40
+
+/*
+** bmp file format:
+** header:
+** file_header:
+** 2: signature = "BM"
+** 4: file size
+** 4: reserved
+** 4: offset to pixel array
+** info_header:
+** 4: header size
+** 4: image width
+** 4: image height
+** 2: number of color planes
+** 2: bits per pixel
+** 4: compression
+** 4: image size
+** 4: horizontal resolution
+** 4: vertical resolution
+** 4: colors in color table
+** 4: important color count
+** data:
+** pixel in rgb format (without alpha component)
+** padding added at the end of each pixel row
+** so the length of the row is a multiple of 4
+*/
+
+static void bmp_write_pixels(int fd, t_image *image, uint8_t *bmp_data)
+{
+ int i;
+ int j;
+ uint8_t padding[3];
+ int padding_size;
+
+ ft_bzero(padding, 3);
+ padding_size = (4 - (image->width * IMG_DEPTH) % 4) % 4;
+ i = image->height;
+ while (--i >= 0)
+ {
+ 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];
+ }
+ 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])
+{
+ int file_size;
+
+ file_size = FILE_HEADER_SIZE + INFO_HEADER_SIZE + (IMG_DEPTH * image->width
+ + ((4 - (image->width * IMG_DEPTH) % 4) % 4)) * image->height;
+ ft_bzero(file_header, FILE_HEADER_SIZE);
+ ft_bzero(info_header, INFO_HEADER_SIZE);
+ file_header[0] = (uint8_t)('B');
+ file_header[1] = (uint8_t)('M');
+ file_header[2] = (uint8_t)(file_size);
+ file_header[3] = (uint8_t)(file_size >> 8);
+ file_header[4] = (uint8_t)(file_size >> 16);
+ file_header[5] = (uint8_t)(file_size >> 24);
+ file_header[10] = (uint8_t)(FILE_HEADER_SIZE + INFO_HEADER_SIZE);
+ info_header[0] = (uint8_t)(INFO_HEADER_SIZE);
+ info_header[4] = (uint8_t)(image->width);
+ info_header[5] = (uint8_t)(image->width >> 8);
+ info_header[6] = (uint8_t)(image->width >> 16);
+ info_header[7] = (uint8_t)(image->width >> 24);
+ info_header[8] = (uint8_t)(image->height);
+ info_header[9] = (uint8_t)(image->height >> 8);
+ info_header[10] = (uint8_t)(image->height >> 16);
+ info_header[11] = (uint8_t)(image->height >> 24);
+ info_header[12] = (uint8_t)(1);
+ 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)
+{
+ int fd;
+ uint8_t *bmp_data;
+
+ if ((fd = open(filename, O_WRONLY | O_CREAT, 0644)) < 0)
+ return (false);
+ if ((bmp_data = malloc(sizeof(unsigned char) *
+ (image->width * IMG_DEPTH))) == NULL)
+ {
+ close(fd);
+ return (false);
+ }
+ write(fd, file_header, FILE_HEADER_SIZE);
+ write(fd, info_header, INFO_HEADER_SIZE);
+ bmp_write_pixels(fd, image, bmp_data);
+ close(fd);
+ return (true);
+}
+
+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);
+}
diff --git a/src/event.c b/src/event.c
index b4ad067..59378b0 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 13:24:24 by cacharle ### ########.fr */
+/* Updated: 2020/02/26 18:20:48 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -59,6 +59,8 @@ int event_keydown(int key, t_state *state)
if (state->samples < 1.0)
state->samples = 1.0;
}
+ else if (key == MLXK_C)
+ capture(state, "test.bmp");
else
return (0);
state->updated = false;
diff --git a/src/render.c b/src/render.c
index 8ef528d..16bb920 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 13:29:04 by cacharle ### ########.fr */
+/* Updated: 2020/02/26 18:21:33 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -119,6 +119,15 @@ int render_update(t_state *state)
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);
+ free(iterations_str);
+ free(samples_str);
state->updated = true;
return (0);
}
diff --git a/src/state.c b/src/state.c
index 2aafee6..a02547b 100644
--- a/src/state.c
+++ b/src/state.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 09:58:01 by cacharle #+# #+# */
-/* Updated: 2020/02/26 13:23:18 by cacharle ### ########.fr */
+/* Updated: 2020/02/26 18:08:49 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/test.bmp b/test.bmp
new file mode 100644
index 0000000..508839d
--- /dev/null
+++ b/test.bmp
Binary files differ