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

Author Topic: [FIXED] Create a block (using sf::RenderTexture) of tiles  (Read 947 times)

0 Members and 1 Guest are viewing this topic.

MarcusM

  • Newbie
  • *
  • Posts: 14
    • View Profile
[FIXED] Create a block (using sf::RenderTexture) of tiles
« on: December 12, 2013, 08:45:42 am »
FIXED: Did some modifcations, here's the final result:
// TileBlock.cpp

#include "TileBlock.h"

#include <iostream>

FlxTileBlock::FlxTileBlock(int x, int y, unsigned int w, unsigned int h)
{
    m_texture.create(w, h);
    m_texture.clear(sf::Color(0, 0, 255));

    m_sprite.setPosition(x, y);

    active = false;
    immovable = true;

    width = w;
    height = h;
}
void FlxTileBlock::draw(sf::RenderTexture &rendertexture)
{
    rendertexture.draw(m_sprite);
}
void FlxTileBlock::loadTiles(sf::Image& tilesheet, unsigned int tilewidth, unsigned int tileheight)
{
    unsigned int total = tilesheet.getSize().x / tilewidth;

    unsigned int row = 0;
    unsigned int column;
    unsigned int destinationX;
    unsigned int destinationY = 0;
    unsigned int widthInTiles = width/tilewidth;
    unsigned int heightInTiles = height/tileheight;

    while (row < heightInTiles)
    {
        destinationX = 0;
        column = 0;
       
        while (column < widthInTiles)
        {
            int randomFrame = rand()%total;
           
            sf::Texture tempTexture;
            tempTexture.loadFromImage(tilesheet, sf::IntRect(randomFrame*tilewidth, 0, tilewidth, tileheight));

            sf::Sprite tempSprite(tempTexture);
            tempSprite.setPosition(destinationX, destinationY);

            m_texture.draw(tempSprite);

            destinationX += tilewidth;
            column++;
        }
        destinationY += tileheight;
        row++;
    }

    // update sprite
    m_texture.display();
    m_sprite.setTexture(m_texture.getTexture());
}



Hi there.

I am currently trying to create a block of tiles, when given the position, width, height and a tilesheet.

I am using a sf::RenderTexture so that I am able to create temporary textures and sprites and then draw them ontop of my RenderTexture. The RenderTexture is then used to create a sprite for my block.

However, when I draw it on top of my map, it's just black (the clearColor) as if nothing had been drawn on top of it. Is it because that sf::RenderTexture must keep a reference to the textures that were used on top of it?

Here's my current code:
// TileBlock.cpp

#include "TileBlock.h"

#include <iostream>

FlxTileBlock::FlxTileBlock(int x, int y, unsigned int w, unsigned int h)
{
    m_texture.create(w, h);
    m_texture.clear(sf::Color(0, 0, 0));

    m_sprite.setPosition(x, y);

    active = false;
    immovable = true;

    width = w;
    height = h;
}
void FlxTileBlock::draw(sf::RenderTexture &rendertexture)
{
    rendertexture.draw(m_sprite);
}
void FlxTileBlock::loadTiles(sf::Texture& tilesheet, unsigned int tilewidth, unsigned int tileheight)
{
    unsigned int total = tilesheet.getSize().x / tilewidth;

    // create temporary image from sf::Texture
    sf::Image image = tilesheet.copyToImage();

    unsigned int row = 0;
    unsigned int column;
    unsigned int destinationX;
    unsigned int destinationY;
    unsigned int widthInTiles = width/tilewidth;
    unsigned int heightInTiles = height/tileheight;

    while (row < heightInTiles)
    {
        destinationX = 0;
        column = 0;
       
        while (column < widthInTiles)
        {
            int randomFrame = rand()%total;
            randomFrame = 2;
           
            sf::Texture tempTexture;
            tempTexture.loadFromImage(image, sf::IntRect(randomFrame*tilewidth, 0, tilewidth, tileheight));

            sf::Sprite tempSprite(tempTexture);
            tempSprite.setPosition(destinationX, destinationY);

            m_texture.draw(tempSprite);

            std::cout << "drew something" << std::endl;

            destinationX += tilewidth;
            column++;
        }
        destinationY += tileheight;
        row++;
    }

    // update sprite
    m_texture.display();
    m_sprite.setTexture(m_texture.getTexture());
}

 

// TileBlock.h

#ifndef _TILEBLOCK_H_
#define _TILEBLOCK_H_

#include "stdafx.h"
#include "base.h"
#include "sys.h"

#include "Entities.h"

class FlxTileBlock
{
private:
    sf::RenderTexture m_texture;
    sf::Sprite m_sprite;

public:
    FlxTileBlock(int x, int y, unsigned int w, unsigned int h);

    int width;
    int height;

    bool active;
    bool immovable;

    void draw(sf::RenderWindow &window);
    void draw(sf::RenderTexture &rendertexture);

    void loadTiles(sf::Texture& tilesheet, unsigned int tilewidth=0, unsigned int tileheight=0);

};

#endif
 
« Last Edit: December 12, 2013, 02:05:22 pm by MarcusM »