Jump to content
  • Advertisement
Sign in to follow this  
CodeTitan

SDL with OpenGL

This topic is 5078 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

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!

Share this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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;
}




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!