🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SDL & OpenGL

Started by
11 comments, last by BakaHitokiri 19 years, 11 months ago
I'm having trouble with getting something on my screen with openGL and SDL. I'm using devc++, and everything works fine, the screen just stays black :( according to my log file it does call the drawing function, which was copied from nehes tutorials.
- BkH ^^
Advertisement
Did you copy the OpenGL setup code too? You'll need to set it up as usual. Hm, you might also need to use some extra SDL functions before creating the window to set the color depth or something.
I wrote a function that does about the same thing as NeHe's initGL function, which is called at the end of the constructor of the class im writing. SDL works fine, the window is created and the caption set, the window just doesnt display anything :(
- BkH ^^
void zoku::reset() {
fprintf(log, "ZK: reseting OpenGL settings.\n");
fflush(log);
glViewport(0, 0, viewportWidth, viewportHeight); // Set our viewport
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Choose a color to clear the screen to (black)
glClearDepth(1.0); // Enable the clearing of the depth buffer
glDepthFunc(GL_LESS); // The type of depth test to do, we use a
// simple but fast one
glEnable(GL_DEPTH_TEST); // Enable depth testing
glShadeModel(GL_SMOOTH); // Type of color shading to do

glMatrixMode(GL_PROJECTION); // Select the projection matrix
glLoadIdentity(); // And reset it

// Next, we need to calculated the aspect ratio of the screen:
gluPerspective(45.0f,(GLfloat)viewportWidth/(GLfloat)viewportHeight,0.1f,100.0f);

// Select the modelview matrix
glMatrixMode(GL_MODELVIEW);
}


Is the function i was talking about
- BkH ^^
Did you add SDL_OPENGL to flags parameter, when you called SDL_SetVideoMode()?
You should also set OGL properties with SDL_GL_SetAttribute() before setting video mode. SetAttribute is needed also to create the OGL context.

If you did those properly too, i'm afraid that i can't help you. And even if you did, i should give a tip that i found out, when dealing with the same stuff: Don't trust the pixelformat information of the SDL_Surface you got from SetVideoMode. Instead, use SDL_GL_GetAttribute() to get the correct values.
Are you double-buffering? If so, are you calling SDL_GL_SwapBuffers( ) at the end of your drawing routine?
i tried both with and without the SDL_GL_SetAttribute parts, and I am calling them before im calling SDL_SetVideomode. i also swap the buffers at the end of my drawing routine. I simply dont know whats going on :(
- BkH ^^
one thing that stumped me for a while, and might be the same thing in your case...
don't call any opengl functions before establishing a rendering context.

what happened is that i tried to setup my matrix before hand for the view. and i couldn't see shit.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
So basicly, i should call the SDL_GL_SetAttribute() lines before i set the videomode and the reset() function after i did?
- BkH ^^
I thought i might be using the wrong SDL_GL_SetAttribute() calls, so heres the code:

{... more setup code

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

// Try and set the videomode
screen = SDL_SetVideoMode(640, 480, 0, videomode);

reset();

... more setup code
}

and the draw function:

void zoku::draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

// draw a triangle
glBegin(GL_POLYGON); // start drawing a polygon
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // we're done with the polygon

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units

// draw a square (quadrilateral)
glBegin(GL_QUADS); // start drawing a polygon (4 sided)
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // done with the polygon

// swap buffers to display, since we're double buffered.
SDL_GL_SwapBuffers();
}
- BkH ^^

This topic is closed to new replies.

Advertisement