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

Author Topic: Can not draw a "textured" sprite in another class  (Read 1345 times)

0 Members and 1 Guest are viewing this topic.

I_Red_I

  • Newbie
  • *
  • Posts: 11
    • View Profile
Can not draw a "textured" sprite in another class
« on: February 20, 2018, 01:42:09 pm »
Good evening,

I create a "tile mapped" game.

I have a Block class that allows me to create object blocks and a world class that allows me to manage my world (composed of object block).

Let me explain, in the constructor of World I fill a two-dimensional list of object Block. When they are created, Block's constructor assigns to the object a sprite to which it applies a texture. Then using a World method that I call in the Main, I draw Block objects contained in World (in the two-dimensional list) .. only I get the famous white square.

So the Sprite is there, it's the texture that no longer exists. I did some research and concluded that the texture was removed at the exit of the class in which it was created (which is why it works when I draw from the Block class). I also understood that I had to use dynamic allocations to solve this problem ... but I can not do it .. really .. it's been 3 weeks that I'm on it .. Can someone help me ?

Cordially.

Here is my code because I doubt that my explanation is sufficiently clear:


Block.hpp :
#ifndef Block_hpp
#define Block_hpp
 
#include <cstdio>
#include <string>
#include <SFML/Graphics.hpp>
 
class Block : public sf::Drawable
{
     
public:
    Block();
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        target.draw(block_sprite, states);
    }
     
protected:
    sf::Sprite block_sprite;
    sf::Texture block_wood;
};
 
#endif
 

Block.cpp
#include "Block.hpp"
#include <cstdio>
#include <iostream>
#include <SFML/Graphics.hpp>
 
Block::Block()
{
    if (!block_wood.loadFromFile("textures/planks_spruce.png"))
    {
        std::cout << "ok" << std::endl;
    }
 
    block_sprite.setTexture(block_wood);
     
    block_sprite.setPosition(0, 0);
     
     
}
 

World.hpp :
#ifndef World_hpp
#define World_hpp
 
#include <cstdio>
#include <SFML/Graphics.hpp>
#include <string>
#include "Block.hpp"
 
class World
{
public:
     
    World();
    void render(sf::RenderWindow& window) const;
     
protected:
    Block map[20][20];
};
#endif
 

World.cpp
#include "World.hpp"
#include "Block.hpp"
#include <SFML/Graphics.hpp>
#include <string>
#include <cstdio>
 
World::World()
{
    Block block;
    map[0][0] = block;
}
 
void World::render(sf::RenderWindow& window) const
{
    window.draw(map[0][0]);
}
 

main.cpp
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdio>
#include "World.hpp"
 
int main(int, char const**)
{
    const int w = 800;
    const int h = 450;
 
    sf::RenderWindow window;
    sf::View view;
    World world;
    sf::Event event;
     
    window.create(sf::VideoMode(w, h), "SFML window",sf::Style::Default);
    window.setVerticalSyncEnabled(true);
     
    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
 
        window.clear();
         
        view.reset(sf::FloatRect(0,0,w,h));
        world.render(window);
         
        window.display();
    }
    return EXIT_SUCCESS;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Can not draw a "textured" sprite in another class
« Reply #1 on: February 20, 2018, 01:54:32 pm »
No need to duplicate your question when you already have replies on the other forum.

https://fr.sfml-dev.org/forums/index.php?topic=23732.0
Laurent Gomila - SFML developer

I_Red_I

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can not draw a "textured" sprite in another class
« Reply #2 on: February 20, 2018, 02:51:27 pm »
I thought increase my chances of solving my problem :P
Sorry I didn't know I could not do that, I thought it was two different "communities".
I will delete it ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Can not draw a "textured" sprite in another class
« Reply #3 on: February 20, 2018, 03:24:53 pm »
It is not strictly disallowed, and indeed the people on english forum is unlikely to go to the french forum, but since your original topic is actively replied, especially by a member of the SFML team, you could at least wait  a little bit before duplicating your question.

A much better solution is to directly post on the english forum. The french forum is there mostly for historical reasons and for those who can't speak english; but you won't find many "experts" there.
Laurent Gomila - SFML developer

I_Red_I

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can not draw a "textured" sprite in another class
« Reply #4 on: February 20, 2018, 04:49:00 pm »
Yes sorry, I should have waited before doing it. Yes I didn't think about it before post in the french forum, that's what I would do next time .. the English community is much denser by force of circumstances..
In the meantime, do I have to delete my post?

 

anything