added smarter way to render that could be converted to utilize opencl

parent 27f0ca93
......@@ -4,13 +4,14 @@ DEBUG_CFLAGS = -g -DDEBUG
LDLIBS = $(shell pkg-config --libs sdl2 SDL2_ttf)
CFLAGS := $(CFLAGS) -O3
CFLAGS := $(CFLAGS) -pg
CFLAGS := $(CFLAGS) $(DEBUG_CFLAGS)
SRCDIR=src
BIULDDIR=build
# compiler
#CC = gcc
CC = clang
CC = gcc
#CC = clang
OBJS = $(BIULDDIR)/mouse.o $(BIULDDIR)/ui.o
......
No preview for this file type
No preview for this file type
File added
No preview for this file type
......@@ -7,7 +7,6 @@
#include <stdio.h>
#include <SDL2/SDL.h>
#include <complex.h>
#include<omp.h>
#include "mandelbrot.h"
#include "mouse.h"
......@@ -30,6 +29,71 @@ mandelbrot(Complex c) {
return MAX_ITERATIONS;
}
typedef struct{
short int r;
short int g;
short int b;
} Color;
typedef struct {
Color *pointer;
int size;
}Array;
void
calculate_set(Array *arr, int height, int width, ViewInfo view)
{
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double real = view.x_min + (x * (view.x_max - view.x_min)) / width;
double imag = view.y_min + (y * (view.y_max - view.y_min)) / height;
Complex c = {real, imag};
int iterations = mandelbrot(c);
if (iterations == MAX_ITERATIONS){
arr->pointer[x * width + y].r = 0;
arr->pointer[x * width + y].g = 0;
arr->pointer[x * width + y].b = 0;
} else {
double t = (double)iterations / MAX_ITERATIONS;
t = 0.5 + 0.5 * cos(log(t + 0.0001) * 3.0);
arr->pointer[x * width + y].r = 255 * t;
arr->pointer[x * width + y].g = 255 * t * 0.4;
arr->pointer[x * width + y].b = 255 * t * 0.2;
}
}
}
}
void
render_cl(Array *arr, App *app, ViewInfo view, int start_x, int start_y) {
SDL_SetRenderDrawColor(app->renderer, 0, 0, 50, 255);
SDL_RenderClear(app->renderer);
if (arr->size < app->win_height * app->win_width){
arr->pointer = realloc(arr->pointer, app->win_height * app->win_width * sizeof(Color));
arr->size = app->win_height * app->win_width;
}
calculate_set(arr, app->win_height, app->win_width, view);
for (int x = 0; x < app->win_width; x++) {
for (int y = 0; y < app->win_height; y++) {
SDL_SetRenderDrawColor(app->renderer,
arr->pointer[x * app->win_width + y].r,
arr->pointer[x * app->win_width + y].g,
arr->pointer[x * app->win_width + y].b,
255);
SDL_RenderDrawPoint(app->renderer, x, y);
}
}
}
void
render(App *app, ViewInfo view, int start_x, int end_x, int start_y, int end_y) {
......@@ -103,11 +167,16 @@ main(int argc, char *argv[])
UI ui;
init_ui(&ui);
Array screen = {
malloc(app.win_height * app.win_width * sizeof(Color)),
app.win_height * app.win_width
};
int running = 1;
while (running)
{
frame_start = SDL_GetTicks();
SDL_Event event;
while (SDL_PollEvent(&event))
......@@ -158,12 +227,14 @@ main(int argc, char *argv[])
}
SDL_SetRenderDrawColor(app.renderer, 0xff, 0xff, 0xff, 0xff);
SDL_RenderClear(app.renderer);
//drawing happens here
render(&app,
view,
0, app.win_width,
0,app.win_height);
render_cl(&screen, &app, view, 0, 0);
// render(&app,
// view,
// 0, app.win_width,
// 0,app.win_height);
int msec = SDL_GetTicks() - frame_start;
float fps = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment