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

Author Topic: Pointer to RenderWindow object?  (Read 10370 times)

0 Members and 1 Guest are viewing this topic.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Pointer to RenderWindow object?
« 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!

keyforge

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Pointer to RenderWindow object?
« Reply #1 on: October 11, 2011, 05:38:22 am »
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.)

Code: [Select]

int main()
{
    sf::RenderWindow window;
    window.Create();

    Game game(window);
    game.Run();
}


Code: [Select]

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:
Code: [Select]

void some_func()
{
    int width = game->GetWindow().GetWidth();
}
Need a place to upload your code, files or screenshots? Use SFML Uploads!

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Pointer to RenderWindow object?
« Reply #2 on: October 11, 2011, 05:54:03 am »
Woh, that is perfect! Thank you so much for taking the time to respond! I really appreciate that!

- Brock

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Pointer to RenderWindow object?
« Reply #3 on: October 11, 2011, 06:22:29 am »
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
Code: [Select]

#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
Code: [Select]

#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
Code: [Select]

#include "Game.h"
#include <SFML/Graphics.hpp>
#include <iostream>

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

void Game::Run()
{
    window.Display();
}


Error:
Code: [Select]

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?

keyforge

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Pointer to RenderWindow object?
« Reply #4 on: October 11, 2011, 06:44:49 am »
In the Run() function, window.Display() should be Window.Display().
Need a place to upload your code, files or screenshots? Use SFML Uploads!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Pointer to RenderWindow object?
« Reply #5 on: October 11, 2011, 07:45:13 am »
You can't store a copy (sf::RenderWindow) of the window in your class, you must store a reference (sf::RenderWindow&) to it.
Laurent Gomila - SFML developer

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Pointer to RenderWindow object?
« Reply #6 on: October 11, 2011, 07:24:06 pm »
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!

 

anything