stefan
stefan

Reputation: 11

SFML pass renderwindow

Im trying to make a game with the SFML.

I'm making a sf::RenderWindow but when I try to pass the window to another class it fails. I can't access the window. Because I think it's good to make a separate class for handling events like 'close the window' etc. but then I can't access it. How can I fix this?

RenderWindow *window;  
window = new RenderWindow(VideoMode(768, 614), "Tower Defence ver 2.0");  

Upvotes: 0

Views: 3656

Answers (4)

killer
killer

Reputation: 382

try this

class Foo
{
 public:
    Foo(sf::RenderWindow& ptrwindow)
     : ptrwindow(ptrwindow)
    {
          // your code here
    };
    sf::RenderWindow* window()
    {
         return &this->ptrwindow;
    }

  private:
        sf::RenderWindow ptrwindow;
};

int main()
{
 sf::RenderWindow* mywindow = new sf::RenderWindow()
 Foo myfoo(*mywindow);
 myfoo.window()->create(sf::VideoMode(768, 614), "Tower Defence ver 2.0")
}

Upvotes: 0

Canvas
Canvas

Reputation: 5897

Create yourself a header file and define your function like so

Header file

#pragma once

#include "SFML/Graphics.hpp"

class MyClass
{
public:
    sf::Sprite Sprite;

    MyClass();
    void Setup(sf::Texture& texture);
    void Draw(sf::RenderWindow& window);
};

Cpp file

#include "Bullet.h"

MyClass::MyClass()
{

}

void MyClass::Setup(sf::Texture& texture)
{
    Sprite.setTexture(texture);
    Sprite.setPosition(0, 0);
}

void MyClass::Draw(sf::RenderWindow& window)
{
    window.draw(Sprite);
}

Then in your game loop for drawing you can call something like this

// myClass is an object of type MyClass
// renderWindow is your sf::RenderWindow object
myClass.Draw(renderWindow);

Hope this helps. Let me know if you need any more guidance.

Upvotes: 1

Kevin Wang
Kevin Wang

Reputation: 3330

Which version of SFML are you using? This is not possible in SFML 1.6, but is in SFML 2.0 (upcoming version).

Upvotes: 0

malkia
malkia

Reputation: 1387

RenderWindow is in a namespace 'sf'

Maybe you have somewhere "using namespace sf;" and it's missing in other places.

Try prefixing it with sf::RenderWindow everywhere.

Upvotes: 0

Related Questions