SDL. would YOU recommend it ?
Hi folks,
after quite a few problems with NeHe's basecode when resizing my opengl window on various different systems(esp Win7 grrrr) I had SDL recommended to me.
It looks like a breeze to incorporate SDL and it's not only multi-platform but handles things like keyboard and mouse events too. However, development seems to be stalled from what I can see - which is a worry. 1.3 seems to be vaporware and I really don't like that it creates a DOS/Command window in addition to the main window.
I'd be far happier going on the recommendation of people far more knowledgeable and experienced than me since I'm pretty new to opengl.
All I'm looking for really is something that will handle the window management for me, the opengl is already pretty much complete.
Opinions and advice greatly appreciated.
Thanks.
Quote:Original post by FlyingSolo
I really don't like that it creates a DOS/Command window in addition to the main window.
This has nothing to do with SDL, it depends on your project settings (whether it's a console or Win32 subsystem).
Quote:
All I'm looking for really is something that will handle the window management for me, the opengl is already pretty much complete.
SDL is great for this task:
SDL_GL_SetAttribute(SDL_GL_xxx, vvv); // set the OpenGL attributes like VSync, double buffer, etc.SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);while (running){ SDL_Event evt; while (SDL_PollEvent(&evt)) /* handle events */; // render GL stuff SDL_GL_SwapBuffers();}
That's all it takes to get GL running with SDL.
You might also want to look into SFML, which is very similar.
SDL also runs in a tighter loop so its faster than the other predecessor window API's. It handles event for you, just use this code
SDL_Event event;
int main(int argc, char **argv)
{
while ( !quit )
{
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
quit = true
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE )
quit = true
}
}
}
SDL_Quit();
}
and also the init functions might look like this
bool Init(void)
{
if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 )
return false;
if ( SDL_SetVideoMode( screenWidth, screenHeight, bitsperpixel, SDL_OPENGL | SDL_GL_DOUBLEBUFFER ) == NULL )
return false;
//setup opengl stuff
return true;
}
and the documentation is pretty easy to follow, such as the event struct and the keycodes
SDL_Event event;
int main(int argc, char **argv)
{
while ( !quit )
{
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
quit = true
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE )
quit = true
}
}
}
SDL_Quit();
}
and also the init functions might look like this
bool Init(void)
{
if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 )
return false;
if ( SDL_SetVideoMode( screenWidth, screenHeight, bitsperpixel, SDL_OPENGL | SDL_GL_DOUBLEBUFFER ) == NULL )
return false;
//setup opengl stuff
return true;
}
and the documentation is pretty easy to follow, such as the event struct and the keycodes
SDL is a mature and capable library. Some big studios have used it when porting their games to Linux.
SDL 1.3 is available, you can build it and use it, for the "core" stuff it works. There are rough edges (particularly around the new features) and it does seem to be taking forever to get released, I would agree there. Then again these are volunteers contributing. I wouldn't classiffy it as vapourware though.
But yes, I would highly recommend SDL to accompany OpenGL.
And as nullsquared mentions, the console window is nothing to do with SDL.
SDL 1.3 is available, you can build it and use it, for the "core" stuff it works. There are rough edges (particularly around the new features) and it does seem to be taking forever to get released, I would agree there. Then again these are volunteers contributing. I wouldn't classiffy it as vapourware though.
But yes, I would highly recommend SDL to accompany OpenGL.
And as nullsquared mentions, the console window is nothing to do with SDL.
Yessir I'd recommend it. My current project utilizes SDL. My only personal issue with it is actually concerning SDL_net, which apparently is only usable for TCP communications, wheras my project uses the UDP protocol... thus I was forced to use winsock as I am comfortable incorporating it into my source.
I think you might get something out of checking out my engine 'method', WIP but it's opensource: www.van-noland.com
You can use UDP with SDL_Net. That said, the sockets interface is virtually identical across all operating systems, so it doesn't buy you too much and it makes it hard to do some things (I don't believe it is possible to make a socket non-blocking, for instance).
Well I guess that answered my question! Thank you all for some great replies.
But... with a very simple program I'm seeing memory usage increasing at about 4k/sec for no apparent reason.
Am I missing something with SDL? It seemed insanely easy to set up.
My wee test prog is below (error checking removed for some clarity). All this does is clear to yellow and draw a red quad.
[Edited by - FlyingSolo on February 14, 2010 2:34:48 PM]
But... with a very simple program I'm seeing memory usage increasing at about 4k/sec for no apparent reason.
Am I missing something with SDL? It seemed insanely easy to set up.
My wee test prog is below (error checking removed for some clarity). All this does is clear to yellow and draw a red quad.
#pragma comment(lib, "Glu32.lib")#include <windows.h> #include <stdio.h> #include "SDL.h"#include "SDL_opengl.h"SDL_Surface* screen;SDL_Event event;void InitGL(void){ glClearColor( 1.0f, 1.0f, 0.0f, 0.0f ); glViewport( 0, 0, 800, 600 ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho(0.0f, 800, 600, 0.0f, -1.0f, 1.0f); glMatrixMode( GL_MODELVIEW ); glLoadIdentity();}void Resize(GLsizei width, GLsizei height){ if (height==0) { height=1; } glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glOrtho(0.0f, 800, 600, 0.0f, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }void DrawScene(void){ glColor4f(1.0f,0.0f,0.0f,1.0f); glBegin(GL_QUADS); glVertex2d(200,200); glVertex2d(400,200); glVertex2d(400,400); glVertex2d(200,400); glEnd();}int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State{ SDL_Init(SDL_INIT_VIDEO); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); screen = SDL_SetVideoMode(800,600,16,SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE); InitGL(); while (1) { while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_KEYDOWN: switch ( event.key.keysym.sym ) { case SDLK_ESCAPE: exit(0); break; default: break; } case SDL_VIDEORESIZE: //User resized window screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE); // Create new window Resize(event.resize.w, event.resize.h); break; //Event handled, fetch next :) case SDL_QUIT: exit (0); break; default: break; } } DrawScene(); SDL_GL_SwapBuffers(); SDL_Delay( 50 ); }}
[Edited by - FlyingSolo on February 14, 2010 2:34:48 PM]
The only thing I see right off is that you appear to be bypassing the SDL implementation of WinMain(). Without digging around a bit I couldn't tell you what the consequences of this would be, but I imagine it might cause some problems.
If you check out some SDL tutorials (the Lazy Foo tutorials are good, assuming you haven't already read them), you'll see that your typical SDL-based application has what appears to be a 'normal' main() function. There's actually some macros at work that allow SDL to create a 'main' function appropriate for the target platform and operating system, but you don't need to worry about that - just write main() as you would normally, and everything should work correctly. (Note that since in this context 'main' is actually a user-defined function, you must use the 'main' signature that matches what SDL expects, and you can't omit the return value as you could in a 'real' main function.)
Oh, and to get your code to format more nicely, you can use [ source ] tags (no spaces).
If you check out some SDL tutorials (the Lazy Foo tutorials are good, assuming you haven't already read them), you'll see that your typical SDL-based application has what appears to be a 'normal' main() function. There's actually some macros at work that allow SDL to create a 'main' function appropriate for the target platform and operating system, but you don't need to worry about that - just write main() as you would normally, and everything should work correctly. (Note that since in this context 'main' is actually a user-defined function, you must use the 'main' signature that matches what SDL expects, and you can't omit the return value as you could in a 'real' main function.)
Oh, and to get your code to format more nicely, you can use [ source ] tags (no spaces).
You also might want to check out SFML http://www.sfml-dev.org/
SFML essentially serves the same purpose as SDL
SFML essentially serves the same purpose as SDL
I guess you are using WinMain function because of the additional console window.
As was said above: use a normal main function and add the following options to the Visual Studio's linker:
/SUBSYSTEM:WINDOWS
/ENTRY:mainCRTStartup
This will disable the additional console window.
As was said above: use a normal main function and add the following options to the Visual Studio's linker:
/SUBSYSTEM:WINDOWS
/ENTRY:mainCRTStartup
This will disable the additional console window.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement