Compare commits

...

10 Commits

Author SHA1 Message Date
Simon From Jakobsen
89cea44086 fix and stub errors 2025-10-29 12:38:39 +01:00
8841bded6f Add zombies 2025-10-29 12:18:34 +01:00
e141be92f0 Implement shooting arrows 2025-10-28 14:00:01 +01:00
c9299b3e04 Add sprites 2025-10-28 10:09:25 +01:00
9247f1bf68 Implement game mutex for thread safety 2025-10-28 09:46:50 +01:00
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
d46ee05126 Add bow 2025-10-27 10:30:03 +01:00
23 changed files with 284 additions and 65 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
build/
a.out
.vscode/

View File

@ -2,19 +2,34 @@ OBJS= \
build/src/main.o \
build/src/Map.o \
build/src/Game.o \
build/src/Arrow.o \
build/src/Zombo.o \
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

BIN
assets/arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 B

BIN
assets/bow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

BIN
assets/bow_arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 B

BIN
assets/zombo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 370 B

8
compile_flags.txt Normal file
View File

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

25
src/Arrow.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "Arrow.hpp"
#include <cmath>
#include <numbers>
Arrow::Arrow(GameRenderer *renderer, const double x, const double y, const double angle)
: renderer(renderer), x(x), y(y), angle(angle)
{
sprite = renderer->load_sprite("./assets/arrow.png", 22, 8);
}
void Arrow::draw(const double player_x, const double player_y) const
{
renderer->draw_sprite_rotated(
sprite,
(int)(x - sprite.width / 2 + renderer->screen_width / 2 - player_x),
(int)(y - sprite.height / 2 + renderer->screen_height / 2 - player_y),
angle * 180 / std::numbers::pi
);
}
void Arrow::update()
{
x += std::cos(angle) * speed;
y += std::sin(angle) * speed;
}

27
src/Arrow.hpp Normal file
View File

@ -0,0 +1,27 @@
#ifndef ARROW_HPP
#define ARROW_HPP
#include "GameRenderer.hpp"
#include "Sprite.hpp"
class Arrow
{
private:
GameRenderer *renderer;
Sprite sprite;
double x;
double y;
double angle;
static constexpr int speed = 5;
public:
Arrow(GameRenderer *renderer, double x, double y, double angle);
void draw(double offset_x, double offset_y) const;
void update();
};
#endif

View File

@ -1,37 +1,56 @@
#include <thread>
#include <functional>
#include "GameRenderer.hpp"
#include "Player.hpp"
#include "Game.hpp"
#include <chrono>
using namespace std::literals::chrono_literals;
Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
Game::~Game()
{
stopping = true;
update_thread.join();
for (const Arrow *arrow : arrows) {
delete arrow;
}
}
void Game::update()
void Game::update(std::stop_token stop_token)
{
while (!stopping) {
while (!stop_token.stop_requested()) {
game_mutex.lock();
player.update();
for (Arrow *arrow : arrows) {
arrow->update();
}
for (Zombo *zombo : zombos) {
zombo->update(player.x, player.y); // doesn't work :((((
}
renderer.redraw();
map.check_bounds(player.x, player.y);
game_mutex.unlock();
std::this_thread::sleep_for(16666us);
}
}
void Game::draw() const
void Game::draw()
{
const std::lock_guard lock(game_mutex);
renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF);
map.draw(player.x, player.y);
for (const Arrow *arrow : arrows) {
arrow->draw(player.x, player.y);
}
player.draw();
renderer.flush();
@ -39,7 +58,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;
@ -87,6 +106,14 @@ void Game::run()
}
}
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) {
arrows.push_back(new Arrow(&renderer, player.x, player.y, player.angle));
}
int 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);
draw();
}
}

View File

