Compare commits

..

No commits in common. "89cea4408648ae7e107b8593c84b7177620fedbc" and "6bf8c2cb88617758f4ad5beacbf6251a81a07508" have entirely different histories.

23 changed files with 65 additions and 284 deletions

1
.gitignore vendored
View File

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

View File

@ -2,34 +2,19 @@ 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
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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 295 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 357 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 470 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 370 B

View File

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

View File

@ -1,25 +0,0 @@
#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;
}

View File

@ -1,27 +0,0 @@
#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,56 +1,37 @@
#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()
{
for (const Arrow *arrow : arrows) {
delete arrow;
}
stopping = true;
update_thread.join();
}
void Game::update(std::stop_token stop_token)
void Game::update()
{
while (!stop_token.stop_requested()) {
game_mutex.lock();
while (!stopping) {
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()
void Game::draw() const
{
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();
@ -58,7 +39,7 @@ void Game::draw()
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;
@ -106,14 +87,6 @@ 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,13 +2,9 @@
#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
{
@ -16,23 +12,20 @@ private:
GameRenderer renderer;
Player player;
Map map;
std::vector<Arrow*> arrows;
std::vector<Zombo*> zombos;
std::jthread update_thread;
std::mutex game_mutex;
unsigned int ticks = 0;
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();
void run();
void draw();
void draw() const;
};
#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,13 +54,14 @@ 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
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
{
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
const SDL_Point center = { .x = center_x, .y = center_y };
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, &center, 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);

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) const;
void draw_sprite_rotated(Sprite sprite, int x, int y, double angle, int center_x, int center_y) 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;

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, Tile::grass)),
tile_offset_x(-renderer->screen_width / tile_size / 2.0),
tile_offset_y(-renderer->screen_height / tile_size / 2.0)
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)
{
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 (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]);
}
@ -69,31 +69,31 @@ void Map::check_bounds(const double player_x, const double player_y)
}
// Generate to the right
if (player_x > tile_offset_x * tile_size + tiles.size() * tile_size - renderer->screen_width / 2) {
if (player_x > (int)(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 > 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;
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 = (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);
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;
}
}
}

View File

@ -11,22 +11,21 @@ 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;
[[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:
double tile_offset_x;
double tile_offset_y;
int tile_size;
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

View File

@ -1,12 +1,9 @@
#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
@ -16,13 +13,6 @@ 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, bow_sprite;
Sprite hero_sprite;
public:
double x = 0;
@ -16,9 +16,7 @@ public:
double x_vel = 0;
double y_vel = 0;
double angle = 0.0;
static constexpr double speed = 1.5;
const double speed = 1.5;
Player(GameRenderer *renderer);

View File

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

View File

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

View File

@ -1,42 +0,0 @@
#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

View File

@ -1,29 +0,0 @@
#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)
);
}

View File

@ -1,26 +0,0 @@
#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,9 +3,10 @@
int main()
{
srand((unsigned int)time(nullptr));
srand(time(nullptr));
Game game = Game();
Game game;
game.run();
return 0;