Hi, I'm making a simple game and I'm trying to give it a sort of trail/fading effect by clearing the main render texture with a low alpha value.
The effect works as intended except one thing, when moving, the player and objects will leave a permanent trail of themselves too. I suppose that it's because I don't clear the window so the render texture will blend with the previous non-cleared window's texture (maybe ?) and I haven't found a solution after searching for days about custom blending mode, tried making shaders, using another render texture under the main one etc.
To illustrate my problem better, here is a gif :
https://imgur.com/dc2R47XAnd here are some parts of my code that I think are the most important :
Rendering :
sf::RenderWindow *window = adata->window;
float delta = adata->app_clock.getElapsedTime().asSeconds();
sf::Vector2f view_position = adata->game_view.getCenter();
sf::Vector2f camera_lag;
adata->rtex_main->clear(sf::Color(0, 0, 0, 5));
adata->rtex_hud->clear(sf::Color::Black);
camera_lag.x = LERP(view_position.x, adata->player->position.x, CAMERA_LAGGING_DISTANCE * delta);
camera_lag.y = LERP(view_position.y, adata->player->position.y, CAMERA_LAGGING_DISTANCE * delta);
adata->game_view.setCenter(camera_lag);
adata->rtex_main->setView(adata->game_view);
render_gameobjects(adata);
render_player(adata);
adata->rtex_hud->setView(adata->hud_view);
render_texts(adata);
adata->rtex_main->display();
adata->rtex_hud->display();
sf::Texture main_texture = adata->rtex_main->getTexture();
sf::Texture hud_texture = adata->rtex_hud->getTexture();
sf::Shader *overlay_shader = get_shader(adata, "overlay");
overlay_shader->setUniform("game_texture", main_texture);
overlay_shader->setUniform("hud_texture", hud_texture);
adata->sprite_main.setTexture(main_texture);
adata->sprite_hud.setTexture(hud_texture);
window->draw(adata->sprite_main, adata->state_main);
window->display();
Initialization of render textures :
sf::RenderWindow window(sf::VideoMode(WIN_W, WIN_H), "Application");
load_assets(adata);
adata->window = &window;
adata->rtex_main = new sf::RenderTexture();
if (!adata->rtex_main->create(WIN_W, WIN_H)) {
adata->exit_code = EXIT_FAILURE;
return;
}
adata->rtex_hud = new sf::RenderTexture();
if (!adata->rtex_hud->create(WIN_W, WIN_H)) {
adata->exit_code = EXIT_FAILURE;
return;
}
sf::RenderStates state_main;
state_main.blendMode = sf::BlendAlpha;
state_main.shader = get_shader(adata, "overlay");
state_main.texture = NULL;
state_main.transform = sf::Transform::Identity;
adata->state_main = state_main;
sf::RenderStates state_hud;
state_hud.blendMode = sf::BlendNone;
state_hud.shader = NULL;
state_hud.texture = NULL;
state_hud.transform = sf::Transform::Identity;
adata->state_hud = state_hud;
init_player(adata);
load_interface(adata);
window_loop(adata);
Window loop :
sf::RenderWindow *window = adata->window;
while (window->isOpen()) {
sf::Event event;
while (window->pollEvent(event)) {
register_event(adata, event);
}
float elapsed_seconds = adata->app_clock.getElapsedTime().asSeconds();
if (elapsed_seconds > GLOBAL_RATE) {
update_app(adata);
render_app(adata);
adata->app_clock.restart();
}
}
(btw the shader just make it so the overlay and the main texture are added together, removing it doesn't change anything, removing the hud render texture doesn't change anything either).
The code's a bit messy, I hope you can read it without too much hassle.