SFML Window scopes?

Nov 1, 2012 at 7:01pm
Hello, so I tried to write SFML programs with multiple files and when I use a window defined elsewhere it cannot find it. Is their some way of creating Global windows?
Nov 1, 2012 at 8:34pm
when I use a window defined elsewhere it cannot find it


What do you mean?
Nov 1, 2012 at 9:19pm
When I open a window in one file I get errors when using it in another file.
Nov 1, 2012 at 9:24pm
Like
1
2
	sf::Window Splash;
    Splash.create(sf::VideoMode(800, 600), "Splash!");


I can not do anything using Splash in other files...
Nov 1, 2012 at 9:41pm
That's hardly helpful.

How are you attempting to use it?
Nov 1, 2012 at 9:46pm
Main File:
1
2
    sf::Window Splash;
    Splash.create(sf::VideoMode(800, 600), "Splash!");

Other File:
 
Splash.close();

Thats all I'm trying to do.
Last edited on Nov 1, 2012 at 9:46pm
Nov 1, 2012 at 10:04pm
Is that a "Local Window?" since its defined inside of a function? Could I define it as a external global?
Nov 2, 2012 at 12:46am
It is local to the function it was defined in, however if were to make a global variable out of every variable we needed access to outside of the function it was defined in, we would be quickly overwhelmed with global variables. Consider, instead, passing it by reference to the code that needs it.
Nov 2, 2012 at 2:55am
Hopefully this helps, you have to pass the window to the other (class?) file.

Example:
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "a.h"

int main () {
   sf::Window Splash;
   Splash.create(sf::VideoMode(800,600), "Splash!");

   A a(Splash);

   a.Close_Splash();

   return 0;
}


Other file (a.h):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A {
Public:
   A (sf::RenderWindow &window);

   Close_Splash();

Private:
   sf::RenderWindow &window;
};

A::A (sf::RenderWindow &window):
   Window(window)
{}

void A::Close_Splash() {
   Window.close();
}


This *should* work.
Last edited on Nov 2, 2012 at 2:57am
Nov 2, 2012 at 9:04pm
Thanks. I never though of just using classes and references.
Topic archived. No new replies allowed.