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
2 changed files with 74 additions and 74 deletions

View File

@ -1,3 +1,4 @@
#include <mutex>
#include <thread> #include <thread>
#include <functional> #include <functional>
#include "GameRenderer.hpp" #include "GameRenderer.hpp"
@ -7,48 +8,47 @@
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
Game::~Game()
{
for (const Arrow *arrow : arrows) {
delete arrow;
}
}
void Game::update(std::stop_token stop_token) void Game::update(std::stop_token stop_token)
{ {
while (!stop_token.stop_requested()) { while (!stop_token.stop_requested()) {
game_mutex.lock(); {
auto lock = std::lock_guard(game_mutex);
player.update(); ticks++;
for (Arrow *arrow : arrows) { player.update();
arrow->update();
for (auto& arrow : arrows) {
arrow.update();
}
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);
} }
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); std::this_thread::sleep_for(16666us);
} }
} }
void Game::draw() void Game::draw()
{ {
const std::lock_guard lock(game_mutex); std::lock_guard lock(game_mutex);
renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF); renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF);
map.draw(player.x, player.y); map.draw(player.x, player.y);
for (const Arrow *arrow : arrows) { for (const auto& arrow : arrows) {
arrow->draw(player.x, player.y); arrow. draw(player.x, player.y);
} }
player.draw(); player.draw();
@ -58,61 +58,65 @@ void Game::draw()
void Game::run() 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) { while (true) {
SDL_Event e; SDL_Event e;
SDL_WaitEvent(&e); SDL_WaitEvent(&e);
if (e.type == SDL_QUIT) { {
break; auto lock = std::lock_guard(game_mutex);
}
if (e.type == SDL_KEYDOWN) { if (e.type == SDL_QUIT) {
switch (e.key.keysym.sym) { break;
case SDLK_RIGHT:
case SDLK_d:
player.x_vel = player.speed;
break;
case SDLK_LEFT:
case SDLK_a:
player.x_vel = -player.speed;
break;
case SDLK_DOWN:
case SDLK_s:
player.y_vel = player.speed;
break;
case SDLK_UP:
case SDLK_w:
player.y_vel = -player.speed;
break;
} }
}
if (e.type == SDL_KEYUP) { if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) { switch (e.key.keysym.sym) {
case SDLK_RIGHT: case SDLK_RIGHT:
case SDLK_d: case SDLK_d:
case SDLK_LEFT: player.x_vel = player.speed;
case SDLK_a: break;
player.x_vel = 0; case SDLK_LEFT:
break; case SDLK_a:
case SDLK_DOWN: player.x_vel = -player.speed;
case SDLK_s: break;
case SDLK_UP: case SDLK_DOWN:
case SDLK_w: case SDLK_s:
player.y_vel = 0; player.y_vel = player.speed;
break; break;
case SDLK_UP:
case SDLK_w:
player.y_vel = -player.speed;
break;
}
} }
}
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) { if (e.type == SDL_KEYUP) {
arrows.push_back(new Arrow(&renderer, player.x, player.y, player.angle)); switch (e.key.keysym.sym) {
} case SDLK_RIGHT:
case SDLK_d:
case SDLK_LEFT:
case SDLK_a:
player.x_vel = 0;
break;
case SDLK_DOWN:
case SDLK_s:
case SDLK_UP:
case SDLK_w:
player.y_vel = 0;
break;
}
}
int mouse_x, mouse_y; if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) {
SDL_GetMouseState(&mouse_x, &mouse_y); arrows.push_back(Arrow(&renderer, player.x, player.y, player.angle));
player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2); }
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(); draw();
} }

View File

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