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

Author Topic: GPU usage jumping to 100% after changing resolution  (Read 956 times)

0 Members and 1 Guest are viewing this topic.

PGco

  • Newbie
  • *
  • Posts: 2
    • View Profile
GPU usage jumping to 100% after changing resolution
« on: January 17, 2024, 11:20:25 pm »
My game is just a black background with white text, but, when the user changes resolution the GPU jumps to 100% and stays there. This issue happens on Windows, Linux, and Wine on Linux. It happens every time. I don't understand if I'm doing something wrong, or if SFML is.

The code for when the game starts up is this:
void TextInit()
{
  font.loadFromFile("TextFont (FreeMono).otf");
  font2.loadFromFile("UserInputFont (FreeMono).otf");

  text = sf::Text();
  text.setFont(font);
  text.setPosition(0,0);
  text.setFillColor(sf::Color(255,255,255,255));
  text.setCharacterSize(20);

  userInput = sf::Text(UserInputPre(), font2);
  userInput.setPosition(0,1000);
  userInput.setFillColor(sf::Color(255,255,255,255));

  videoMode = sf::VideoMode(width, height);
 
  if(doFullscreen)
    window.create(videoMode, "Total Control", sf::Style::Fullscreen);
  else
    window.create(videoMode, "Total Control");

  window.setMouseCursorVisible(true);
  window.setMouseCursorGrabbed(false);
 
  camera = sf::View(sf::FloatRect(0,0,width,height));

  //window.setFramerateLimit(30);
  window.setVerticalSyncEnabled(true);

  window.setView(camera);

  texture.loadFromFile("Background.png");
  bg = sf::Sprite(texture);
  bg.setPosition(0,1005);
  bg.setScale(60, 5);
  uiBoxHeight = bg.getPosition().y;
 
}
The code to change resolution is this:
void ChangeResolution(int size)
{
  std::vector<sf::VideoMode> vms = sf::VideoMode::getFullscreenModes();

  width = vms[size].width;
  height = vms[size].height;

  videoMode = sf::VideoMode(width, height, vms[size].bitsPerPixel);
 
  if(doFullscreen)
    window.create(videoMode, "Total Control", sf::Style::Fullscreen);
  else
    window.create(videoMode, "Total Control");

  camera = sf::View(sf::FloatRect(0,0,width,height));

  userInput.setPosition(0,height-userInput.getCharacterSize()-55);
  bg.setPosition(0,height-userInput.getCharacterSize()-55);

}

Here is the draw code:
void TextManager()
{
  bg.setColor(colours[inputBackgroundCol]);
 
  window.clear(colours[backgroundCol]);
  window.draw(text);
  window.draw(bg);
  window.draw(userInput);
  window.display();
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: GPU usage jumping to 100% after changing resolution
« Reply #1 on: January 18, 2024, 05:46:19 am »
If you don't enable vsync (or don't set a framerate limit) it will take as many resources as it can to go as fast as possible.
I see you enable it at first, but you need to enable it again after you call create.

PGco

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: GPU usage jumping to 100% after changing resolution
« Reply #2 on: January 18, 2024, 05:45:02 pm »
Oh thanks! That makes sense, I don't know why I assumed it would vsync would stay enabled after closing and reopening the window.

 

anything