@ -2,9 +2,13 @@
#define GAME_HPP
#include <thread>
#include <vector>
#include <mutex>
#include "Arrow.hpp"
#include "GameRenderer.hpp"
#include "Player.hpp"
#include "Map.hpp"
#include "Zombo.hpp"
class Game
{
@ -12,20 +16,23 @@ private:
GameRenderer renderer;
Player player;
Map map;
std::vector<Arrow*> arrows;
std::vector<Zombo*> zombos;
std::thread update_thread;
bool stopping = false;
std::jthread update_thread;
std::mutex game_mutex;
unsigned int ticks = 0;
void update();
void update(std::stop_token stop_token);
public:
Game();
Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
~Game();
void run();
void draw() const;
void draw();
};
#endif

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
@ -54,14 +54,13 @@ void GameRenderer::draw_sprite(const Sprite sprite, const int x, const int y) co
SDL_RenderCopy(renderer, sprite.texture, nullptr, &rect);
}
void GameRenderer::draw_sprite_rotated(const Sprite sprite, const int x, const int y, const double angle, const int center_x, const int center_y) const
void GameRenderer::draw_sprite_rotated(const Sprite sprite, const int x, const int y, const double angle) const
{
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
const SDL_Point center = { .x = center_x, .y = center_y };
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, &center, 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_RenderClear(renderer);

View File

@ -2,7 +2,7 @@
#define GAME_RENDERER_HPP
#include <string>
#include <SDL2/SDL.h>
// #include <SDL2/SDL.h>
#include "Sprite.hpp"
class GameRenderer
@ -25,9 +25,9 @@ public:
void draw_sprite(Sprite sprite, int x, int y) const;
void draw_sprite_rotated(Sprite sprite, int x, int y, double angle, int center_x, int center_y) 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;

View File

@ -4,47 +4,47 @@
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)),
tile_offset_x(-renderer->screen_width / tile_size / 2),
tile_offset_y(-renderer->screen_height / tile_size / 2)
tiles(renderer->screen_width / tile_size, std::vector(renderer->screen_height / tile_size, Tile::grass)),
tile_offset_x(-renderer->screen_width / tile_size / 2.0),
tile_offset_y(-renderer->screen_height / tile_size / 2.0)
{
grass_sprite = renderer->load_sprite("./assets/grass_tile.png", tile_size, tile_size);
path_sprite = renderer->load_sprite("./assets/path_tile.png", tile_size, 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]);
}
@ -69,31 +69,31 @@ void Map::check_bounds(int player_x, int player_y)
}
// Generate to the right
if (player_x > (int)(tile_offset_x * tile_size + tiles.size() * tile_size - renderer->screen_width / 2)) {
if (player_x > tile_offset_x * tile_size + tiles.size() * tile_size - renderer->screen_width / 2) {
const std::vector<Tile> new_tiles = generate_tiles(tiles.back());
tiles.push_back(new_tiles);
}
// Generate downwards
if (player_y > (int)(tile_offset_y * tile_size + tiles[0].size() * tile_size - renderer->screen_height / 2)) {
if (player_y > 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 = (int)(tile_x * tile_size + tile_offset_x * tile_size + renderer->screen_width / 2.0 - player_x);
int screen_y = (int)(tile_y * tile_size + tile_offset_y * tile_size + renderer->screen_height / 2.0 - 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

@ -11,21 +11,22 @@ class Map
private:
GameRenderer *renderer;
std::vector<std::vector<Tile>> tiles;
int tile_size;
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:
double tile_offset_x;
double tile_offset_y;
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

View File

@ -1,9 +1,12 @@
#include "GameRenderer.hpp"
#include "Player.hpp"
#include <cmath>
#include <numbers>
Player::Player(GameRenderer *renderer) : renderer(renderer)
{
hero_sprite = renderer->load_sprite("./assets/hero_front.png", 40, 40);
bow_sprite = renderer->load_sprite("./assets/bow_arrow.png", 22, 32);
}
void Player::draw() const
@ -13,6 +16,13 @@ void Player::draw() const
renderer->screen_width / 2 - hero_sprite.width / 2,
renderer->screen_height / 2 - hero_sprite.height / 2
);
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),
angle * 180 / std::numbers::pi
);
}
void Player::update()

View File

@ -7,7 +7,7 @@ class Player
{
private:
GameRenderer *renderer;
Sprite hero_sprite;
Sprite hero_sprite, bow_sprite;
public:
double x = 0;
@ -16,7 +16,9 @@ public:
double x_vel = 0;
double y_vel = 0;
const double speed = 1.5;
double angle = 0.0;
static constexpr double speed = 1.5;
Player(GameRenderer *renderer);

View File

@ -1,7 +1,8 @@
#ifndef SPRITE_HPP
#define SPRITE_HPP
#include <SDL2/SDL_render.h>
#include "Whatever.hpp"
// #include <SDL2/SDL_render.h>
typedef struct {
SDL_Texture *texture;

View File

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

42
src/Whatever.hpp Normal file
View File

@ -0,0 +1,42 @@
#ifndef WHATEVER_HPP
#define WHATEVER_HPP
using SDL_Texture = void;
using SDL_Renderer = void;
using SDL_Window = void;
enum SDL_Event_type {
SDL_QUIT,
SDL_KEYDOWN,
SDL_KEYUP,
SDL_MOUSEBUTTONDOWN,
};
enum SDL_Event_keysym_sym {
SDLK_RIGHT,
SDLK_d,
SDLK_LEFT,
SDLK_a,
SDLK_DOWN,
SDLK_s,
SDLK_UP,
SDLK_w,
};
struct SDL_Event {
SDL_Event_type type;
struct {
struct {
SDL_Event_keysym_sym sym;
} keysym;
} key;
struct {
int button;
} button;
};
void SDL_WaitEvent(SDL_Event*);
void SDL_GetMouseState(int*, int*);
#endif

29
src/Zombo.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "Zombo.hpp"
Zombo::Zombo(GameRenderer *renderer, const double x, const double y) : renderer(renderer), x(x), y(y)
{
sprite = renderer->load_sprite("./assets/zombo.png", 40, 40);
}
void Zombo::update(const double player_x, const double player_y)
{
double x_vel = 0, y_vel = 0;
if (player_x > x) x_vel = speed;
else if (player_x < x) x_vel = -speed;
if (player_y > y) y_vel = speed;
else if (player_y < y) y_vel = -speed;
x += x_vel;
y += y_vel;
}
void Zombo::draw(const double player_x, const double player_y) const
{
renderer->draw_sprite(
sprite,
(int)(x - sprite.width / 2 + renderer->screen_width / 2 - player_x),
(int)(y - sprite.height / 2 + renderer->screen_height / 2 - player_y)
);
}

26
src/Zombo.hpp Normal file
View File

@ -0,0 +1,26 @@
#ifndef ZOMBO_HPP
#define ZOMBO_HPP
#include "Sprite.hpp"
#include "GameRenderer.hpp"
class Zombo
{
private:
GameRenderer *renderer;
Sprite sprite;
double x;
double y;
static constexpr int speed = 3;
public:
Zombo(GameRenderer *renderer, double x, double y);
void update(double player_x, double player_y);
void draw(double player_x, double player_y) const;
};
#endif

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;