Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: STL map RenderTexture don't seem to be behaving  (Read 2293 times)

0 Members and 1 Guest are viewing this topic.

malkovich

  • Newbie
  • *
  • Posts: 4
    • View Profile
STL map RenderTexture don't seem to be behaving
« on: January 05, 2014, 12:52:42 am »
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
 

malkovich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: STL map RenderTexture don't seem to be behaving
« Reply #1 on: January 05, 2014, 01:23:11 am »
I fixed my issue. It was because my Scene class methods weren't being overrided (not virtual), so having the map recognize them as only Scene pointers made it call a blank method. D'Oh!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: STL map RenderTexture don't seem to be behaving
« Reply #2 on: January 05, 2014, 09:19:24 am »
It makes no sense to derive Scene from RenderTexture. A scene may have a render texture, but it certainly is not one. See also LSP.

I wouldn't derive GSWindow from sf::Window, either. Prefer composition over inheritance whereever possible.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

malkovich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: STL map RenderTexture don't seem to be behaving
« Reply #3 on: January 05, 2014, 03:04:04 pm »
By inheriting sf::RenderTexture for a scene, I'm given all the nice methods to automatically draw and receive events from the RenderWindow. However, I'm up for any alternatives. What do you suggest I do?

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: STL map RenderTexture don't seem to be behaving
« Reply #4 on: January 05, 2014, 03:13:07 pm »
I'm interested in a different solution too, thats the best practice for things like these?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: STL map RenderTexture don't seem to be behaving
« Reply #5 on: January 05, 2014, 03:14:22 pm »
By inheriting sf::RenderTexture for a scene, I'm given all the nice methods to automatically draw and receive events from the RenderWindow.
Don't inherit for convenience, but when it makes sense (see LSP).

You should encapsulate your classes, users of the scene don't need access to the render texture. So, keep it as an implementation detail by using composition (member variables) instead of inheritance. In general, separate responsibilities as much as possible. Also try to abstract from SFML where it makes sense -- in your game, SFML should only be required for some components like graphics or input handling, but it shouldn't be the core. Ideally, your design is such that SFML could be replaced by SDL or another library without rewriting the entire game, but "only" some modular parts of it. Even if it requires more code and additional class definitions, in the long term it will be much easier to maintain the project if dependencies are kept local.

If you need more tips, I've recently aggregated a list of topics :)
« Last Edit: January 05, 2014, 03:17:14 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

malkovich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: STL map RenderTexture don't seem to be behaving
« Reply #6 on: January 06, 2014, 01:01:44 am »
Nexus, you're the best.

 

anything