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

Author Topic: Window draws a white screen instead of map  (Read 1341 times)

0 Members and 1 Guest are viewing this topic.

osidu2

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Window draws a white screen instead of map
« on: August 06, 2017, 12:53:46 am »
Hello guys,
So I wanted to draw a tile map, making each tile a different object and then store them in vector, so I can go through it, and tile by tile draw them on the screen. The problem is that instead of a tile map, I get a clean white screen. I'm posting a code, could you help me?

That's my tile map class.

Quote
#pragma once

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "tile.h"
#include <vector>

class TileMap
{
public:
   std::string source;
   std::vector <Tile> tileContainer;
   TileMap(std::string mapSource)
   {
      source = mapSource;
   }


   void readMap()
   {
      std::fstream plik;
      std::string stringId;
      sf::Vector2f loadCounter(0, 0);
      plik.open(source, std::ios::in || std::ios::out);
      if (plik.good())
      {
         while (!plik.eof())
         {
            plik >> stringId;
            int id = stoi(stringId);
            if (id == 0)
            {
               Tile tile(id, "tiles.png", sf::Vector2f(32, 32));
               tileContainer.push_back(tile);
            }
            else if (id == 1)
            {
               Tile tile(id, "tiles.png", sf::Vector2f(128, 32));
               tileContainer.push_back(tile);
            }
         }
      }
   }


   void drawMap(sf::RenderWindow &okno)
   {

      sf::Vector2f loadCounter(0, 0);
      if (tileContainer.size() != 0)
      {
         for (int i = 0; i < tileContainer.size(); i++)
         {
            tileContainer.sprite.setPosition(loadCounter.x * 32, loadCounter.y * 32);
            okno.draw(tileContainer.sprite);
            std::cout << tileContainer.id;
            loadCounter.x++;
            if (loadCounter.x == 16)
            {
               loadCounter.x = 0;
               loadCounter.y++;
               if (loadCounter.y == 16)
               {
                  loadCounter.y = 0;
                  loadCounter.x = 0;
               }
            }
         }
      }
   }
};

and that's my tile class
Quote
#pragma once

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <cmath>
#include <string>

class Tile
{
public:
   float id;
   sf::Texture texture;
   sf::FloatRect bounds;
   sf::Sprite sprite;
   bool walkable;
   Tile(float tileId, std::string tileSource,sf::Vector2f graphCords)
   {
      id = tileId;
      texture.loadFromFile(tileSource);
      sprite.setTexture(texture);
      float x = graphCords.x;
      float y = graphCords.y;
      sprite.setTextureRect(sf::IntRect(x,y, 32, 32));
   }
};

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11028
    • View Profile
    • development blog
    • Email
Re: Window draws a white screen instead of map
« Reply #1 on: August 06, 2017, 01:09:07 am »
It's also known as the "White Square Problem" as documented in the tutorial.

When you push_back the created tile into a vector, you're making a copy of it. Thus moving the texture in memory and thus invalidating the sprite's reference to the texture.

The best solution is to not store the texture and sprite together, but use something like Thor's resource holder.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

osidu2

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Window draws a white screen instead of map
« Reply #2 on: August 06, 2017, 01:22:31 am »
Oh Ok, I see. I will check this out for sure and post whether I managed to overcome the problem or not. Im really gratefull for your answer.