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

Author Topic: Why can't i see my sprite?  (Read 1761 times)

0 Members and 1 Guest are viewing this topic.

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Why can't i see my sprite?
« on: April 16, 2017, 07:10:50 pm »
I'm trying to implement constructors and inheritence in my program. In previous version, everything was running ok, but now i can't draw my sprite (player1.playerS). I dont know where problem or problems are lying.
Here are files of my program:
sfml001.cpp:
#include <SFML/Graphics.hpp>
#include "events.h"
#include "player.h"

using namespace sf;

int main()
{
    VideoMode VMode(1280, 720, 32);
    RenderWindow window( VMode, "Game");

    Player player1;
    window.setFramerateLimit(120);
    while(window.isOpen()){ //zwraca w petli  while informacje, czy okno aplikacji jest otwarte
        EventsInProgram event1(window, player1.plRectSourceSprite);

        window.clear(Color(0, 110, 0));

        window.draw(player1.playerS);
        window.display();
    }
    return 0;
}
 

player.cpp
#include <SFML/Graphics.hpp>
#include "player.h"
#include <string>
using namespace sf;
using namespace std;
Player::Player(VideoMode VM , IntRect plRectSourceSprite){//, string tex){
VMode = VM;
Texture playerT;
playerT.loadFromFile("heroanimation.png");
Sprite playerS(playerT, plRectSourceSprite);
}
 

player.h
#pragma once
#include <SFML/Graphics.hpp>
#include <string>
using namespace sf;
using namespace std;

class Player{
public:

    Sprite playerSprite;
    IntRect playerRectSourceSprite;
    IntRect plRectSourceSprite;
    Texture playerT;

    VideoMode VMode;
    Clock playerMoveClock;
    Clock playerAnimationClock;
    Sprite playerS;
    Vector2f playerSpeedVector;
    Vector2f playerPosition;
    int playerSpeed = 300;
    //int playerLives = 1;
private:

public:
    Player(VideoMode VM = {1280, 720, 32}, IntRect plRectSourceSprite = {0, 0, 60, 60});//, string tex ="heroanimation.png");//, Texture tex );
    ~Player();
};
 

events.cpp
#include <SFML/Graphics.hpp>
#include "events.h"
#include "player.h"
using namespace sf;

EventsInProgram::EventsInProgram(RenderWindow & window, IntRect  playerRectSourceSprite){
    eventList(window, playerRectSourceSprite);

}

 EventsInProgram::~EventsInProgram(){}

void EventsInProgram::eventList(RenderWindow & window,   IntRect  playerRectSourceSprite){//, Event & zdarzenie){
       Event zdarzenie;
       bool pause01 = 0;
     while( window.pollEvent( zdarzenie ) ){
            if( zdarzenie.type == Event::Closed ){
                window.close();
            }
            if(Keyboard::isKeyPressed(Keyboard::Escape)){
                if(pause01 = 0) pause01 = 1;
                else if(pause01 = 1) pause01 = 0;
            }
            if(zdarzenie.type == Event::KeyReleased) playerRectSourceSprite.top = 0;
        }
}
 

events.h
#pragma once
#include <SFML/Graphics.hpp>
#include "player.h"
using namespace sf;

class EventsInProgram {
public:
    EventsInProgram(RenderWindow & window, IntRect  playerRectSourceSprite);
     ~EventsInProgram();

    void eventList(RenderWindow & window, IntRect  playerRectSourceSprite);

};
 
« Last Edit: April 17, 2017, 11:10:20 am by MDK »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10883
    • View Profile
    • development blog
    • Email
Re: Why can't i see my sprite?
« Reply #1 on: April 16, 2017, 08:09:01 pm »
If you have no idea what is going on, then that sounds to me like you didn't spend any time actually trying to figure out what is.

Have you used your debugger and set break points at critical positions to figure out what the output is?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Why can't i see my sprite?
« Reply #2 on: April 16, 2017, 08:26:54 pm »
I know that this can be very easy to solve. But i'm looking in my code 3rd day today and can't see what's wrong. When i run program everything is running, and i think that window.draw(player1.playerS) should see playerS inside Player class. But after moving, adding and removing code for few hours a day i don't have more ideas. It's hard to say, but i got stuck.

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Why can't i see my sprite?
« Reply #3 on: April 16, 2017, 10:47:35 pm »
One problem I see is that you are creating a local Texture playerT in the Player constructor, which hides the member with the same name.

I recommend you learn how to use a debugger and activate all the warnings in your compiler.

Hapax

  • Hero Member
  • *****
  • Posts: 3364
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Why can't i see my sprite?
« Reply #4 on: April 17, 2017, 01:37:00 am »
Code tags are [code=cpp] and [/code]. Note that the closing tag is [/code], not [/cpp]
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*