Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cfee2379ab | |||
| 90c5485fc5 | |||
| c3b47c30c4 | |||
| 3b05a7e4e9 | |||
| cfe9fc856a | |||
| d76ba79547 | |||
| be39284078 | |||
| 8d6214f12e | |||
| 62a0b9719f | |||
| bd9bc5e2e2 |
@@ -1,28 +0,0 @@
|
|||||||
//Bomb.hpp
|
|
||||||
#pragma once
|
|
||||||
#include "Position.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
class Bomb {
|
|
||||||
private:
|
|
||||||
Position _position;
|
|
||||||
unsigned short _timer;
|
|
||||||
bool _active;
|
|
||||||
public:
|
|
||||||
Bomb(): _position({0, 0}), _timer(3), _active(false) {
|
|
||||||
std::cout << "Bomb created at default position " << std::endl;
|
|
||||||
};
|
|
||||||
|
|
||||||
Bomb(Position position) : _position(position), _timer(3), _active(false) {
|
|
||||||
std::cout << "Bomb created at " << position << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
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; }
|
|
||||||
};
|
|
||||||
+10
-8
@@ -1,10 +1,12 @@
|
|||||||
//Bomberman.cpp
|
|
||||||
#include "Bomberman.hpp"
|
#include "Bomberman.hpp"
|
||||||
|
|
||||||
Bomberman::Bomberman(const Position &position, unsigned short numberOfBombs)
|
std::ostream& operator<<(std::ostream& os, const Bomberman& b) {
|
||||||
: _position(position), _numberOfBombs(numberOfBombs) {
|
os << "Bomberman at (" << b._x << ", " << b._y << ") with "
|
||||||
std::cout << "Bomberman created at position ("
|
<< b._maxBomb << " max bombs, "
|
||||||
<< _position.getX() << ", " << _position.getY()
|
<< b._maxRange << " max range, "
|
||||||
<< ") with " << _numberOfBombs
|
<< b._bombPlanted << " bombs planted, "
|
||||||
<< " bombs!" << std::endl;
|
<< "speed: " << b._speed
|
||||||
}
|
<< ", alive: " << (b._isAlive ? "yes" : "no")
|
||||||
|
<< ", invincible: " << (b._isInvincible ? "yes" : "no");
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|||||||
+12
-11
@@ -1,16 +1,17 @@
|
|||||||
// Bomberman.hpp
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
#include<iostream>
|
||||||
#include "Position.hpp"
|
|
||||||
#include "Bomb.hpp"
|
|
||||||
class Bomberman {
|
|
||||||
|
|
||||||
|
class Bomberman {
|
||||||
private:
|
private:
|
||||||
Position _position;
|
int _x, _y;
|
||||||
unsigned short _numberOfBombs;
|
unsigned short _maxBomb = 1;
|
||||||
Bomb _bomb;
|
unsigned short _maxRange = 1;
|
||||||
|
unsigned short _bombPlanted = 0;
|
||||||
|
float _speed = 1.0f;
|
||||||
|
bool _isAlive = true;
|
||||||
|
bool _isInvincible = false;
|
||||||
public:
|
public:
|
||||||
Bomberman(){std::cout << "Bomberman created!" << std::endl;}
|
Bomberman() = default;
|
||||||
Bomberman(const Position &position, unsigned short numberOfBombs);
|
~Bomberman() = default;
|
||||||
~Bomberman(){std::cout << "Bomberman destroyed!" << std::endl;};
|
friend std::ostream& operator<<(std::ostream& os, const Bomberman& b);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
enum class Element {
|
||||||
|
SOL,
|
||||||
|
MUR,
|
||||||
|
};
|
||||||
|
|
||||||
|
class Carte {
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::string _titre;
|
||||||
|
std::string _auteur;
|
||||||
|
std::string _description;
|
||||||
|
std::vector<std::vector<int>> _tiles;
|
||||||
|
public:
|
||||||
|
Carte() {std::cout << "carte crée\n";}
|
||||||
|
virtual ~Carte(){std::cout << "carte détruite\n";}
|
||||||
|
|
||||||
|
};
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
#include "Carte_Basique.hpp"
|
||||||
|
|
||||||
|
Carte_Basique::Carte_Basique() {
|
||||||
|
|
||||||
|
_titre = "Carte Basique";
|
||||||
|
_auteur = "nullEd";
|
||||||
|
_description = "Description de la carte basique";
|
||||||
|
for (int i = 0; i < 11; ++i) {
|
||||||
|
if (i == 0 || i == 10) {
|
||||||
|
std::vector<int> row(11, static_cast<int>(Element::MUR));
|
||||||
|
_tiles.push_back(row);
|
||||||
|
}
|
||||||
|
else if (i % 2 == 0) {
|
||||||
|
std::vector <int> row;
|
||||||
|
for (int j = 0; j < 11; ++j) {
|
||||||
|
if (j == 0 || j == 10)
|
||||||
|
row.push_back(static_cast<int>(Element::MUR));
|
||||||
|
else if (j % 2 == 0)
|
||||||
|
row.push_back(static_cast<int>(Element::MUR));
|
||||||
|
else
|
||||||
|
row.push_back(static_cast<int>(Element::SOL));
|
||||||
|
}
|
||||||
|
_tiles.push_back(row);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::vector<int> row(11, static_cast<int>(Element::SOL));
|
||||||
|
row[0] = static_cast<int>(Element::MUR);
|
||||||
|
row[10] = static_cast<int>(Element::MUR);
|
||||||
|
_tiles.push_back(row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "Carte Basique créée\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
Carte_Basique::~Carte_Basique() {
|
||||||
|
std::cout << "Destruction de la carte basique\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Carte_Basique& carte) {
|
||||||
|
os << "Titre: " << carte._titre << "\n";
|
||||||
|
os << "Auteur: " << carte._auteur << "\n";
|
||||||
|
os << "Description: " << carte._description << "\n";
|
||||||
|
os << "Tiles:\n";
|
||||||
|
for (const auto& row : carte._tiles) {
|
||||||
|
for (const auto& tile : row) {
|
||||||
|
os << tile << " ";
|
||||||
|
}
|
||||||
|
os << "\n";
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include "Carte.hpp"
|
||||||
|
|
||||||
|
class Carte_Basique : public Carte {
|
||||||
|
|
||||||
|
public:
|
||||||
|
Carte_Basique();
|
||||||
|
~Carte_Basique();
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const Carte_Basique& carte) ;
|
||||||
|
};
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
#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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IMG_Init(IMG_INIT_PNG) == 0) {
|
||||||
|
SDL_Quit();
|
||||||
|
throw std::runtime_error("SDL_image could not initialize! IMG_Error: " + \
|
||||||
|
std::string(IMG_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);
|
||||||
|
}
|
||||||
|
IMG_Quit();
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::Run() {
|
||||||
|
Carte_Basique carte;
|
||||||
|
std::cout << carte << std::endl;
|
||||||
|
//Main game loop
|
||||||
|
while (_isRunning) {
|
||||||
|
//Handle events
|
||||||
|
while (SDL_PollEvent(&_event)) {
|
||||||
|
if (_event.type == SDL_QUIT) {
|
||||||
|
_isRunning = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Clear the screen
|
||||||
|
SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255);
|
||||||
|
SDL_RenderClear(_renderer);
|
||||||
|
|
||||||
|
//Render game objects here
|
||||||
|
|
||||||
|
//Update the screen
|
||||||
|
SDL_RenderPresent(_renderer);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include "Carte_Basique.hpp"
|
||||||
|
|
||||||
|
class Game {
|
||||||
|
private:
|
||||||
|
SDL_Window* _window = nullptr;
|
||||||
|
SDL_Renderer* _renderer = nullptr;
|
||||||
|
SDL_Event _event;
|
||||||
|
bool _isRunning = false;
|
||||||
|
public:
|
||||||
|
Game();
|
||||||
|
void Run();
|
||||||
|
~Game();
|
||||||
|
|
||||||
|
};
|
||||||
@@ -1,20 +1,25 @@
|
|||||||
NAME = Bomberman
|
NAME = Bomberman
|
||||||
|
|
||||||
CC = g++
|
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 \
|
SRC = main.cpp \
|
||||||
Bomberman.cpp \
|
Bomberman.cpp \
|
||||||
|
Game.cpp \
|
||||||
|
Carte_Basique.cpp \
|
||||||
|
|
||||||
OBJ = $(SRC:.cpp=.o)
|
OBJ = $(SRC:.cpp=.o)
|
||||||
|
|
||||||
all: $(NAME)
|
|
||||||
|
|
||||||
$(NAME): $(OBJ)
|
$(NAME): $(OBJ)
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ)
|
||||||
|
|
||||||
$(OBJ): $(SRC)
|
$(OBJ): $(SRC)
|
||||||
$(CC) $(CFLAGS) -c $(SRC) -o $@
|
$(CC) $(CFLAGS) $(LDFLAGS) -c $(SRC)
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJ)
|
rm -f $(OBJ)
|
||||||
fclean: clean
|
fclean: clean
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
//Position.hpp
|
|
||||||
#pragma once
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include "Sprite.hpp"
|
||||||
|
|
||||||
|
Sol::Sol(SDL_Renderer* renderer) : Sprite() {
|
||||||
|
// Constructor implementation
|
||||||
|
if (renderer == nullptr)
|
||||||
|
throw std::runtime_error("Renderer is null");
|
||||||
|
surface = IMG_Load("map/SOL.png");
|
||||||
|
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
std::cout << "Sol created." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Sol::~Sol() {
|
||||||
|
// Destructor implementation
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
texture = nullptr;
|
||||||
|
surface = nullptr;
|
||||||
|
std::cout << "Sol destroyed." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sol::draw() {
|
||||||
|
// Drawing implementation
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Sprite.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Sol: public Sprite {
|
||||||
|
public:
|
||||||
|
Sol();
|
||||||
|
virtual ~Sol();
|
||||||
|
void draw() override;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
#include "Sprite.hpp"
|
||||||
|
|
||||||
|
//Implementation du constructeur avec SDL_Renderer comme paramètre
|
||||||
|
//(en gros je vais mettre le constructeur de Sol dans la classe abstraite Sprite pour automatiser la création de texture selon le iflePath)
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_image.h>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
class Sprite {
|
||||||
|
private:
|
||||||
|
SDL_Surface* surface = nullptr;
|
||||||
|
SDL_Texture* texture = nullptr;
|
||||||
|
float posX = 0.0f;
|
||||||
|
float posY = 0.0f;
|
||||||
|
public:
|
||||||
|
Sprite() = default;
|
||||||
|
Sprite(SDL_Renderer* renderer, std::string filePath);
|
||||||
|
virtual ~Sprite() = default;
|
||||||
|
|
||||||
|
// Pure virtual function to be implemented by derived classes
|
||||||
|
virtual void draw() const = 0;
|
||||||
|
|
||||||
|
// Function to set the position of the sprite
|
||||||
|
virtual void setPosition(float x, float y) { posX = x; posY = y; }
|
||||||
|
// Function to get the current position of the sprite
|
||||||
|
virtual void getPosition(float &x, float &y) const { x = posX; y = posY; }
|
||||||
|
};
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 978 B |
Binary file not shown.
@@ -1,6 +1,14 @@
|
|||||||
#include "Bomberman.hpp"
|
#include "Bomberman.hpp"
|
||||||
|
#include "Game.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
int main (void){
|
int main (void){
|
||||||
Bomberman bomberman;
|
try{
|
||||||
|
Game game;
|
||||||
|
game.Run();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user