I am writing a simple SDL2 application with 2 windows.
The first window (window variable) is shown when the application starts, the second one (window2 variable) is hidden.
Expected behavior:
I click on the first window, the second window pops up, then I close the second window.
And I can close and reopen the window as much as I want.
Observed behavior:
Once I close the second window, if I reclick in the first window, the second window doesn't appear as expected.
As stated in my comment: the window doesn't appear in my window manager (i.e. Wayland).
The code:
#include <SDL2/SDL.h>
int main()
{
SDL_Window* window, *window2 = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return 1;
} else {
window = SDL_CreateWindow("ONE", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
window2 = SDL_CreateWindow("TWO", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_HIDDEN);
if (window == NULL || window2 == NULL) {
SDL_DestroyWindow(window);
SDL_DestroyWindow(window2);
return 1;
}
bool running = true;
while(running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
if (SDL_GetWindowID(window) == event.window.windowID) {
running = false;
} else {
SDL_HideWindow(window2);
}
}
} else if (event.type == SDL_MOUSEBUTTONDOWN) {
SDL_ShowWindow(window2);
}
}
}
}
SDL_DestroyWindow(window);
SDL_DestroyWindow(window2);
SDL_Quit();
return 0;
}
This is an SDL bug that may or may not have been fixed by this patch.
You should call SDL_RaiseWindow to place the second window on top of the other.
From lazyfoo's legendary SDL tutorials:
void LWindow::focus()
{
//Restore window if needed
if( !mShown )
SDL_ShowWindow( mWindow );
//Move window forward
SDL_RaiseWindow( mWindow );
}
Related
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(¤tEvent) != 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
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.
I know this question has been asked before but most of the time answer was just to add delay or an event loop. However I have added an event loop and the window ist not showing. Only the console. I am running this program in Visual Studio 2019.
#include <iostream>
#include "GL/glew.h"
#define SDL_MAIN_HANDLED
#include "SDL.h"
int main() {
SDL_Window* window;
SDL_Init(SDL_INIT_EVERYTHING);
//fenster erstellen
window = SDL_CreateWindow("C++ OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL);
//opengl context setzen
SDL_GLContext glContext = SDL_GL_CreateContext(window);
bool close = false;
while (!close) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
close = true;
}
}
if (close) {
break;
}
}
return 0;
}
You need to include SDL_MainReady as you are not using SDL_main.
See here
So you code would be adjusted like
int main() {
SDL_Window* window;
SDL_SetMainReady();
SDL_Init(SDL_INIT_EVERYTHING);
...
return 0;
}
I'm doing Lazy Foo's tutorial on SDL (I'm using SDL2-2.0.9), and at the texture rendering part I encountered the following problem: the program compiles and runs as expected, no issue here, but when I close the window, the console doesn't close and the process continues running, so I have to close the console separately.
When I tried to debug it, I found out that the program indeed leaves the main cycle and reaches the "return 0" line in the main function successfully, but then it just hangs like that until I close the console.
The issue is only present when I use the SDL renderer with any option other than SDL_RENDERER_SOFTWARE. If I use SDL_RENDERER_SOFTWARE - the program closes as expected. With other options it stays at "return 0" running other threads (crypt32.dll, ntdll.dll and nvd3dum, in this order in the thread view, meaning that the process is stuck in crypt32).
I'm aware that my main function is not the "real main" as it has been hijacked by SDL, so exit(0) works fine as an ad-hoc solution. But I want to know, why exactly does that happen and is there any other way to fix this, so that I don't have to use exit(0) ?
Here is an example (simplified) code, which demonstrates this issue for me:
#include "SDL.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *bitmapTex = NULL;
SDL_Surface *bitmapSurface = NULL;
int width = 640, height = 480;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Could not initialize SDL");
return 1;
}
win = SDL_CreateWindow("Hello World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
bitmapSurface = SDL_LoadBMP("res/x.bmp");
bitmapTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
SDL_FreeSurface(bitmapSurface);
bool quit = false;
while (!quit) {
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bitmapTex, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(bitmapTex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
printf("REACHED RETURN 0");
return 0;
}
Works as intended, but after closing the window I see "REACHED RETURN 0" printed in console and that's it, the console stays there. The code can be simplified further, the issue will be present as long as there is an instance of SDL_Renderer created.
UPD: The callstack during the hanging:
> ntdll.dll!_NtWaitForMultipleObjects#20()
KernelBase.dll!_WaitForMultipleObjectsEx#20()
crypt32.dll!ILS_WaitForThreadProc()
kernel32.dll!#BaseThreadInitThunk#12()
ntdll.dll!__RtlUserThreadStart()
ntdll.dll!__RtlUserThreadStart#8()
UPD2: The problem is not with the loop at all, I created the simplest application where I just create a window and a renderer and then return 0, it still gives me a hanging console. Like this:
#include <SDL.h>
int main(int argc, char* args[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) return 1;
SDL_Window* window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
return 0;
}
Same thing when I destroy them properly. The problem is in the renderer.
UPD3: Here is the Parallel Stack window during the "hanging". There is no "main" thread since I close it successfully, these are the threads which stop the program from closing properly. Other than that, it doesn't give me any understanding of the problem.
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;
}