ouverture de fenetre possible

faire le Game::run
This commit is contained in:
YANNIS
2025-05-29 18:25:10 +02:00
parent bd9bc5e2e2
commit 62a0b9719f
10 changed files with 91 additions and 68 deletions
-26
View File
@@ -1,26 +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) {
};
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; }
};
+10 -1
View File
@@ -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;
}
+12 -16
View File
@@ -1,21 +1,17 @@
// Bomberman.hpp
#pragma once
#include <iostream>
#include "Position.hpp"
#include "Bomb.hpp"
class Bomberman {
#include<iostream>
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);
};
BIN
View File
Binary file not shown.
+38
View File
@@ -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();
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <SDL2/SDL.h>
#include <iostream>
class Game {
private:
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
bool isRunning = false;
public:
Game();
void Run();
~Game();
};
BIN
View File
Binary file not shown.
+9 -4
View File
@@ -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)
-19
View File
@@ -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;
}
};
+7 -2
View File
@@ -1,8 +1,13 @@
#include "Bomberman.hpp"
#include "Game.hpp"
#include <iostream>
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;
}