Sir
Sir

Reputation: 8280

How to use a global for my render window

I have a global assigned as a pointer to my window like this:

globals.cpp & globals.h has:

#include <SFML/Graphics.hpp>
sf::RenderWindow* window

Then in main.cpp i put:

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <map>
#include <string>

using namespace std;

#include "globals.h"

window(VideoMode( (800,600) , "Test") ); //line 22

How ever this appears to be incorrect. As i get this error:

main.cpp(22): error C2228: left of '.VideoMode' must have class/struct/union

What am doing wrong here?

Upvotes: 1

Views: 2085

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409176

Looking at e.g. this tutorial it seems that the VideoMode constructor takes three arguments: Two for the resolution and one for the bitmap depth (but the depth have a default value).

You should create the window like this:

window = new sf::RenderWindow(sf::VideoMode(800, 600), "Test");

Upvotes: 3

Praetorian
Praetorian

Reputation: 109119

Maybe I've totally misunderstood the question, but why do you have the pointer defined in both the header and the source file? Seems to me you should do the following:

In globals.h

extern sf::RenderWindow *window;

In globals.cpp

sf::RenderWindow *window;

In main.cpp

window = new sf::RenderWindow( sf::VideoMode(800,600), "Test" );

And don't forget to delete window once you're done with it.


Also, I strongly urge you to replace the global pointer with

std::unique_ptr<sf::RenderWindow> window;

and initialize it as

window( new sf::RenderWindow( sf::VideoMode(800,600), "Test" ) );

Now you don't need to worry about deleteing it!

Upvotes: 6

Related Questions