配置好SDL之后,就想在SDL中使用openGL,原以为会像在GLFW中那样简单的,实际上确花费了一整个下午不断查看文档,实例才搞定问题。
总结如下:
1)SDL对OpenGL进行了部分的封装,一些OpenGL的函数需要用SDL来实现,而不是像GLFW中那样直接用;
2)SDL的事件机制花费了一些时间来理解;
3)在处理OpenGL窗口大小变化的时候,需要先调用SDL_SetVideoMode才可以得到正确结果。
最终基本实现了一个opengl的小型框架,包括一些简单的事件处理。
代码:
-
-
-
-
-
-
-
-
- #include <SDL/SDL.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <stdio.h>
- #include <stdlib.h>
- SDL_Surface *screen;
- void quit( int code )
- {
- SDL_Quit( );
- exit( code );
- }
- void handleKeyEvent( SDL_keysym* keysym )
- {
- switch( keysym->sym )
- {
- case SDLK_ESCAPE:
- quit( 0 );
- break;
- case SDLK_SPACE:
- break;
- default:
- break;
- }
- }
- void resizeGL(int width,int height)
- {
- if ( height == 0 )
- {
- height = 1;
- }
- glViewport( 0, 0, (GLint)width, (GLint)height );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
- void handleEvents()
- {
- SDL_Event event;
- while( SDL_PollEvent( &event ) ) {
- switch( event.type ) {
- case SDL_KEYDOWN:
- handleKeyEvent( &event.key.keysym );
- break;
- case SDL_QUIT:
- quit( 0 );
- break;
- case SDL_VIDEORESIZE:
- screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16,
- SDL_OPENGL|SDL_RESIZABLE);
- if ( screen )
- {
- resizeGL(screen->w, screen->h);
- }
- break;
- }
- }
- }
- void initSDL(int width,int height,int bpp,int flags)
- {
- if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
- {
- fprintf( stderr, "Video initialization failed: %s\n",
- SDL_GetError( ) );
- quit( 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 );
- screen= SDL_SetVideoMode( width, height, bpp,flags);
- if(!screen )
- {
- fprintf( stderr, "Video mode set failed: %s\n",SDL_GetError( ) );
- quit( 1 );
- }
- resizeGL(screen->w, screen->h);
- SDL_WM_SetCaption( "OpenGL Test", NULL );
- }
- void renderGL()
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity( );
- glTranslatef( 0.0, 0.0, -5.0 );
- glBegin(GL_QUADS);
- glColor3f(1.0f,0.0f,0.0f);
- glVertex3f(-1.0f , -1.0f , 1.0f );
- glColor3f(0.0f,1.0f,0.0f);
- glVertex3f( 1.0f , -1.0f , 1.0f );
- glColor3f(0.0f,0.0f,1.0f);
- glVertex3f( 1.0f , 1.0f , 1.0f );
- glColor3f(1.0f,1.0f,0.0f);
- glVertex3f(-1.0f , 1.0f , 1.0f );
- glEnd();
- SDL_GL_SwapBuffers( );
- }
- void initGL( int width, int height )
- {
- float ratio = (float) width / (float) height;
- glShadeModel( GL_SMOOTH );
- glClearColor( 0, 0, 0, 0 );
- glViewport( 0, 0, width, height );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 60.0, ratio, 1.0, 100.0 );
- }
- int main( int argc, char* argv[] )
- {
- int width = 640;
- int height = 480;
- int bpp = 32;
- int flags= SDL_OPENGL|SDL_RESIZABLE;
- initSDL(width, height, bpp,flags);
- initGL( width, height );
- while(true)
- {
- handleEvents( );
- renderGL( );
- }
- return 0;
- }