2025-10-24 10:57:22 +02:00

93 lines
2.0 KiB
C++

#include <thread>
#include "GameRenderer.hpp"
#include "Player.hpp"
#include "Game.hpp"
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();
}
void Game::update()
{
while (!stopping) {
player.update();
renderer.redraw();
map.check_bounds(player.x, player.y);
std::this_thread::sleep_for(16666us);
}
}
void Game::draw() const
{
renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF);
map.draw(player.x, player.y);
player.draw();
renderer.flush();
}
void Game::run()
{
update_thread = std::thread(&Game::update, this);
while (true) {
SDL_Event e;
SDL_WaitEvent(&e);
if (e.type == SDL_QUIT) {
break;
}
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
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) {
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;
}
}
draw();
}
}