Add GameState and MainGame
This commit is contained in:
@@ -0,0 +1,71 @@
|
|||||||
|
#ifndef GAMESTATE
|
||||||
|
#define GAMESTATE
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
class GameState
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void Initialize(sf::RenderWindow* window) {
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Update(sf::RenderWindow* window) {
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Render(sf::RenderWindow* window) {
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Destroy(sf::RenderWindow* window) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class GameStateParent
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GameStateParent() {
|
||||||
|
this->state = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetWindow(sf::RenderWindow* window) {
|
||||||
|
this->window = window;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetState(GameState* state) {
|
||||||
|
if(this->state != NULL) {
|
||||||
|
this->state->Destroy(this->window);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->state = state;
|
||||||
|
if(this->state != NULL) {
|
||||||
|
this->state->Initialize(this->window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update() {
|
||||||
|
if(this->state != NULL) {
|
||||||
|
this->state->Update(this->window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Render() {
|
||||||
|
if(this->state != NULL) {
|
||||||
|
this->state->Render(this->window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~GameStateParent() {
|
||||||
|
if(this->state != NULL) {
|
||||||
|
this->state->Destroy(this->window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
sf::RenderWindow* window;
|
||||||
|
GameState* state;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern GameStateParent gameState;
|
||||||
|
|
||||||
|
#endif // GAMESTATE
|
||||||
|
|
||||||
+7
-33
@@ -1,14 +1,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
#include "inputmanager.h"
|
#include "state/maingame.h"
|
||||||
#include "entitymanager.h"
|
|
||||||
|
|
||||||
InputManager inputManager;
|
InputManager inputManager;
|
||||||
|
GameStateParent gameState;
|
||||||
void UpdateCollisions(Entity* entityA, Entity* entityB) {
|
|
||||||
std::cout << "Collisions!" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -22,21 +18,13 @@ int main()
|
|||||||
sf::RenderWindow window(sf::VideoMode(1280, 720), gameName, windowStyle, settings);
|
sf::RenderWindow window(sf::VideoMode(1280, 720), gameName, windowStyle, settings);
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
||||||
|
gameState.SetWindow(&window);
|
||||||
|
gameState.SetState(new MainGame());
|
||||||
|
|
||||||
sf::Clock fpsTimer;
|
sf::Clock fpsTimer;
|
||||||
sf::Clock timer;
|
sf::Clock timer;
|
||||||
sf::Time timeElapsed;
|
sf::Time timeElapsed;
|
||||||
|
|
||||||
EntityManager entityManager;
|
|
||||||
entityManager.AddEntity("test", new Entity("data\\gfx\\test.png"));
|
|
||||||
entityManager.AddEntity("test", new Entity("data\\gfx\\test.png"));
|
|
||||||
entityManager.SetCollisionMethod(UpdateCollisions);
|
|
||||||
|
|
||||||
// Entity entity;
|
|
||||||
// entity.Load("data\\gfx\\test.png");
|
|
||||||
|
|
||||||
// Entity entity2;
|
|
||||||
// entity2.Load("data\\gfx\\test.png");
|
|
||||||
|
|
||||||
// Run the program as long as the window is open
|
// Run the program as long as the window is open
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
@@ -65,26 +53,12 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(inputManager.IsPressed(InputManager::Left)) {
|
|
||||||
std::cout << "LEFT" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(inputManager.IsPressed(InputManager::Down)) {
|
|
||||||
std::cout << "DOWN" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if(entity.Collision(&entity2)) {
|
|
||||||
// std::cout << "COLLISION!" << std::endl;
|
|
||||||
// }
|
|
||||||
|
|
||||||
window.clear(sf::Color(72, 152, 72));
|
window.clear(sf::Color(72, 152, 72));
|
||||||
|
|
||||||
// window.draw(entity);
|
gameState.Update();
|
||||||
// window.draw(entity2);
|
gameState.Render();
|
||||||
|
|
||||||
entityManager.Render(&window);
|
|
||||||
|
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -18,10 +18,12 @@ DEPENDPATH += $$PWD/include
|
|||||||
TEMPLATE = app
|
TEMPLATE = app
|
||||||
|
|
||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
inputmanager.cpp
|
inputmanager.cpp \
|
||||||
|
state/maingame.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
inputmanager.h \
|
inputmanager.h \
|
||||||
entity.h \
|
entity.h \
|
||||||
entitymanager.h
|
entitymanager.h \
|
||||||
|
gamestate.h \
|
||||||
|
state/maingame.h
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include "maingame.h"
|
||||||
|
|
||||||
|
void UpdateCollisions(Entity* entityA, Entity* entityB) {
|
||||||
|
std::cout << "Collisions!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainGame::Initialize(sf::RenderWindow* window) {
|
||||||
|
entityManager = new EntityManager();
|
||||||
|
entityManager->SetCollisionMethod(UpdateCollisions);
|
||||||
|
|
||||||
|
this->entityManager->AddEntity("test", new Entity("data\\gfx\\test.png"));
|
||||||
|
this->entityManager->AddEntity("test", new Entity("data\\gfx\\test.png"));
|
||||||
|
this->entityManager->Get("test0")->velocity.x = 0.5;
|
||||||
|
this->entityManager->Get("test")->setPosition(sf::Vector2f(50, 50));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainGame::Update(sf::RenderWindow* window) {
|
||||||
|
if(inputManager.IsPressed(InputManager::Left)) {
|
||||||
|
std::cout << "LEFT" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(inputManager.IsPressed(InputManager::Down)) {
|
||||||
|
std::cout << "DOWN" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->entityManager->Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainGame::Render(sf::RenderWindow* window) {
|
||||||
|
this->entityManager->Render(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainGame::Destroy(sf::RenderWindow* window) {
|
||||||
|
delete this->entityManager;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef MAINGAME_H
|
||||||
|
#define MAINGAME_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "gamestate.h"
|
||||||
|
#include "entitymanager.h"
|
||||||
|
#include "inputmanager.h"
|
||||||
|
|
||||||
|
class MainGame : public GameState
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Initialize(sf::RenderWindow* window);
|
||||||
|
void Update(sf::RenderWindow* window);
|
||||||
|
void Render(sf::RenderWindow* window);
|
||||||
|
void Destroy(sf::RenderWindow* window);
|
||||||
|
|
||||||
|
private:
|
||||||
|
EntityManager* entityManager;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MAINGAME_H
|
||||||
Reference in New Issue
Block a user