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

Author Topic: Sprite becomes white after renderWindow.create()?  (Read 2163 times)

0 Members and 1 Guest are viewing this topic.

Septagon

  • Newbie
  • *
  • Posts: 7
    • View Profile
Sprite becomes white after renderWindow.create()?
« on: August 14, 2012, 11:54:32 pm »
I am attempting to add hotkeys for changing window resolution as well as toggling full screen.

I am using something similar to the working solution suggested in this thread:
http://en.sfml-dev.org/forums/index.php?topic=2580.msg16913#msg16913

Here is the setup function:
void setUpWindow(int newWindowWidth, int newWindowHeight, int newZoom, bool fullScreen) {
    sf::VideoMode videoMode(newZoom * newWindowWidth, newZoom * newWindowHeight, 16);
    renderWindow.Close();
    if (fullScreen) {
        renderWindow.Create(videoMode, "Dungeon Crawler 2012/08/14", sf::Style::Fullscreen);
    }
    else {
        renderWindow.Create(videoMode, "Dungeon Crawler 2012/08/14");
    }

    // The game is all drawn to a single sf::Image which is then rendered to a single sf::Sprite
    // Set up for this follows
    sf::Image newImage(windowWidth, windowHeight, sf::Color(0, 0, 0));
    screenImage = newImage;
    screenImage.SetSmooth(false);
    // set up an indexed image too (note that this isn't scaled up with the zoom, it's at native resolution)
    IndexedImage newIndexedScreen(windowWidth, windowHeight);
    indexedScreen = newIndexedScreen;

    sf::Sprite newSprite;
    screenSprite = newSprite;
    screenSprite.SetImage(screenImage);
    screenSprite.SetX(0.0f);
    screenSprite.SetY(0.0f);
    screenSprite.Scale(newZoom, newZoom);
}
 

This function works the first time it is called on program launch. However, there is a problem if I press one of my new hotkeys to change resolution. The function is called and the resolution is switched correctly. The sprite ("screenSprite" above) is visible and scaled correctly, but it becomes white. I wonder if there is something wrong with the way I am initializing things upon switching resolution.

I am using Windows 7, C++, and SFML 1.6.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Sprite becomes white after renderWindow.create()?
« Reply #1 on: August 15, 2012, 01:10:25 am »
Upfront my standard suggestion: I advice you to switch to SFML 2. ;)

As for you're problem, if I read this correctly your screenImage and screenSprite are global variables is that correct? I strongly advice you against such code designs and suggest you use a class and wrap things up in there.

So I'm not sure if this is the problem but it could maybe be, shouldn't it be on the creation of the newImage also newWindowWidth and newWindowHeight?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Septagon

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Sprite becomes white after renderWindow.create()?
« Reply #2 on: August 15, 2012, 06:55:18 am »
I read in another thread that 1.6 is more stable than 2.0, but I'd be willing to switch if that's no longer the case.

windowWidth and windowHeight are globals and their values are the same, but I might as well change that so as to make the example more self contained. Here:
void setUpWindow(int newWindowWidth, int newWindowHeight, int newZoom, bool fullScreen) {
    sf::VideoMode videoMode(newZoom * newWindowWidth, newZoom * newWindowHeight, 16);
    renderWindow.Close();
    if (fullScreen) {
        renderWindow.Create(videoMode, "Dungeon Crawler 2012/04/03", sf::Style::Fullscreen);
    }
    else {
        renderWindow.Create(videoMode, "Dungeon Crawler 2012/04/16");
    }

    sf::Image newImage(newWindowWidth, newWindowHeight, sf::Color(0, 0, 0));
    screenImage = newImage;
    screenImage.SetSmooth(false);
    // set up an indexed image too (note that this isn't scaled up with the zoom, it's at native resolution)
    IndexedImage newIndexedScreen(newWindowWidth, newWindowHeight);
    indexedScreen = newIndexedScreen;

    // Set up an sf::Sprite for displaying things on the screen. This will be our draw canvas.
    sf::Sprite newSprite;
    screenSprite = newSprite;
    screenSprite.SetImage(screenImage);
    screenSprite.SetX(0.0f);
    screenSprite.SetY(0.0f);

    screenSprite.Scale(newZoom, newZoom);
}

 

anything