LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1339|回复: 0

如何让sdl+opengl运行在framebuffer模式下?

[复制链接]
发表于 2008-12-9 14:42:56 | 显示全部楼层 |阅读模式
我把http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01的代码下载后在kde环境下可以运行,
但是换到framebuffer模式下运行就报错Video mode set failed: OpenGL not available,请问这是为什么?

下面贴代码:
  1. /*
  2. * This code was created by Jeff Molofee '99
  3. * (ported to Linux/SDL by Ti Leggett '01)
  4. *
  5. * If you've found this code useful, please let me know.
  6. *
  7. * Visit Jeff at http://nehe.gamedev.net/
  8. *
  9. * or for port-specific comments, questions, bugreports etc.
  10. * email to leggett@eecs.tulane.edu
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <GL/gl.h>
  15. #include <GL/glu.h>
  16. #include "SDL.h"
  17. /* screen width, height, and bit depth */
  18. #define SCREEN_WIDTH  640
  19. #define SCREEN_HEIGHT 480
  20. #define SCREEN_BPP     16
  21. /* Setup our booleans */
  22. #define TRUE  1
  23. #define FALSE 0
  24. /* This is our SDL surface */
  25. SDL_Surface *surface;
  26. /* function to release/destroy our resources and restoring the old desktop */
  27. void Quit( int returnCode )
  28. {
  29.     /* clean up the window */
  30.     SDL_Quit( );
  31.     /* and exit appropriately */
  32.     exit( returnCode );
  33. }
  34. /* function to reset our viewport after a window resize */
  35. int resizeWindow( int width, int height )
  36. {
  37.     /* Height / width ration */
  38.     GLfloat ratio;
  39.     /* Protect against a divide by zero */
  40.     if ( height == 0 )
  41.         height = 1;
  42.     ratio = ( GLfloat )width / ( GLfloat )height;
  43.     /* Setup our viewport. */
  44.     glViewport( 0, 0, ( GLint )width, ( GLint )height );
  45.     /* change to the projection matrix and set our viewing volume. */
  46.     glMatrixMode( GL_PROJECTION );
  47.     glLoadIdentity( );
  48.     /* Set our perspective */
  49.     gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
  50.     /* Make sure we're chaning the model view and not the projection */
  51.     glMatrixMode( GL_MODELVIEW );
  52.     /* Reset The View */
  53.     glLoadIdentity( );
  54.     return( TRUE );
  55. }
  56. /* function to handle key press events */
  57. void handleKeyPress( SDL_keysym *keysym )
  58. {
  59.     switch ( keysym->sym )
  60.         {
  61.         case SDLK_ESCAPE:
  62.             /* ESC key was pressed */
  63.             Quit( 0 );
  64.             break;
  65.         case SDLK_F1:
  66.             /* F1 key was pressed
  67.              * this toggles fullscreen mode
  68.              */
  69.             SDL_WM_ToggleFullScreen( surface );
  70.             break;
  71.         default:
  72.             break;
  73.         }
  74.     return;
  75. }
  76. /* general OpenGL initialization function */
  77. int initGL( GLvoid )
  78. {
  79.     /* Enable smooth shading */
  80.     glShadeModel( GL_SMOOTH );
  81.     /* Set the background black */
  82.     glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  83.     /* Depth buffer setup */
  84.     glClearDepth( 1.0f );
  85.     /* Enables Depth Testing */
  86.     glEnable( GL_DEPTH_TEST );
  87.     /* The Type Of Depth Test To Do */
  88.     glDepthFunc( GL_LEQUAL );
  89.     /* Really Nice Perspective Calculations */
  90.     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
  91.     return( TRUE );
  92. }
  93. /* Here goes our drawing code */
  94. int drawGLScene( GLvoid )
  95. {
  96.     /* These are to calculate our fps */
  97.     static GLint T0     = 0;
  98.     static GLint Frames = 0;
  99.     /* Clear The Screen And The Depth Buffer */
  100.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  101.     glLoadIdentity( );
  102.     /* Draw it to the screen */
  103.     SDL_GL_SwapBuffers( );
  104.     /* Gather our frames per second */
  105.     Frames++;
  106.     {
  107.         GLint t = SDL_GetTicks();
  108.         if (t - T0 >= 5000) {
  109.             GLfloat seconds = (t - T0) / 1000.0;
  110.             GLfloat fps = Frames / seconds;
  111.             printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
  112.             T0 = t;
  113.             Frames = 0;
  114.         }
  115.     }
  116.     return( TRUE );
  117. }
  118. int main( int argc, char **argv )
  119. {
  120.     /* Flags to pass to SDL_SetVideoMode */
  121.     int videoFlags;
  122.     /* main loop variable */
  123.     int done = FALSE;
  124.     /* used to collect events */
  125.     SDL_Event event;
  126.     /* this holds some info about our display */
  127.     const SDL_VideoInfo *videoInfo;
  128.     /* whether or not the window is active */
  129.     int isActive = TRUE;
  130.     /* initialize SDL */
  131.     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  132.         {
  133.             fprintf( stderr, "Video initialization failed: %s\n",
  134.                      SDL_GetError( ) );
  135.             Quit( 1 );
  136.         }
  137.     /* Fetch the video info */
  138.     videoInfo = SDL_GetVideoInfo( );
  139.     if ( !videoInfo )
  140.         {
  141.             fprintf( stderr, "Video query failed: %s\n",
  142.                      SDL_GetError( ) );
  143.             Quit( 1 );
  144.         }
  145.     /* the flags to pass to SDL_SetVideoMode */
  146.     videoFlags  = SDL_OPENGL;          /* Enable OpenGL in SDL */
  147.     videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
  148.     videoFlags |= SDL_HWPALETTE;       /* Store the palette in hardware */
  149.     videoFlags |= SDL_RESIZABLE;       /* Enable window resizing */
  150.     /* This checks to see if surfaces can be stored in memory */
  151.     if ( videoInfo->hw_available )
  152.         videoFlags |= SDL_HWSURFACE;
  153.     else
  154.         videoFlags |= SDL_SWSURFACE;
  155.     /* This checks if hardware blits can be done */
  156.     if ( videoInfo->blit_hw )
  157.         videoFlags |= SDL_HWACCEL;
  158.     /* Sets up OpenGL double buffering */
  159.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  160.     /* get a SDL surface */
  161.     surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
  162.                                 videoFlags );
  163.     /* Verify there is a surface */
  164.     if ( !surface )
  165.         {
  166.             fprintf( stderr,  "Video mode set failed: %s\n", SDL_GetError( ) );
  167.             Quit( 1 );
  168.         }
  169.     /* initialize OpenGL */
  170.     if ( initGL( ) == FALSE )
  171.         {
  172.             fprintf( stderr, "Could not initialize OpenGL.\n" );
  173.             Quit( 1 );
  174.         }
  175.     /* Resize the initial window */
  176.     resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
  177.     /* wait for events */
  178.     while ( !done )
  179.         {
  180.             /* handle the events in the queue */
  181.             while ( SDL_PollEvent( &event ) )
  182.                 {
  183.                     switch( event.type )
  184.                         {
  185.                         case SDL_ACTIVEEVENT:
  186.                             /* Something's happend with our focus
  187.                              * If we lost focus or we are iconified, we
  188.                              * shouldn't draw the screen
  189.                              */
  190.                             if ( event.active.gain == 0 )
  191.                                 isActive = FALSE;
  192.                             else
  193.                                 isActive = TRUE;
  194.                             break;
  195.                         case SDL_VIDEORESIZE:
  196.                             /* handle resize event */
  197.                             surface = SDL_SetVideoMode( event.resize.w,
  198.                                                         event.resize.h,
  199.                                                         16, videoFlags );
  200.                             if ( !surface )
  201.                                 {
  202.                                     fprintf( stderr, "Could not get a surface after resize: %s\n", SDL_GetError( ) );
  203.                                     Quit( 1 );
  204.                                 }
  205.                             resizeWindow( event.resize.w, event.resize.h );
  206.                             break;
  207.                         case SDL_KEYDOWN:
  208.                             /* handle key presses */
  209.                             handleKeyPress( &event.key.keysym );
  210.                             break;
  211.                         case SDL_QUIT:
  212.                             /* handle quit requests */
  213.                             done = TRUE;
  214.                             break;
  215.                         default:
  216.                             break;
  217.                         }
  218.                 }
  219.             /* draw the scene */
  220.             if ( isActive )
  221.                 drawGLScene( );
  222.         }
  223.     /* clean ourselves up and exit */
  224.     Quit( 0 );
  225.     /* Should never get here */
  226.     return( 0 );
  227. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表