Empty SDL2 window taking lots (40%+) GPU to render? - c++

I've been trying to learn how to use SDL2, and am trying to follow the tutorials here for that. From example 7 (texture loading / hardware rendering), I've put together this stripped down example. As far as I can tell, the code is only calling SDL_RenderClear() and SDL_RenderPresent() in the render loop, VSYNC'd to my monitor (100 Hz in this case). However, I'm seeing Windows report 40% GPU utilization when doing this, which seems high for doing nothing. My GPU sits at 1-2% utilization when the SDL2 program is not running.
Removing just the SDL_RenderPresent() call brings the usage all the way down to 1-2% again, but then nothing gets drawn, which makes me think that the issue is either in SDL2's rendering, or (much more likely), how I've configured it.
Is this high GPU utilization (when doing nothing) expected? Is there anything I can do to minimize the GPU utilization (while still using hardware rendering)?
===================================
Machine details:
Windows 10
Threadripper 3970x CPU (3.7 GHz 32 core)
256 GB RAM DDR4 3200 MHz
1x RTX 3090 GPU
3x 1440p 100Hz monitors
GPU utilization (shown via Task Manager and Xbox Game Bar):
Code:
#define SDL_MAIN_HANDLED
/*This source code copyrighted by Lazy Foo' Productions (2004-2020)
and may not be redistributed without written permission.*/
// Using SDL, SDL_image, standard IO, and strings
#include <SDL.h>
#include <stdio.h>
#include <string>
// Screen dimension constants
const int SCREEN_WIDTH = 1920;
const int SCREEN_HEIGHT = 1080;
// Starts up SDL and creates window
bool init();
// Frees media and shuts down SDL
void close();
// The window we'll be rendering to
SDL_Window* gWindow = NULL;
// The window renderer
SDL_Renderer* gRenderer = NULL;
bool init() {
// Initialization flag
bool success = true;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
// Create window
gWindow = SDL_CreateWindow(
"SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
// Create renderer for window
gRenderer =
SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (gRenderer == NULL) {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
// Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0x00, 0x00, 0x00);
}
}
}
return success;
}
void close() {
// Destroy window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
// Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[]) {
// Start up SDL and create window
if (!init()) {
printf("Failed to initialize!\n");
} else {
// Main loop flag
bool quit = false;
// Event handler
SDL_Event e;
// While application is running
while (!quit) {
// Handle events on queue
while (SDL_PollEvent(&e) != 0) {
// User requests quit
if (e.type == SDL_QUIT) {
quit = true;
}
}
// Clear screen
SDL_RenderClear(gRenderer);
//Update screen
SDL_RenderPresent( gRenderer );
}
}
// Free resources and close SDL
close();
return 0;
}

Related

BGFX graphics not updating

A am trying to compile a simple bgfx program (from this link) and failing, because the window is not updating. Closing the window is fine, so i think the problem is in bgfx. I also tried other tutorials and those didn't work either
Code:
#include <stdio.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
SDL_Window* window = NULL;
const int WIDTH = 640;
const int HEIGHT = 480;
int main (int argc, char* args[]) {
// Initialize SDL systems
if(SDL_Init( SDL_INIT_VIDEO ) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n",
SDL_GetError());
}
else {
//Create a window
window = SDL_CreateWindow("BGFX Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIDTH, HEIGHT,
SDL_WINDOW_SHOWN);
if(window == NULL) {
printf("Window could not be created! SDL_Error: %s\n",
SDL_GetError());
}
}
// Collect information about the window from SDL
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(window, &wmi)) {
return 1;
}
bgfx::PlatformData pd;
// and give the pointer to the window to pd
pd.ndt = wmi.info.x11.display;
pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
// Tell bgfx about the platform and window
bgfx::setPlatformData(pd);
// Render an empty frame
bgfx::renderFrame();
// Initialize bgfx
bgfx::init();
// Reset window
bgfx::reset(WIDTH, HEIGHT, BGFX_RESET_VSYNC);
// Enable debug text.
bgfx::setDebug(BGFX_DEBUG_TEXT /*| BGFX_DEBUG_STATS*/);
// Set view rectangle for 0th view
bgfx::setViewRect(0, 0, 0, uint16_t(WIDTH), uint16_t(HEIGHT));
// Clear the view rect
bgfx::setViewClear(0,
BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH,
0x443355FF, 1.0f, 0);
// Set empty primitive on screen
bgfx::touch(0);
// Poll for events and wait till user closes window
bool quit = false;
SDL_Event currentEvent;
while(!quit) {
while(SDL_PollEvent(&currentEvent) != 0) {
if(currentEvent.type == SDL_QUIT) {
quit = true;
}
}
}
// Free up window
SDL_DestroyWindow(window);
// Shutdown SDL
SDL_Quit();
return 0;
}
Why is this happening, and how to fix it?
P.S.: I am using kali linux

