SDL_WM_SetCaption not working - c++

any idea why the following code isn't working? Nothing happens when it's called, the window title is still untitled. I'm on Ubuntu linux.
SDL_Init( SDL_INIT_VIDEO );
SDL_WM_SetCaption("Window Title", "Icon Title");
SDL_Surface* screen = SDL_SetVideoMode( 512, 512, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
Vector2 center = Vector2(256,256);
const char* c = "test";
SDL_WM_SetCaption( c, 0 );
SDL_Event event;

Make your first call to SDL_WM_SetCaption after SDL_SetVideoMode has been called. Also, remove the second test call. If you are using SDL 1.3, (it sounds like you are using 1.2, so you can probably ignore this), call SDL_SetWindowTitle:

Your code may not be at fault. Ubuntu may be responsible for that one. Or rather, the window manager/compositor Compiz that it uses:
https://bugs.launchpad.net/ubuntu/+source/compiz/+bug/257391
Switching from Compiz to Metacity could help you figure out if you are affected by this bug as well.
There may be something else going on: I don't get a title when I call SetCaption after SetVideoMode but I do get one when I set the caption before, which is not the behavior you are observing.

Related

SDL_GL_SwapWindow crashes upon third call

I have made a very basic SDL application which kept going for a few iterations of the main loop, but then crashed. I have located the error to lie in SDL_GL_SwapWindow, which works the first two times, then crashes. This is a very simplified version that I have actually tried and it did the same thing.
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* SDLWindow = SDL_CreateWindow("Balls", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
SDL_GLContext SDLGLContext = SDL_GL_CreateContext(SDLWindow);
glewInit();
glViewport(0, 0, 800, 600);
SDL_GL_SwapWindow(SDLWindow);
SDL_GL_SwapWindow(SDLWindow);
SDL_GL_SwapWindow(SDLWindow); //crashes here
return 0;
First two calls work just fine, the third one crashes the program. Strangely, the crash occured in igd10umd32.dll and the debugger didn't allow me to read that file. I looked that up on the internet, but no attempts to fix the library worked. Besides, if the problem was in this library, it probably wouldn't appear under such strange conditions.
And what's even weirder is that the program works fine on my second laptop. And when I compiled it there and ran it on this laptop, it also worked fine.
I'm using Windows 10. I've worked with SDL/OpenGL many times and never encountered anything like this. Any ideas what may cause this very chaotic behavior?
Solved. It was nothing more than an NVIDIA bug. Updating the driver got rid of all problems.

Explanation of SDL2 windows/surfaces?

I made a short program to test out SDL2, though there are some things I don't understand how they work.
So I have created a window and a surface:
SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;
Now I have this (the part I don't get):
window = SDL_CreateWindow("Window name", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);
So the first line: I use the SDL_createWindow() function to create a window called window I assume. The second line, I got no idea whats going on - explanation?
Finally I have this:
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
followed by some clean up code to set the pointers back to nullptr and exit the program/destroy windows etc.
The code you have pasted does the following things: Creates a SDL window called "Window name", sets its horizontal and vertical positions to center, sets the window size to 640 x 480 and marks it as shown.
The second line acquires the SDL surface bind to this window.
What this means is: Create Window , actually sets up and openGL window and a GPU texture (the Surface, althou SDL2 has seperate class for Textures), to which it is going to draw. Modifying the surface acquired with GetWindowSurface will modify the pixel on the window you have just created.
Bliting is applying a array of pixel to a target texture, in the meaning : hey i got this image/prerendered frame etc.. and I want to apply it to this surface so i can show it. Blit it.
I hope this is helpful : >
You can find more information for SDL here
Official SDL wiki
LazyFoo
LazyFoo provides a full tutorial and explanations of everything for the old SDL, but a lot of the things are the same in SDL2

Fullscreening a window in SDL2 with openGL

My program starts with a loading window while it is compiling shaders, loading textures etc. I then want to be able to launch a fullscreen application and use these resources. My understanding is that the openGL context must be the same before and after. I tried two methods for this: first of all I tried making a second window which was fullscreen, and used the SDL_GL_makecurrent command on this window to 'transfer' the context across (couldn't find where I read about this method), and secondly tried just fullscreening the loading window. Both of these methods resulted in the loading screen being moved to the top left corner of the screen. However opengl commands no longer ran properly in fullscreen, including clearing buffers which meant that the window contained the contents of my desktop/background applications.
Is there a proper way of doing this? Or is this a strange bug in sdl/opengl drivers?
Code to fullscreen original window:
//opengl commands work fine up to here
//now to fullscreen
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_SetWindowSize(window, 1366, 768); //tried this on either side of line above and without either line
glViewport(0, 0, 1366, 768); //update viewport
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
//window should be whited, other draw commands tried and all fail or distort
SDL_GL_SwapWindow(window);
Creating a new window and using previous context:
//Fine up to here
window2 = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1366, 768, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_SHOWN);
SDL_GL_MakeCurrent(window2, glContext); //created with SDL_GL_CreateContext(oldwindow);
//draw commands dont work
PS: running ubuntu
Update: In the second code, reusing the context in a new window, it returns an error saying 'invalid window' when it fails, which is most of the time but not always. When it fails, the screen ends up completely corrupted(black with weird white squares and patterns), ending the program will not clear the screen of this (although screenshots are perfectly fine?), but it can be restored by ctrl+f1 to terminal then ctrl+f7 back
I dont really know if its a bug. I experienced the same issue with sdl2 and opengl.
Create an regular window
attach to opengl context.
fullscreen
BOOM. black screen and crashed window.
I only noticed that issue in ubuntu.
Trought some tests i found a quick way to fix it:
Uint32 flags = 0;
flags |= SDL_WINDOW_RESIZABLE;
//bla bla bla your tags
flags |= SDL_WINDOW_OPENGL;
m_window = SDL_CreateWindow( "hello gl", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_screen.x, m_screen.y,flags);
m_glContext = SDL_GL_CreateContext(m_window);
//Set right the way the screen to fullscrene false
SDL_SetWindowFullscreen(m_window, SDL_FALSE);
Now the fullscreen seems to work without problem.

DWM being disabled causes front-buffer corruption in OpenGL window

I'm experiencing some weird graphical corruption with my SDL2 + OpenGL GUI application. Now I apologize if I'm not using the appropriate term to describe the situation. It seems that the front buffer is being invalidated and other applications are being drawn in the buffer?
Important note: this corruption only occurs in Windows XP or when using the Windows Basic theme in Windows 7.
The corruption can be reproduced in a few ways:
by moving the window around. It seems that the front buffer gets corrupted with the image of other applications in front or behind it.
if there's an application behind, it'll occasionally flash into the front buffer.
I can load up a google webpage in Chrome above the opengl application, close the chrome process, and then move the opengl application around and I'll see the google homepage covering the whole inside of the window.
by moving another application over the opengl window.
It seems that I also see the corruption when I'm moving other windows around, even if they don't overlap the opengl window, so long as I've already tried one of the ones posted just above.
inline int UI::RenderingThread()
{
UI::Window::Instance().RenderingThreadEnter();
while( UI::Window::Instance().RenderingThreadActive() )
{
unsigned int ticks = SDL_GetTicks();
UI::Window::Instance().RenderingThreadUpdate();
UI::Window::Instance().RenderingThreadRender();
ticks = SDL_GetTicks() - ticks;
if( ticks < 33 )
{
SDL_Delay( 33 - ticks );
}
}
UI::Window::Instance().RenderingThreadExit();
return 0;
}
As you probably guessed, I'm using multithreading and I have my rendering in a separate thread. Within the RenderingThreadRender() function, I only redraw if there's a change in content, or I've requested a redraw.
case SDL_WINDOWEVENT:
switch( sdl_event.window.event )
{
default:
UI::Window::Instance().Redraw();
break;
}
This is done to allow all SDL_WINDOWEVENT events to redraw in hopes that one of them would fix the problem. Unfortunately that has not solved the issue.
I'm hesitant to simply constantly redraw my application at 30 or 60 fps since I've noticed that by doing so, other application would be sluggish when moving them around.
Initialization:
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
this->window = SDL_CreateWindow( this->caption.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
UI::WindowSize::Instance().GetWindowWidth(), UI::WindowSize::Instance().GetWindowHeight(),
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glViewport(0, 0, UI::WindowSize::Instance().GetWindowWidth(), UI::WindowSize::Instance().GetWindowHeight());
// Set the OpenGL view
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0, UI::WindowSize::Instance().GetWindowWidth(), UI::WindowSize::Instance().GetWindowHeight(), 0, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glDisable( GL_DITHER );
Rendering:
if( this->redraw )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// a few DrawArrays
SDL_GL_SwapWindow( this->window );
}
Does anyone have any idea what could be causing this issue, and how to fix it?
EDIT: Some more research has pointed me to the following two questions:
https://superuser.com/questions/316738/what-happens-with-the-off-screen-front-buffers-in-windows-7-when-dwm-is-disabled
C++ OpenGL window only tracks background
However, this doesn't solve my problem since I'm already using Double Buffering.
I've found the solution to my problem. It's unlikely that anyone will experience the same issue that I was having, but you never know. Essentially a C# Window of size equal or larger than my application, was being created before. This window was hidden, however because XP has a global framebuffer instead of a composition, there was a conflict causing the corruption.
All in all, if you're having a similar issue, make sure there isn't another context.

Emscripten call to SDL_Init freezes Browser Textinput

I am currently crosscompiling a Sprite Engine under mingw.
Therefore i have 2 Questions.
The behavior of SDL is Emulated by Emscripten through the WebGL Layer.
i don't even have to link the SDL libraries when compiling with emcc.
Question is:
If i initalize my App Like this:
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) == -1)return -1;
SDL_Surface *screen= SDL_SetVideoMode(640, 480, 24, SDL_SWSURFACE);
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 0, 0));
SDL_Flip(screen);
then i am NOT able to put text into a textfield of the Browser, but i am getting the SDL_Events.
All other Browser Inputs like checkboxes or selectboxes are working.
If initialize my App like this (Emscripten works also without SDL_Init!):
SDL_Surface *screen= SDL_SetVideoMode(640, 480, 24, SDL_HWSURFACE);
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 0, 0));
SDL_Flip(screen);
then i am able to put tet into the browser textfield but i am not getting SDL_Events.
Is there a workaround to get the Browser Text Input Fields and SDL_Events working?
Question
This line of code compiled on my WIN32 System fills the screen blue
SDL_FillRect(screen,NULL, SDL_MapRGB(screen->format, 255, 0, 0));
the same line compiled with Emscripten fills the screen red.
Is there a way to switch the SDL colors in Emscripten or in the SDL headers?
From your native code, add this before the SDL_CreateWindow call:
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#canvas");
More info here: https://wiki.libsdl.org/SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT
Emscripten, by default, captures all user events to the page. This makes sense for a fullscreen game for example. In your use case, you probably want to modify Emscripten's SDL_Init to not listen to key events, or change its receiveEvent return value.
Had the same problem. For me, doing this fixed it:
Module['doNotCaptureKeyboard'] = true;