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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Blackdrazon

Pages: [1]
1
Graphics / Re: Transparent RenderTexture
« on: July 30, 2018, 09:41:11 am »
First, your code is very inefficient (why do you copy textures? why do you allocate sprites dynamically? ...). What's the benefit of rendering to layers, compared to drawing everything directly to the window? If it's done in the right order, it should give the same results -- with less headache ;)

The answer to most of these questions is "because I'm bad at this language / programming in general" but I had inadequate reasons. It doesn't really matter, though, since you're right and there's no need to use the layer system at all.  Drawing to layers was the tutorial's idea, I just didn't question it.

I suppose that's what I'll do, nothing more to say, really.

In your loop you also use temporary RT which is very inefficient.

I'll keep that in mind too in case something similar comes up, thanks!

2
Graphics / Transparent RenderTexture
« on: July 30, 2018, 09:16:24 am »
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?

Pages: [1]
anything