aboutsummaryrefslogtreecommitdiff
path: root/src/render.c
blob: e06c967306126c879d2f10b3d94c0ab35189b350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
}