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

Author Topic: White rectangle issue  (Read 1418 times)

0 Members and 2 Guests are viewing this topic.

FellowCoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
White rectangle issue
« on: December 27, 2019, 03:16:03 am »
Can someone explain me why is my code is outputitng an white rectangle instead of the properly texture?

Sorry for my bad english btw :-[

main.cpp:

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

int main(){
        sf::RenderWindow janela(sf::VideoMode(512, 512), "titulo", sf::Style::Default);
        sf::Texture texture, tex1, tex2, tex3, tex4;

        texture.loadFromFile("Idle (1).png");
        tex1.loadFromFile("Idle (2).png");
        tex2.loadFromFile("Idle (3).png");
        tex3.loadFromFile("Idle (4).png");
        tex4.loadFromFile("Idle (5).png");

        std::vector<sf::Texture> texturas;
        texturas.push_back(texture);texturas.push_back(tex1);texturas.push_back(tex2);texturas.push_back(tex3);texturas.push_back(tex4);




        Player jogador(texturas);
        //jogador.corpo.setTexture(texture);

        sf::Clock clock;
        sf::Time tempo;


        while(janela.isOpen()){
                sf::Event e;
                while(janela.pollEvent(e)){
                        if(e.type == sf::Event::Closed){
                                janela.close();}


                }


                tempo = clock.getElapsedTime();
                if(tempo.asSeconds() <=20){
                        std::cout<<tempo.asSeconds()<<std::endl;
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                        jogador.corpo.move(0.1f,0);
                        std::cout<<jogador.corpo.getPosition().x;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                        jogador.corpo.move(-0.1f,0);
                        std::cout<<jogador.corpo.getPosition().x;
                }
                if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
                        jogador.corpo.setPosition(sf::Mouse::getPosition(janela).x,sf::Mouse::getPosition(janela).y);
                }
                janela.clear(sf::Color(150, 150, 150));
                janela.draw(jogador.corpo);
                janela.display();

        }




        return 0;
}

Player.h

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

class Player{
  public:
    Player(std::vector<sf::Texture> texturas);
    ~Player();
    sf::Sprite corpo;


    void Anim();


  private:



};


#endif /* end of include guard:  */

Player.cpp:

#include "Player.h"

Player::Player(std::vector<sf::Texture> texturas)
{
  corpo.setTexture(texturas[0]);
  corpo.setPosition(20.f, 20.f);
  corpo.setOrigin(250,250);
  corpo.setScale(0.1f,0.1f);
}

Player::~Player()
{

}

void Player::Anim(){

}
 


Also i tried using pointers in the textures but i dont think i used it correctly  :-\



Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: White rectangle issue
« Reply #1 on: December 27, 2019, 05:58:20 am »
hi

it looks you passing textures container by value to player class constructor, this will make a new copy of passing objects. use reference or pointer to fix it like `Player(const std::vector<sf::Texture>& texturas)`

btw, since all textures have same file names except of its numbers, you can use `std::to_string()` with a for-loop.

std::vector<sf::Texture> texturas(5);
for (int i = 0; i < texturas.size(); i++) {
        texturas[i].loadFromFile("Idle (" + std::to_string(i+1) + ").png");
}
« Last Edit: December 27, 2019, 06:54:40 am by Mortal »

 

anything