try fix memory error
This commit is contained in:
parent
7584609f33
commit
012d021a08
15
src/Game.cpp
15
src/Game.cpp
@ -1,3 +1,4 @@
|
|||||||
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "GameRenderer.hpp"
|
#include "GameRenderer.hpp"
|
||||||
@ -17,7 +18,8 @@ Game::~Game()
|
|||||||
void Game::update(std::stop_token stop_token)
|
void Game::update(std::stop_token stop_token)
|
||||||
{
|
{
|
||||||
while (!stop_token.stop_requested()) {
|
while (!stop_token.stop_requested()) {
|
||||||
game_mutex.lock();
|
{
|
||||||
|
auto lock = std::lock_guard(game_mutex);
|
||||||
|
|
||||||
ticks++;
|
ticks++;
|
||||||
|
|
||||||
@ -38,8 +40,7 @@ void Game::update(std::stop_token stop_token)
|
|||||||
renderer.redraw();
|
renderer.redraw();
|
||||||
|
|
||||||
map.check_bounds(player.x, player.y);
|
map.check_bounds(player.x, player.y);
|
||||||
|
}
|
||||||
game_mutex.unlock();
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(16666us);
|
std::this_thread::sleep_for(16666us);
|
||||||
}
|
}
|
||||||
@ -47,7 +48,7 @@ void Game::update(std::stop_token stop_token)
|
|||||||
|
|
||||||
void Game::draw()
|
void Game::draw()
|
||||||
{
|
{
|
||||||
const std::lock_guard lock(game_mutex);
|
std::lock_guard lock(game_mutex);
|
||||||
|
|
||||||
renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF);
|
renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF);
|
||||||
|
|
||||||
@ -64,12 +65,15 @@ void Game::draw()
|
|||||||
|
|
||||||
void Game::run()
|
void Game::run()
|
||||||
{
|
{
|
||||||
update_thread = std::jthread(std::bind_front(&Game::update, this));
|
auto update_thread = std::jthread(std::bind_front(&Game::update, this));
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
SDL_WaitEvent(&e);
|
SDL_WaitEvent(&e);
|
||||||
|
|
||||||
|
{
|
||||||
|
auto lock = std::lock_guard(game_mutex);
|
||||||
|
|
||||||
if (e.type == SDL_QUIT) {
|
if (e.type == SDL_QUIT) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -119,6 +123,7 @@ void Game::run()
|
|||||||
int mouse_x, mouse_y;
|
int mouse_x, mouse_y;
|
||||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||||
player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2);
|
player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2);
|
||||||
|
}
|
||||||
|
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,6 @@ private:
|
|||||||
std::vector<Arrow*> arrows;
|
std::vector<Arrow*> arrows;
|
||||||
std::vector<Zombo*> zombos;
|
std::vector<Zombo*> zombos;
|
||||||
|
|
||||||
std::jthread update_thread;
|
|
||||||
std::mutex game_mutex;
|
std::mutex game_mutex;
|
||||||
unsigned int ticks = 0;
|
unsigned int ticks = 0;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user