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

Author Topic: Program crashing when window is re-opend from minimized state? [SOLVED]  (Read 2518 times)

0 Members and 1 Guest are viewing this topic.

TheGuerilla

  • Newbie
  • *
  • Posts: 27
  • Cynical Prick.exe
    • View Profile
    • Email
Title states it pretty well. Basically, I have a bit of code that automatically applies a given shader to the whole screen (Assuming the shader pointer is not null), and when I re open the windows it crashes. It would be easier to explain this with a gif. (Sorry, can't embed imgur gifs because they technically aren't gifs)

Anyway, here is the exact code where it crashes. (I know it says "Code: C" but this is actually C++)
if (vScreenShader != nullptr && !vSkipFrameShader && vGameWindow->hasFocus()) {
        // Check that the screen texture is the same size as the screen
        if (vScreen.tex.getSize().x != vWindowWidth || vScreen.tex.getSize().y != vWindowHeight) {
                vScreen.tex.create(vWindowWidth, vWindowHeight);
                vScreen.spr.setTexture(vScreen.tex);
        }

        vScreen.tex.update(*vGameWindow);
        vScreen.spr.setTexture(vScreen.tex);

        // Account for current shader and view
        sf::View currentView = vGameWindow->getView();
        sf::Shader* currentShader = (sf::Shader*)vRenderState.shader;

        vRenderState.shader = vScreenShader;
        vGameWindow->setView(vGameWindow->getDefaultView());
        vGameWindow->draw(vScreen.spr, vRenderState);

        vRenderState.shader = currentShader;
        vGameWindow->setView(currentView);
        }
It crashes at line 8 when I try to update the texture with the window. The window used to crash when I minimized it, but when I added the "&& vGameWindow->hasFocus()" to line 1, it fixed that problem. I don't know what I'm doing wrong, I've tried looking for another function to stop this block of code like "if window is opening" or "is minimized," but nothing. I don't know if this is important, but this code works when the window is open. (The gif shows off a sick scanline shader) How do I stop it from updating when the window is regaining focus?

thanks in advance
« Last Edit: June 26, 2016, 09:03:13 am by Laurent »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Program crashing when window is re-opend from minimized state?
« Reply #1 on: June 25, 2016, 09:13:47 pm »
You should just use a render texture rather than copying the window contents (since it's significantly slower).

According to the video, this is most likely caused by your graphics card driver doing something wrong.

Either way, I'd recommend doing the following:

If you want post processing effects:

  • Render your game scene to a sf::RenderTexture.
  • Render the render texture to your window using a sprite (similar to what you do right now).
If you don't want the effects, just render to your window directly.

TheGuerilla

  • Newbie
  • *
  • Posts: 27
  • Cynical Prick.exe
    • View Profile
    • Email
Re: Program crashing when window is re-opend from minimized state?
« Reply #2 on: June 26, 2016, 12:17:19 am »
Okay, so I gave your method a try, and while digging through the documentation noticed a few other inefficient things I was doing; but long story short, it works. I will eventually test the speed difference, but it's certainly a lot easier to work with than the last method.

thanks!