Sign in to follow this  
samgj

OpenGL with SDL doesn't show anything

Recommended Posts

samgj    400

I wrote a short OpenGL/SDL program (this is the first time I've done so). The program is based off the one in the OpenGL Red Book but, nothing is on screen when I run it.

 

Here's the code:

#include <stdio.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

SDL_Surface *screen;

SDL_Event event;

int main(void)
{

 SDL_Init(SDL_INIT_VIDEO); //Initilization

 SDL_WM_SetCaption("\"Hello, World!\"", "\"Hello, World!\""); //The window caption is "Hello, World!"

 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );     //-+
 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );   // |
 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );    // |-Window attributes
 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );  // |
 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); //-+

 screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL); //Create a window

 //Drawing
  //Setup

   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(1.0, 1.0, 1.0);
   glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

  //Polygon

   glBegin(GL_POLYGON);
    glVertex2f(0.0, 0.0);
    glVertex2f(0.0, 240.0);
    glVertex2f(320.0, 240.0);
    glVertex2f(320.0, 0.0);
   glEnd();
   glFlush();

 while(1)
 {
  glFlush();
  SDL_Flip(screen); //Update the screen

 }

 return 0;
}

 

Share this post


Link to post
Share on other sites
DaDweeDow    133

SDL_Flip() is the wrong function, SDL_GL_SwapBuffers() or something is necessary for opengl, flip is for the SDL drawing.

 

edit: And I think those vertexes are going to be in the next county with those matrices loaded, but I think they will cover the screen.  

 

edit: also not sure the backbuffer is guaranteed to work that way in OpenGL, where repeated calls to flip without drawing will maintain the image.  I would just draw it once with an SDL_Delay(2000); or something just to see if it's working.  and while I'm at it:

 

 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

make the glOrtho call essentially a no op, since you LoadIdentity afterwards.  

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(.......);

 

is basically the way you always do it.  

Edited by DaDweeDow

Share this post


Link to post
Share on other sites
chebert    102

Hey,

 

 

I actually ran into this problem a while ago myself.

 

I think you need to use something like GLEW and just make a call to

glewInit();

 

but this might be just if you are using the modern graphics technique of shaders.

 

And yah, your draw code should ideally be in your while loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this