summaryrefslogtreecommitdiff
path: root/main.c
blob: 376b1f85f57345183ff8cb4267caad40a701af1f (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* main.c
 * Created: 2023-09-02
 * Author: Aryadev Chavali
 * Description: Entrypoint of program
 */

#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>
#include <unistd.h>

#include <raylib.h>
#include <raymath.h>

#define WIDTH       1024
#define HEIGHT      1024
#define MAX_THREADS 4
uint64_t MAX_ITER = 1 << 2;

Color cells[WIDTH * HEIGHT];
pthread_t threads[MAX_THREADS];
pthread_mutex_t mutex;

#define SQUARE(a) (a * a)

Color iter_to_colour(size_t iterations)
{
  if (iterations == MAX_ITER)
    return BLACK;

  // quadratic ratio
  float ratio = Remap(iterations * (MAX_ITER - iterations), 0,
                      SQUARE(MAX_ITER) / 4, 0, 1);

  return (Color){255 * SQUARE(ratio), 10, 255 * SQUARE(1 - ratio), 255};
}

struct ThreadArg
{
  size_t x_0, x_t;
  size_t y_0, y_t;
  bool done;
};

void *generate_colours(void *state)
{
  struct ThreadArg *ptr = (struct ThreadArg *)state;
  ptr->done             = false;
  for (size_t i = ptr->x_0; i < ptr->x_t; ++i)
    for (size_t j = ptr->y_0; j < ptr->y_t; ++j)
    {
      Vector2 init   = {Remap(i, 0, WIDTH, -1.5, 0.5),
                        Remap(j, 0, HEIGHT, -1, 1)};
      Vector2 update = {0};
      size_t iterations;
      for (iterations = 0;
           iterations < MAX_ITER && Vector2LengthSqr(update) <= 4; ++iterations)
      {
        double new_x = SQUARE(update.x) - SQUARE(update.y) + init.x;
        update.y     = (2 * update.x * update.y) + init.y;
        update.x     = new_x;
      }
      Color c                = iter_to_colour(iterations);
      cells[(i * WIDTH) + j] = c;
    }
  ptr->done = true;
  return NULL;
}

void threads_start_render(struct ThreadArg *args)
{
  for (size_t i = 0; i < MAX_THREADS; ++i)
    pthread_create(&threads[i], NULL, &generate_colours, &args[i]);
}

bool threads_done(struct ThreadArg *args)
{
  for (size_t i = 0; i < MAX_THREADS; ++i)
    if (!args[i].done)
      return false;
  return true;
}

void threads_cancel_render(void)
{
  for (size_t i = 0; i < MAX_THREADS; ++i)
    pthread_join(threads[i], NULL);
}

#define ZOOM_INC 0.3f
#define MOVE_INC WIDTH / 50
int main(void)
{
  InitWindow(WIDTH, HEIGHT, "Mandelbrot simulation");
  Camera2D camera = {0};
  camera.zoom     = 1.0f;

  Image img = GenImageColor(WIDTH, HEIGHT, BLACK);
  ImageFormat(&img, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
  /* Texture2D texture = LoadTextureFromImage(img); */
  UnloadImage(img);

  SetTargetFPS(60);

  pthread_mutex_init(&mutex, NULL);
  struct ThreadArg args[] = {{0, WIDTH / 2, 0, HEIGHT, false},
                             {WIDTH / 2, WIDTH, 0, HEIGHT / 4, false},
                             {WIDTH / 2, WIDTH, HEIGHT / 4, HEIGHT / 2, false},
                             {WIDTH / 2, WIDTH, HEIGHT / 2, HEIGHT, false}};
  threads_start_render(args);

  const size_t delta = 1;

  for (size_t prev = 0, ticks = 0; !WindowShouldClose(); ++ticks)
  {
    if (IsKeyPressed(KEY_UP) || IsKeyDown(KEY_UP))
      camera.target.y -= MOVE_INC;
    else if (IsKeyPressed(KEY_DOWN) || IsKeyDown(KEY_DOWN))
      camera.target.y += MOVE_INC;
    else if (IsKeyPressed(KEY_RIGHT) || IsKeyDown(KEY_RIGHT))
      camera.target.x += MOVE_INC;
    else if (IsKeyPressed(KEY_LEFT) || IsKeyDown(KEY_LEFT))
      camera.target.x -= MOVE_INC;
    else if (IsKeyPressed(KEY_N) || IsKeyDown(KEY_N))
    {
      camera.zoom += ZOOM_INC;
      if (camera.zoom > 3.0f)
        camera.zoom = 3.0f;
    }
    else if (IsKeyPressed(KEY_M) || IsKeyDown(KEY_M))
    {
      camera.zoom -= ZOOM_INC;
      if (camera.zoom < 0.1f)
        camera.zoom = 0.1f;
    }
    else if (IsKeyPressed(KEY_SPACE))
    {
      while (!threads_done(args))
        continue;
      memset(cells, 0, WIDTH * HEIGHT);
      MAX_ITER *= 2;
      threads_cancel_render();
      threads_start_render(args);
    }

    if (ticks - prev > delta)
    {
      prev = ticks;
      /* UpdateTexture(texture, cells); */
    }

    BeginDrawing();
    BeginMode2D(camera);
    ClearBackground(BLACK);
    char iters[128];
    sprintf(iters, "iterations=%lu", MAX_ITER);
    /* DrawTexture(texture, 1024, 1024, BLUE); */
    for (size_t i = 0; i < WIDTH; ++i)
    {
      for (size_t j = 0; j < HEIGHT; ++j)
      {
        DrawPixel(i, j, cells[(i * WIDTH) + j]);
      }
    }
    DrawText(iters, 200, 200, 50, RAYWHITE);
    EndMode2D();
    EndDrawing();
  }
  threads_cancel_render();
  /* UnloadTexture(texture); */
  CloseWindow();
  return 0;
}