[SDL] sdl and opengl newb problem (sdlopenglblit)

geinitz at yahoo.com geinitz at yahoo.com
Tue Apr 13 00:49:56 PDT 2004


hi, i'm sorry, i've read over the archives the last few days and even played 
with the testgl program in the test/ directory of the sdl source package.  
yet i still have not been able to find a solution to my problem.  i am using 
the SDL_OPENGLBLIT mode and i am aware that this is deprecated and should not 
be used any longer, but for now i like the convenience of it as opposed to 
texturing each image i want to use.  i am working on a simple 2D game that 
mostly just blits some images to the screen but then occasionally i want to 
draw some opengl stuff on top of those images.  but no matter what i've tried 
my opengl does not appear on the screen.  everything compiles and runs fine, 
but i just never see anything.  i think i may just being doing some simple 
little thing wrong so i was hoping someone might be able to point out my 
mistake since i cannot find it.  the frustrating thing is i used to do a 
similar thing with the glut but i wanted to start using SDL so that i could 
use SDL_Image and use several different types of graphics file formats.  
anyway, here is the relevant code and thanks in advance for any help.  



// SDL AND OPENGL SETUP

  // initialize SDL
  if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    shutdown("sdl init failed");

  // fetch video info
  videoInfo = SDL_GetVideoInfo( );
  if(!videoInfo)
    shutdown("video query failed");

  // flags to pass to SDL_SetVideoMode
  videoFlags  = SDL_OPENGLBLIT;          // openGL in SDL
  videoFlags |= SDL_GL_DOUBLEBUFFER;     // double buffering
  // check to see if surfaces can be stored in memory
  (videoInfo->hw_available) ? videoFlags |= SDL_HWSURFACE : videoFlags |= 
SDL_SWSURFACE;
  // check if hardware blits can be done
  if(videoInfo->blit_hw)
    videoFlags |= SDL_HWACCEL;
  // set up OpenGL double buffering
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  // get a SDL surface
  if(mainSurface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, 
videoFlags) == NULL) 
    shutdown("set video mode failed");

  // Set the background black
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

  // Depth buffer setup
  glClearDepth(1.0f);

  // Set Line Antialiasing
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

  // Enable Blending
  glEnable( GL_BLEND );
    
  // Type Of Blending To Use
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  
  // Setup our viewport
  glViewport(0, 0, (GLint)SCREEN_WIDTH, (GLint)SCREEN_HEIGHT);

  // change to the projection matrix and set our viewing volume
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  // set ortho view
  glOrtho(0.0f, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, -1.0f, 1.0f);

  // make sure we're changing model view and not projection
  glMatrixMode(GL_MODELVIEW);

  // reset view
  glLoadIdentity();



// DISPLAY FUNCTION

  SDL_BlitSurface(background, NULL, mainSurface, NULL);
  SDL_BlitSurface(gameInfoBox, NULL, mainSurface, &gameInfoBoxPosition);
  // there are also a bunch of other blits for other images too, 
  // this is all working great though

  if(SDL_LockSurface(mainSurface) != 0)  //tried it without this also
    shutdown("sdl surface was not locked\n");

  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glLineWidth(5.0);

  // draw a simple, big red line across the screen, just to test for now
  glColor3ub(255,0,0);    
  glBegin(GL_LINE_STRIP);
    glVertex2i(0,0);
    glVertex2i(SCREEN_WIDTH,SCREEN_HEIGHT);
  glEnd();
        
  SDL_UnlockSurface(mainSurface);
      
  SDL_Flip(mainSurface);
  SDL_GL_SwapBuffers();



aaaagh, this is driving me crazy, thanks again for any help or pointers.
-steve




More information about the SDL mailing list