c++ sdl window freeze and issues with sdl - c++

SDL just pisses me off, please help.
I'm trying just to show a window, this is the code :
#include <iostream>
#define SDL_MAIN_HANDLED
#include "SDL.h"
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window *window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 600, 480, SDL_WINDOW_SHOWN);
if (window == NULL)
return 1;
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
}
}
}
SDL_Quit();
std::cout << "Hello :)" << std::endl;
return 0;
}
Now, the issue is that it says that the program now responding and I have a "loading" icon for the mouse. Second issue is that I cannot use SDL_INIT_EVERYTHING for some reason, it just gets stuck and nothing outputs when I try to output after init.
I tried multiple sdl files x86 , x64.
I have windows 10 64bit OS.
I really start to lose my sanity here , please help.
EDIT :
the window works perfectly fine with SDL_INIT_EVERYTHING but it takes the computer to load everything for 1 minute and 50 seconds. which is a lot of time.
But when I only init SDL_INIT_VIDEO , it's not responding.
Any solution ?

Okay, so I have downloaded an older version 2.0.5 instead of the new "stable" version and seems like it works. I guess the new version just have bugs that needs to be fixed.

Related

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.

SDL_Init(SDL_INIT_VIDEO)returns 1

Just wondering , anyone got that issue:
I'm using Visual Studio Community and after adding class and moving SDL_Init(SDL_INIT_VIDEO) from main function to Someclass.cpp my Window is not appearing anymore. SDL_Init returns 1.
I thought it's just my code related issue but I copied tutor code and that's not working as well in my environment.(his eclipse works as it should) It used to work properly when SDL_Init(SDL_INIT_VIDEO) was called from main.cpp.
I was trying to add additional dependencies in Project/Settings/Linker/Input/Additional Dependencies
but no success:( Maybe I was doing something wrong.
SDL Window still doesn't appear.
This is the function which initialize SDL video:
SDL_Window* Screen::Init(uint32_t w, uint32_t h, uint32_t mag){
if (SDL_Init(SDL_INIT_VIDEO))
{
std::cout << "Error SDL_Init Failed" << SDL_GetError() << std::endl;
return nullptr;
}
mWidth = w;
mHeight = h;
moptrWindow = SDL_CreateWindow("Arcade", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, mWidth * mag, mHeight * mag, 0);
if (moptrWindow)
{
mnoptrWindowSurface = SDL_GetWindowSurface(moptrWindow);
SDL_PixelFormat* pixelFormat = mnoptrWindowSurface->format;
Color::InitColorFormat(pixelFormat);
mClearColor = Color::Black();
mBackBuffer.Init(pixelFormat->format, mWidth, mHeight);
mBackBuffer.Clear(mClearColor);
}
return moptrWindow;
}
The main function from where Init is called:
int main(int argc, const char* argv[]){
Screen theScreen;
theScreen.Init(SCREEN_WIDTH, SCREEN_HEIGHT, MAGNIFICATION);
theScreen.Draw(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, Color::Yellow());
theScreen.SwapScreens();
SDL_Event sdlEvent;
bool running = true;
while (running)
{
while (SDL_PollEvent(&sdlEvent))
{
switch (sdlEvent.type)
{
case SDL_QUIT:
running = false;
break;
}
}
}
return 0;
}
Output is:
1
C:\Users\MyPC\projects\drawing-line\Debug\drawing-line.exe (process 13032) exited with code 0.
Press any key to close this window . . .
If anyone wants to try my code there is link to google drive:
that's whole project
Thank you.
Luke
Eventually I resolved the problem. I changed all settings to X64 and got rid of const in: int main(int argc,const char* argv[]). Now works as it should.Took me while:)

Program still running after return 0; with SDL_Renderer

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.

SDL and SDL_image program doing nothing in Eclipse

