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

Author Topic: RenderWindow as parameter  (Read 1475 times)

0 Members and 1 Guest are viewing this topic.

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
RenderWindow as parameter
« on: August 16, 2016, 01:54:44 pm »
Hello, I try to do a Map class to generate map on my window and actually it won't work, but without any errors, and I don't know why. It just splash me a blank screen, but it should be gray with my window.clear(sf::Color(128, 128, 128));
PS: If I remove my map.draw(window); the screen is just like it should be, gray.

Map.hpp
//
//  Map.hpp
//  TutoSFML
//
//  Created by Christopher Yovanovitch on 8/16/16.
//  Copyright © 2016 Christopher Yovanovitch. All rights reserved.
//

#ifndef Map_h
#define Map_h

#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <vector>
#include <sstream>

#include "ResourcePath.hpp"

class Map
{
public:
    void load(std::string mapName);
    void draw(sf::RenderWindow &renderWindow);
private:
    sf::Sprite mTiles;
    std::vector<std::vector<sf::Vector2i>> mMap;
};


#endif /* Map_h */
 

Map.cpp
//
//  Map.cpp
//  TutoSFML
//
//  Created by Christopher Yovanovitch on 8/16/16.
//  Copyright © 2016 Christopher Yovanovitch. All rights reserved.
//

#include "Map.hpp"

void Map::load(std::string mapName)
{
    std::ifstream openfile(resourcePath() + mapName);
   
    sf::Texture tileTexture;
   
    std::vector<std::vector<sf::Vector2i>> map;
    std::vector<sf::Vector2i> tempMap;
   
    if (openfile.is_open() && !openfile.eof())
    {
        std::string str;
        std::getline(openfile, str);
        tileTexture.loadFromFile(resourcePath() + str);
        mTiles.setTexture(tileTexture);
       
        while (!openfile.eof())
        {
            std::getline(openfile, str);
            std::stringstream stream(str);
            std::string value;
            while (std::getline(stream, value, ' '))
            {
                if (value.length() > 0)
                {
                    std::string x = value.substr(0, value.find(','));
                    std::string y = value.substr(value.find(',') + 1);
                   
                    tempMap.push_back(sf::Vector2i(stoi(x), stoi(y)));
                }
            }
           
            map.push_back(tempMap);
            tempMap.clear();
        }
    }
    mMap = map;
}

void Map::draw(sf::RenderWindow &renderWindow)
{
    for (unsigned int i = 0; i < mMap.size(); ++i)
    {
        for (unsigned int j = 0; j < mMap[i].size(); ++j)
        {
            if (mMap[i][j].x != -1 && mMap[i][j].y != -1)
            {
                mTiles.setPosition(j * 32, i * 32);
                mTiles.setTextureRect(sf::IntRect(mMap[i][j].x * 32, mMap[i][j].y * 32, 32, 32));
                renderWindow.draw(mTiles);
            }
        }
    }
}
 

Main.cpp
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
#include <iostream>

#include "Map.hpp"

int main(int, char const**)
{
    sf::RenderWindow window(sf::VideoMode(1664, 1280, 32), "SFML TUTO");
   
    Map map;
    map.load("map1.txt");
   
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
           
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
        }
       
        window.clear(sf::Color(128, 128, 128));
       
        map.draw(window);

        window.display();
    }
   
    return EXIT_SUCCESS;
}
 

TileSet.png (32*32)


map1.txt
tileset.png
0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4
0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,4 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,4 0,0 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4
0,0 0,0 0,0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 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4
0,0 0,0 0,0 0,0 0,4 0,4 0,4 0,4 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,4 0,0 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4
0,0 0,0 0,0 0,0 0,4 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,4 0,0 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4
0,2 0,2 0,2 0,2 0,2 0,2 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,4 0,0 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4 0,4
 
« Last Edit: August 16, 2016, 01:56:55 pm by yovano_c »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: RenderWindow as parameter
« Reply #1 on: August 16, 2016, 02:00:22 pm »
Your texture is destroyed at the end of your load function.
http://www.sfml-dev.org/faq.php#graphics-white-rect

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: RenderWindow as parameter
« Reply #2 on: August 16, 2016, 02:18:45 pm »
Thanks a lot. I pass my texture to the private elements of my class, and is good :)

 

anything