diff --git a/Bomb.hpp b/Bomb.hpp deleted file mode 100644 index 36becd1..0000000 --- a/Bomb.hpp +++ /dev/null @@ -1,26 +0,0 @@ -//Bomb.hpp -#pragma once -#include "Position.hpp" -#include - -class Bomb { - private: - Position _position; - unsigned short _timer; - bool _active; - public: - Bomb(): _position({0, 0}), _timer(3), _active(false) { - }; - - Bomb(Position position) : _position(position), _timer(3), _active(false) { - } - - void activate() { _active = true; } - void deactivate() { _active = false; } - bool isActive() const { return _active; } - - void setPosition(Position position) { _position = position; } - Position getPosition() const { return _position; } - - unsigned short getTimer() const { return _timer; } -}; diff --git a/Bomberman.cpp b/Bomberman.cpp index 51a84ac..689780a 100644 --- a/Bomberman.cpp +++ b/Bomberman.cpp @@ -1,3 +1,12 @@ -//Bomberman.cpp #include "Bomberman.hpp" +std::ostream& operator<<(std::ostream& os, const Bomberman& b) { + os << "Bomberman at (" << b._x << ", " << b._y << ") with " + << b._maxBomb << " max bombs, " + << b._maxRange << " max range, " + << b._bombPlanted << " bombs planted, " + << "speed: " << b._speed + << ", alive: " << (b._isAlive ? "yes" : "no") + << ", invincible: " << (b._isInvincible ? "yes" : "no"); + return os; + } diff --git a/Bomberman.hpp b/Bomberman.hpp index 8aefa58..4c14161 100644 --- a/Bomberman.hpp +++ b/Bomberman.hpp @@ -1,21 +1,17 @@ -// Bomberman.hpp #pragma once -#include -#include "Position.hpp" -#include "Bomb.hpp" -class Bomberman { +#include +class Bomberman { private: - Position _position; - unsigned short _maxNumberOfBombs = 1; - unsigned short _numberOfBombsPlanted = 0; - Bomb _bomb; + int _x, _y; + unsigned short _maxBomb = 1; + unsigned short _maxRange = 1; + unsigned short _bombPlanted = 0; + float _speed = 1.0f; + bool _isAlive = true; + bool _isInvincible = false; public: - Bomberman(){std::cout << "Bomberman created!" << std::endl;} - ~Bomberman(){std::cout << "Bomberman destroyed!" << std::endl;}; - int plantBomb(); - friend std::ostream& operator<<(std::ostream &os, const Bomberman &bomberman) { - os << "Bomberman at " << bomberman._position << ", Number of bombs: " << bomberman._numberOfBombs; - return os; - } + Bomberman() = default; + ~Bomberman() = default; + friend std::ostream& operator<<(std::ostream& os, const Bomberman& b); }; diff --git a/Bomberman.hpp.gch b/Bomberman.hpp.gch new file mode 100644 index 0000000..7ecfd6f Binary files /dev/null and b/Bomberman.hpp.gch differ diff --git a/Game.cpp b/Game.cpp new file mode 100644 index 0000000..3cac6ba --- /dev/null +++ b/Game.cpp @@ -0,0 +1,38 @@ +#include "Game.hpp" + +Game::Game(){ + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + throw std::runtime_error("SDL could not initialize! SDL_Error: " + \ + std::string(SDL_GetError())); + } + + window = SDL_CreateWindow("Bomberman", \ + SDL_WINDOWPOS_CENTERED, \ + SDL_WINDOWPOS_CENTERED, \ + 800, 600, \ + SDL_WINDOW_SHOWN); + if (window == nullptr) { + throw std::runtime_error("Window could not be created! SDL_Error: " + \ + std::string(SDL_GetError())); + } + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if (renderer == nullptr) { + SDL_DestroyWindow(window); + throw std::runtime_error("Renderer could not be created! SDL_Error: " + \ + std::string(SDL_GetError())); + } + + isRunning = true; +} + +Game::~Game() { + if (renderer) { + SDL_DestroyRenderer(renderer); + } + if (window) { + SDL_DestroyWindow(window); + } + SDL_Quit(); +} diff --git a/Game.hpp b/Game.hpp new file mode 100644 index 0000000..a969623 --- /dev/null +++ b/Game.hpp @@ -0,0 +1,15 @@ +#pragma once +#include +#include + +class Game { + private: + SDL_Window* window = nullptr; + SDL_Renderer* renderer = nullptr; + bool isRunning = false; + public: + Game(); + void Run(); + ~Game(); + +}; diff --git a/Game.hpp.gch b/Game.hpp.gch new file mode 100644 index 0000000..57bb125 Binary files /dev/null and b/Game.hpp.gch differ diff --git a/Makefile b/Makefile index 99bc61e..d8f7607 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,24 @@ NAME = Bomberman CC = g++ -CFLAGS = -Wall -Wextra -Werror -g3 +CFLAGS = -Wall -Wextra -Werror -g3 -I/usr/include/SDL2 -D_GNU_SOURCE=1 -D_REENTRANT +LDFLAGS = -lSDL2 SRC = main.cpp \ Bomberman.cpp \ + Game.cpp \ + +DEPS = Bomberman.hpp \ + Game.hpp \ OBJ = $(SRC:.cpp=.o) $(NAME): $(OBJ) - $(CC) $(CFLAGS) -o $@ $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) -$(OBJ): $(SRC) - $(CC) $(CFLAGS) -c $(SRC) +$(OBJ): $(SRC) $(DEPS) + $(CC) $(CFLAGS) $(LDFLAGS) -c $(SRC) $(DEPS) all: $(NAME) diff --git a/Position.hpp b/Position.hpp deleted file mode 100644 index bf99fe0..0000000 --- a/Position.hpp +++ /dev/null @@ -1,19 +0,0 @@ -//Position.hpp -#pragma once -#include - -class Position { - private: - int _x; - int _y; - public: - Position() : _x(0), _y(0) {} - ~Position() {} - Position(int x, int y) : _x(x), _y(y) {} - int getX() const { return _x; } - int getY() const { return _y; } - friend std::ostream& operator<<(std::ostream& os, const Position& pos) { - os << "Position(" << pos._x << ", " << pos._y << ")"; - return os; - } -}; diff --git a/main.cpp b/main.cpp index a658139..83e1560 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,13 @@ #include "Bomberman.hpp" +#include "Game.hpp" #include int main (void){ - Bomberman bomberman; - std::cout << bomberman << std::endl; + try{ + Game game; + } catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } return 0; }