SDL with OpenGL

Started by
8 comments, last by CodeTitan 19 years, 8 months ago
Recently, I found SDL. It is quite useful for everything I need in game programming. However, I do not know how to get OpenGL to work with it. Are there any tutorials on how to do this? I have already looked at Cone3D. I do not like the idea of using someone else's basecode. Does anyone know where I could learn how to set SDL up with OpenGL? Thanks in advance!
Advertisement
Using OpenGL with SDL from the SDL Doc Project.
This is great(note sarcasm). My source code gives XP an error, and asks me to send an error report. I have no idea what the problem is.
#include "SDL_OpenGL.h"#include "SDL.h"#include <stdio.h>#include <stdlib.h>int main (int argc, char **argv){	SDL_Surface *screen;	const SDL_VideoInfo* info = NULL;	int width	 = 0;    int height	 = 0;	int bpp		 = 0;	int flags	 = 0;		width = 640;    height = 480;    bpp = info->vfmt->BitsPerPixel;	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 );	flags = SDL_OPENGL | SDL_FULLSCREEN;    /* Initialize the SDL library */    if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {        fprintf(stderr,                "Couldn't initialize SDL: %s\n", SDL_GetError());        exit(1);    }    /* Clean up on exit */    atexit(SDL_Quit);        screen = SDL_SetVideoMode(640, 480, 8, SDL_OPENGL);    if ( screen == NULL ) {        fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",                        SDL_GetError());        exit(1);    }	if( !info ) {        /* This should probably never happen. */        fprintf( stderr, "Video query failed: %s\n",             SDL_GetError( ) );        exit( 1 );    }	SDL_Event event;     /* Poll for events. SDL_PollEvent() returns 0 when there are no  */  /* more events on the event queue, our while loop will exit when */  /* that occurs.                                                  */  while( SDL_PollEvent( &event ) ){    /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */    switch( event.type ){      case SDL_KEYDOWN:        printf( "Key press detected\n" );        break;      case SDL_KEYUP:        printf( "Key release detected\n" );        break;      default:        break;    }  }	return 0;}
There is always Mine, even though I never quite finished it, but should get you started.

You can use it as a replacement for NeHe's first 4 lessons, and fill it with the meat in all his other tutorials to get cross compatible versions, or in most cases at least.
You set info to NULL then try to do a double dereference on it?

const SDL_VideoInfo* info = NULL;
.
.
.
bpp = info->vfmt->BitsPerPixel;

I'm pretty sure that you'd be better of replacing that assignment to NULL with this instead:

SDL_VideoInfo* info = SDL_GetVideoInfo();

You should probably also initialize the SDL subsystems with SDL_Init() *before* you start making calls to the subsystem, i.e. with SDL_GL_SetAttribute.

There should be plenty of tutorials out there on setting up SDL to work with OpenGL, and if all else fails there's always the documentation.
We are the music makers and we are the dreamers of the dreams. - WonkaAsking Smart Questions | BookPool
Quote:Original post by CodeTitan
This is great(note sarcasm). My source code gives XP an error, and asks me to send an error report. I have no idea what the problem is. *** Source Snippet Removed ***


You should not be needing or using a SDL_Surface at all, nor can you use any of the SDL 2D video functions, instead you'll be using OpenGL.
That Aeongames website helped a lot! It gave the code exactly how I wanted it, the explanations were clear, and the code compiled (a few semicolons here and there missed)! Now that initializing is done, I have rendering errors... I can't seem to draw OpenGL primitives! Here is my rendering function:
int glRender(void){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer    glLoadIdentity(); // reset current model view matrix	glColor3f(1.0, 1.0, 0.0);	glBegin(GL_TRIANGLES);		glVertex3f(1.0, 0.0, 1.0);		glVertex3f(1.0, 0.0, 0.0);		glVertex3f(0.0, 1.0, 0.0);	glEnd();    SDL_GL_SwapBuffers(); // finally we swap our buffers	return 0;}

Instead of displaying the triangle, it displays a black screen!
Quote:Original post by CodeTitan
That Aeongames website helped a lot! It gave the code exactly how I wanted it, the explanations were clear, and the code compiled (a few semicolons here and there missed)! Now that initializing is done, I have rendering errors... I can't seem to draw OpenGL primitives! Here is my rendering function:
*** Source Snippet Removed ***
Instead of displaying the triangle, it displays a black screen!


Thanks!, I wrote that you know [smile] if you can point me to exactly where the missing semi-colons are I will fix it.

it seems like are missing a glTranslate call right after glLoadIdentity, try this:

int glRender(void){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer        glLoadIdentity(); // reset current model view matrix	glColor3f(1.0, 1.0, 0.0);        glTranslatef(0.0f,0.0f,-6.0f);//<--------Move back since you are at exactly the origin (0,0,0)	glBegin(GL_TRIANGLES);        // also modified your triangle since it was a bit odd	glVertex3f( 0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 0.0f);	glVertex3f( 1.0f,-1.0f, 0.0f);						glEnd();    SDL_GL_SwapBuffers(); // finally we swap our buffers	return 0;}
Well, you could always use ClanLib, where OpenGL is its primary drawing target, basically.

<a href="http://www.clanlib.org>click

It's mind numbingly easy to get started with, granted you get through building all the libraries, and what not (they have precompiled windows binaries for VC6 and above, I think, though).

Anyway, I prefer it over SDL, for some reason.
"Creativity requires you to murder your children." - Chris Crawford
Yeah, I did miss that translation call. What a simple mistake. Thanks!

This topic is closed to new replies.

Advertisement