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

Author Topic: C++ & SFML class vector - showing the wrong texture  (Read 1962 times)

0 Members and 1 Guest are viewing this topic.

Nero

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
C++ & SFML class vector - showing the wrong texture
« on: May 09, 2018, 04:24:53 pm »
So, my problem is this : I have a class called thing. The class basically creates a rectangle with a texture. When I'm drawing the rectangle on the window without using a vector, all works fine, but when I do, it takes the wrong texture (another texture from the same folder) even tho in the class it says nothing about that texture.

Here is the code :

Main.cpp
#include <SFML/Graphics.hpp>    
#include <iostream>
#include "thing.h"
#include <vector>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 720), "whatever", sf::Style::Close);

    std::vector<thing> things;
    things.emplace_back(thing());

sf::Texture textureanother1;
textureanother1.loadFromFile("textures/another1.png");

sf::Texture textureanother2;
textureanother2.loadFromFile("textures/another2.png");

sf::RectangleShape another1(sf::Vector2f(132.0f, 94.0f));
another1.setTexture(&textureanother1);
another1.setOrigin(66.0f, 94.0f);
another1.setPosition(500.0f, 680.0f);

sf::RectangleShape another2(sf::Vector2f(126.0f, 55.0f));
another2.setTexture(&textureanother2);
another2.setOrigin(63.0f, 55.0f);
another2.setPosition(500.0f, 605.0f);

    while (window.isOpen())
    {
        sf::Event evnt;
        while (window.pollEvent(evnt))
        {
            if (evnt.type == sf::Event::Closed)
            {
                window.close();
            }
            else
            {
                break;
            }
        }

        window.draw(another1);
        window.draw(things.at(0).thingItem);
        window.draw(another2);
        window.display();
    }
    return 0;
}

Thing.h

#pragma once

#include <SFML/Graphics.hpp>

class thing
{
public:
    thing();
    ~thing();

    sf::Texture thingTexture;
    sf::RectangleShape thingItem;
};

Thing.cpp

#include "thing.h"

thing::thing()
{
    thingTexture.loadFromFile("textures/sometexture.png");
    thingItem.setTexture(&thingTexture);
    thingItem.setSize(sf::Vector2f(16.25f, 99.25f));
    thingItem.setOrigin(8.125f, 99.25f);
    thingItem.setPosition(500.0f, 99.25f);

}

thing::~thing() = default;

Again, ALL other rectangles except the one created with the class and a vector work fine. The one created with a class and a vector works fine ONLY when it's used without a vector. What is wrong? There are no errors or warnings.

Instead of that : (which is showed when I use the class thing without a vector)



It shows that : (I used a vector, like in the code)



PS: I changed a bit names and deleted commens so you won't be confused by the strange words.
« Last Edit: May 09, 2018, 04:30:14 pm by Nero »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: C++ & SFML class vector - showing the wrong texture
« Reply #1 on: May 09, 2018, 08:25:29 pm »
So sometexture is the correct image?

things.emplace_back(thing());
This is calling the copy constructor, instead just do emplace_back() without arguments.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/