Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

OpenGL SDL/OpenGL - some things from NeHe

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been looking at some OpenGL using Nehe's tutorials. I started out using SDL, but I switched to his Win32 example to get more of a feel for the nuts-and-bolts. But my question is, he does some advanced things like checking for an active GDI context etc., and he is able to resize the Win32 window with everything redrawn at the new size respective to the window. His code, however, is pure Win32 and wouldn't work on any other platform without being rewritten from scratch. How would I achieve the same from SDL? I'd like to be able to resize the window, redrawing the objects within it respective to the new window size, like NeHe, and also be able to check if OpenGL has been successfully initialised etc., and if we have an active render context, setting pixel modes, that sort of thing. I'd also like to be able to flick between fullscreen and normal at a keypress, using SDL. Again this is possible in the Win32 version. Not only is the SDL approach probably easier, but allows it to be cross platform. How would I approach these things? I'm a noob when it comes to OpenGL, I might get round to buying a book soon actually. Thanks, ukdeveloper.

Share this post


Link to post
Share on other sites
Advertisement
To resize the screen you are going to have to call SDL_SetVideoMode while you're program is captuing events. Something like this:


while (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_VIDEORESIZE:
sdlbase.screen = SDL_SetVideoMode(event.resize.w,
event.resize.h,
bpp, VideoFlags);

ResizeWindow(event.resize.w, event.resize.h);

...

ResizeWindow is your regular resize function from opengl.

Unfortunetly you cant toggle between fullscreen in window it only works in X by uinsg this function: SDL_WM_ToggleFullScreen()

BTW if you want to resize the window you must pass: SDL_RESIZABLE to SDL_SetVideoMode

If you want fullscreen mode pass: SDL_FULLSCREEN

for double buffering:

before calling SDL_SetVideoMode call:
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

Here is how I set all my Video options:


const SDL_VideoInfo *VideoInfo;

VideoInfo = SDL_GetVideoInfo();

if (!VideoInfo)
{
printf("ERROR video query failed: %s\n", SDL_GetError());
return false;
}

/* Video Options */
sdlinit->VideoFlags = SDL_OPENGL;
sdlinit->VideoFlags |= SDL_GL_DOUBLEBUFFER;
sdlinit->VideoFlags |= SDL_HWPALETTE;
sdlinit->VideoFlags |= SDL_RESIZABLE;

/* Check for hardware surface, enable if hardware allows */
if (VideoInfo->hw_available)
sdlinit->VideoFlags |= SDL_HWSURFACE;
else
sdlinit->VideoFlags |= SDL_SWSURFACE;

/* Hardware blits, enable if can */
if (VideoInfo->blit_hw)
sdlinit->VideoFlags |= SDL_HWACCEL;

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 1);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 1);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

sdlinit->screen = SDL_SetVideoMode(sdlinit->xres, sdlinit->yres,
sdlinit->bpp, sdlinit->VideoFlags);

if (sdlinit->screen == NULL)
{
printf("ERROR when setting video mode: %s\n", SDL_GetError());
return false;
}


VideoFlags is an int type.

Have Fun

Share this post


Link to post
Share on other sites
please use the source tags to surround source code and this should be in the NeHe forum under affilates if this has something to do with NeHe,
Richard

Share this post


Link to post
Share on other sites
If you don't want to have to reload all of your images and so forth when the context is lost, you can add the flag SDL_OPENGLBLIT to the window creation (in addition to the flag SDL_RESIZABLE). Then, when the resize event's been issued, you need to call this:

SDL_UpdateRect(yourSurface,0,0,0,0);
glViewport(0,0,event.resize.w,event.resize.h);

It's a quick hack for a bug that shouldn't exist, but enjoy it anyway.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!