SDL入门学习续-在SDL中使用OpenGL

夏冰 2012-12-01

配置好SDL之后,就想在SDL中使用openGL,原以为会像在GLFW中那样简单的,实际上确花费了一整个下午不断查看文档,实例才搞定问题。

总结如下:

1)SDL对OpenGL进行了部分的封装,一些OpenGL的函数需要用SDL来实现,而不是像GLFW中那样直接用;

2)SDL的事件机制花费了一些时间来理解;

3)在处理OpenGL窗口大小变化的时候,需要先调用SDL_SetVideoMode才可以得到正确结果。

最终基本实现了一个opengl的小型框架,包括一些简单的事件处理。

代码:

  1. /*****************************************************************************
  2. Copyright: 2012, ustc All rights reserved.
  3. contact:[email protected]
  4. File name: main.c
  5. Description:using opengl in SDL.
  6. Author:Silang Quan
  7. Version: 1.0
  8. Date: 2012.12.01
  9. *****************************************************************************/
  10. #include <SDL/SDL.h>
  11. #include <GL/gl.h>
  12. #include <GL/glu.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. SDL_Surface *screen;
  16. void quit( int code )
  17. {
  18. SDL_Quit( );
  19. /* Exit program. */
  20. exit( code );
  21. }
  22. void handleKeyEvent( SDL_keysym* keysym )
  23. {
  24. switch( keysym->sym )
  25. {
  26. case SDLK_ESCAPE:
  27. quit( 0 );
  28. break;
  29. case SDLK_SPACE:
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. void resizeGL(int width,int height)
  36. {
  37. if ( height == 0 )
  38. {
  39. height = 1;
  40. }
  41. //Reset View
  42. glViewport( 0, 0, (GLint)width, (GLint)height );
  43. //Choose the Matrix mode
  44. glMatrixMode( GL_PROJECTION );
  45. //reset projection
  46. glLoadIdentity();
  47. //set perspection
  48. gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
  49. //choose Matrix mode
  50. glMatrixMode( GL_MODELVIEW );
  51. glLoadIdentity();
  52. }
  53. void handleEvents()
  54. {
  55. // Our SDL event placeholder.
  56. SDL_Event event;
  57. //Grab all the events off the queue.
  58. while( SDL_PollEvent( &event ) ) {
  59. switch( event.type ) {
  60. case SDL_KEYDOWN:
  61. // Handle key Event
  62. handleKeyEvent( &event.key.keysym );
  63. break;
  64. case SDL_QUIT:
  65. // Handle quit requests (like Ctrl-c).
  66. quit( 0 );
  67. break;
  68. case SDL_VIDEORESIZE:
  69. //Handle resize event
  70. screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16,
  71. SDL_OPENGL|SDL_RESIZABLE);
  72. if ( screen )
  73. {
  74. resizeGL(screen->w, screen->h);
  75. }
  76. break;
  77. }
  78. }
  79. }
  80. void initSDL(int width,int height,int bpp,int flags)
  81. {
  82. // First, initialize SDL's video subsystem.
  83. if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  84. {
  85. fprintf( stderr, "Video initialization failed: %s\n",
  86. SDL_GetError( ) );
  87. quit( 1 );
  88. }
  89. atexit(SDL_Quit);
  90. //Set some Attribute of OpenGL in SDL
  91. SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
  92. SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
  93. SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  94. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  95. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  96. //Set the video mode
  97. screen= SDL_SetVideoMode( width, height, bpp,flags);
  98. if(!screen )
  99. {
  100. fprintf( stderr, "Video mode set failed: %s\n",SDL_GetError( ) );
  101. quit( 1 );
  102. }
  103. resizeGL(screen->w, screen->h);
  104. //Set caption
  105. SDL_WM_SetCaption( "OpenGL Test", NULL );
  106. }
  107. void renderGL()
  108. {
  109. // Clear the color and depth buffers.
  110. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  111. // We don't want to modify the projection matrix. */
  112. glMatrixMode( GL_MODELVIEW );
  113. glLoadIdentity( );
  114. // Move down the z-axis.
  115. glTranslatef( 0.0, 0.0, -5.0 );
  116. //Draw a square
  117. glBegin(GL_QUADS);
  118. glColor3f(1.0f,0.0f,0.0f);
  119. glVertex3f(-1.0f , -1.0f , 1.0f );
  120. glColor3f(0.0f,1.0f,0.0f);
  121. glVertex3f( 1.0f , -1.0f , 1.0f );
  122. glColor3f(0.0f,0.0f,1.0f);
  123. glVertex3f( 1.0f , 1.0f , 1.0f );
  124. glColor3f(1.0f,1.0f,0.0f);
  125. glVertex3f(-1.0f , 1.0f , 1.0f );
  126. glEnd();
  127. SDL_GL_SwapBuffers( );
  128. }
  129. void initGL( int width, int height )
  130. {
  131. float ratio = (float) width / (float) height;
  132. // Our shading model--Gouraud (smooth).
  133. glShadeModel( GL_SMOOTH );
  134. // Set the clear color.
  135. glClearColor( 0, 0, 0, 0 );
  136. // Setup our viewport.
  137. glViewport( 0, 0, width, height );
  138. //Change to the projection matrix and set our viewing volume.
  139. glMatrixMode( GL_PROJECTION );
  140. glLoadIdentity();
  141. gluPerspective( 60.0, ratio, 1.0, 100.0 );
  142. }
  143. int main( int argc, char* argv[] )
  144. {
  145. // Dimensions of our window.
  146. int width = 640;
  147. int height = 480;
  148. // Color depth in bits of our window.
  149. int bpp = 32;
  150. int flags= SDL_OPENGL|SDL_RESIZABLE;
  151. //Set the SDL
  152. initSDL(width, height, bpp,flags);
  153. //Set the OpenGL
  154. initGL( width, height );
  155. //main loop
  156. while(true)
  157. {
  158. /* Process incoming events. */
  159. handleEvents( );
  160. /* Draw the screen. */
  161. renderGL( );
  162. }
  163. return 0;
  164. }

SDL入门学习续-在SDL中使用OpenGL

相关推荐