BiggerRadius

//main.cpp #include "gameManager.h" /*cmake -G "MinGW Makefiles" ..*/ int main() { GameManager g

Apr 21st, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. //main.cpp
  2. #include "gameManager.h"
  3. /*cmake -G "MinGW Makefiles" ..*/
  4. int main() {
  5.     GameManager gameManager;
  6.     if (!gameManager.initialize()) {
  7.         return 1;
  8.     }
  9.  
  10.     sf::RenderWindow& window = gameManager.getWindow();
  11.  
  12.     while (window.isOpen()) {
  13.         while (const std::optional event = window.pollEvent()) {
  14.             if (event->is<sf::Event::Closed>()) {
  15.                 window.close();
  16.             }
  17.         }
  18.         window.clear();
  19.         gameManager.updateWorld(window);
  20.         window.display();
  21.     }
  22.     return 0;
  23. }
  24. //entityImplementation.cpp
  25. #include <SFML/Graphics.hpp>
  26. #include "entity.h"
  27.  
  28.     Entity::Entity(sf::Texture &texture,sf::Vector2f position):sprite(texture){
  29.         sprite.setPosition(position);
  30.     }
  31.     void Entity::setPos(sf::Vector2f position){
  32.         sprite.setPosition(position);
  33.     }
  34.     sf::Vector2f Entity::getPos(){
  35.         return sprite.getPosition();
  36.     }
  37.     void Entity::setTexture(sf::Texture& texture){
  38.         sprite.setTexture(texture);
  39.     }
  40.     void Entity::draw(sf::RenderWindow& window) {
  41.         window.draw(this->sprite);
  42.     }
  43.      Entity::~Entity(){
  44.      };
  45. //entityManager.cpp
  46. #include <string>
  47. #include "entity.h"
  48. #include <SFML/Graphics.hpp>
  49. #include "entityManager.h"
  50. #include <memory>
  51. #include <exception>
  52. #include "player.h"
  53.  
  54. sf::Texture* EntityManager::loadTexture(std::string path) {
  55.         auto it = textureMap.find(path);
  56.         if (it != textureMap.end()) {
  57.             return &it->second;
  58.         } else {
  59.             sf::Texture newTexture;
  60.             if (newTexture.loadFromFile(path)) {
  61.                 textureMap[path] = newTexture;
  62.                 return &textureMap[path];
  63.             } else {
  64.                 throw std::runtime_error("Error loading texture: "+ path);
  65.             }
  66.         }
  67.     }
  68.     std::unique_ptr<Entity> EntityManager::entityFactory(std::string entityType,std::string path,sf::Vector2f position){
  69.         sf::Texture* texture = loadTexture(path);
  70.         if (entityType == "Player"){
  71.             return std::make_unique<Player>(*texture,position);
  72.         }
  73.         throw std::runtime_error("Error loading texture: " + path + " of type: " + entityType);
  74.     }    
  75. //gameManager.cpp
  76. #include "gameManager.h"
  77. #include <iostream>
  78. #include <stdexcept>
  79.  
  80. GameManager::GameManager() : initialization_failed(false), window(sf::VideoMode({1920u, 1080u}), "Game") {}
  81.  
  82. GameManager::~GameManager() {}
  83.  
  84. bool GameManager::initialize() {
  85.     try {
  86.         player = entityManager.entityFactory("Player", "build/assets/texture/cube.png", sf::Vector2f(250, 250));
  87.         if (player) {
  88.             world = World(std::move(player));
  89.         } else {
  90.             std::cerr << "Error: entityFactory returned null!" << std::endl;
  91.             initialization_failed = true;
  92.         }
  93.     } catch (const std::runtime_error& error) {
  94.         std::cerr << "Error creating game object: " << error.what() << std::endl;
  95.         initialization_failed = true;
  96.     }
  97.     if (initialization_failed) {
  98.         std::cerr << "Game initialization failed. Exiting." << std::endl;
  99.         return false;
  100.     }
  101.     window.setFramerateLimit(144);
  102.     return true;
  103. }
  104.  
  105. sf::RenderWindow& GameManager::getWindow() {
  106.     return window;
  107. }
  108.  
  109. void GameManager::updateWorld(sf::RenderWindow& window) {
  110.     world.update(window);
  111. }
  112.  
  113. void GameManager::runGameLoop() {
  114.     while (window.isOpen()) {
  115.         while (const std::optional event = window.pollEvent()) {
  116.             if (event->is<sf::Event::Closed>()) {
  117.                 window.close();
  118.             }
  119.         }
  120.         window.clear();
  121.         updateWorld(window);
  122.         window.display();
  123.     }
  124. }
  125. //player.cpp
  126. #include "entity.h"
  127. #include "player.h"
  128. #include <SFML/Graphics.hpp>
  129.  
  130. Player::Player(sf::Texture& texture, sf::Vector2f position) : Entity(texture, position){
  131. }
  132. //world.cpp
  133. #include "world.h"
  134. #include "player.h"
  135.  
  136. World::World(std::unique_ptr<Entity> player):player(std::move(player)){
  137. }
  138.  
  139. World::World() : player(nullptr) {}
  140.  
  141. Entity* World::getPlayer() const {
  142.     return player.get();
  143. }
  144.  
  145. void World::update(sf::RenderWindow& window){
  146.     player->draw(window);
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment