Skip to main content
added main function
Source Link
anonymous
  • 524
  • 1
  • 8
  • 22

Edit: The Main Function :-

int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Untitled game"); window.setVerticalSyncEnabled(true); Player player; World world; //variables while(window.isOpen()) { //event handling //updating //drawing window.clear(sf::Color::Black); world.draw(&window); window.display(); } return 0; } 

Edit: The Main Function :-

int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Untitled game"); window.setVerticalSyncEnabled(true); Player player; World world; //variables while(window.isOpen()) { //event handling //updating //drawing window.clear(sf::Color::Black); world.draw(&window); window.display(); } return 0; } 
Source Link
anonymous
  • 524
  • 1
  • 8
  • 22

How to properly pass sprites to a std::vector without destroying the sprite's texture in SFML?

From the official SFML tutorials, The White Box Problem:-

"When you set the texture of a sprite, all it does internally is store a pointer to the texture instance. Therefore, if the texture is destroyed or moves elsewhere in memory, the sprite ends up with an invalid texture pointer." Thus you will see a sprite without a texture.

I have a class called World. In this class I made a 2d integer array called level and a vector of type Block called blocks. Now I wanted to store the 'Block' objects inside the vector whenever level[ i ][ j ] = 1.

header file of the 'World' class:-

#ifndef WORLD_H #define WORLD_H #include <vector> #include "Block.h" #include "Grass.h" #include <SFML/Graphics.hpp> using namespace std; class World { public: World(); void draw(sf::RenderWindow *window); vector<Block> blocks; private: int level[12][16]; int wd; int hi; }; #endif // WORLD_H 

cpp file of the 'World' class :-

#include "World.h" #include "Grass.h" #include "Block.h" #include <iostream> #include <SFML/Graphics.hpp> using namespace std; World::World() : level{ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1}, {1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} } { wd = 16; hi = 12; int count = 1; //make a 'Block' object and pass it in the vector when level[ i ][ j ] = 1. for(int i = 0; i<hi; i++) { for(int j = 0; j<wd; j++) { if(level[i][j] == 1) { Block block(j*50, i*50); blocks.push_back(block); } } } } void World::draw(sf::RenderWindow *window) { for(unsigned int i = 0; i<blocks.size(); i++) { blocks[i].draw(window); } } 

The 'Block' class has two members - sf::Texture blockT and sf::Sprite block. It also has a draw(RenderWindow *window) method. This is how the 'Block' class is made :-

header file for block class

#ifndef BLOCK_H #define BLOCK_H #include <iostream> #include <SFML/Graphics.hpp> using namespace std; class Block { public: Block(float x, float y); void draw(sf::RenderWindow *window); private: sf::Texture blockT; sf::Sprite block; }; #endif // BLOCK_H 

cpp file for 'Block' class

#include "Block.h" #include <iostream> #include <SFML/Graphics.hpp> using namespace std; Block::Block(float px, float py) { if(!(blockT.loadFromFile("textures/block.png"))) { cout<<"Could not load block texture."<<endl; } block.setTexture(blockT); block.setPosition(sf::Vector2f(px, py)); cout<<px<<endl; cout<<py<<endl; } void Block::draw(sf::RenderWindow *window) { window->draw(block); } 

When I run the program, in place of blocks, only white box is shown. I don't understand how the texture is getting destroyed. This is what the output looks like :-

messed up textures

As you can see, the white places are sprites each of size 50*50 without any texture.