Compare commits
No commits in common. "d2476fa5dfbf97fb5e7b27f38da9956df97db3bf" and "d46ee05126c8971ca659d1cade03a5a3a5aebbb4" have entirely different histories.
d2476fa5df
...
d46ee05126
23
Makefile
23
Makefile
@ -5,29 +5,16 @@ OBJS= \
|
||||
build/src/Player.o \
|
||||
build/src/GameRenderer.o
|
||||
|
||||
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)
|
||||
LIBS=-lSDL2 -lSDL2_image
|
||||
|
||||
build/%.o: %.cpp $(wildcard *.hpp)
|
||||
@mkdir -p `dirname $@`
|
||||
g++ $(CXX_FLAGS) -c -o $@ $<
|
||||
mkdir -p `dirname $@`
|
||||
g++ $(CFLAGS) -c -o $@ $< $(LIBS)
|
||||
|
||||
all: $(OBJS)
|
||||
g++ $(OBJS) $(LINKER_FLAGS)
|
||||
g++ $(CFLAGS) $(OBJS) $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -r build
|
||||
rm a.out
|
||||
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
-xc++
|
||||
-std=c++23
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wconversion
|
||||
-pedantic
|
||||
-pedantic-errors
|
||||
16
src/Game.cpp
16
src/Game.cpp
@ -1,15 +1,21 @@
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include "GameRenderer.hpp"
|
||||
#include "Player.hpp"
|
||||
#include "Game.hpp"
|
||||
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
void Game::update(std::stop_token stop_token)
|
||||
Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
while (!stop_token.stop_requested()) {
|
||||
stopping = true;
|
||||
update_thread.join();
|
||||
}
|
||||
|
||||
void Game::update()
|
||||
{
|
||||
while (!stopping) {
|
||||
player.update();
|
||||
|
||||
renderer.redraw();
|
||||
@ -33,7 +39,7 @@ void Game::draw() const
|
||||
|
||||
void Game::run()
|
||||
{
|
||||
update_thread = std::jthread(std::bind_front(&Game::update, this));
|
||||
update_thread = std::thread(&Game::update, this);
|
||||
|
||||
while (true) {
|
||||
SDL_Event e;
|
||||
|
||||
@ -13,14 +13,15 @@ private:
|
||||
Player player;
|
||||
Map map;
|
||||
|
||||
std::jthread update_thread;
|
||||
std::thread update_thread;
|
||||
bool stopping = false;
|
||||
|
||||
void update(std::stop_token stop_token);
|
||||
void update();
|
||||
|
||||
public:
|
||||
Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
|
||||
Game();
|
||||
|
||||
~Game() = default;
|
||||
~Game();
|
||||
|
||||
void run();
|
||||
|
||||
|
||||
@ -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 uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) const
|
||||
void GameRenderer::clear_screen(const int r, const int g, const int b, const int a) const
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
@ -27,7 +27,7 @@ public:
|
||||
|
||||
void draw_sprite_rotated(Sprite sprite, int x, int y, double angle) const;
|
||||
|
||||
void clear_screen(uint8_t r, uint8_t g, uint8_t b, uint8_t a) const;
|
||||
void clear_screen(int r, int g, int b, int a) const;
|
||||
|
||||
void flush() const;
|
||||
|
||||
|
||||
44
src/Map.cpp
44
src/Map.cpp
@ -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, Tile::grass)),
|
||||
tiles(renderer->screen_width / tile_size, std::vector(renderer->screen_height / tile_size, 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 (unsigned int x = 0; x < tiles.size(); x++) {
|
||||
for (int x = 0; x < tiles.size(); x++) {
|
||||
if (rand() % 2 == 0) y += rand() % 3 - 1;
|
||||
|
||||
tiles[x][y] = Tile::path;
|
||||
tiles[x][y] = path;
|
||||
}
|
||||
|
||||
// Create vertical starting path
|
||||
int x = renderer->screen_width / tile_size / 2;
|
||||
for (unsigned int y = 0; y < tiles[x].size(); y++) {
|
||||
for (int y = 0; y < tiles[x].size(); y++) {
|
||||
if (rand() % 2 == 0) x += rand() % 3 - 1;
|
||||
|
||||
tiles[x][y] = Tile::path;
|
||||
tiles[x][y] = path;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Tile> Map::generate_tiles(const std::vector<Tile> &prev_tiles) const
|
||||
std::vector<Tile> Map::generate_tiles(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(), grass);
|
||||
|
||||
int path_idx = 0;
|
||||
for (unsigned int i = 0; i < prev_tiles.size(); i++) {
|
||||
if (prev_tiles[i] == Tile::path) path_idx = i;
|
||||
for (int i = 0; i < prev_tiles.size(); i++) {
|
||||
if (prev_tiles[i] == path) path_idx = i;
|
||||
}
|
||||
|
||||
if (rand() % 2 == 0) path_idx += rand() % 3 - 1;
|
||||
|
||||
new_tiles[path_idx] = Tile::path;
|
||||
new_tiles[path_idx] = path;
|
||||
|
||||
return new_tiles;
|
||||
}
|
||||
|
||||
void Map::check_bounds(const double player_x, const double player_y)
|
||||
void Map::check_bounds(int player_x, int 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(const double player_x, const double player_y)
|
||||
// Generate upwards
|
||||
if (tile_offset_y * tile_size + renderer->screen_height > player_y) {
|
||||
std::vector<Tile> prev_tiles;
|
||||
for (unsigned int x = 0; x < tiles.size(); x++) {
|
||||
for (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 (unsigned int x = 0; x < tiles.size(); x++) {
|
||||
for (int x = 0; x < tiles.size(); x++) {
|
||||
tiles[x].insert(tiles[x].begin(), new_tiles[x]);
|
||||
}
|
||||
|
||||
@ -77,23 +77,23 @@ void Map::check_bounds(const double player_x, const double 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 (unsigned int x = 0; x < tiles.size(); x++) {
|
||||
for (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 (unsigned int x = 0; x < tiles.size(); x++) {
|
||||
for (int x = 0; x < tiles.size(); x++) {
|
||||
tiles[x].push_back(new_tiles[x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Map::draw(const double player_x, const double player_y) const
|
||||
void Map::draw(int player_x, int player_y) const
|
||||
{
|
||||
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;
|
||||
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;
|
||||
|
||||
if (
|
||||
screen_x < -tile_size || screen_y < -tile_size ||
|
||||
@ -103,8 +103,8 @@ void Map::draw(const double player_x, const double player_y) const
|
||||
}
|
||||
|
||||
switch (tiles[tile_x][tile_y]) {
|
||||
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;
|
||||
case grass: renderer->draw_sprite(grass_sprite, screen_x, screen_y); break;
|
||||
case path: renderer->draw_sprite(path_sprite, screen_x, screen_y); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,22 +10,22 @@ class Map
|
||||
{
|
||||
private:
|
||||
GameRenderer *renderer;
|
||||
int tile_size;
|
||||
std::vector<std::vector<Tile>> tiles;
|
||||
int tile_size;
|
||||
int tile_offset_x;
|
||||
int tile_offset_y;
|
||||
|
||||
Sprite grass_sprite;
|
||||
Sprite path_sprite;
|
||||
|
||||
[[nodiscard]] std::vector<Tile> generate_tiles(const std::vector<Tile> &prev_tiles) const;
|
||||
std::vector<Tile> generate_tiles(std::vector<Tile> prev_tiles) const;
|
||||
|
||||
public:
|
||||
Map(GameRenderer *renderer, int tile_size);
|
||||
|
||||
void draw(double player_x, double player_y) const;
|
||||
void draw(int player_x, int player_y) const;
|
||||
|
||||
void check_bounds(double player_x, double player_y);
|
||||
void check_bounds(int player_x, int player_y);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -17,8 +17,8 @@ void Player::draw() const
|
||||
|
||||
renderer->draw_sprite_rotated(
|
||||
bow_sprite,
|
||||
(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),
|
||||
renderer->screen_width / 2 - bow_sprite.width / 2 + std::cos(angle) * 30,
|
||||
renderer->screen_height / 2 - bow_sprite.height / 2 + std::sin(angle) * 30,
|
||||
angle * 180 / M_PI
|
||||
);
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ public:
|
||||
|
||||
double angle = 0.0;
|
||||
|
||||
static constexpr double speed = 1.5;
|
||||
const double speed = 1.5;
|
||||
|
||||
Player(GameRenderer *renderer);
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#ifndef TILE_HPP
|
||||
#define TILE_HPP
|
||||
|
||||
enum class Tile
|
||||
enum Tile
|
||||
{
|
||||
grass,
|
||||
path,
|
||||
|
||||
@ -3,9 +3,10 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
srand((unsigned int)time(nullptr));
|
||||
srand(time(nullptr));
|
||||
|
||||
Game game = Game();
|
||||
|
||||
Game game;
|
||||
game.run();
|
||||
|
||||
return 0;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user