Advertisement
Guest User

gui

a guest
Feb 27th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. //Game.cpp
  2.  
  3. #include "Game.h"
  4.  
  5. //Static Functions
  6.  
  7. //Init Functions
  8.  
  9. //Constructors/Destructors
  10.  
  11. void Game::InitWindow()
  12. {
  13.     //Creates SFML window using options from a window.ini file
  14.     ifstream ifs("Config/window.ini");
  15.  
  16.     string title = "None";
  17.     VideoMode window_bounds(800, 600);
  18.     unsigned framerate_limit = 120;
  19.     bool vertical_sync_enabled = false;
  20.  
  21.     if (ifs.is_open())
  22.     {
  23.         getline(ifs, title);
  24.         ifs >> window_bounds.width >> window_bounds.height;
  25.         ifs >> framerate_limit;
  26.         ifs >> vertical_sync_enabled;
  27.     }
  28.  
  29.     ifs.close();
  30.  
  31.     this->window = new RenderWindow(window_bounds, title);
  32.     this->window->setFramerateLimit(framerate_limit);
  33.     this->window->setVerticalSyncEnabled(vertical_sync_enabled);
  34. }
  35.  
  36. void Game::InitStates()
  37. {
  38.     this->states.push(new GameState(this->window));
  39. }
  40.  
  41. Game::Game()
  42. {
  43.     this->InitWindow();
  44.     this->InitStates();
  45. }
  46.  
  47. Game::~Game()
  48. {
  49.     delete this->window;
  50.  
  51.     while (!this->states.empty())
  52.     {
  53.         delete this->states.top();
  54.         this->states.pop();
  55.     }
  56. }
  57.  
  58. //Functions
  59. void Game::endApplication()
  60. {
  61.     cout << "EndingApplication" << "\n";
  62. }
  63.  
  64. void Game::Updatedt()
  65. {
  66.     //Updates Delta Time with the frame render time
  67.     this->dt = this->dtClock.restart().asSeconds();
  68. }
  69.  
  70. void Game::UpdateSFMLEvents()
  71. {
  72.     while (this->window->pollEvent(this->SFEvent))
  73.     {
  74.         if (this->SFEvent.type == Event::Closed)
  75.         {
  76.             this->window->close();
  77.         }
  78.     }
  79. }
  80.  
  81. void Game::Update()
  82. {
  83.     this->UpdateSFMLEvents();
  84.  
  85.     if (!this->states.empty())
  86.     {
  87.         this->states.top()->Update(this->dt);
  88.  
  89.         if (this->states.top()->getQuit())
  90.         {
  91.             this->states.top()->endState();
  92.             delete this->states.top();
  93.             this->states.pop();
  94.         }
  95.     }
  96.     //Application End
  97.     else
  98.     {
  99.         this->endApplication();
  100.         this->window->close();
  101.     }
  102. }
  103.  
  104. void Game::Render()
  105. {
  106.     this->window->clear();
  107.     //things to render
  108.     if (!this->states.empty())
  109.     {
  110.         this->states.top()->Render();
  111.     }
  112.     this->window->display();
  113. }
  114.  
  115. void Game::Run()
  116. {
  117.     while (this->window->isOpen())
  118.     {
  119.         this->Updatedt();
  120.         this->Update();
  121.         this->Run();
  122.     }
  123. }
  124.  
  125. //Game.h
  126.  
  127. #pragma once
  128. #include "GameState.h"
  129.  
  130. class Game
  131. {
  132. private:
  133.     //Variables
  134.     RenderWindow *window;
  135.     Event SFEvent;
  136.  
  137.     Clock dtClock;
  138.     float dt;
  139.  
  140.     stack<State*> states;
  141.  
  142.     //Init
  143.     void InitWindow();
  144.     void InitStates();
  145.  
  146. public:
  147.     //Constructors/Destructors
  148.     Game();
  149.     virtual ~Game();
  150.  
  151.     //Functions
  152.  
  153.     //Tool Functions
  154.     void endApplication();
  155.  
  156.     //Update
  157.     void Updatedt();
  158.     void UpdateSFMLEvents();
  159.     void Update();
  160.  
  161.     //Render
  162.     void Render();
  163.  
  164.     //Core
  165.     void Run();
  166. };
  167.  
  168. //GameState.h
  169.  
  170. #pragma once
  171. #include "State.h"
  172.  
  173. class GameState :
  174.     public State
  175. {
  176. private:
  177.  
  178. public:
  179.     GameState(RenderWindow* window);
  180.     virtual ~GameState();
  181.  
  182.     //Functions
  183.     void endState();
  184.     void updateKeybinds(const float& dt);
  185.     void Update(const float& dt);
  186.     void Render(RenderTarget* target = nullptr);
  187. };
  188.  
  189. //gamestate.cpp
  190.  
  191. #include "GameState.h"
  192.  
  193. GameState::GameState(RenderWindow* window)
  194.     : State(window)
  195. {
  196.  
  197. }
  198.  
  199. GameState::~GameState()
  200. {
  201.  
  202. }
  203.  
  204. void GameState::endState()
  205. {
  206.     cout << "Ending Game State" << "\n";
  207. }
  208.  
  209. void GameState::updateKeybinds(const float& dt)
  210. {
  211.     this->CheckForQuit();
  212.  
  213.  
  214. }
  215.  
  216. void GameState::Update(const float& dt)
  217. {
  218.     this->updateKeybinds(dt);
  219.    
  220.     if (Keyboard::isKeyPressed(Keyboard::A))
  221.     {
  222.         cout << "A" << "\n";
  223.     }
  224. }
  225.  
  226. void GameState::Render(RenderTarget* target)
  227. {
  228.  
  229. }
  230.  
  231. //State.h
  232. #pragma once
  233.  
  234. #include <SFML/Audio.hpp>
  235. #include <SFML/Graphics.hpp>
  236. #include <SFML/Network.hpp>
  237. #include <SFML/System.hpp>
  238. #include <SFML/Window.hpp>
  239.  
  240. #include <iostream>
  241. #include <map>
  242. #include <conio.h>
  243. #include <ctime>
  244. #include <cstdlib>
  245. #include <fstream>
  246. #include <sstream>
  247. #include <stack>
  248. #include <vector>
  249.  
  250. using namespace std;
  251. using namespace sf;
  252.  
  253. class State
  254. {
  255. private:
  256.     RenderWindow* window;
  257.     vector<Texture> textures;
  258.     bool quit;
  259. public:
  260.     State(RenderWindow* window);
  261.     virtual ~State();
  262.  
  263.     const bool& getQuit() const;
  264.     virtual void CheckForQuit();
  265.     virtual void endState() = 0;
  266.  
  267.     virtual void updateKeybinds(const float& dt) = 0;
  268.     virtual void Update(const float& dt) = 0;
  269.     virtual void Render(RenderTarget* target = nullptr) = 0;
  270. };
  271.  
  272. //State.cpp
  273.  
  274. #include "State.h"
  275.  
  276.  
  277. State::State(RenderWindow* window)
  278. {
  279.     this->window = window;
  280.     this->quit = false;
  281. }
  282.  
  283. State::~State()
  284. {
  285.  
  286. }
  287.  
  288. const bool& State::getQuit() const
  289. {
  290.     return this->quit;
  291. }
  292.  
  293. void State::CheckForQuit()
  294. {
  295.     if (Keyboard::isKeyPressed(Keyboard::Escape))
  296.     {
  297.         this->quit = true;
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement