From c4b5e0a48835b6bdbf4d3d92fd42b29a22662ed4 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Tue, 16 Jan 2018 01:21:27 +0100 Subject: Mandelbrot drawing possible --- src/defs.h | 8 ++++ src/mandelbrot-zoom.c | 124 +++++++++++++++++++++++++------------------------- src/mandelbrot-zoom.h | 22 ++------- src/render.c | 72 +++++++++++++++++++++++++++++ src/render.h | 45 ++++++++++++++++++ src/sets.c | 46 +++++++++++++++++++ src/sets.h | 18 ++++++++ 7 files changed, 257 insertions(+), 78 deletions(-) create mode 100644 src/render.c create mode 100644 src/render.h create mode 100644 src/sets.c create mode 100644 src/sets.h diff --git a/src/defs.h b/src/defs.h index 66bffb4..1ed2282 100644 --- a/src/defs.h +++ b/src/defs.h @@ -8,8 +8,16 @@ #ifndef DEFS_H_ #define DEFS_H_ +#ifndef NULL +#define NULL 0 +#endif + +#ifndef TRUE #define TRUE 1 +#endif +#ifndef FALSE #define FALSE 0 +#endif typedef unsigned char u8; typedef signed char s8; diff --git a/src/mandelbrot-zoom.c b/src/mandelbrot-zoom.c index 7aecae4..3d15b3d 100644 --- a/src/mandelbrot-zoom.c +++ b/src/mandelbrot-zoom.c @@ -14,118 +14,120 @@ int main(int argc, char **argv) builder = gtk_builder_new(); gtk_builder_add_from_file (builder, "C:/Users/Superleo1810/eclipse-workspace/mandelbrot-zoom/glade/settings.glade", NULL); // gtk = zu behindert für relative pfade - ui.settings = GTK_WINDOW(gtk_builder_get_object(builder, "settings")); + ui_settings.settings = GTK_WINDOW(gtk_builder_get_object(builder, "settings")); //gtk_builder_connect_signals(builder, NULL); - ui.iterationsSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "iterationsSp")); - ui.colorFromBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorFromBtn")); - ui.colorToBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorToBtn")); - ui.exportCb = GTK_CHECK_BUTTON(gtk_builder_get_object(builder, "exportCb")); - ui.gifRd = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "gifRd")); - ui.widthSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "widthSp")); - ui.heightSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "heightSp")); - ui.fpsRenderSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "fpsRenderSp")); - ui.fpsVideoSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "fpsVideoSp")); - ui.bitrateSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "bitrateSp")); - ui.exportTf = GTK_ENTRY(gtk_builder_get_object(builder, "exportTf")); - ui.startBtn = GTK_BUTTON(gtk_builder_get_object(builder, "startBtn")); - ui.exitBtn = GTK_BUTTON(gtk_builder_get_object(builder, "exitBtn")); - ui.colorDialog = GTK_COLOR_SELECTION_DIALOG(gtk_builder_get_object(builder, "colorDialog")); - - gtk_spin_button_set_increments(ui.iterationsSp, 1, 2); - gtk_spin_button_set_increments(ui.widthSp, 1, 2); - gtk_spin_button_set_increments(ui.heightSp, 1, 2); - gtk_spin_button_set_increments(ui.fpsRenderSp, 1, 2); - gtk_spin_button_set_increments(ui.fpsVideoSp, 1, 2); - gtk_spin_button_set_increments(ui.bitrateSp, 1, 2); - - gtk_spin_button_set_value(ui.iterationsSp, 1000); - gtk_spin_button_set_value(ui.widthSp, 1920); - gtk_spin_button_set_value(ui.heightSp, 1080); - gtk_spin_button_set_value(ui.fpsRenderSp, 30); - gtk_spin_button_set_value(ui.fpsVideoSp, 30); - gtk_spin_button_set_value(ui.bitrateSp, 1000); - - gtk_spin_button_set_range(ui.iterationsSp, 1, 16777215); // 3 bytes á 8 bit = 24 bit - gtk_spin_button_set_range(ui.widthSp, 1, 1920); - gtk_spin_button_set_range(ui.heightSp, 1, 1080); - gtk_spin_button_set_range(ui.fpsRenderSp, 1, 60); - gtk_spin_button_set_range(ui.fpsVideoSp, 1, 60); - gtk_spin_button_set_range(ui.bitrateSp, 1, 65535); - - g_signal_connect(ui.iterationsSp, "value-changed", G_CALLBACK(on_iterationsSp_valueChanged), NULL); - g_signal_connect(ui.colorFromBtn, "clicked", G_CALLBACK(on_colorFromBtn_clicked), NULL); - g_signal_connect(ui.colorToBtn, "clicked", G_CALLBACK(on_colorToBtn_clicked), NULL); - g_signal_connect(ui.exportCb, "toggled", G_CALLBACK(on_exportCb_toggled), NULL); - g_signal_connect(ui.widthSp, "value-changed", G_CALLBACK(on_widthSp_valueChanged), NULL); - g_signal_connect(ui.heightSp, "value-changed", G_CALLBACK(on_heightSp_valueChanged), NULL); - g_signal_connect(ui.fpsRenderSp, "value-changed", G_CALLBACK(on_fpsRenderSp_valueChanged), NULL); - g_signal_connect(ui.fpsVideoSp, "value-changed", G_CALLBACK(on_fpsVideoSp_valueChanged), NULL); - g_signal_connect(ui.bitrateSp, "value-changed", G_CALLBACK(on_bitrateSp_valueChanged), NULL); - g_signal_connect(ui.exportTf, "changed", G_CALLBACK(on_exportTf_changed), NULL); - g_signal_connect(ui.startBtn, "clicked", G_CALLBACK(on_startBtn_clicked), NULL); - g_signal_connect(ui.exitBtn, "clicked", G_CALLBACK(on_exitBtn_clicked), NULL); + ui_settings.iterationsSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "iterationsSp")); + ui_settings.colorFromBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorFromBtn")); + ui_settings.colorToBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorToBtn")); + ui_settings.exportCb = GTK_CHECK_BUTTON(gtk_builder_get_object(builder, "exportCb")); + ui_settings.gifRd = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "gifRd")); + ui_settings.widthSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "widthSp")); + ui_settings.heightSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "heightSp")); + ui_settings.fpsRenderSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "fpsRenderSp")); + ui_settings.fpsVideoSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "fpsVideoSp")); + ui_settings.bitrateSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "bitrateSp")); + ui_settings.exportTf = GTK_ENTRY(gtk_builder_get_object(builder, "exportTf")); + ui_settings.startBtn = GTK_BUTTON(gtk_builder_get_object(builder, "startBtn")); + ui_settings.exitBtn = GTK_BUTTON(gtk_builder_get_object(builder, "exitBtn")); + ui_settings.colorDialog = GTK_COLOR_SELECTION_DIALOG(gtk_builder_get_object(builder, "colorDialog")); + + gtk_spin_button_set_increments(ui_settings.iterationsSp, 1, 2); + gtk_spin_button_set_increments(ui_settings.widthSp, 1, 2); + gtk_spin_button_set_increments(ui_settings.heightSp, 1, 2); + gtk_spin_button_set_increments(ui_settings.fpsRenderSp, 1, 2); + gtk_spin_button_set_increments(ui_settings.fpsVideoSp, 1, 2); + gtk_spin_button_set_increments(ui_settings.bitrateSp, 1, 2); + + gtk_spin_button_set_value(ui_settings.iterationsSp, 1000); + gtk_spin_button_set_value(ui_settings.widthSp, 1920); + gtk_spin_button_set_value(ui_settings.heightSp, 1080); + gtk_spin_button_set_value(ui_settings.fpsRenderSp, 30); + gtk_spin_button_set_value(ui_settings.fpsVideoSp, 30); + gtk_spin_button_set_value(ui_settings.bitrateSp, 1000); + + gtk_spin_button_set_range(ui_settings.iterationsSp, 1, 16777215); // 3 bytes á 8 bit = 24 bit + gtk_spin_button_set_range(ui_settings.widthSp, 1, 1920); + gtk_spin_button_set_range(ui_settings.heightSp, 1, 1080); + gtk_spin_button_set_range(ui_settings.fpsRenderSp, 1, 60); + gtk_spin_button_set_range(ui_settings.fpsVideoSp, 1, 60); + gtk_spin_button_set_range(ui_settings.bitrateSp, 1, 65535); + + g_signal_connect(ui_settings.iterationsSp, "value-changed", G_CALLBACK(on_iterationsSp_valueChanged), NULL); + g_signal_connect(ui_settings.colorFromBtn, "clicked", G_CALLBACK(on_colorFromBtn_clicked), NULL); + g_signal_connect(ui_settings.colorToBtn, "clicked", G_CALLBACK(on_colorToBtn_clicked), NULL); + g_signal_connect(ui_settings.exportCb, "toggled", G_CALLBACK(on_exportCb_toggled), NULL); + g_signal_connect(ui_settings.widthSp, "value-changed", G_CALLBACK(on_widthSp_valueChanged), NULL); + g_signal_connect(ui_settings.heightSp, "value-changed", G_CALLBACK(on_heightSp_valueChanged), NULL); + g_signal_connect(ui_settings.fpsRenderSp, "value-changed", G_CALLBACK(on_fpsRenderSp_valueChanged), NULL); + g_signal_connect(ui_settings.fpsVideoSp, "value-changed", G_CALLBACK(on_fpsVideoSp_valueChanged), NULL); + g_signal_connect(ui_settings.bitrateSp, "value-changed", G_CALLBACK(on_bitrateSp_valueChanged), NULL); + g_signal_connect(ui_settings.exportTf, "changed", G_CALLBACK(on_exportTf_changed), NULL); + g_signal_connect(ui_settings.startBtn, "clicked", G_CALLBACK(on_startBtn_clicked), NULL); + g_signal_connect(ui_settings.exitBtn, "clicked", G_CALLBACK(on_exitBtn_clicked), NULL); g_object_unref(builder); - gtk_widget_show(GTK_WIDGET(ui.settings)); + gtk_widget_show(GTK_WIDGET(ui_settings.settings)); gtk_main(); return 0; } void on_iterationsSp_valueChanged() { - config.iterations = gtk_spin_button_get_value(ui.widthSp); + config.iterations = gtk_spin_button_get_value(ui_settings.iterationsSp); } void on_colorFromBtn_clicked() { currentColor = COLOR_FROM; - gtk_dialog_run(ui.colorDialog); + gtk_dialog_run(ui_settings.colorDialog); } void on_colorToBtn_clicked() { currentColor = COLOR_TO; - gtk_dialog_run(ui.colorDialog); + gtk_dialog_run(ui_settings.colorDialog); } void on_exportCb_toggled() { - config.video = gtk_toggle_button_get_active(ui.exportCb); + config.video = gtk_toggle_button_get_active(ui_settings.exportCb); } void on_exportTf_changed() { - config.path = CHAR_PTR(gtk_entry_get_text(ui.exportTf)); + config.path = CHAR_PTR(gtk_entry_get_text(ui_settings.exportTf)); } void on_widthSp_valueChanged() { - config.width = gtk_spin_button_get_value(ui.widthSp); + config.width = gtk_spin_button_get_value(ui_settings.widthSp); } void on_heightSp_valueChanged() { - config.height = gtk_spin_button_get_value(ui.heightSp); + config.height = gtk_spin_button_get_value(ui_settings.heightSp); } void on_fpsRenderSp_valueChanged() { - config.renderFPS = gtk_spin_button_get_value(ui.fpsRenderSp); + config.renderFPS = gtk_spin_button_get_value(ui_settings.fpsRenderSp); } void on_fpsVideoSp_valueChanged() { - config.videoFPS = gtk_spin_button_get_value(ui.fpsVideoSp); + config.videoFPS = gtk_spin_button_get_value(ui_settings.fpsVideoSp); } void on_bitrateSp_valueChanged() { - config.bitrate = gtk_spin_button_get_value(ui.bitrateSp); + config.bitrate = gtk_spin_button_get_value(ui_settings.bitrateSp); } void on_startBtn_clicked() { - printf("config {\n\t.video = %u\n\t.filetype = %u\n\t.width = %u\n\t.height = %u\n\t.renderFPS = %u\n\t.videoFPS = %u\n\t.bitrate = %u\n\t.path = %s\n}\n", config.video, 0, config.width, config.height, config.renderFPS, config.videoFPS, config.bitrate, config.path); + printf("config {\n\t.iterations = %u\n\t.video = %u\n\t.filetype = %u\n\t.width = %u\n\t.height = %u\n\t.renderFPS = %u\n\t.videoFPS = %u\n\t.bitrate = %u\n\t.path = %s\n}\n", config.iterations, config.video, 0, config.width, config.height, config.renderFPS, config.videoFPS, config.bitrate, config.path); + render_init(&config, mandelbrot_r); + render_show(); } void on_exitBtn_clicked() diff --git a/src/mandelbrot-zoom.h b/src/mandelbrot-zoom.h index 5032087..80d3259 100644 --- a/src/mandelbrot-zoom.h +++ b/src/mandelbrot-zoom.h @@ -9,24 +9,12 @@ #define MANDELBROT_ZOOM_H_ #include -#include "defs.h" #include "settings.h" #include "conversion.h" +#include "render.h" +#include "sets.h" -typedef struct config { - u32 iterations; - bool video; - u8 filetype; - u16 width; - u16 height; - u8 renderFPS; - u8 videoFPS; - u32 bitrate; - const char *path; - // TODO: key mapping als option in die struct -} Config; - -typedef struct ui_settings { +typedef struct Ui_settings { GtkWindow *settings; GtkSpinButton *iterationsSp; GtkButton *colorFromBtn; @@ -42,9 +30,9 @@ typedef struct ui_settings { GtkButton *startBtn; GtkButton *exitBtn; GtkColorSelectionDialog *colorDialog; -} ui_settings; +} Ui_settings; -ui_settings ui; +Ui_settings ui_settings; Config config; u8 currentColor; diff --git a/src/render.c b/src/render.c new file mode 100644 index 0000000..e06c967 --- /dev/null +++ b/src/render.c @@ -0,0 +1,72 @@ +/* + * render.c + * + * Created on: 15.01.2018 + * Author: Superleo1810 + */ + +#include "render.h" + +void render_init(Config *config, u32 (*sfunc) (double, double, u32)) +{ + _config = config; + _sfunc = sfunc; + + ui_render.window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL)); + gtk_window_set_default_size(ui_render.window, config->width, config->height); + + ui_render.frame = GTK_FRAME(gtk_frame_new(NULL)); + gtk_container_add(GTK_CONTAINER(ui_render.window), ui_render.frame); + + ui_render.drawing_area = GTK_DRAWING_AREA(gtk_drawing_area_new()); + gtk_widget_set_size_request(GTK_WIDGET(ui_render.drawing_area), config->width, config->height); + g_signal_connect(G_OBJECT(ui_render.drawing_area), "draw", G_CALLBACK(on_draw), NULL); + gtk_container_add(GTK_CONTAINER(ui_render.frame), ui_render.drawing_area); + +} + +void render_show() +{ + gtk_widget_show_all(GTK_WIDGET(ui_render.window)); +} + +void on_draw(GtkWidget *widget, cairo_t *cr, gpointer data) +{ + cairo_surface_t *image; + // TODO: MUSS UNBEDINGT SEPARAT GECALLT WERDEN, SONST KEINE 60 FPS + image = render_surface(); + cairo_set_source_surface(cr, image, 0, 0); + cairo_paint(cr); +} + +cairo_surface_t *render_surface() +{ + cairo_surface_t *result; + unsigned char *current_row; + int stride; + double x_math, y_math; + u32 iterations; + + result = cairo_image_surface_create(CAIRO_FORMAT_RGB24, _config->width, _config->height); + if (cairo_surface_status(result) != CAIRO_STATUS_SUCCESS) + return result; + + cairo_surface_flush(result); + current_row = cairo_image_surface_get_data(result); + stride = cairo_image_surface_get_stride(result); + for (int y = 0; y < _config->height; y++) { + u32 *row = (void *) current_row; + for (int x = 0; x < _config->width; x++) { + x_math = /*x_MIN*/-2.0 + ((double) x * (/*x_MAX*/1.0 - /*x_MIN*/-2.0)) / _config->width; + y_math = /*y_MIN*/-1.0 + ((double) (_config->height - y) * (/*y_MAX*/1.0 - /*y_MIN*/-1.0)) / _config->height; + iterations = _sfunc(x_math, y_math, _config->iterations); + row[x] = (((1<<24)-1)*iterations)/_config->iterations; +// if (iterations > 1<<8) +// printf("schon gruen\n"); + } + + current_row += stride; + } + cairo_surface_mark_dirty(result); + return result; +} diff --git a/src/render.h b/src/render.h new file mode 100644 index 0000000..d9850fd --- /dev/null +++ b/src/render.h @@ -0,0 +1,45 @@ +/* + * render.h + * + * Created on: 15.01.2018 + * Author: Superleo1810 + */ + +#ifndef RENDER_H_ +#define RENDER_H_ + +#include +#include "defs.h" + +typedef struct config { + u32 iterations; + u32 colorFrom; + u32 colorTo; + bool video; + u8 filetype; + u16 width; + u16 height; + u8 renderFPS; + u8 videoFPS; + u32 bitrate; + const char *path; + // TODO: key mapping als option in die struct +} Config; + +typedef struct Ui_render { + GtkWindow *window; + GtkFrame *frame; + GtkDrawingArea *drawing_area; +} Ui_render; + +Config *_config; +Ui_render ui_render; +u32 (*_sfunc) (double, double, u32); + +void render_init(Config *config, u32 (*sfunc) (double, double, u32)); +void render_show(); +cairo_surface_t *render_surface(); + +void on_draw(GtkWidget *widget, cairo_t *cr, gpointer data); + +#endif /* RENDER_H_ */ diff --git a/src/sets.c b/src/sets.c new file mode 100644 index 0000000..6787a6e --- /dev/null +++ b/src/sets.c @@ -0,0 +1,46 @@ +/* + * sets.c + * + * Created on: 15.01.2018 + * Author: Superleo1810 + */ + +#include "sets.h" + +u32 mandelbrot_s(double x, double y, u32 iterations) +{ + double cx = x, cy = y, x2; + u32 m = 0; + while(m <= iterations && (x*x)+(y*y) <= 4) + { + x2 = x; + x = (x*x) - (y*y) + cx; + y = 2.0*x2*y + cy; + m++; + } + return m; +} + +u32 mandelbrot_r(double x, double y, u32 iterations) +{ + return _mandelbrot_r(x, y, 0.0, 0.0, 0, iterations, 4.0); +} + +u32 _mandelbrot_r(double x, double y, double zx, double zy, u32 n, u32 iterations, double threshold) +{ + if ((n <= iterations) && ((zx * zx + zy * zy) < threshold)) { + double zx_new = (zx * zx - zy * zy + x); + double zy_new = (2 * zx * zy + y); + if ((zx_new == zx) && (zy_new == zy)) { + return iterations; + } + n = _mandelbrot_r(x, y, zx_new, zy_new, n + 1, iterations, threshold); + } + return n; +} + +u32 julia(double x, double y, u32 iterations) +{ + // TODO: Julia-Menge + return 0; +} diff --git a/src/sets.h b/src/sets.h new file mode 100644 index 0000000..76d3ace --- /dev/null +++ b/src/sets.h @@ -0,0 +1,18 @@ +/* + * sets.h + * + * Created on: 15.01.2018 + * Author: Superleo1810 + */ + +#ifndef SETS_H_ +#define SETS_H_ + +#include "defs.h" + +u32 mandelbrot_s(double x, double y, u32 iterations); +u32 mandelbrot_r(double x, double y, u32 iterations); +u32 _mandelbrot_r(double x, double y, double zx, double zy, u32 n, u32 iterations, double threshold); +u32 julia(double x, double y, u32 iterations); + +#endif /* SETS_H_ */ -- cgit v1.2.1