thread stop clean up
This commit is contained in:
27
main.cpp
27
main.cpp
@@ -4,6 +4,7 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
|
#include "raymath.h"
|
||||||
#include "rng.h"
|
#include "rng.h"
|
||||||
#include "spawner.h"
|
#include "spawner.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
@@ -21,13 +22,23 @@ float level_bottom = level_height + padding;
|
|||||||
|
|
||||||
Rectangle walls[] = {};
|
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_speed = 5.0f;
|
||||||
auto player_invulnerability = 1.0f;
|
auto player_invulnerability = 1.0f;
|
||||||
|
|
||||||
uint32_t score;
|
uint32_t score;
|
||||||
|
|
||||||
Tear tears[100] = {};
|
std::array<Tear, 100> tears;
|
||||||
float tear_speed = 10;
|
float tear_speed = 10;
|
||||||
float tear_range = 300;
|
float tear_range = 300;
|
||||||
float tear_radius = 10.0f;
|
float tear_radius = 10.0f;
|
||||||
@@ -127,7 +138,7 @@ struct ItemSpawner final : Spawner<Item, 100> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void spawn() override {
|
void spawn() override {
|
||||||
const auto item_type = static_cast<ItemType>(Rng::generate(ItemType::ITEM_TYPE_COUNT));
|
const auto item_type = static_cast<ItemType>(Rng::generate(ITEM_TYPE_COUNT));
|
||||||
const auto x = Rng::generate(static_cast<int>(level_left), static_cast<int>(level_right));
|
const auto x = Rng::generate(static_cast<int>(level_left), static_cast<int>(level_right));
|
||||||
const auto y = Rng::generate(static_cast<int>(level_top), static_cast<int>(level_bottom));
|
const auto y = Rng::generate(static_cast<int>(level_top), static_cast<int>(level_bottom));
|
||||||
|
|
||||||
@@ -206,13 +217,6 @@ int main() {
|
|||||||
enemy_spawner.start();
|
enemy_spawner.start();
|
||||||
item_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;
|
std::string score_text_buffer;
|
||||||
score_text_buffer.reserve(64);
|
score_text_buffer.reserve(64);
|
||||||
|
|
||||||
@@ -335,7 +339,8 @@ int main() {
|
|||||||
|
|
||||||
const int text_left = static_cast<int>(level_left) + 10;
|
const int text_left = static_cast<int>(level_left) + 10;
|
||||||
|
|
||||||
DrawRectangle(level_left, static_cast<int>(level_height) - 110, 200, 110, {130, 130, 130, 100});
|
DrawRectangle(static_cast<int>(level_left), static_cast<int>(level_height) - 110, 200, 110,
|
||||||
|
{130, 130, 130, 100});
|
||||||
|
|
||||||
std::format_to(std::back_inserter(score_text_buffer), "Score: {}", score);
|
std::format_to(std::back_inserter(score_text_buffer), "Score: {}", score);
|
||||||
DrawText(score_text_buffer.c_str(), text_left, static_cast<int>(level_height) - 100, 20, BLACK);
|
DrawText(score_text_buffer.c_str(), text_left, static_cast<int>(level_height) - 100, 20, BLACK);
|
||||||
|
|||||||
14
spawner.h
14
spawner.h
@@ -11,11 +11,10 @@ template<typename T, size_t N>
|
|||||||
struct Spawner {
|
struct Spawner {
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
std::condition_variable cv;
|
std::condition_variable cv;
|
||||||
std::atomic<bool> running;
|
|
||||||
std::atomic<bool> paused;
|
std::atomic<bool> paused;
|
||||||
std::jthread timer;
|
std::jthread timer;
|
||||||
long long rate_secs;
|
std::atomic<unsigned long long> rate_secs;
|
||||||
T values[N];
|
std::array<T, N> values;
|
||||||
|
|
||||||
explicit Spawner(long long rate_secs);
|
explicit Spawner(long long rate_secs);
|
||||||
|
|
||||||
@@ -44,9 +43,8 @@ void Spawner<T, N>::spawn() {
|
|||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
void Spawner<T, N>::start() {
|
void Spawner<T, N>::start() {
|
||||||
running.store(true);
|
timer = std::jthread([&] (const std::stop_token &st) {
|
||||||
timer = std::jthread([&] {
|
while (!st.stop_requested()) {
|
||||||
while (running.load()) {
|
|
||||||
const auto start = std::chrono::steady_clock::now();
|
const auto start = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
std::unique_lock lock{mutex};
|
std::unique_lock lock{mutex};
|
||||||
@@ -55,14 +53,14 @@ void Spawner<T, N>::start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto next = start + std::chrono::seconds(rate_secs);
|
auto next = start + std::chrono::seconds(rate_secs);
|
||||||
cv.wait_until(lock, next, [&] { return !running.load(); });
|
cv.wait_until(lock, next);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
Spawner<T, N>::~Spawner() {
|
Spawner<T, N>::~Spawner() {
|
||||||
running.store(false);
|
timer.request_stop();
|
||||||
cv.notify_all();
|
cv.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user