OpenGL (SDL 1.1.0 snapshot)

Give us code, Mike! The code! The code! (insert taped recording of revolting
peasants)

Okay! Okay! :slight_smile:

SDL 1.1.0 CVS snapshot is now available:
http://www.devolution.com/~slouken/SDL/cvs.html

Check out the WhatsNew file for a brief description of the API changes.

The OpenGL support has only been implemented on Linux so far, but
you are more than welcome to contribute code for other platforms.

You can get the 1.1.0 development branch by following the instructions
on the page, but check out the “devel_1_1_0” tag:
cvs -d :pserver:guest at cvs.lokigames.com:/cvs checkout -r devel_1_1_0 SDL

Enjoy! :slight_smile:
-Sam Lantinga (slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

Any eta on when you might have OpenGL support for windows? I currently have
it working and was going to merge my changes but looking into the depths of
SDL I doubt I’ll get it right. Here is what I am currently doing after I’ve
called SDL_SetVideoMode I call a function called initGL. Currently this is
broken into two calls, one to init the win32 specific portion, the other
standard gl code. Getting gl to work under windows is easy once you have an
HWND you can draw to. What I’m fearing is that you are going to have to
create an entirly new device, as you don’t want to be using a DirectDraw to
create the window when you simple Win32 will do the trick. To get full
screen mode use the the ChangeDisplaySettings function call. This URL gives
a very detailed explaination of getting it to work:
http://www.demonews.com/hosted/nehe/opengl.htm

Also if I’ve been using my newsread to read the maillist stuff. If I post
through the newsserver will anybody see this - ie, will it post back to the
maillist?

Gerry

// win32 specific OpenGL variables
static HWND m_hWnd = NULL; // window handle
static HDC m_hDC = NULL; // device context
static HGLRC m_hRC = NULL; // rendering context

static bool initBaseGL(void) // This Will Be Called Right After The GL
Window Is Created
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The
Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The
Depth Buffer
glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To
Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
NEW )
glShadeModel(GL_SMOOTH); // Enables Smooth Color
Shading
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

glViewport(0, 0, m_ScreenWidth, m_ScreenHeight);            // Reset The

Current Viewport And Perspective Transformation

glMatrixMode(GL_PROJECTION);
glLoadIdentity();                           // Reset The Projection

Matrix
gluOrtho2D(0.0, m_ScreenWidth, m_ScreenHeight, 0.0);
// ASSERT(m_ScreenHeight != 0);
// gluPerspective(45.0f,
(GLfloat)(m_ScreenWidth)/(GLfloat)(m_ScreenHeight), 0.1f, 100.0f); //
Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);

return true;

}

// TODO: remove the Win32 specific code)
static bool initWin32GL(void)
{
// get HWND from SDL
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
SDL_GetWMInfo(&info);

m_hWnd = info.window;
m_hDC  = GetDC(m_hWnd);

GLuint pixelFormat;

PIXELFORMATDESCRIPTOR pfd =


    sizeof(PIXELFORMATDESCRIPTOR),  //  size of this pfd
    1,                     // version number
    PFD_DRAW_TO_WINDOW |   // support window
    PFD_SUPPORT_OPENGL |   // support OpenGL
    PFD_DOUBLEBUFFER,      // double buffered
    PFD_TYPE_RGBA,         // RGBA type
    static_cast<BYTE>(m_ScreenBpp), // color depth
    0, 0, 0, 0, 0, 0,      // color bits ignored
    0,                     // no alpha buffer
    0,                     // shift bit ignored
    0,                     // no accumulation buffer
    0, 0, 0, 0,            // accum bits ignored
    16,                    // 32-bit z-buffer
    0,                     // no stencil buffer
    0,                     // no auxiliary buffer
    PFD_MAIN_PLANE,        // main layer
    0,                     // reserved
    0, 0, 0                // layer masks ignored
};

pixelFormat = ChoosePixelFormat(m_hDC, &pfd);       // finds closest

match to pixel format requested

if (pixelFormat == 0)
{
    Diagnostics_alert("Can't find a suitable PixelFormat.");
    return false;
}

if (!SetPixelFormat(m_hDC, pixelFormat, &pfd))
{
    Diagnostics_alert("Can't set the requested PixelFormat.");
    return false;
}

m_hRC = wglCreateContext(m_hDC);
if (m_hRC == NULL)
{
    Diagnostics_alert("Can't create a GL Rendering Context.");
    return false;
}

if (!wglMakeCurrent(m_hDC, m_hRC))
{
    Diagnostics_alert("Can't activate GLRC.");
    return false;
}

return true;

}

static bool initGL(void)
{
if (!initWin32GL())
return false;

if (!initBaseGL())
    return false;

return true;

}

When I’m ready to clean up the gl I call this:

static void restoreGL(void)
{
wglMakeCurrent(NULL, NULL);

if (m_hRC != NULL)
{
    wglDeleteContext(m_hRC);
    m_hRC = NULL;
}

if (m_hWnd != NULL && m_hDC != NULL)
    ReleaseDC(m_hWnd, m_hDC);

m_hWnd = NULL;
m_hDC  = NULL;

}

Any eta on when you might have OpenGL support for windows?

Nice concise code. I’ll probably be able to integrate something like it
in the next few days.

This URL gives a very detailed explaination of getting it to work:
http://www.demonews.com/hosted/nehe/opengl.htm

Wow, very nice set of tutorials. :slight_smile:

Also if I’ve been using my newsread to read the maillist stuff. If I post
through the newsserver will anybody see this - ie, will it post back to the
maillist?

It should. If a post doesn’t make it, resend it to the mailing list.

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

Also while you are at :slight_smile: The current method that I’m using sometimes gives
a lot of these messages in the debug window:

First-chance exception in combat.exe (GDI32.DLL): 0xC0000005: Access
Violation.

This only happens sometimes and in both release and debug builds. I’m
pretty sure it’s because I’m doing something nasty with the device context.
When you create a window for OpenGL rendering you have to make sure that the
window owns the HDC (ie, the HDC is not shared as is the norm).

There is a good write up on this in more detail and instructions on how to
do this here:
http://www.starstonesoftware.com/OpenGL/whyyou.htm

Sam, it appears that posting to the news server also causes this to go to
the maillist (that is if you are reading this from your inbox).

Also, do think its reasonable to use the IMGlib to load images to use as
OpenGL textures? Is there very much overhead in creating a SDL_Surface from
this library? I would imagine it would be a straight memory buffer so it
seems pretty reasonable.

Thanks for the great library Sam/Loki.

Gerry