SDL and OpenGL

Started by
47 comments, last by Stompy9999 19 years ago
Well, I installed version 1.2.8 of SDL and it replaced the city that used to show up with a grey blur. Here is a screenshot of what is going on:

Screenshot

Here is the source code:

#include <SDL/SDL.h>#include <gl/gl.h>/* PLEASE NOTE: the program will require SDL.dll which is located in              dev-c++'s dll directory. You have to copy it to you			  program's home directory or the path. */int main(int argc, char *argv[]){  SDL_Event event;  float theta = 0.0f;  SDL_Init(SDL_INIT_VIDEO);  SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE);  glViewport(0, 0, 600, 300);  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  glClearDepth(1.0);  glDepthFunc(GL_LESS);  glEnable(GL_DEPTH_TEST);  glShadeModel(GL_SMOOTH);  glMatrixMode(GL_PROJECTION);  glMatrixMode(GL_MODELVIEW);  int done;  for(done = 0; !done;){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glLoadIdentity();    glTranslatef(0.0f,0.0f,0.0f);    glRotatef(theta, 0.0f, 0.0f, 1.0f);    glBegin(GL_TRIANGLES);    glColor3f(1.0f, 0.0f, 0.0f);    glVertex2f(0.0f, 1.0f);    glColor3f(0.0f, 1.0f, 0.0f);    glVertex2f(0.87f, -0.5f);    glColor3f(0.0f, 0.0f, 1.0f);    glVertex2f(-0.87f, -0.5f);    glEnd();    theta += .5f;    SDL_GL_SwapBuffers();    SDL_PollEvent(&event);    if(event.key.keysym.sym == SDLK_ESCAPE)      done = 1;  }  SDL_Quit();  return(0);}


Any suggestions?
-----------------------------Play Stompy's Revenge! Now!
Advertisement
Heh. It's anybody's guess what's going on there. I tested that code and it works fine for me, if fine means a rapidly spinning red/blue/green triangle. Which it does. I'd say your problems lie elsewhere, perhaps corrupted libraries or DLLs. Try checking the various error codes possibly generated by SDL or OpenGL and see if anything funny shows up.
Quote:Original post by Stompy9999
Any suggestions?


First, let's make sure SDL can be initialized [wink]:

// First thing to do is initialze SDLif ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){    printf("Unable to init SDL: %s\n", SDL_GetError());    abort();}


See if that does anything in terms of telling you if SDL was initialized or not. If it was see if this code works for you:

#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>#include <windows.h>#include <GL/gl.h>#include <GL/glu.h> #pragma comment(lib,"SDLmain.lib")#pragma comment(lib,"SDL.lib")#pragma comment(lib,"OpenGL32.lib")#pragma comment(lib,"Glu32.lib") int main(int argc, char *argv[]){      if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )      {            printf("Unable to init SDL: %s\n", SDL_GetError());            exit(1);      }      atexit(SDL_Quit);       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 );       if ( NULL == SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL ) )            exit(1);       glMatrixMode( GL_PROJECTION );      glLoadIdentity();      glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, 1, 50);      glMatrixMode(GL_MODELVIEW);       bool done = 0;      while( !done )      {            SDL_Event event;            while ( SDL_PollEvent(&event) )            {                  if ( event.type == SDL_QUIT )                        done = 1;                   if ( event.type == SDL_KEYDOWN )                        if ( event.key.keysym.sym == SDLK_ESCAPE )                              done = 1;            }            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );            glLoadIdentity( );            /****************************************/            glTranslatef( 0, 0, -5 );            glColor3f(0.5f,0.5f,1.0f);            glBegin(GL_QUADS);                  glVertex3f(-1.0f, 1.0f, 0.0f);                  glVertex3f( 1.0f, 1.0f, 0.0f);                  glVertex3f( 1.0f,-1.0f, 0.0f);                  glVertex3f(-1.0f,-1.0f, 0.0f);            glEnd();            /****************************************/            SDL_GL_SwapBuffers();      }      SDL_Quit();      return 0;}


