summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-09-03 19:54:40 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-09-03 20:00:02 +0100
commitab7db03aaa0490e0fac7a00add0b85b304553c78 (patch)
treef9e07a944a75d1026ff8b4644f0f5281941dfb5c
parent913e540b9266dc0d4ba3dbd57c4fe0abdf138c4e (diff)
downloadoreobrot-ab7db03aaa0490e0fac7a00add0b85b304553c78.tar.gz
oreobrot-ab7db03aaa0490e0fac7a00add0b85b304553c78.tar.bz2
oreobrot-ab7db03aaa0490e0fac7a00add0b85b304553c78.zip
Figured out how to use textures, changed colouring algorithm
Less intense on the main thread now, no memory leaks so far. To ensure we're not wasting cycles on updating an already completed texture, I use both threads_done and rendered which allows for checking if a new render batch has been started or not. This allows us to only update the texture if rendering has not completed.
-rw-r--r--main.c53
1 files changed, 27 insertions, 26 deletions
diff --git a/main.c b/main.c
index 376b1f8..4ae63d9 100644
--- a/main.c
+++ b/main.c
@@ -23,7 +23,6 @@ uint64_t MAX_ITER = 1 << 2;
Color cells[WIDTH * HEIGHT];
pthread_t threads[MAX_THREADS];
-pthread_mutex_t mutex;
#define SQUARE(a) (a * a)
@@ -36,7 +35,8 @@ Color iter_to_colour(size_t iterations)
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};
+ return (Color){255 * SQUARE(ratio), 255 * SQUARE(SQUARE(ratio)), 255 * ratio,
+ 255};
}
struct ThreadArg
@@ -64,8 +64,7 @@ void *generate_colours(void *state)
update.y = (2 * update.x * update.y) + init.y;
update.x = new_x;
}
- Color c = iter_to_colour(iterations);
- cells[(i * WIDTH) + j] = c;
+ cells[(i * WIDTH) + j] = iter_to_colour(iterations);
}
ptr->done = true;
return NULL;
@@ -99,21 +98,23 @@ int main(void)
Camera2D camera = {0};
camera.zoom = 1.0f;
- Image img = GenImageColor(WIDTH, HEIGHT, BLACK);
- ImageFormat(&img, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
- /* Texture2D texture = LoadTextureFromImage(img); */
- UnloadImage(img);
+ Image img = {.data = cells,
+ .width = WIDTH,
+ .height = HEIGHT,
+ .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
+ .mipmaps = 1};
+ Texture2D texture = LoadTextureFromImage(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;
+ const size_t delta = 10;
+ bool rendered = false;
for (size_t prev = 0, ticks = 0; !WindowShouldClose(); ++ticks)
{
@@ -139,39 +140,39 @@ int main(void)
}
else if (IsKeyPressed(KEY_SPACE))
{
- while (!threads_done(args))
- continue;
+ threads_cancel_render();
memset(cells, 0, WIDTH * HEIGHT);
MAX_ITER *= 2;
- threads_cancel_render();
+ rendered = false;
threads_start_render(args);
}
if (ticks - prev > delta)
{
- prev = ticks;
- /* UpdateTexture(texture, cells); */
+ prev = ticks;
+ bool done = threads_done(args);
+ if (!done || !rendered)
+ UpdateTexture(texture, cells);
+ else if (done)
+ rendered = true;
}
+ char iters[128];
+ sprintf(iters, "iterations=%lu", MAX_ITER);
+
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]);
- }
- }
+ DrawTexturePro(texture, (Rectangle){0, 0, WIDTH, HEIGHT},
+ (Rectangle){WIDTH / 2, HEIGHT / 2, WIDTH, HEIGHT},
+ (Vector2){WIDTH / 2, HEIGHT / 2}, -90, WHITE);
DrawText(iters, 200, 200, 50, RAYWHITE);
EndMode2D();
EndDrawing();
}
+ write_to_png("test.png");
threads_cancel_render();
- /* UnloadTexture(texture); */
+ UnloadTexture(texture);
CloseWindow();
return 0;
}