I have been trying to make a PNG image appear on-screen to my SDL window. I am using the Eclipse CDT. SDL.h and SDL_image.h both seem to have been correctly linked, in that the functions pop up with colour on the compiler. When I run my code, however, literally nothing happens. There are no errors in the compiler, no comments, nothing. The window doesn't appear. I would really appreciate if anyone could help me out on the matter.
Also, SDL has worked previously on my computer before (without using SDL_image) - in which I ran a particle simulation that worked perfectly fine.
My code:
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
using namespace std;
SDL_Window *m_window; //Window upon which the game will be displayed.
SDL_Renderer *m_renderer; //Renderer used to draw the objects on the window.
SDL_Texture *playerTex;
int SCREEN_WIDTH = 600;
int SCREEN_HEIGHT = 600;
int main(int argc, char* args[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "Video init failed" << endl;
return 1;
}
//Creates the actual SDL-window and stores it in the m_window variable.
m_window = SDL_CreateWindow("Marko Beocanin SDD Project",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_FULLSCREEN);
//Error-checking method that determines if SDL could not create a window - returns false if unsuccessful.
if (m_window == NULL) {
cout << "Window Creation failed" << endl;
SDL_Quit();
IMG_Quit();
return 2;
}
//Creates an SDL-Renderer: a tool used to actually draw objects on the Window
m_renderer = SDL_CreateRenderer(m_window, -1, 0);
//Error-checking method that determines if SDL could not create a renderer - returns false if unsuccessful.
if (m_renderer == NULL) {
cout << "Renderer creation failed." << endl;
SDL_DestroyWindow(m_window);
SDL_Quit();
IMG_Quit();
return 3;
}
SDL_Surface *tmpSurface = IMG_Load("img.png");
playerTex = SDL_CreateTextureFromSurface(m_renderer, tmpSurface);
SDL_FreeSurface(tmpSurface);
SDL_RenderClear(m_renderer);
SDL_RenderCopy(m_renderer, playerTex, NULL, NULL);
SDL_RenderPresent(m_renderer);
SDL_Delay(2000);
SDL_DestroyWindow(m_window);
SDL_Quit();
IMG_Quit();
return 0;
}
The problem I had was a result of me using the wrong SDL_image library - I was using x64 instead of x86, which meant that it didn't throw an error per se, just didn't work properly!

SDL2 multiple windows focus

I have some problems with windows focus in SDL2.
I got two windows and listen to focus gain and lost events.
When I click on Window 2, the following events trigger:
"Window 1 lost focus"
"Window 2 gained focus."
When I click on Window 1, the following events trigger:
"Window 2 lost focus."
"Window 1 gained focus."
"Window 1 lost focus."
I can clearly tell the window has focus by the glowing effect my operating system draws around it.
Also, other SDL2 functions to get focus information give the same, wrong, answer when tested on Window 1.
I trimmed down the code to an almost-minimal test case:
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* w1=SDL_CreateWindow("Window 1",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
250,200,SDL_WINDOW_SHOWN);
SDL_Window* w2=SDL_CreateWindow("Window 2",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
200,250,SDL_WINDOW_SHOWN);
bool quit=false;
while(!quit){
SDL_Event e;
while(!quit && SDL_PollEvent(&e)){
switch(e.type){
case SDL_WINDOWEVENT :
{ // this block just scopes 'targetWindow' and 'title'
SDL_Window* targetWindow=SDL_GetWindowFromID(e.window.windowID);
const char* title=SDL_GetWindowTitle(targetWindow);
switch(e.window.event){
case SDL_WINDOWEVENT_FOCUS_GAINED :
// tell which window gained focus
cout << title << " gained focus!" << endl;
break;
case SDL_WINDOWEVENT_FOCUS_LOST :
// tell which window lost focus
cout << title << " lost focus!" << endl;
break;
}
}
break;
case SDL_QUIT :
quit=true;
break;
}
}
}
SDL_Quit();
return 0;
}
Is this a bug in SDL2 multi-windows support?
Does it depend on the underlying windowing system?
More importantly, is there a way to have correct focus information for multiple windows with SDL2?
I did a little more research on this and find out that the issue I described is a known bug as can be seen here.
There's a patch at the other end of the link but that's already included in the latest version of SDL.
Personally, I solved this by installing version 2.0.3 of the library.