Sorry about the formatting, its from my web page [lol].

- Drew
OMG, it's even doing this with non-SDL OpenGL apps. I wrote a windows OpenGL app and it did the same thing!

WHAT IS GOING ON?!
-----------------------------Play Stompy's Revenge! Now!
Try reinstalling the drivers for your video card or updating to the latest.
Quote:Original post by Drew_Benton
Quote:Original post by Stompy9999
Any suggestions?


First, let's make sure SDL can be initialized [wink]:

// First thing to do is initialze SDLif ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){    printf("Unable to init SDL: %s\n", SDL_GetError());    abort();}


See if that does anything in terms of telling you if SDL was initialized or not. If it was see if this code works for you:

*** Source Snippet Removed ***

Sorry about the formatting, its from my web page [lol].

- Drew


hey nice to see some SDL OGL samples. but would you know by anychance how to do it without having to include windows.h?
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Quote:Original post by Thanhda
hey nice to see some SDL OGL samples. but would you know by anychance how to do it without having to include windows.h?


Actually yes! I will make a sample of that.

#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>#include <SDL/SDL_OpenGL.h>#pragma comment(lib,"SDLmain.lib")#pragma comment(lib,"SDL.lib")#pragma comment(lib,"OpenGL32.lib")#pragma comment(lib,"Glu32.lib")int main(int argc, char *argv[]){      if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )      {            printf("Unable to init SDL: %s\n", SDL_GetError());            exit(1);      }      atexit(SDL_Quit);      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 );	  if ( NULL == SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL ) )            exit(1);      glMatrixMode( GL_PROJECTION );      glLoadIdentity();      glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, 1, 50);      glMatrixMode(GL_MODELVIEW);	  bool done = 0;      while( !done )      {            SDL_Event event;            while ( SDL_PollEvent(&event) )            {                  if ( event.type == SDL_QUIT )                        done = 1;                   if ( event.type == SDL_KEYDOWN )                        if ( event.key.keysym.sym == SDLK_ESCAPE )                              done = 1;            }            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );            glLoadIdentity( );            /****************************************/            glTranslatef( 0, 0, -5 );            glColor3f(0.5f,0.5f,1.0f);            glBegin(GL_QUADS);                  glVertex3f(-1.0f, 1.0f, 0.0f);                  glVertex3f( 1.0f, 1.0f, 0.0f);                  glVertex3f( 1.0f,-1.0f, 0.0f);                  glVertex3f(-1.0f,-1.0f, 0.0f);            glEnd();            /****************************************/            SDL_GL_SwapBuffers();      }      SDL_Quit();      return 0;}


That's it! [wink]. I need to do some updating of my articles. I have had someone give me a list of fixes and suggestions I am working on right now. This is done in my SDL vs GLFW article but not this one, I'm slacking!

- Drew
Quote:Original post by Drew_Benton
Quote:Original post by Thanhda
hey nice to see some SDL OGL samples. but would you know by anychance how to do it without having to include windows.h?


Actually yes! I will make a sample of that.

*** Source Snippet Removed ***

That's it! [wink]. I need to do some updating of my articles. I have had someone give me a list of fixes and suggestions I am working on right now. This is done in my SDL vs GLFW article but not this one, I'm slacking!

- Drew


excellent it works, thanks!
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Guys, check out the NeHe basecode. It has ports to SDL+OpenGL, It's listed as "linux/SDL", though i'm sure it compiles fine on windows too. It just provides the basic "open window, wait to press ESC" functionality:

basecode link

Tutorial page
Thanks leiavoia! I myself have seen that and used it before, but the reason I made this version is that it is something a little bit more simpler and cleaner in showing just how to set it up. In the SDL docs they have a nasty example of doing it. So it is quite confusing [headshake].

However, I would like to point out that in some of the tutorials, the SDL code will not work properly on Windows. This is just because it was made for Linux, and while the syntax is the same for Windows, the procedures are not. The code I am referring to is just the way of setting up the window - everything else is great to use.

This topic is closed to new replies.

Advertisement