Hi everyone.
So here is my situation: I'm trying to implement a system that allows me to globally change through different game states, in this case, a Scene class. The Scene class inherits sf::RenderTexture so I get all the benefits of drawing and displaying sf::Sprites and such. The way I manage the Scenes is through an STL map where I would store a pointer to the Scene along with an std::string for the key. I then implemented a method called getCurrScene() where it would return a pointer to the scene by returning the item of the map with the index of the current string. The current string is set manually through a method called setCurrentString. My problem is that the pointer that seems to be returned by the STL map of Scene pointers are blank. I get no errors, but when I run my program, I end up with a distorted window, the same window you would get if you don't update/draw to the window correctly. From testing with cout, none of the methods of the scene are being called! Any help would be appreciate.
GSWindow.cpp
#include "GSWindow.h"
GSWindow::GSWindow() : sf::RenderWindow(sf::VideoMode(600,600),"Greenberg Rocket"){
addScene(new Menu(),"menu");
addScene(new Game(),"game");
//The current scene to be rendered to the window
Scenes::setCurrentString("menu");
setFramerateLimit(60);
while(isOpen()){
sf::Clock clock;
sf::Event event;
while(pollEvent(event)){
if(event.type == sf::Event::Closed){
close();
}
getCurrScene()->pollEvent(event);
}
clear(sf::Color(0,0,0));
getCurrScene()->update(clock.getElapsedTime().asSeconds());
getCurrScene()->display();
sf::Sprite s(getCurrScene()->getTexture());
draw(s);
display();
}
}
void GSWindow::addScene(Scene *s, std::string n){
scenes[n] = s;
}
Scene* GSWindow::getCurrScene(){
return scenes.find(Scenes::getCurrentString())->second;
}
int main(int argc, char* argv[]){
new GSWindow();
return 0;
}
GSWindow.h
#ifndef GS_WINDOW_H
#define GS_WINDOW_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include "ResourcePath.hpp"
#include "Menu.h"
#include "Game.h"
#include "Scenes.h"
#include <map>
class GSWindow : public sf::RenderWindow {
public:
GSWindow();
void addScene(Scene* s, std::string n);
Scene* getCurrScene();
private:
std::map<std::string,Scene*> scenes;
};
#endif
Scene.h
#ifndef SCENE_H
#define SCENE_H
#include <SFML/Graphics.hpp>
#include "SpriteTween.h"
#include <vector>
class Scene : public sf::RenderTexture {
public:
Scene();
void update(float time) {};
void pollEvent(sf::Event& event) {};
};
#endif