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

Author Topic: Default Code Problem, trying to use a RenderWindow as arg  (Read 2455 times)

0 Members and 1 Guest are viewing this topic.

Portalboat

  • Newbie
  • *
  • Posts: 9
    • View Profile
Default Code Problem, trying to use a RenderWindow as arg
« on: January 31, 2011, 07:19:29 am »
Code: [Select]
#include <SFML/Graphics.hpp>
class Player
{
public:
int health;
int attackDamage;
int friendshipMeter;
int Move();
sf::Sprite SpritePlayer;
sf::Image ImagePlayer;
sf::RenderWindow App;
sf::Sprite Initialize()
{
ImagePlayer.LoadFromFile("squiddlePlayer.png");
SpritePlayer.SetImage(ImagePlayer);
return SpritePlayer;
}
void Update(sf::RenderWindow App)
{

float ElapsedTime = App.GetFrameTime();
// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left))  SpritePlayer.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) SpritePlayer.Move( 100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up))    SpritePlayer.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down))  SpritePlayer.Move(0,  100 * ElapsedTime);

// Rotate the sprite
if (App.GetInput().IsKeyDown(sf::Key::Add))      SpritePlayer.Rotate(-100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Subtract)) SpritePlayer.Rotate( 100 * ElapsedTime);
}
};


Code: [Select]
#include "Player.h"
#include <SFML/Graphics.hpp>
int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "The Day the Unicorns Couldn't Play");
Player Player;
sf::Sprite spritePlayer = Player.Initialize();
    // Start game loop
    while (App.IsOpened())
    {
Player.Update(App);
sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();
App.Draw(spritePlayer);

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


Those are my two files, Main.cpp and Player.h. When I have the Player.Update(App) line in, the code doesn't compile. But it appears to be a error with the default code:
Code: [Select]
error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
        c:\program files\sfml\sfml-1.6\include\sfml\system\noncopyable.hpp(57) : see declaration of 'sf::NonCopyable::NonCopyable'
        c:\program files\sfml\sfml-1.6\include\sfml\system\noncopyable.hpp(41) : see declaration of 'sf::NonCopyable'
        This diagnostic occurred in the compiler generated function 'sf::Window::Window(const sf::Window &)'


I don't understand why simply calling a function could call this error. Unless I can't use the RenderWindow as a argument? If I can't, then what can I do to have an update function?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Default Code Problem, trying to use a RenderWindow as arg
« Reply #1 on: January 31, 2011, 07:37:39 am »
You can't pass your window by value (that would create a copy of it, which is disallowed), pass it by reference instead.
Laurent Gomila - SFML developer

Portalboat

  • Newbie
  • *
  • Posts: 9
    • View Profile
Default Code Problem, trying to use a RenderWindow as arg
« Reply #2 on: January 31, 2011, 04:01:18 pm »
That's what I thought....so I would need to use a pointer?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Default Code Problem, trying to use a RenderWindow as arg
« Reply #3 on: January 31, 2011, 04:15:27 pm »
A reference.
Laurent Gomila - SFML developer

Portalboat

  • Newbie
  • *
  • Posts: 9
    • View Profile
Default Code Problem, trying to use a RenderWindow as arg
« Reply #4 on: January 31, 2011, 11:48:41 pm »
Code: [Select]
   sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFMLApp");
sf::RenderWindow &rApp = App;
Player Player;
sf::Sprite spritePlayer = Player.Initialize();
    // Start game loop
    while (App.IsOpened())
    {
Player.Update(rApp);
sf::Event Event;
        while (App.GetEvent(Event))


Okay, so I'm using a reference now. I still get the same error.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Default Code Problem, trying to use a RenderWindow as arg
« Reply #5 on: January 31, 2011, 11:53:04 pm »
That is not what referencing means!


Just use your previous code, and change this:

Code: [Select]
void Update(sf::RenderWindow App)

to this:

Code: [Select]
void Update(sf::RenderWindow &App)

Should work.