Sign in to follow this  
FlyingSolo

OpenGL SDL. would YOU recommend it ?

Recommended Posts

FlyingSolo    117
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.

Share this post


Link to post
Share on other sites
nullsquared    126
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.

Share this post


Link to post
Share on other sites
zaneski13    104
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

Share this post


Link to post
Share on other sites
rip-off    10979
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.

Share this post


Link to post
Share on other sites
deftware    1778

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

Share this post


Link to post
Share on other sites
rip-off    10979
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).

Share this post


Link to post
Share on other sites
FlyingSolo    117
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.


#pragma comment(lib, "Glu32.lib")
#include &lt;windows.h&gt; #include &lt;stdio.h&gt; #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]

Share this post


Link to post
Share on other sites
jyk    2094
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).

Share this post


Link to post
Share on other sites
shmok123    102
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.

Share this post


Link to post
Share on other sites
FlyingSolo    117
Thank you one and all - excellent stuff, I've learned a lot.

Thanks for the heads up on the console, that's my inexperience with Visual C.

Tried setting up a new SDL project using Windows (/SUBSYSTEM:WINDOWS) but now I'm up to my armpits with msvcrt linker issues - I've seen these before and have no idea how to resolve them. Seems I'm not alone there though.

SDL apps have to be built as Multi-threaded DLL ? That means redisting the msvcr90.dll file with my app - that's new.

finally got a good handle on opengl, SDL is a breeze, it's the rotten compiler that's holding me up! Sometimes life just doesn't seem fair!

The Lazy Foo tut's don't work btw (msvcrt errors as above) which is a shame. Guess they're for 2005 not 2008.

More to learn - thanks all.

Share this post


Link to post
Share on other sites
Kambiz    758
Qt Logo

I recommend dropping SDL and similar libraries in favor of Qt. It is free, cross platform, it has excellent documentation and it is in active development. It provides you with much much more that SDL. Especially with a complete GUI library and a high level hardware accelerated painting engine.

Share this post


Link to post
Share on other sites
nullsquared    126
Quote:
Original post by FlyingSolo
SDL apps have to be built as Multi-threaded DLL ? That means redisting the msvcr90.dll file with my app - that's new.

Yes, unless you rebuild SDL with another option.
Quote:

More to learn - thanks all.

My exact settings for compiling with SDL on MSVC2008 are:
SDLmain.lib
SDL.lib
(in that order)
/SUBSYSTEM:CONSOLE in linker command line settings
int main(int argc, char **argv) for the main function

Share this post


Link to post
Share on other sites
FlyingSolo    117
Once again - thanks to all! Although google is an invaluable tool these days, there is no substitute for people with real experience. I really do appreciate your help. I know us relative newbies are a pain in the ass !

All seems to be working happily with SDL now although I did have some fun with some demo code I found. Pressing the ALT key would cause the app to resize to something like 32767x100 :) Sorted now thank God.

I did look at Qt briefly a while back, I think it would be like using the entire 101st Airborne to break up a bar fight though. My app, although complex, is relatively small. I've bookmarked that though for future projects though.

Thanks for the MSVC tips, always very useful to have. I've been using Delphi since v1.0 and boy does that spoil you. You can give no thought whatsoever to setup and just concentrate on your code - it really does rock. (but I love C !)

Big thanks to M$ for making Visual C a freebie, but as with most modern software it's bloated and there are more options and tweaks than I could shake a sh1tty stick at! I started on DEC PDP-8i's about 30 years ago and it was a lot less complex then!

So, for now - it all seems happy again, and my goal of getting this running on the Mac and possibly Linux too has been made easier by SDL - and you guys.

Thank you all ! :-)

Share this post


Link to post
Share on other sites
ishpeck    154
Quote:
Original post by FlyingSolo
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.



I've used SDL for a long time now and have recently started folding OpenGL into my SDL programming experience.

I've used Allegro (back in the day), SFML, and tinkered around with DirectX. SDL is the most intuitive and best documented of all the libraries I've used. Combined with OpenGL, you can get some pretty fast graphics in place with lucid audio and user input handling.