SDL2 cannot open any BMP files

Im trying to play around with SDL2 following lazyfoo's tutorials to get accustomed to it, but even the basic most program doesn't work properly. I can open a basic blank window with no image and keep it open, but as soon as I try to open a BMP file in a window, it all acts weird and doesn't work anymore. My code, that initially shows no errors:
#include <SDL2/SDL.h>
#include <cstdio>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window *newWindow = nullptr;
SDL_Surface *loadedImage = nullptr;
SDL_Surface *screenSurface = nullptr;
bool quit = false;
SDL_Event event;
bool initWindow() {
bool state = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::puts("Error init");
state = false;
}
else
{
newWindow = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOWEVENT_SHOWN);
if (nullptr == newWindow)
{
std::puts("Error window");
state = false;
}
else
{
screenSurface = SDL_GetWindowSurface(newWindow);
}
}
return state;
}
bool loadMedia() {
bool success = true;
loadedImage = SDL_LoadBMP("LAND3.BMP");
if (loadedImage == nullptr)
{
printf("Error image %s \n", SDL_GetError());
success = false;
}
return success;
}
void closeWindow() {
SDL_FreeSurface(loadedImage);
loadedImage = nullptr;
SDL_DestroyWindow(newWindow);
newWindow = nullptr;
SDL_Quit();
}
int main(int argc, char *args[]) {
if (!initWindow())
{
std::puts("Error init main");
}
else
{
if (!loadMedia())
{
std::puts("Error image main");
}
else
{
while (!quit)
{
if (event.type == SDL_QUIT)
{
quit = true;
}
else
{
SDL_BlitSurface(loadedImage, nullptr, screenSurface, nullptr);
SDL_UpdateWindowSurface(newWindow);
}
}
}
}
closeWindow();
return 0;
}
When running this program, I get no errors but the UI starts acting all crazy; resolution gets very small(way smaller than 480p set up by me), all windows resize and this lasts for a brief period. If I replace the while(!quit) loop with a SDL_Delay(1000), this behaviour lasts approximately as long as the delay.
Initially my suspicion was that the file I was using the first time was corrupted(I had just renamed an existing picture), but then I downloaded a sample BMP file and nothing changed.
When using the debugger I get an error from loadMedia() that the file could not be loaded, regardless of which one I use. I am using MinGW and cLion.
What might be the issue?
The constant SDL_WINDOWEVENT_SHOWN is not a valid flag for the function SDL_CreateWindow. That constant is not intended as a flag, but as an event ID. You probably meant to use the constant SDL_WINDOW_SHOWN instead.
The constant SDL_WINDOWEVENT_SHOWN happens to have the same value as SDL_WINDOW_FULLSCREEN (both have the value 1). Therefore, your incorrect function call
newWindow = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOWEVENT_SHOWN);
is equivalent to:
newWindow = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_FULLSCREEN);
In other words, you are unintentionally asking SDL to create the window in 640*480 fullscreen mode. That is probably the reason for your desktop being resized.
The reason for the file not being found is probably because the file is not in your program's current working directory. You can either ensure that the file is in that directory or you can use an absolute path to the file, for example "C:\\Users\\MyUsername\\Desktop\\LAND3.BMP".
I doubt that your problem of not being able to kill your application using the task manager is the fault of SDL. It is probably a design flaw of Microsoft Windows. See this link on how to make the task manager more accessible from a hung fullscreen application.

