Compare commits

...

4 Commits

Author SHA1 Message Date
Simon From Jakobsen
127b96a92f don't use new and delete 2025-10-29 13:09:16 +01:00
Simon From Jakobsen
012d021a08 try fix memory error 2025-10-29 13:07:33 +01:00
Simon From Jakobsen
7584609f33 fix and stub errors 2025-10-29 12:42:41 +01:00
ec2e16e7d7 Try to implement zombie spawning 2025-10-29 12:34:16 +01:00
8 changed files with 128 additions and 78 deletions

1
.gitignore vendored
View File

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

View File

@ -1,4 +1,6 @@
#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)
@ -12,7 +14,7 @@ void Arrow::draw(const double player_x, const double player_y) const
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 / M_PI
angle * 180 / std::numbers::pi
);
}

View File

@ -1,38 +1,39 @@
#include <mutex>
#include <thread>
#include <functional>
#include "GameRenderer.hpp"
#include "Player.hpp"
#include "Game.hpp"
#include <chrono>
using namespace std::literals::chrono_literals;
Game::~Game()
{
for (const Arrow *arrow : arrows) {
delete arrow;
}
}
void Game::update(std::stop_token stop_token)
{
while (!stop_token.stop_requested()) {
game_mutex.lock();
{
auto lock = std::lock_guard(game_mutex);
ticks++;
player.update();
for (Arrow *arrow : arrows) {
arrow->update();
for (auto& arrow : arrows) {
arrow.update();
}
for (Zombo *zombo : zombos) {
zombo->update(player.x, player.y); // doesn't work :((((
for (auto& zombo : zombos) {
zombo.update(player.x, player.y);
}
if (ticks % 1000 == 0) {
zombos.push_back(Zombo(&renderer, 20.0, 20.0)); // doesn't work :((((
}
renderer.redraw();
map.check_bounds(player.x, player.y);
game_mutex.unlock();
}
std::this_thread::sleep_for(16666us);
}
@ -40,14 +41,14 @@ void Game::update(std::stop_token stop_token)
void Game::draw()
{
const std::lock_guard lock(game_mutex);
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);
for (const auto& arrow : arrows) {
arrow. draw(player.x, player.y);
}
player.draw();
@ -57,12 +58,15 @@ void Game::draw()
void Game::run()
{
update_thread = std::jthread(std::bind_front(&Game::update, this));
auto update_thread = std::jthread(std::bind_front(&Game::update, this));
while (true) {
SDL_Event e;
SDL_WaitEvent(&e);
{
auto lock = std::lock_guard(game_mutex);
if (e.type == SDL_QUIT) {
break;
}
@ -106,12 +110,13 @@ void Game::run()
}
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) {
arrows.push_back(new Arrow(&renderer, player.x, player.y, player.angle));
arrows.push_back(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

@ -1,8 +1,8 @@
#ifndef GAME_HPP
#define GAME_HPP
#include <thread>
#include <vector>
#include <mutex>
#include "Arrow.hpp"
#include "GameRenderer.hpp"
#include "Player.hpp"
@ -15,10 +15,9 @@ private:
GameRenderer renderer;
Player player;
Map map;
std::vector<Arrow*> arrows;
std::vector<Zombo*> zombos;
std::vector<Arrow> arrows;
std::vector<Zombo> zombos;
std::jthread update_thread;
std::mutex game_mutex;
unsigned int ticks = 0;
@ -27,8 +26,6 @@ private:
public:
Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
~Game();
void run();
void draw();

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

View File

@ -1,5 +1,7 @@
#include "GameRenderer.hpp"
#include "Player.hpp"
#include <cmath>
#include <numbers>
Player::Player(GameRenderer *renderer) : renderer(renderer)
{
@ -19,7 +21,7 @@ void Player::draw() const
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 / M_PI
angle * 180 / std::numbers::pi
);
}

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;

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