Add jthread and fix warnings
This commit is contained in:
parent
80a2ba78a2
commit
d2476fa5df
2
Makefile
2
Makefile
@ -23,7 +23,7 @@ LINKER_FLAGS += $(shell pkg-config sdl2 SDL2_image --libs)
|
|||||||
|
|
||||||
build/%.o: %.cpp $(wildcard *.hpp)
|
build/%.o: %.cpp $(wildcard *.hpp)
|
||||||
@mkdir -p `dirname $@`
|
@mkdir -p `dirname $@`
|
||||||
g++ $(CXXFLAGS) -c -o $@ $<
|
g++ $(CXX_FLAGS) -c -o $@ $<
|
||||||
|
|
||||||
all: $(OBJS)
|
all: $(OBJS)
|
||||||
g++ $(OBJS) $(LINKER_FLAGS)
|
g++ $(OBJS) $(LINKER_FLAGS)
|
||||||
|
|||||||
16
src/Game.cpp
16
src/Game.cpp
@ -1,21 +1,15 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
#include "GameRenderer.hpp"
|
#include "GameRenderer.hpp"
|
||||||
#include "Player.hpp"
|
#include "Player.hpp"
|
||||||
#include "Game.hpp"
|
#include "Game.hpp"
|
||||||
|
|
||||||
using namespace std::literals::chrono_literals;
|
using namespace std::literals::chrono_literals;
|
||||||
|
|
||||||
Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
|
void Game::update(std::stop_token stop_token)
|
||||||
|
|
||||||
Game::~Game()
|
|
||||||
{
|
{
|
||||||
stopping = true;
|
while (!stop_token.stop_requested()) {
|
||||||
update_thread.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Game::update()
|
|
||||||
{
|
|
||||||
while (!stopping) {
|
|
||||||
player.update();
|
player.update();
|
||||||
|
|
||||||
renderer.redraw();
|
renderer.redraw();
|
||||||
@ -39,7 +33,7 @@ void Game::draw() const
|
|||||||
|
|
||||||
void Game::run()
|
void Game::run()
|
||||||
{
|
{
|
||||||
update_thread = std::thread(&Game::update, this);
|
update_thread = std::jthread(std::bind_front(&Game::update, this));
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
|
|||||||
@ -13,15 +13,14 @@ private:
|
|||||||
Player player;
|
Player player;
|
||||||
Map map;
|
Map map;
|
||||||
|
|
||||||
std::thread update_thread;
|
std::jthread update_thread;
|
||||||
bool stopping = false;
|
|
||||||
|
|
||||||
void update();
|
void update(std::stop_token stop_token);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Game();
|
Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
|
||||||
|
|
||||||
~Game();
|
~Game() = default;
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
|
|||||||
@ -41,7 +41,7 @@ GameRenderer::~GameRenderer()
|
|||||||
|
|
||||||
Sprite GameRenderer::load_sprite(const std::string &file, const int width, const int height) const
|
Sprite GameRenderer::load_sprite(const std::string &file, const int width, const int height) const
|
||||||
{
|
{
|
||||||
return (Sprite) {
|
return Sprite {
|
||||||
.texture = IMG_LoadTexture(renderer, file.c_str()),
|
.texture = IMG_LoadTexture(renderer, file.c_str()),
|
||||||
.width = width,
|
.width = width,
|
||||||
.height = height
|
.height = height
|
||||||
@ -60,7 +60,7 @@ void GameRenderer::draw_sprite_rotated(const Sprite sprite, const int x, const i
|
|||||||
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
|
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameRenderer::clear_screen(const int r, const int g, const int b, const int a) const
|
void GameRenderer::clear_screen(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) const
|
||||||
{
|
{
|
||||||
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|||||||
@ -27,7 +27,7 @@ public:
|
|||||||
|
|
||||||
void draw_sprite_rotated(Sprite sprite, int x, int y, double angle) const;
|
void draw_sprite_rotated(Sprite sprite, int x, int y, double angle) const;
|
||||||
|
|
||||||
void clear_screen(int r, int g, int b, int a) const;
|
void clear_screen(uint8_t r, uint8_t g, uint8_t b, uint8_t a) const;
|
||||||
|
|
||||||
void flush() const;
|
void flush() const;
|
||||||
|
|
||||||
|
|||||||
26
src/Map.cpp
26
src/Map.cpp
@ -13,7 +13,7 @@ Map::Map(GameRenderer *renderer, int tile_size) :
|
|||||||
|
|
||||||
// Create horizontal starting path
|
// Create horizontal starting path
|
||||||
int y = renderer->screen_height / tile_size / 2;
|
int y = renderer->screen_height / tile_size / 2;
|
||||||
for (int x = 0; x < tiles.size(); x++) {
|
for (unsigned int x = 0; x < tiles.size(); x++) {
|
||||||
if (rand() % 2 == 0) y += rand() % 3 - 1;
|
if (rand() % 2 == 0) y += rand() % 3 - 1;
|
||||||
|
|
||||||
tiles[x][y] = Tile::path;
|
tiles[x][y] = Tile::path;
|
||||||
@ -21,7 +21,7 @@ Map::Map(GameRenderer *renderer, int tile_size) :
|
|||||||
|
|
||||||
// Create vertical starting path
|
// Create vertical starting path
|
||||||
int x = renderer->screen_width / tile_size / 2;
|
int x = renderer->screen_width / tile_size / 2;
|
||||||
for (int y = 0; y < tiles[x].size(); y++) {
|
for (unsigned int y = 0; y < tiles[x].size(); y++) {
|
||||||
if (rand() % 2 == 0) x += rand() % 3 - 1;
|
if (rand() % 2 == 0) x += rand() % 3 - 1;
|
||||||
|
|
||||||
tiles[x][y] = Tile::path;
|
tiles[x][y] = Tile::path;
|
||||||
@ -33,7 +33,7 @@ std::vector<Tile> Map::generate_tiles(const std::vector<Tile> &prev_tiles) const
|
|||||||
std::vector<Tile> new_tiles = std::vector(prev_tiles.size(), Tile::grass);
|
std::vector<Tile> new_tiles = std::vector(prev_tiles.size(), Tile::grass);
|
||||||
|
|
||||||
int path_idx = 0;
|
int path_idx = 0;
|
||||||
for (int i = 0; i < prev_tiles.size(); i++) {
|
for (unsigned int i = 0; i < prev_tiles.size(); i++) {
|
||||||
if (prev_tiles[i] == Tile::path) path_idx = i;
|
if (prev_tiles[i] == Tile::path) path_idx = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ std::vector<Tile> Map::generate_tiles(const std::vector<Tile> &prev_tiles) const
|
|||||||
return new_tiles;
|
return new_tiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::check_bounds(int player_x, int player_y)
|
void Map::check_bounds(const double player_x, const double player_y)
|
||||||
{
|
{
|
||||||
// Generate to the left
|
// Generate to the left
|
||||||
if (tile_offset_x * tile_size + renderer->screen_width > player_x) {
|
if (tile_offset_x * tile_size + renderer->screen_width > player_x) {
|
||||||
@ -56,12 +56,12 @@ void Map::check_bounds(int player_x, int player_y)
|
|||||||
// Generate upwards
|
// Generate upwards
|
||||||
if (tile_offset_y * tile_size + renderer->screen_height > player_y) {
|
if (tile_offset_y * tile_size + renderer->screen_height > player_y) {
|
||||||
std::vector<Tile> prev_tiles;
|
std::vector<Tile> prev_tiles;
|
||||||
for (int x = 0; x < tiles.size(); x++) {
|
for (unsigned int x = 0; x < tiles.size(); x++) {
|
||||||
prev_tiles.push_back(tiles[x][0]);
|
prev_tiles.push_back(tiles[x][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<Tile> new_tiles = generate_tiles(prev_tiles);
|
const std::vector<Tile> new_tiles = generate_tiles(prev_tiles);
|
||||||
for (int x = 0; x < tiles.size(); x++) {
|
for (unsigned int x = 0; x < tiles.size(); x++) {
|
||||||
tiles[x].insert(tiles[x].begin(), new_tiles[x]);
|
tiles[x].insert(tiles[x].begin(), new_tiles[x]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,23 +77,23 @@ void Map::check_bounds(int player_x, int player_y)
|
|||||||
// Generate downwards
|
// Generate downwards
|
||||||
if (player_y > (int)(tile_offset_y * tile_size + tiles[0].size() * tile_size - renderer->screen_height / 2)) {
|
if (player_y > (int)(tile_offset_y * tile_size + tiles[0].size() * tile_size - renderer->screen_height / 2)) {
|
||||||
std::vector<Tile> prev_tiles;
|
std::vector<Tile> prev_tiles;
|
||||||
for (int x = 0; x < tiles.size(); x++) {
|
for (unsigned int x = 0; x < tiles.size(); x++) {
|
||||||
prev_tiles.push_back(tiles[x].back());
|
prev_tiles.push_back(tiles[x].back());
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<Tile> new_tiles = generate_tiles(prev_tiles);
|
const std::vector<Tile> new_tiles = generate_tiles(prev_tiles);
|
||||||
for (int x = 0; x < tiles.size(); x++) {
|
for (unsigned int x = 0; x < tiles.size(); x++) {
|
||||||
tiles[x].push_back(new_tiles[x]);
|
tiles[x].push_back(new_tiles[x]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::draw(int player_x, int player_y) const
|
void Map::draw(const double player_x, const double player_y) const
|
||||||
{
|
{
|
||||||
for (int tile_x = 0; tile_x < tiles.size(); tile_x++) {
|
for (unsigned int tile_x = 0; tile_x < tiles.size(); tile_x++) {
|
||||||
for (int tile_y = 0; tile_y < tiles[tile_x].size(); tile_y++) {
|
for (unsigned int tile_y = 0; tile_y < tiles[tile_x].size(); tile_y++) {
|
||||||
int screen_x = tile_x * tile_size + tile_offset_x * tile_size + renderer->screen_width / 2 - player_x;
|
int screen_x = tile_x * tile_size + tile_offset_x * tile_size + renderer->screen_width / 2 - (int)player_x;
|
||||||
int screen_y = tile_y * tile_size + tile_offset_y * tile_size + renderer->screen_height / 2 - player_y;
|
int screen_y = tile_y * tile_size + tile_offset_y * tile_size + renderer->screen_height / 2 - (int)player_y;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
screen_x < -tile_size || screen_y < -tile_size ||
|
screen_x < -tile_size || screen_y < -tile_size ||
|
||||||
|
|||||||
@ -10,8 +10,8 @@ class Map
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
GameRenderer *renderer;
|
GameRenderer *renderer;
|
||||||
std::vector<std::vector<Tile>> tiles;
|
|
||||||
int tile_size;
|
int tile_size;
|
||||||
|
std::vector<std::vector<Tile>> tiles;
|
||||||
int tile_offset_x;
|
int tile_offset_x;
|
||||||
int tile_offset_y;
|
int tile_offset_y;
|
||||||
|
|
||||||
@ -23,9 +23,9 @@ private:
|
|||||||
public:
|
public:
|
||||||
Map(GameRenderer *renderer, int tile_size);
|
Map(GameRenderer *renderer, int tile_size);
|
||||||
|
|
||||||
void draw(int player_x, int player_y) const;
|
void draw(double player_x, double player_y) const;
|
||||||
|
|
||||||
void check_bounds(int player_x, int player_y);
|
void check_bounds(double player_x, double player_y);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -17,8 +17,8 @@ void Player::draw() const
|
|||||||
|
|
||||||
renderer->draw_sprite_rotated(
|
renderer->draw_sprite_rotated(
|
||||||
bow_sprite,
|
bow_sprite,
|
||||||
renderer->screen_width / 2 - bow_sprite.width / 2 + std::cos(angle) * 30,
|
(int)(renderer->screen_width / 2 - bow_sprite.width / 2 + std::cos(angle) * 30),
|
||||||
renderer->screen_height / 2 - bow_sprite.height / 2 + std::sin(angle) * 30,
|
(int)(renderer->screen_height / 2 - bow_sprite.height / 2 + std::sin(angle) * 30),
|
||||||
angle * 180 / M_PI
|
angle * 180 / M_PI
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
srand(time(nullptr));
|
srand((unsigned int)time(nullptr));
|
||||||
|
|
||||||
Game game;
|
Game game;
|
||||||
game.run();
|
game.run();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user