Compare commits

...

4 Commits

Author SHA1 Message Date
d2476fa5df Add jthread and fix warnings 2025-10-27 14:04:51 +01:00
80a2ba78a2 Various code improvements 2025-10-27 12:45:07 +01:00
d7eb5c7f4d Use enum class 2025-10-27 12:35:48 +01:00
4f34d06f73 Improve makefile 2025-10-27 12:29:39 +01:00
12 changed files with 70 additions and 57 deletions

View File

@ -5,16 +5,29 @@ OBJS= \
build/src/Player.o \
build/src/GameRenderer.o
LIBS=-lSDL2 -lSDL2_image
MAKEFLAGS += -j $(shell nproc)
CXX_FLAGS = -std=c++23 -Wall -Wextra -Wpedantic -Wconversion -pedantic -pedantic-errors
RELEASE=0
ifeq ($(RELEASE), 1)
CXX_FLAGS += -Werror
else
CXX_FLAGS += -fsanitize=address,undefined
LINKER_FLAGS += -fsanitize=address,undefined
endif
CXX_FLAGS += $(shell pkg-config sdl2 SDL2_image --cflags)
LINKER_FLAGS += $(shell pkg-config sdl2 SDL2_image --libs)
build/%.o: %.cpp $(wildcard *.hpp)
mkdir -p `dirname $@`
g++ $(CFLAGS) -c -o $@ $< $(LIBS)
@mkdir -p `dirname $@`
g++ $(CXX_FLAGS) -c -o $@ $<
all: $(OBJS)
g++ $(CFLAGS) $(OBJS) $(LIBS)
g++ $(OBJS) $(LINKER_FLAGS)
clean:
rm -r build
rm a.out

8
compile_flags.txt Normal file
View File

@ -0,0 +1,8 @@
-xc++
-std=c++23
-Wall
-Wextra
-Wpedantic
-Wconversion
-pedantic
-pedantic-errors

View File

@ -1,21 +1,15 @@
#include <thread>
#include <functional>
#include <iostream>
#include "GameRenderer.hpp"
#include "Player.hpp"
#include "Game.hpp"
using namespace std::literals::chrono_literals;
Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
Game::~Game()
void Game::update(std::stop_token stop_token)
{
stopping = true;
update_thread.join();
}
void Game::update()
{
while (!stopping) {
while (!stop_token.stop_requested()) {
player.update();
renderer.redraw();
@ -39,7 +33,7 @@ void Game::draw() const
void Game::run()
{
update_thread = std::thread(&Game::update, this);
update_thread = std::jthread(std::bind_front(&Game::update, this));
while (true) {
SDL_Event e;

View File

@ -13,15 +13,14 @@ private:
Player player;
Map map;
std::thread update_thread;
bool stopping = false;
std::jthread update_thread;
void update();
void update(std::stop_token stop_token);
public:
Game();
Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
~Game();
~Game() = default;
void run();

View File

@ -41,7 +41,7 @@ GameRenderer::~GameRenderer()
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()),
.width = width,
.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);
}
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_RenderClear(renderer);

View File

@ -27,7 +27,7 @@ public:
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;

View File

@ -4,7 +4,7 @@
Map::Map(GameRenderer *renderer, int tile_size) :
renderer(renderer),
tile_size(tile_size),
tiles(renderer->screen_width / tile_size, std::vector(renderer->screen_height / tile_size, grass)),
tiles(renderer->screen_width / tile_size, std::vector(renderer->screen_height / tile_size, Tile::grass)),
tile_offset_x(-renderer->screen_width / tile_size / 2),
tile_offset_y(-renderer->screen_height / tile_size / 2)
{
@ -13,38 +13,38 @@ Map::Map(GameRenderer *renderer, int tile_size) :
// Create horizontal starting path
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;
tiles[x][y] = path;
tiles[x][y] = Tile::path;
}
// Create vertical starting path
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;
tiles[x][y] = path;
tiles[x][y] = Tile::path;
}
}
std::vector<Tile> Map::generate_tiles(std::vector<Tile> prev_tiles) const
std::vector<Tile> Map::generate_tiles(const std::vector<Tile> &prev_tiles) const
{
std::vector<Tile> new_tiles = std::vector(prev_tiles.size(), grass);
std::vector<Tile> new_tiles = std::vector(prev_tiles.size(), Tile::grass);
int path_idx = 0;
for (int i = 0; i < prev_tiles.size(); i++) {
if (prev_tiles[i] == path) path_idx = i;
for (unsigned int i = 0; i < prev_tiles.size(); i++) {
if (prev_tiles[i] == Tile::path) path_idx = i;
}
if (rand() % 2 == 0) path_idx += rand() % 3 - 1;
new_tiles[path_idx] = path;
new_tiles[path_idx] = Tile::path;
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
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
if (tile_offset_y * tile_size + renderer->screen_height > player_y) {
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]);
}
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]);
}
@ -77,23 +77,23 @@ void Map::check_bounds(int player_x, int player_y)
// Generate downwards
if (player_y > (int)(tile_offset_y * tile_size + tiles[0].size() * tile_size - renderer->screen_height / 2)) {
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());
}
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]);
}
}
}
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 (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_y = tile_y * tile_size + tile_offset_y * tile_size + renderer->screen_height / 2 - player_y;
for (unsigned int tile_x = 0; tile_x < tiles.size(); tile_x++) {
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 - (int)player_x;
int screen_y = tile_y * tile_size + tile_offset_y * tile_size + renderer->screen_height / 2 - (int)player_y;
if (
screen_x < -tile_size || screen_y < -tile_size ||
@ -103,8 +103,8 @@ void Map::draw(int player_x, int player_y) const
}
switch (tiles[tile_x][tile_y]) {
case grass: renderer->draw_sprite(grass_sprite, screen_x, screen_y); break;
case path: renderer->draw_sprite(path_sprite, screen_x, screen_y); break;
case Tile::grass: renderer->draw_sprite(grass_sprite, screen_x, screen_y); break;
case Tile::path: renderer->draw_sprite(path_sprite, screen_x, screen_y); break;
}
}
}

View File

@ -10,22 +10,22 @@ class Map
{
private:
GameRenderer *renderer;
std::vector<std::vector<Tile>> tiles;
int tile_size;
std::vector<std::vector<Tile>> tiles;
int tile_offset_x;
int tile_offset_y;
Sprite grass_sprite;
Sprite path_sprite;
std::vector<Tile> generate_tiles(std::vector<Tile> prev_tiles) const;
[[nodiscard]] std::vector<Tile> generate_tiles(const std::vector<Tile> &prev_tiles) const;
public:
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

View File

@ -17,8 +17,8 @@ void Player::draw() const
renderer->draw_sprite_rotated(
bow_sprite,
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_width / 2 - bow_sprite.width / 2 + std::cos(angle) * 30),
(int)(renderer->screen_height / 2 - bow_sprite.height / 2 + std::sin(angle) * 30),
angle * 180 / M_PI
);
}

View File

@ -18,7 +18,7 @@ public:
double angle = 0.0;
const double speed = 1.5;
static constexpr double speed = 1.5;
Player(GameRenderer *renderer);

View File

@ -1,7 +1,7 @@
#ifndef TILE_HPP
#define TILE_HPP
enum Tile
enum class Tile
{
grass,
path,

View File

@ -3,10 +3,9 @@
int main()
{
srand(time(nullptr));
Game game = Game();
srand((unsigned int)time(nullptr));
Game game;
game.run();
return 0;