SDL has several child projects that help round it off more thoroughly. SDL_image is pretty much necessary for me because I tend to store my sprites in a format other than .BMP. SDL_ttf is a bit obnoxious and I just defer to OpenGL for showing text. SDL_mixer is rather necessary and not very hard to learn.

I've never bothered with socket programming so I can't comment on SDL_net.

In short, I'd recommend SDL. It's portable and it just plain works. Maybe I'm just too dumb but I never could get SDLdotNET (the C# wrapper for SDL) or SFML to really work for me and DirectX gives me ulcers. I've gotten the best results in the least amount of time with good, old-fashioned SDL projects.

Share this post


Link to post
Share on other sites
Shinkage    595
On an interesting note, the Palm Pre will be using SDL as its official graphics library with the release of the native programming SDK. As a Pre owner, I found that interesting.

Share this post


Link to post
Share on other sites
Prune    224
SDL is a jack of all trades and a master of none.

For example of the lack of optimization, I complained on the mailing list that "SDL_SemWait() on Windows calls WaitForSingleObject(), which obtains a kernel lock all of the time and thus has significant overhead even when it returns right away--from an Intel forum, it's about 40 times slower than a user-space lock. It would make sense to change this to only call when the program actually expects it to block. A user-space lock should be done using interlocked operations similar to the way it's in pthreads-win32, then WFSO called if it has to wait." then I was told to make a change myself I felt it necessary. I used pthreads instead and specialized substitutes can be optimally selected to replace all other aspects of SDL as well.

Share this post


Link to post
Share on other sites
Ectara    3097
@Prune

It is much a volunteer project. If you have taken the time to do this research, and go so far as to tell them what the problem is and how it could be fixed, why not fix it? Thus this issue would no longer exist.

Share this post


Link to post
Share on other sites
swiftcoder    18437
Quote:
Original post by Ectara
@Prune

It is much a volunteer project. If you have taken the time to do this research, and go so far as to tell them what the problem is and how it could be fixed, why not fix it? Thus this issue would no longer exist.
Prune's example isn't the only one, and while his is a relatively technical issue, some of the others aren't.

A personal gripe of mine: we have been waiting since sometime in 2003 for the SDL_WM_ToggleFullScreen() function to be implemented outside of linux. The function has been in the API all this time, but somehow, it never quite is implemented...

Share this post


Link to post
Share on other sites
Steve132    433
I really like FreeGLUT. Its under active development and really simple, its designed to work with OpenGL from the start, AND it works with OpenGL 3.0. I like it a lot.

Share this post


Link to post
Share on other sites
swiftcoder    18437
Quote:
Original post by Steve132
I really like FreeGLUT. Its under active development and really simple, its designed to work with OpenGL from the start, AND it works with OpenGL 3.0. I like it a lot.
Another worthy choice is glfw-lite, unfortunately only available from the subversion repository. Basically, they stripped all the cruft (threading, image loading, etc.) out of glfw, and updated it considerably.

Share this post


Link to post
Share on other sites
MarkS    180
Quote:
Original post by FlyingSolo
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.


I've never touched SDL. However, I have been using OpenGL for many years. I would really recommend that you try and get the base code working, if for no other reason than understanding how to implement an OpenGL context fully in Windows. I'm using OpenGL under Windows 7 and have had no trouble with it at all.

SDL may be the best thing for your project; I simply do not know. However, using it to avoid learning how / having to solve a problem will only cause you headaches in the future.

Also, the NeHe tutorials are not the best way to get started. They have caused me a great deal of frustration. Many of them are poorly written and the Windows code is horrible. When I was starting to learn Windows programming a couple of years ago, I made the mistake of trying to use the NeHe examples. I ended up getting more information and understanding out of a 10 year old Windows 95 game programming book.

Exactly what problems are you having?

Share this post


Link to post
Share on other sites
Cathbadh    100
I like SFML because it is a bit more modular than SDL. In terms of window management and input events, it is pretty much identical to SDL, but you have the ability to link just the system and window libraries and just leave out all the crap you don't need, like the sprite system, sound, and networking to help keep your namespaces uncluttered.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now