SFML community forums
Help => Graphics => Topic started by: bglaze on October 11, 2011, 05:24:09 am
-
I would like to create my window object in my Main function, and then set my Game class to have a member variable called "window" so that I can access the window's properties via game.window or game->window.
Is there a way to do this?
Thanks!
-
Yes, this is possible and quite easy to achieve! Here's some psuedo-code that should explain this. (I'm passing the window by a reference instead, there's no need to use a pointer there.)
int main()
{
sf::RenderWindow window;
window.Create();
Game game(window);
game.Run();
}
class Game
{
public:
Game(sf::RenderWindow& window) : m_window(window)
{
}
sf::RenderWindow& GetWindow()
{
return m_window;
}
void Run()
{
while(m_window.IsOpened())
{
m_window.Display();
}
}
private:
sf::RenderWindow& m_window;
}
Here's an example of usage:
void some_func()
{
int width = game->GetWindow().GetWidth();
}
-
Woh, that is perfect! Thank you so much for taking the time to respond! I really appreciate that!
- Brock
-
It still isn't working for me for some reason. Window works fine for me if I don't try to imbed it into my Game class, however, when I do it this way I get an error saying NonCopyable. Here is my code for my main file and my header files, and below them is the error.
main.cpp
#include "Game.h"
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::VideoMode VMode(600, 390, 32);
sf::RenderWindow window;
window.Create(VMode, "Yellow Snake", sf::Style::Close);
Game game(window);
game.Run();
return 0;
}
Game.h
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
#include <iostream>
class Game
{
public:
Game(sf::RenderWindow& window);
sf::RenderWindow Window;
void Run();
protected:
private:
};
#endif // GAME_H
Game.cpp
#include "Game.h"
#include <SFML/Graphics.hpp>
#include <iostream>
Game::Game(sf::RenderWindow& window):
Window(window)
{
}
void Game::Run()
{
window.Display();
}
Error:
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\..\..\..\..\include\SFML\System\NonCopyable.hpp|57|error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\..\..\..\..\include\SFML\Window\Window.hpp|55|error: within this context|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\..\..\..\..\include\SFML\System\NonCopyable.hpp|57|error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\..\..\..\..\include\SFML\Window\Input.hpp|44|error: within this context|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\..\..\..\..\include\SFML\Window\Window.hpp|55|note: synthesized method 'sf::Input::Input(const sf::Input&)' first required here |
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\..\..\..\..\include\SFML\Graphics\RenderWindow.hpp|45|note: synthesized method 'sf::Window::Window(const sf::Window&)' first required here |
s Files\CPP\Dev\Yellow_Snake2\Game.cpp||In constructor 'Game::Game(sf::RenderWindow&)':|
s Files\CPP\Dev\Yellow_Snake2\Game.cpp|6|note: synthesized method 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' first required here |
s Files\CPP\Dev\Yellow_Snake2\Game.cpp||In member function 'void Game::Run()':|
s Files\CPP\Dev\Yellow_Snake2\Game.cpp|12|error: 'window' was not declared in this scope|
s Files\CPP\Dev\Yellow_Snake2\Game.cpp|13|error: expected ';' before '}' token|
||=== Build finished: 9 errors, 0 warnings ===|
Any advice here?
-
In the Run() function, window.Display() should be Window.Display().
-
You can't store a copy (sf::RenderWindow) of the window in your class, you must store a reference (sf::RenderWindow&) to it.
-
keyforge and Laurent:
Thank you both so much. I am up and running, and I learned a lot about references with this mistake. I appreciate the help very much!