Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include "game_manager.h"
- /*cmake -G "MinGW Makefiles" ..*/
- int main() {
- GameManager gameManager;
- if (!gameManager.initialize()) {
- return 1;
- }
- gameManager.runGameLoop();
- return 0;
- }
- //game_manager.cpp
- #include "game_manager.h"
- #include <iostream>
- #include <stdexcept>
- #include <SFML/Graphics.hpp>
- #include <cmath>
- #include <map>
- #include <string>
- enum class GameManager::GameStates{Play,Pause,Menu};
- void GameManager::changeState(GameManager::GameStates newState){currentState = newState;}
- GameManager::GameStates GameManager::getCurrentState(){return currentState;}
- GameManager::GameManager() : initialization_failed(false), window(sf::VideoMode({1920u, 1080u}), "Game") {
- instance = this;
- }
- GameManager::~GameManager() {}
- bool GameManager::initialize() {
- window.setVerticalSyncEnabled(true);
- menu.setupMenu();
- changeState(GameStates::Menu);
- initialization_failed = false;
- try {
- player = entityManager.entityFactory("Player", "build/assets/texture/cube.png", sf::Vector2f(250, 250));
- world = World(std::move(player));
- } catch (const std::runtime_error& error) {
- std::cerr << "Error creating game object: " << error.what() << std::endl;
- initialization_failed = true;
- }
- if (initialization_failed) {
- std::cerr << "Game initialization failed. Exiting." << std::endl;
- return false;
- }
- window.setFramerateLimit(144);
- return true;
- }
- sf::RenderWindow& GameManager::getWindow() {
- return window;
- }
- void GameManager::menuLoop(){
- while (getCurrentState() == GameStates::Menu){//Menu state
- window.display();
- world.draw(window,World::DrawContext::Menu);
- const auto& buttons = menu.getButtons();
- if (!buttons.empty() && buttons[0]->isClicked(sf::Mouse::getPosition() , sf::Mouse::Button::Left)){
- changeState(GameStates::Play);
- }
- }
- }
- void GameManager::runGameLoop() {
- menuLoop();
- auto player = world.getPlayer();
- std::map <std::string,bool> inputs;
- sf::Vector2f normalizedMovementVector;
- auto InputToMovementDirection = [&player,this] (std::map<std::string,bool> inputs){
- sf::Vector2f direction;
- for (auto input: inputs){
- if(input.second){
- if(input.first == "pressingEnter"){
- //reserved
- }
- if(input.first == "pressingW"){
- direction.y -= player->getSpeed().y;
- }
- if(input.first == "pressingA"){
- direction.x -= player->getSpeed().x;
- }
- if(input.first == "pressingS"){
- direction.y += player->getSpeed().y;
- }
- if(input.first == "pressingD"){
- direction.x += player->getSpeed().x;
- }
- if(input.first == "pressingLeftMouse"){
- //reserved
- }
- if(input.first == "pressingRightMouse"){
- //reserved
- }
- }
- }
- return direction;
- };
- while (window.isOpen()){
- while (const std::optional event = window.pollEvent()) {
- if (event->is<sf::Event::Closed>()) {
- window.close();
- }//this line of code is going to be changed
- if(const auto* keyPressed = event->getIf<sf::Event::KeyPressed>()){
- if (keyPressed->scancode == sf::Keyboard::Scancode::Escape){
- if(getCurrentState() != GameStates::Menu){
- if(getCurrentState() == GameStates::Pause){
- changeState(GameStates::Play);
- }else if (getCurrentState() == GameStates::Play){
- changeState(GameStates::Pause);
- }
- }
- }
- }
- }
- using namespace sf::Keyboard;
- using namespace sf::Mouse;
- inputs["pressingEnter"] = isKeyPressed(Key::Enter);
- inputs["pressingW"] = isKeyPressed(Key::W);
- inputs["pressingA"] = isKeyPressed(Key::A);
- inputs["pressingS"] = isKeyPressed(Key::S);
- inputs["pressingD"] = isKeyPressed(Key::D);
- inputs["pressingLeftMouse"] = isButtonPressed(sf::Mouse::Button::Left);
- inputs["pressingRightMouse"] = isButtonPressed(sf::Mouse::Button::Right);
- normalizedMovementVector = InputToMovementDirection(inputs);
- sf::Time deltaTime = clock.restart();
- window.clear();
- if(getCurrentState() == GameStates::Play){
- float dtSeconds = deltaTime.asSeconds();
- world.update(dtSeconds, normalizedMovementVector);
- world.draw(window,World::DrawContext::Game);
- } else if (getCurrentState() == GameStates::Pause){
- pause.setupPause();
- world.draw(window,World::DrawContext::Pause);
- const auto& buttons = pause.getButtons();
- if (!buttons.empty() && buttons[0]->isClicked(sf::Mouse::getPosition() , sf::Mouse::Button::Left)){
- changeState(GameStates::Menu);
- menuLoop();
- }
- //paused state
- }
- window.display();
- }
- }
- const Menu& GameManager::getMenu(){
- return this->menu;
- }
- const Pause& GameManager::getPause(){
- return this->pause;
- }
- //world.cpp
- #include "world.h"
- #include "player.h"
- #include <memory>
- #include <iostream>
- #include "game_manager.h"
- World::World(std::shared_ptr<Entity> player):player(player){
- }
- World::World() : player(nullptr) {}
- Entity* World::getPlayer() const {
- return player.get();
- }
- void World::update(float dtSeconds,sf::Vector2f movementInput){
- Entity* player = this->getPlayer();
- player->setPos(player->getPos() + movementInput);
- }
- void World::draw(sf::RenderWindow& window,World::DrawContext DrawContext){
- const auto gameManager = GameManager::instance;
- if(DrawContext == DrawContext::Game){
- player->draw(window);
- }
- if (DrawContext == DrawContext::Menu){
- gameManager->getMenu().draw(window);
- }
- if (DrawContext == DrawContext::Pause){
- gameManager->getPause().draw(window);
- }
- }
- //menu.cpp
- #include "button_manager.h"
- #include "button.h"
- #include <SFML/Graphics.hpp>
- #include "menu.h"
- void Menu::setupMenu(){
- buttons.push_back(buttonManager.ButtonFactory("build/assets/texture/play.png",sf::Vector2f{500,500}));
- }
- void Menu::draw(sf::RenderWindow& window)const{
- for(auto& button: buttons){
- window.draw(button.get()->getSprite());
- }
- }
- std::vector<std::shared_ptr<Button>> Menu::getButtons(){
- return this->buttonManager.getButtons();
- }
- //pause.cpp
- #include "button_manager.h"
- #include "button.h"
- #include <SFML/Graphics.hpp>
- #include "pause.h"
- void Pause::setupPause(){
- buttons.push_back(buttonManager.ButtonFactory("build/assets/texture/Menu.png",sf::Vector2f{500,500}));
- }
- void Pause::draw(sf::RenderWindow& window)const{
- for(auto& button: buttons){
- window.draw(button.get()->getSprite());
- }
- }
- std::vector<std::shared_ptr<Button>> Pause::getButtons(){
- return this->buttonManager.getButtons();
- }
- //button_manager.cpp
- #include "button_manager.h"
- #include <functional>
- #include <memory>
- #include "button.h"
- std::shared_ptr<Button> ButtonManager::ButtonFactory(std::string path,sf::Vector2f position){
- sf::Texture* texture = textureManager.loadTexture(path);
- auto button = std::make_shared<Button>(*texture,position);
- buttons.push_back(button);
- return button;
- };
- std::vector<std::shared_ptr<Button>> ButtonManager::getButtons(){
- return buttons;
- }
- //button_implementation.cpp
- #include "button.h"
- #include <functional>
- #include <SFML/Graphics.hpp>
- Button::Button(sf::Texture& texture,sf::Vector2f position):effect(effect),sprite(texture){
- sprite.setPosition(position);
- }
- sf::Sprite Button::getSprite(){
- return this->sprite;
- }
- bool Button::isClicked(sf::Vector2i mousePosition, sf::Mouse::Button buttonType){
- if(!clicked){
- clicked = true;
- return sprite.getGlobalBounds().contains(sf::Vector2f(mousePosition)) && sf::Mouse::isButtonPressed(buttonType);
- }
- if(!sf::Mouse::isButtonPressed(buttonType)){
- clicked = false;
- }
- return false;
- }
- //entity_manager.cpp
- #include <string>
- #include "entity.h"
- #include <SFML/Graphics.hpp>
- #include "entity_manager.h"
- #include <memory>
- #include <iostream>
- #include "player.h"
- #include "texture_manager.h"
- std::shared_ptr<Entity> EntityManager::entityFactory(std::string entityType,std::string path,sf::Vector2f position){
- sf::Texture* texture = textureManager.loadTexture(path);
- if (entityType == "Player"){
- return std::make_shared<Player>(*texture,position);
- }
- throw std::runtime_error("Error loading texture: " + path + " of type: " + entityType);
- }
- //player.cpp
- #include "entity.h"
- #include "player.h"
- #include <SFML/Graphics.hpp>
- Player::Player(sf::Texture& texture, sf::Vector2f position) : Entity(texture, position){
- setSpeed({1.0,1.0});
- }
- //entity_implementation.cpp
- #include <SFML/Graphics.hpp>
- #include "entity.h"
- Entity::Entity(sf::Texture &texture,sf::Vector2f position):sprite(texture){
- sprite.setPosition(position);
- }
- void Entity::setPos(sf::Vector2f position){
- sprite.setPosition(position);
- }
- sf::Vector2f Entity::getPos(){
- return sprite.getPosition();
- }
- void Entity::setTexture(sf::Texture& texture){
- sprite.setTexture(texture);
- }
- void Entity::draw(sf::RenderWindow& window) {
- window.draw(this->sprite);
- }
- sf::Vector2f Entity::getSpeed(){
- return speed;
- }
- void Entity::setSpeed(sf::Vector2f newSpeed){
- speed = newSpeed;
- }
- Entity::~Entity(){
- };
- //texture_manager.cpp
- #include <SFML/Graphics.hpp>
- #include <string>
- #include <map>
- #include "texture_manager.h"
- sf::Texture* TextureManager::loadTexture(std::string path) {
- auto it = textureMap.find(path);
- if (it != textureMap.end()) {
- return &it->second;
- } else {
- sf::Texture newTexture;
- if (newTexture.loadFromFile(path)) {
- textureMap[path] = newTexture;
- return &textureMap[path];
- } else {
- throw std::runtime_error("Error loading texture: " + path);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment