I'm trying to follow
an old Flash/AS3 tutorial using SFML instead. The objective is to render tiled maps in a series of layers, based on a Tiled editor map. The end result is supposed to looks like this:
The back-most layer will fill the entire area with grass and dirt. Next, the Foreground layer will be drawn, which includes obstructions like the tree stump and bushes. Because most of the layer is empty, it should be transparent. Next, the player is drawn, then the tree's leaves. If the player moves behind the tree, the tree will obscure them. Standard stuff.
My thinking was to draw each layer (except the player) to a RenderTexture, convert the Texture to a Sprite and then draw the Sprite to the Window. Here's the code:
// Preparing the map
for(int i = 0; i < game->tiles.size(); i++) {
sf::RenderTexture rtex;
if(!rtex.create(game->mapWidth * game->tileWidth,
game->mapHeight * game->tileHeight)) {
std::cout << "Error creating map render texture.";
return;
}
// The bottom layer is opaque by default. The rest are transparent.
if(i == 0) rtex.clear();
else rtex.clear(sf::Color(0, 0, 0, 0));
[...identify the tiles and draw them to rtex...]
// Finalize.
rtex.display();
// Store the texture and convert it to a sprite that will be used during draw.
mapLayerTextures.push_back(rtex.getTexture());
mapLayers.push_back(new sf::Sprite(mapLayerTextures.back()));
}
[...]
// Drawing the layers.
for(int i = 0; i<mapLayers.size(); i++) {
game->window.draw(*mapLayers[i]);
}
Unfortunately, the result is this:
Obviously I screwed up the transparency somehow, so the topmost layer (layer 3, the leaves) became opaque white. Checking the other layers, the background is just fine, and the second layer shows similar problems. I think the brown rectangle below the tree, and the fact that layer 2's tree trunk showed up even though everything else is from layer 3, are just side effects of the above problem, but I guess we'll see.
This thread seems to suggests the problem might be Blend Modes (hard to say if we're in the same situation when the images are broken), but I haven't had any luck with BlendNone, as suggested.
Any other suggestions?