From ee5cb71560a41f0caa4b6076854332b17e772bdb Mon Sep 17 00:00:00 2001 From: Grant Horner Date: Mon, 1 Dec 2025 11:56:43 -0500 Subject: [PATCH] thread stop clean up --- main.cpp | 27 ++++++++++++++++----------- spawner.h | 14 ++++++-------- types.h | 3 +-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/main.cpp b/main.cpp index cf3ffb6..d846413 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include #include +#include "raymath.h" #include "rng.h" #include "spawner.h" #include "types.h" @@ -21,13 +22,23 @@ float level_bottom = level_height + padding; Rectangle walls[] = {}; -Player player; +float player_width = 50; +Player player = { + { + .x = level_width / 2 - player_width / 2, + .y = level_height / 2 - player_width / 2, + .width = player_width, + .height = player_width, + }, + 3, + -10, +}; auto player_speed = 5.0f; auto player_invulnerability = 1.0f; uint32_t score; -Tear tears[100] = {}; +std::array tears; float tear_speed = 10; float tear_range = 300; float tear_radius = 10.0f; @@ -127,7 +138,7 @@ struct ItemSpawner final : Spawner { } void spawn() override { - const auto item_type = static_cast(Rng::generate(ItemType::ITEM_TYPE_COUNT)); + const auto item_type = static_cast(Rng::generate(ITEM_TYPE_COUNT)); const auto x = Rng::generate(static_cast(level_left), static_cast(level_right)); const auto y = Rng::generate(static_cast(level_top), static_cast(level_bottom)); @@ -206,13 +217,6 @@ int main() { enemy_spawner.start(); item_spawner.start(); - player.width = 50; - player.height = 50; - player.x = level_width / 2 - player.width / 2; - player.y = level_height / 2 - player.height / 2; - player.lives = 3; - player.last_hit = -10; - std::string score_text_buffer; score_text_buffer.reserve(64); @@ -335,7 +339,8 @@ int main() { const int text_left = static_cast(level_left) + 10; - DrawRectangle(level_left, static_cast(level_height) - 110, 200, 110, {130, 130, 130, 100}); + DrawRectangle(static_cast(level_left), static_cast(level_height) - 110, 200, 110, + {130, 130, 130, 100}); std::format_to(std::back_inserter(score_text_buffer), "Score: {}", score); DrawText(score_text_buffer.c_str(), text_left, static_cast(level_height) - 100, 20, BLACK); diff --git a/spawner.h b/spawner.h index 959d077..77edc82 100644 --- a/spawner.h +++ b/spawner.h @@ -11,11 +11,10 @@ template struct Spawner { std::mutex mutex; std::condition_variable cv; - std::atomic running; std::atomic paused; std::jthread timer; - long long rate_secs; - T values[N]; + std::atomic rate_secs; + std::array values; explicit Spawner(long long rate_secs); @@ -44,9 +43,8 @@ void Spawner::spawn() { template void Spawner::start() { - running.store(true); - timer = std::jthread([&] { - while (running.load()) { + timer = std::jthread([&] (const std::stop_token &st) { + while (!st.stop_requested()) { const auto start = std::chrono::steady_clock::now(); std::unique_lock lock{mutex}; @@ -55,14 +53,14 @@ void Spawner::start() { } auto next = start + std::chrono::seconds(rate_secs); - cv.wait_until(lock, next, [&] { return !running.load(); }); + cv.wait_until(lock, next); } }); } template Spawner::~Spawner() { - running.store(false); + timer.request_stop(); cv.notify_all(); } diff --git a/types.h b/types.h index 2ffb9ea..746b0f1 100644 --- a/types.h +++ b/types.h @@ -4,9 +4,8 @@ #pragma once -#include +#include -#include "raymath.h" #include "raylib.h" struct Player : Rectangle {