OpenGL + SDL

Started by
11 comments, last by Acharis 11 years, 8 months ago
Is there a way to use both OpenGL and SDL (not just for keyboard input but for 2D graphics)? I want to render a 3D scene in the background and blit on top of it various 2D interface elements via SDL.

I'm thinking along the lines of "render OpenGL to SDL surface instead of screen and then blit it" solution. Performance is of extremely low importance.

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Advertisement
OpenGL supports both 2D and 3D graphics, it uses polygons to render graphics which means that you can render both 2D and 3D graphics in the same application (did you notice the Captain Obvious in there? :P ) You would just have to render your 2D interface in relation to the screen and the 3D world in relation to a camera that exist within that 3D space.
I don't know how familiar you are with OpenGL, but here's a useful website that gets you comfortable with the library: http://nehe.gamedev.net/
Hope this helps :)
Yes, I know I can use OpenGL for drawing 2D primitives. But I hoped for SDL blit solution. This would let me share my already existing 2D drawing code, true type fonts code, etc mixed with 3D.

Is it possible?

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Oh, sorry.
I don't think you can use SDLs blitting when you use OpenGL since you can (by the looks of it) only enable one of them at a time, you can write "SDL_SetVideoMode( x, y, 32, SDL_OPENGL )" to enable OpenGLs rendering and "SDL_SetVideoMode( x, y, 32, SDL_SWSURFACE )" to enable SDLs rendering, but never enable both at the same time with that method.
Maybe there's a way to use both OpenGLs rendering and SDLs rendering in the same application, but my answer to you would be that you probably can't.

Something you can do is to load an SDL_Surface and render it on a polygon using OpenGL, but that, I think, is about as "close" as you can get to SDLs way of rendering while using OpenGL. Whatever the case, I think that it's easier to just switch over to OpenGLs rendering, OpenGL is much more useful and faster compared to SDL.
Well, you can render fonts with SDL_ttf to surfaces, and then render the pixel data as a texture in OpenGL. If you are adventurous, you can do roughly the same thing. You create a blank surface, blit your scene on it, and then extract the pixels from the surface to map them on a texture. You will have to repeat the process every time you change something in SDL.
Sorry to just randomly jump in here but I was looking through gamedev and just decided to create an account because I saw this question. I am new to SDL and openGL but I thought maybe I could help with a bit of code from one of my projects. This may be helpful or may not, but it does seem to answer the question of how to get SDL_SWSURFACE and SDL_OPENGL on at the same time.

[source lang="cpp"] int videoFlags;
const SDL_VideoInfo* videoInfo;

SDL_Init( SDL_INIT_EVERYTHING );

videoInfo = SDL_GetVideoInfo();
if(!videoInfo)
return false;

videoFlags = SDL_OPENGL;
videoFlags |= SDL_GL_DOUBLEBUFFER;
videoFlags |= SDL_HWPALETTE;
videoFlags |= SDL_RESIZABLE;

if( videoInfo->hw_available )
videoFlags |= SDL_HWSURFACE;
else
videoFlags |= SDL_SWSURFACE;

if( videoInfo->blit_hw )
videoFlags |= SDL_HWACCEL;

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

screen = SDL_SetVideoMode( screenW,screenH,screenBPP,videoFlags );[/source]
To clarify, I need pixel perfect accuracy, so converting surface to a texture and then display it as 2 triangles is not an option. If my memory is correct, the only way to do pixel perfect render in OpenGL is the use of glDrawPixels (which is unfortunatelly not hardware accelerated, IIRC).

Also, it needs to run on OpenGL 1.3 max (compatibility with older intel video onboard cards, especially in a bit older laptops).


Well, you can render fonts with SDL_ttf to surfaces, and then render the pixel data as a texture in OpenGL. If you are adventurous, you can do roughly the same thing. You create a blank surface, blit your scene on it, and then extract the pixels from the surface to map them on a texture. You will have to repeat the process every time you change something in SDL.
Hmm, if I'm reading this right, you are saying I can first render the 3D scene via OpenGL, then read it via SDL and create SDL_surface of it and the clear the screen and redraw everything I want using SDL blits?

But it would require changing some SDL video setting between these two (from OpenGL to SDL video)?

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

OK, what you do is that you create a surface in memory with SDL_CreateRGBSurface . SDL blitting functions should not care that you are blitting on a memory surface instead of blitting on the screen surface. You just make sure that the memory surface is in the same format as your images (you probably want to convert your sprites with SDL_ConvertSurface when you load to to be of the same format as the one you create with SDL_CreateRGBSurface).

Finally, when you are done drawing to the memory surface, you just generate a texture out of it, and draw it on the whole screen, as if it were a texture you loaded from a file.

So far as fonts go, I do love the simplicity of SDL_ttf. It renders to a SDL_Surface, which you can simply treat as yet another texture, if you were using the OpenGL API directly.
It actually isn't that hard to create an SDL-like texture render API in OpenGL. You can render your 3D scene, clear/disable the depth buffer, and then render your ortho 2D graphics on top if it.

If you're too lazy to do it yourself, I basically already did it for you in my OpenGL Tutorial, both in modern OpenGL and OpenGL 2.1.

Learn to make games with my SDL 2 Tutorials

Clarification what I need:
- I need PIXEL PERFECT solution, so absolutelly no "textures" involved at all. I need a control of a position of a single pixel (for 2D render only).
- Max OpenGL 1.3 (I need it to run on old laptops with IBM gfx card onboard, these sux a lot when it comes to OpenGL support, even if not that old), also it needs to run under outdated drivers (yes, I'm making a very low specs retro game, which is to be played on inferior hardware that is not capable of even running any modern game).
- Ideally (but I can live without it) would be if I could directly use SDL code to rended 2D stuff (interface on top of a 3D scene, no mixing these two together, a separate 3D scene at the bottom and separate 2D on top of it), that would save me a lot of rewriting and keeping 2 versions of the render engine (plus, SDL is much faster when it comes to render compared to glDrawPixels since it can use VRAM).

Yes, I know, these requirements are odd and no one makes games this way nowadays :)



I think, something like this would be the most convenient:
- render 3D scene in OpenGL
- instead of swapping the scene from secondary to primary buffer (screen), copy/convert the scene to SDL_surface
- disable OpenGL, enable SDL video and clear everything on the screen/buffer
- blit via SDL the 3D scene (stored as SDL_surface) and then do the normal SDL blits for the rest
- swap the buffers (SDL) so the primary screen/surface is updated

Is this possible?

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

This topic is closed to new replies.

Advertisement