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

Author Topic: Problems with class files with SFML  (Read 5993 times)

0 Members and 2 Guests are viewing this topic.

beastlyhexquest

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Problems with class files with SFML
« on: August 24, 2014, 12:24:35 pm »
I'm making a small SFML game called skelton town. (not skeleton (its a inside reference)) I have made a file called Player which i want to allow me to make the player, draw him and allow me to input the keyboard inputs for his movement. but when i include the header to allow me use the file in main nothing is drawn. But i get no actual errors. Can you guys help?

Main

#include<SFML/Graphics.hpp>
#include<iostream>
#include<SFML/Window/Keyboard.hpp>
#include "Player.h"
#include <cstdlib>
int main()
{
    sf::RenderWindow Window;
    Window.create(sf::VideoMode(1920, 1080), "Skelton Town", sf::Style::Fullscreen);
Player player;
     while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            switch(Event.type)
            {
        case sf::Event::Closed:
         Window.close();
                 break;
            }

        }

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
    return 0;
   }
        Window.clear();
        Window.draw(player.getSprite());  // Key line here
        Window.display();
    }
}

Player.cpp

#include "Player.h"
#include<iostream>
#include<SFML/Graphics.hpp>
Player::Player()
{


 sf::Texture pTexture;
 sf::Sprite playerImage;
if(!pTexture.loadFromFile("Player.png",sf::IntRect(30,0,64,32)))
    std::cout<<"Error could not load player image"<<std::endl;
playerImage.setTexture(pTexture);

    }

 

Player.h

#ifndef PLAYER_H
#define PLAYER_H
#include<SFML/Graphics.hpp>

class Player
{
    public:
        Player();

        sf::Sprite& getSprite() {return playerImage;}

    private:
        sf::Texture pTexture;
        sf::Sprite playerImage;

};

#endif // PLAYER_H
 

Can Someone Help Me. And Tell me what i have done wrong.

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Problems with class files with SFML
« Reply #1 on: August 24, 2014, 12:38:34 pm »
Probably this is the cause

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problems with class files with SFML
« Reply #2 on: August 24, 2014, 12:50:37 pm »
I don't see any duplicate variable names in his code, what are you referring to?

More importantly, his sf::Texture is obviously getting destroyed in the constructor, so this is probably the white square problem again.

The tutorials explain this: http://sfml-dev.org/tutorials/2.1/graphics-sprite.php#the-white-square-problem

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Problems with class files with SFML
« Reply #3 on: August 24, 2014, 12:54:50 pm »
his sf::Texture is obviously getting destroyed in the constructor, so this is probably the white square problem again.
He's declaring another pTexture and a different playerImage in the constructor while Player::pTexture and Player::playerImage are left untouched.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problems with class files with SFML
« Reply #4 on: August 24, 2014, 01:19:44 pm »
his sf::Texture is obviously getting destroyed in the constructor, so this is probably the white square problem again.
He's declaring another pTexture and a different playerImage in the constructor while Player::pTexture and Player::playerImage are left untouched.

Ah, you're right.  I don't think I've seen that kind of shadowing before.

@beastlyhexquest: You need to use "this" to refer to members of the current object.  Redeclaring them with the same name just makes local variables that hide the members.

 

anything