how to cap sdl2 fps just like pygame

When I write my SDL2 OpenGL program somewhat like this (using VSync):
SDL_GL_SetSwapInterval(1);
while(isRunning)
{
while(SDL_PollEvent(&e))
{
if(e.type == SDL_Quit)
{
isRunning = false;
}
}
SDL_GL_SwapWindow(window);
}
my CPU usage goes as high as 39%-50% for this single program which actually does nothing
whereas when I pass sleep time after calculating time difference to SDL_Delay() makes my programming to freeze completely with a 'not responding'.
I do not want to use SDL_WaitEvent() because my program will show animation which will run regardless of input events.
is there any equivalent to pygame's which neither blocks input nor the video thread
fpsClock = pygame.time.Clock()
while(True):
pygame.display.update()
fpsClock.tick(60)
Have you checked that call to SDL_GL_SetSwapInterval(1) was successful? Your FPS should be capped at your refresh rate if your call was successful.
Did you properly initialized your OpenGL in SDL for double buffering? Like this for exmaple:
SDL_Window *window;
SDL_GLContext context;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
window = SDL_CreateWindow("OpenGL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
if (!window) {
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
return;
}
context = SDL_GL_CreateContext(window);
if (!context) {
fprintf(stderr, "Couldn't create context: %s\n", SDL_GetError());
return;
}
int r, g, b;
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);
printf("Red size: %d, Green size: %d, Blue size: %d\n", r, g, b);
SDL is not a framework, there is no such high level functions as framerate control. However you should have framerate cap with VSync. If there is no capping then it's probably means VSync is not working.
Please consider to check more in depth tutorials on how to setup OpenGL apps in SDL, this for example, see also.

SDL Window Not Showing Up at all

I am just now learning SDL and have downloaded the libraries and added them to my linker, etc with MinGW and I am trying to run a simple demo program to display a window and it will not show up at all. I get no errors at all, the window just doesn't show up.
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: could enter program loop here (see SDL_PollEvent())
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
I just tested this on Linux and MinGW. It may be a problem with SDL_Delay blocking before the window gets a chance to show. Try adding a basic main loop to see if it works. This will create an empty window.
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// A basic main loop to prevent blocking
bool is_running = true;
SDL_Event event;
while (is_running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
is_running = false;
}
}
SDL_Delay(16);
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}

C++ Semi-Transparent Window SDL

I wish to have a semi-transparent SDL background (nothing to do with sub-surfaces or images), such that instead of having a black background it is actually transparent, but the other things I draw are not. My current code is a slightly modified copy of Code::Blocks' SDL project, similar to how various applications have rounded borders or odd shapes besides rectangles.
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
int main ( int argc, char** argv )
{
putenv("SDL_VIDEO_WINDOW_POS");
putenv("SDL_VIDEO_CENTERED=1");
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_NOFRAME);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
I think what you're trying to do is in fact a shaped window (parts of the window are transparent depending on a mask that you provide). It seems there's no way to do that with SDL 1.2, however there is a SDL_SetWindowShape function just for this in SDL 1.3 for which you can find a pre-release snapshot here but it's not even in beta yet so I suggest waiting until it's officialy released :)
this is a link to a pretty neat article about development of an older application for Mac OS 9, which did not have support for shaped windows, either.
It's actually a neat article in general about software development.
But the idea seems pretty smart, and I wonder if you might be able to get it working here, too. Instead of trying to make a transparent background, they actually take a screen-shot of the computer right where their window is going to go, and then use that screen shot for their background. When the user drags the window around on the screen, they continue to update the background with new screen-shots. I think this might be more complicated than you were hoping for, but it's certainly an interesting idea.