aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-19 13:22:59 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-19 13:22:59 +0200
commit6a80b1b70ec069b051c0e31aafac6eb596e20261 (patch)
treeedf9856bcd2b4415796a2a49f0232ba830e69560 /src
parentc5008a4e62fb83eb71f5f94f622c01f2d8fe8b6b (diff)
downloadmandelbrot_cpu-6a80b1b70ec069b051c0e31aafac6eb596e20261.tar.gz
mandelbrot_cpu-6a80b1b70ec069b051c0e31aafac6eb596e20261.tar.bz2
mandelbrot_cpu-6a80b1b70ec069b051c0e31aafac6eb596e20261.zip
Back to basic SDL application boilerplate
Diffstat (limited to 'src')
-rw-r--r--src/color.c22
-rw-r--r--src/error.c13
-rw-r--r--src/event.c56
-rw-r--r--src/main.c12
-rw-r--r--src/mandelbrot.c22
-rw-r--r--src/state.c34
6 files changed, 159 insertions, 0 deletions
diff --git a/src/color.c b/src/color.c
new file mode 100644
index 0000000..f758595
--- /dev/null
+++ b/src/color.c
@@ -0,0 +1,22 @@
+#include "mandel.h"
+
+/* static Color *create_palette(Color start, Color end) */
+/* { */
+ /* int red_step = abs(end.rgb.r - start.rgb.r) / MAX_ITERATION; */
+ /* int green_step = abs(end.rgb.g - start.rgb.g) / MAX_ITERATION; */
+ /* int blue_step = abs(end.rgb.b - start.rgb.b) / MAX_ITERATION; */
+ /* */
+ /* Color *palette = (Color*)malloc(sizeof(Color) * (MAX_ITERATION + 1)); */
+ /* if (palette == NULL) */
+ /* return NULL; */
+ /* for (int i = 0; i < MAX_ITERATION; i++) */
+ /* { */
+ /* #<{(| palette[i] = helper_HSL_to_RGB(i, 0.6, 1.0); |)}># */
+ /* #<{(| printf("%x\n", palette[i].hexcode); |)}># */
+ /* palette[i].rgb.r = i * red_step + start.rgb.r; */
+ /* palette[i].rgb.g = i * green_step + start.rgb.g; */
+ /* palette[i].rgb.b = i * blue_step + start.rgb.b; */
+ /* } */
+ /* palette[MAX_ITERATION].hexcode = 0x0; */
+ /* return palette; */
+/* } */
diff --git a/src/error.c b/src/error.c
new file mode 100644
index 0000000..92e3a71
--- /dev/null
+++ b/src/error.c
@@ -0,0 +1,13 @@
+#include "mandel.h"
+
+void error_check_sdl(const char *code, const char *filename, int line_num)
+{
+ const char *err;
+
+ err = SDL_GetError();
+ if (*err == '\0')
+ return ;
+ SDL_Log("[ERROR SDL] %s\n\t(%s) at %s:%d", err, code, filename, line_num);
+ SDL_Quit();
+ exit(EXIT_FAILURE);
+}
diff --git a/src/event.c b/src/event.c
new file mode 100644
index 0000000..d0354eb
--- /dev/null
+++ b/src/event.c
@@ -0,0 +1,56 @@
+#include "mandel.h"
+
+void event_handle(State *state)
+{
+ SDL_Event e;
+
+ while (SDL_PollEvent(&e))
+ {
+ switch (e.type)
+ {
+ case SDL_QUIT:
+ state->running = false;
+ break;
+ case SDL_KEYDOWN:
+ switch (e.key.keysym.sym)
+ {
+ case SDLK_UP:
+ case SDLK_k:
+
+ break;
+ case SDLK_DOWN:
+ case SDLK_j:
+
+ break;
+ case SDLK_LEFT:
+ case SDLK_h:
+
+ break;
+ case SDLK_RIGHT:
+ case SDLK_l:
+
+ break;
+ case SDLK_PLUS:
+ case SDLK_p:
+
+ break;
+ case SDLK_MINUS:
+ case SDLK_m:
+
+ break;
+ case SDLK_q:
+ state->running = false;
+ }
+ break;
+ case SDL_MOUSEWHEEL:
+ if (e.wheel.y == -1)
+ ;
+ else if (e.wheel.y == 1)
+ ;
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ break;
+ }
+ }
+}
+
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..c19d32a
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,12 @@
+#include "mandel.h"
+
+int main(void)
+{
+ State state;
+
+ if (!state_init(&state))
+ return (1);
+ state_run(&state);
+ state_quit(&state);
+ return EXIT_SUCCESS;
+}
diff --git a/src/mandelbrot.c b/src/mandelbrot.c
new file mode 100644
index 0000000..a996b26
--- /dev/null
+++ b/src/mandelbrot.c
@@ -0,0 +1,22 @@
+#include "mandel.h"
+
+int mandelbrot(double ca, double cb)
+{
+ double zr = ca;
+ double zi = cb;
+ double zr_square;
+ double zi_square;
+ int n;
+ for (n = 0; n < 20; n++)
+ {
+ zi_square = zi * zi;
+ zr_square = zr * zr;
+ if (zr_square + zi_square > 4)
+ return n;
+ zi = 2 * zr * zi;
+ zr = zr_square - zi_square;
+ zi += cb;
+ zr += ca;
+ }
+ return n;
+}
diff --git a/src/state.c b/src/state.c
new file mode 100644
index 0000000..ee68b77
--- /dev/null
+++ b/src/state.c
@@ -0,0 +1,34 @@
+#include "config.h"
+#include "mandel.h"
+
+bool state_init(State *state)
+{
+ SDL_CALL(SDL_Init(SDL_INIT_VIDEO));
+ SDL_CALL(state->window = SDL_CreateWindow(
+ MANDEL_WINDOW_TITLE,
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ MANDEL_WINDOW_WIDTH,
+ MANDEL_WINDOW_HEIGHT,
+ 0));
+ SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0));
+ state->running = true;
+ return state;
+}
+
+void state_run(State *state)
+{
+ while (state->running)
+ {
+ event_handle(state);
+ }
+}
+
+void state_quit(State *state)
+{
+ /* free(state->palette); */
+ /* SDL_FreeSurface(state->surface); */
+ SDL_DestroyRenderer(state->renderer);
+ SDL_DestroyWindow(state->window);
+ SDL_Quit();
+}