Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Game.cpp
- #include "Game.h"
- //Static Functions
- //Init Functions
- //Constructors/Destructors
- void Game::InitWindow()
- {
- //Creates SFML window using options from a window.ini file
- ifstream ifs("Config/window.ini");
- string title = "None";
- VideoMode window_bounds(800, 600);
- unsigned framerate_limit = 120;
- bool vertical_sync_enabled = false;
- if (ifs.is_open())
- {
- getline(ifs, title);
- ifs >> window_bounds.width >> window_bounds.height;
- ifs >> framerate_limit;
- ifs >> vertical_sync_enabled;
- }
- ifs.close();
- this->window = new RenderWindow(window_bounds, title);
- this->window->setFramerateLimit(framerate_limit);
- this->window->setVerticalSyncEnabled(vertical_sync_enabled);
- }
- void Game::InitStates()
- {
- this->states.push(new GameState(this->window));
- }
- Game::Game()
- {
- this->InitWindow();
- this->InitStates();
- }
- Game::~Game()
- {
- delete this->window;
- while (!this->states.empty())
- {
- delete this->states.top();
- this->states.pop();
- }
- }
- //Functions
- void Game::endApplication()
- {
- cout << "EndingApplication" << "\n";
- }
- void Game::Updatedt()
- {
- //Updates Delta Time with the frame render time
- this->dt = this->dtClock.restart().asSeconds();
- }
- void Game::UpdateSFMLEvents()
- {
- while (this->window->pollEvent(this->SFEvent))
- {
- if (this->SFEvent.type == Event::Closed)
- {
- this->window->close();
- }
- }
- }
- void Game::Update()
- {
- this->UpdateSFMLEvents();
- if (!this->states.empty())
- {
- this->states.top()->Update(this->dt);
- if (this->states.top()->getQuit())
- {
- this->states.top()->endState();
- delete this->states.top();
- this->states.pop();
- }
- }
- //Application End
- else
- {
- this->endApplication();
- this->window->close();
- }
- }
- void Game::Render()
- {
- this->window->clear();
- //things to render
- if (!this->states.empty())
- {
- this->states.top()->Render();
- }
- this->window->display();
- }
- void Game::Run()
- {
- while (this->window->isOpen())
- {
- this->Updatedt();
- this->Update();
- this->Run();
- }
- }
- //Game.h
- #pragma once
- #include "GameState.h"
- class Game
- {
- private:
- //Variables
- RenderWindow *window;
- Event SFEvent;
- Clock dtClock;
- float dt;
- stack<State*> states;
- //Init
- void InitWindow();
- void InitStates();
- public:
- //Constructors/Destructors
- Game();
- virtual ~Game();
- //Functions
- //Tool Functions
- void endApplication();
- //Update
- void Updatedt();
- void UpdateSFMLEvents();
- void Update();
- //Render
- void Render();
- //Core
- void Run();
- };
- //GameState.h
- #pragma once
- #include "State.h"
- class GameState :
- public State
- {
- private:
- public:
- GameState(RenderWindow* window);
- virtual ~GameState();
- //Functions
- void endState();
- void updateKeybinds(const float& dt);
- void Update(const float& dt);
- void Render(RenderTarget* target = nullptr);
- };
- //gamestate.cpp
- #include "GameState.h"
- GameState::GameState(RenderWindow* window)
- : State(window)
- {
- }
- GameState::~GameState()
- {
- }
- void GameState::endState()
- {
- cout << "Ending Game State" << "\n";
- }
- void GameState::updateKeybinds(const float& dt)
- {
- this->CheckForQuit();
- }
- void GameState::Update(const float& dt)
- {
- this->updateKeybinds(dt);
- if (Keyboard::isKeyPressed(Keyboard::A))
- {
- cout << "A" << "\n";
- }
- }
- void GameState::Render(RenderTarget* target)
- {
- }
- //State.h
- #pragma once
- #include <SFML/Audio.hpp>
- #include <SFML/Graphics.hpp>
- #include <SFML/Network.hpp>
- #include <SFML/System.hpp>
- #include <SFML/Window.hpp>
- #include <iostream>
- #include <map>
- #include <conio.h>
- #include <ctime>
- #include <cstdlib>
- #include <fstream>
- #include <sstream>
- #include <stack>
- #include <vector>
- using namespace std;
- using namespace sf;
- class State
- {
- private:
- RenderWindow* window;
- vector<Texture> textures;
- bool quit;
- public:
- State(RenderWindow* window);
- virtual ~State();
- const bool& getQuit() const;
- virtual void CheckForQuit();
- virtual void endState() = 0;
- virtual void updateKeybinds(const float& dt) = 0;
- virtual void Update(const float& dt) = 0;
- virtual void Render(RenderTarget* target = nullptr) = 0;
- };
- //State.cpp
- #include "State.h"
- State::State(RenderWindow* window)
- {
- this->window = window;
- this->quit = false;
- }
- State::~State()
- {
- }
- const bool& State::getQuit() const
- {
- return this->quit;
- }
- void State::CheckForQuit()
- {
- if (Keyboard::isKeyPressed(Keyboard::Escape))
- {
- this->quit = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement