-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pre-calculating colors to save on runtime performance
- Loading branch information
1 parent
0df63d8
commit 0633e91
Showing
8 changed files
with
269 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,13 @@ | |
# By: mstrauss <[email protected]> +#+ +:+ +#+ # | ||
# +#+#+#+#+#+ +#+ # | ||
# Created: 2024/03/15 17:50:53 by mstrauss #+# #+# # | ||
# Updated: 2024/03/28 20:28:05 by mstrauss ### ########.fr # | ||
# Updated: 2024/04/03 21:38:28 by mstrauss ### ########.fr # | ||
# # | ||
# **************************************************************************** # | ||
|
||
NAME = $(BIN_DIR)/fractol | ||
|
||
SRCS= $(addprefix $(SRC_DIR)/, main.c julia_math.c mandelbrot_math.c) | ||
SRCS= $(addprefix $(SRC_DIR)/, main.c julia_math.c mandelbrot_math.c utils.c hooks.c colors.c) | ||
|
||
OBJECTS = $(SRCS:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* colors.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: mstrauss <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/03 18:21:46 by mstrauss #+# #+# */ | ||
/* Updated: 2024/04/03 19:42:36 by mstrauss ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "fractol.h" | ||
|
||
/// @brief Calculates all possible color values | ||
/// @param fractol data struct | ||
void pre_calc_colors(t_fractol *fractol) | ||
{ | ||
int n; | ||
float hue; | ||
|
||
n = 0; | ||
while (n <= MAX_ITER) | ||
{ | ||
fractol->colors[n] = hsl_to_rgb(((float)n / MAX_ITER) * 360, 1.0, 0.5); | ||
n++; | ||
} | ||
} | ||
|
||
/// @brief Converts HSL color space to RGB color space | ||
/// @param h Hue, a value between 0 and 360 | ||
/// @param s Saturation, a value between 0 and 1 | ||
/// @param l Lightness, a value between 0 and 1 | ||
/// @return RGB color | ||
int hsl_to_rgb(float h, float s, float l) | ||
{ | ||
float q; | ||
float p; | ||
float r; | ||
float g; | ||
float b; | ||
|
||
if (s == 0) | ||
{ | ||
r = l; | ||
g = l; | ||
b = l; | ||
} | ||
else | ||
{ | ||
q = l < 0.5 ? l * (1 + s) : l + s - l * s; | ||
p = 2 * l - q; | ||
r = hue_to_rgb(p, q, h + 1 / 3.0); | ||
g = hue_to_rgb(p, q, h); | ||
b = hue_to_rgb(p, q, h - 1 / 3.0); | ||
} | ||
return ((int)(r * 255) << 24 | (int)(g * 255) << 16 | (int)(b | ||
* 255) << 8 | 255); | ||
} | ||
|
||
/// @brief Helper function for HSL to RGB conversion | ||
/// @param p, q, t float values | ||
/// @return float value | ||
float hue_to_rgb(float p, float q, float t) | ||
{ | ||
if (t < 0) | ||
t += 1; | ||
if (t > 1) | ||
t -= 1; | ||
if (t < 1 / 6.0) | ||
return (p + (q - p) * 6 * t); | ||
if (t < 1 / 2.0) | ||
return (q); | ||
if (t < 2 / 3.0) | ||
return (p + (q - p) * (2 / 3.0 - t) * 6); | ||
return (p); | ||
} | ||
|
||
/// @brief Calculates the RGBA value | ||
/// @param r red value, 0 - 255 | ||
/// @param g green value, 0 - 255 | ||
/// @param b blue value, 0 - 255 | ||
/// @param a opacity, 0 - 255 | ||
/// @return RGBA value | ||
int get_rgb(int r, int g, int b, int a) | ||
{ | ||
return (r << 24 | g << 16 | b << 8 | a); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: mstrauss <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/03/15 17:47:51 by mstrauss #+# #+# */ | ||
/* Updated: 2024/03/29 18:26:20 by mstrauss ### ########.fr */ | ||
/* Updated: 2024/04/03 21:25:36 by mstrauss ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -25,16 +25,22 @@ | |
# include <stdio.h> | ||
# include <stdlib.h> | ||
|
||
/* ----------------------------- Bytes Per Pixel ---------------------------- */ | ||
|
||
// # ifndef BPP | ||
// # define BPP sizeof(int32_t) | ||
// # endif | ||
|
||
/* ------------------------------ window width ------------------------------ */ | ||
|
||
# ifndef WIDTH | ||
# define WIDTH 600 | ||
# define WIDTH 1000 | ||
# endif | ||
|
||
/* ------------------------------ window height ----------------------------- */ | ||
|
||
# ifndef HEIGHT | ||
# define HEIGHT 600 | ||
# define HEIGHT 1000 | ||
# endif | ||
|
||
/* ------------------------------ max iterations ---------------------------- */ | ||
|
@@ -68,6 +74,7 @@ typedef struct s_fractol | |
mlx_image_t *img; | ||
mlx_t *mlx; | ||
t_math *math; | ||
int colors[MAX_ITER]; | ||
double zoom; | ||
double w_width; | ||
double w_height; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* hooks.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: mstrauss <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/03 15:44:18 by mstrauss #+# #+# */ | ||
/* Updated: 2024/04/03 21:23:54 by mstrauss ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "fractol.h" | ||
|
||
void my_key_func(mlx_key_data_t mkd, void *data) | ||
{ | ||
t_fractol *fractol; | ||
|
||
fractol = (t_fractol *)data; | ||
if (mkd.key == MLX_KEY_ESCAPE) | ||
exit(-1); | ||
(void)data; | ||
} | ||
|
||
void my_scroll_func(double xdelta, double ydelta, void *param) | ||
{ | ||
t_fractol *fractol; | ||
|
||
(void)xdelta; | ||
fractol = (t_fractol *)param; | ||
if (ydelta > 0) | ||
fractol->zoom *= 1.1; | ||
if (ydelta < 0) | ||
fractol->zoom *= 0.9; | ||
redraw(fractol); | ||
} | ||
|
||
void my_resize_func(int32_t width, int32_t height, void *param) | ||
{ | ||
t_fractol *fractol; | ||
|
||
fractol = (t_fractol *)param; | ||
fractol->w_width = width; | ||
fractol->w_height = height; | ||
redraw(fractol); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,50 @@ | |
/* +:+ +:+ +:+ */ | ||
/* By: mstrauss <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/03/26 16:04:25 by mstrauss #+# #+# */ | ||
/* Updated: 2024/03/28 16:07:57 by mstrauss ### ########.fr */ | ||
/* Created: 2024/04/03 17:45:07 by mstrauss #+# #+# */ | ||
/* Updated: 2024/04/03 17:48:23 by mstrauss ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "fractol.h" | ||
|
||
void julia(t_math *math, t_fractol *fractol) | ||
{ | ||
(void)math; | ||
(void)fractol; | ||
uint32_t color; | ||
|
||
math->x = -1; | ||
while (++(math->x) < fractol->w_width) | ||
{ | ||
math->y = -1; | ||
while (++(math->y) < fractol->w_height) | ||
{ | ||
mandelbrot_subroutine(math, fractol); | ||
if (math->n >= MAX_ITER) | ||
color = get_rgb(0, 0, 0, 0); | ||
else | ||
color = get_rgb(0, 255, 0, math->n * 255 / MAX_ITER); | ||
mlx_put_pixel(fractol->img, math->x, math->y, color); | ||
} | ||
} | ||
mlx_image_to_window(fractol->mlx, fractol->img, 0, 0); | ||
} | ||
|
||
void alternate_julia(t_math *math, t_fractol *fractol) | ||
void julia_subroutine(t_math *math, t_fractol *fractol) | ||
{ | ||
(void)math; | ||
(void)fractol; | ||
math->a = (math->x - fractol->w_width / 2.0) * (4.0 / fractol->w_width) | ||
/ fractol->zoom + fractol->offset_x; | ||
math->b = (math->y - fractol->w_height / 2.0) * (4.0 / fractol->w_height) | ||
/ fractol->zoom + fractol->offset_y; | ||
math->ca = math->a; | ||
math->cb = math->b; | ||
math->n = -1; | ||
while (++(math->n) < MAX_ITER) | ||
{ | ||
math->aa = math->a * math->a - math->b * math->b; | ||
math->bb = 2 * math->a * math->b; | ||
math->a = math->aa + math->ca; | ||
math->b = math->bb + math->cb; | ||
if (math->a * math->a + math->b * math->b > 4) | ||
break ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,60 +6,16 @@ | |
/* By: mstrauss <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/03/15 17:47:28 by mstrauss #+# #+# */ | ||
/* Updated: 2024/03/29 18:37:52 by mstrauss ### ########.fr */ | ||
/* Updated: 2024/04/03 21:36:09 by mstrauss ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "fractol.h" | ||
|
||
void prompt_user(void) | ||
{ | ||
pf_printf("\033[0;36m\n ░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒\ | ||
░▒▓██████▓▒░░▒▓█▓▒░ \n\033[0m"); | ||
pf_printf("\033[0;36m ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░\ | ||
▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ \n\033[0m"); | ||
pf_printf("\033[0;36m ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░ ░\ | ||
▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ \n\033[0m"); | ||
pf_printf("\033[0;36m ░▒▓██████▓▒░ ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░ ░▒▓█▓▒░ ░\ | ||
▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ \n\033[0m"); | ||
pf_printf("\033[0;36m ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░ ░\ | ||
▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ \n\033[0m"); | ||
pf_printf("\033[0;36m ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░\ | ||
▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ \n\033[0m"); | ||
pf_printf("\033[0;36m ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░ \ | ||
░▒▓██████▓▒░░▒▓████████▓▒░ \033[0m \033[0;35mby @mstrauss\n\n\033[0m"); | ||
// pf_printf("\n#######################################\n"); | ||
pf_printf(" !!! INVALID PARAMETERS PROVIDED !!! \n\n"); | ||
// pf_printf("#######################################\n\n"); | ||
pf_printf("OPTIONS:\n"); | ||
pf_printf("---------------------------------------\n"); | ||
pf_printf("| \"-m\" or \"--mandelbrot\": |\n"); | ||
pf_printf("| Displays the Mandelbrot set fractal |\n"); | ||
pf_printf("| |\n"); | ||
pf_printf("| \"-j\" or \"--julia\": |\n"); | ||
pf_printf("| Displays the Julia set fractal |\n"); | ||
pf_printf("---------------------------------------\n\n"); | ||
exit(1); | ||
} | ||
|
||
int get_param(char **argv, t_fractol *fractol) | ||
{ | ||
if (ft_strncmp(argv[1], "-m", 2) || ft_strncmp(argv[1], "--mandelbrot", 12)) | ||
return (fractol->fractal_set = 1, 1); | ||
if (ft_strncmp(argv[1], "-j", 2) || ft_strncmp(argv[1], "--julia", 7)) | ||
return (fractol->fractal_set = 2, 2); | ||
if (ft_strncmp(argv[1], "-j", 2) || ft_strncmp(argv[1], "--julia", 7)) | ||
// conditions fuer alternate julia hinzufuegen | ||
return (fractol->fractal_set = 3, 3); | ||
else | ||
return (0); | ||
} | ||
|
||
int get_rgb(int r, int g, int b, int a) | ||
{ | ||
return (r << 24 | g << 16 | b << 8 | a); | ||
} | ||
|
||
/// @brief initializes the fractol struct with default values | ||
/// @param fractol Data struct | ||
/// @param mlx MLX struct | ||
/// @param img Image | ||
void init_fractol(t_fractol *fractol, mlx_t *mlx, mlx_image_t *img) | ||
{ | ||
fractol->img = img; | ||
|
@@ -68,61 +24,10 @@ void init_fractol(t_fractol *fractol, mlx_t *mlx, mlx_image_t *img) | |
fractol->zoom = 1; | ||
fractol->w_width = WIDTH; | ||
fractol->w_height = HEIGHT; | ||
fractol->offset_x = -0.5; | ||
fractol->offset_x = -1.40117; | ||
fractol->offset_y = 0; | ||
} | ||
|
||
void my_key_func(mlx_key_data_t mkd, void *data) | ||
{ | ||
t_fractol *fractol; | ||
|
||
fractol = (t_fractol *)data; | ||
if (mkd.key == MLX_KEY_ESCAPE) | ||
exit(-1); | ||
(void)data; | ||
} | ||
|
||
void my_scroll_func(double xdelta, double ydelta, void *param) | ||
{ | ||
t_fractol *fractol; | ||
|
||
(void)xdelta; | ||
fractol = (t_fractol *)param; | ||
if (ydelta > 0) | ||
fractol->zoom += (ydelta / 4); // Adjust ydelta for zoom speed | ||
if (ydelta < 0) | ||
fractol->zoom += (ydelta / 4); | ||
if (fractol->fractal_set == 1) | ||
mandelbrot(fractol->math, fractol); | ||
else if (fractol->fractal_set == 2) | ||
julia(fractol->math, fractol); | ||
else if (fractol->fractal_set == 3) | ||
alternate_julia(fractol->math, fractol); | ||
} | ||
|
||
void my_resize_func(int32_t width, int32_t height, void *param) | ||
{ | ||
t_fractol *fractol; | ||
|
||
fractol = (t_fractol *)param; | ||
fractol->w_width = width; | ||
fractol->w_height = height; | ||
// redraw(fractol); | ||
} | ||
|
||
void redraw(t_fractol *fractol) | ||
{ | ||
mlx_delete_image(fractol->mlx, fractol->img); | ||
fractol->img = mlx_new_image(fractol->mlx, fractol->w_width, | ||
fractol->w_height); | ||
if (fractol->fractal_set == 1) | ||
mandelbrot(fractol->math, fractol); | ||
else if (fractol->fractal_set == 2) | ||
julia(fractol->math, fractol); | ||
else if (fractol->fractal_set == 3) | ||
alternate_julia(fractol->math, fractol); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
mlx_t *mlx; | ||
|
@@ -131,6 +36,8 @@ int main(int argc, char **argv) | |
|
||
if (argc < 2) | ||
prompt_user(); | ||
mlx_set_setting(MLX_STRETCH_IMAGE, true); | ||
// mlx_set_setting(MLX_MAXIMIZED, true); | ||
mlx = mlx_init(WIDTH, HEIGHT, "fractOOOOOOl", true); | ||
if (!mlx) | ||
return (EXIT_FAILURE); | ||
|
Oops, something went wrong.