如何编译:
例子中的代码同时引用了SDL和GL库,需要通过 sdl-config --lib --cflags 或-lSDL 加入SDL库 , -lGL 加入GL库
引用:
引用: http://pbeblog.wordpress.com/2009/02/16/compile-sdlopengl-applications-using-g/
g++ sourcefile.cpp -o output -lSDL -lGL 或者
g++ sourcefile.cpp -o output `sdl-config --libs --cflags` -lGL -lGLU
Sample code:
引用:
这段代码是画一个圆。在ubuntu下运行成功
- // g++ me.cpp -lSDL -lGL
- #include <math.h>
- #include <GL/gl.h>
- #include <SDL/SDL.h>
- #define RAD2DEG 0.0174532925
- void init(int width, int height)
- {
- // Initialize SDL
- const SDL_VideoInfo* info = NULL;
- SDL_Init(SDL_INIT_VIDEO);
- info = SDL_GetVideoInfo();
- int vidFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
- if (info->hw_available) {vidFlags |= SDL_HWSURFACE;}
- else {vidFlags |= SDL_SWSURFACE;}
- SDL_SetVideoMode(width, height, info->vfmt->BitsPerPixel, vidFlags);
- // Initialize OpenGL for 2d
- glViewport( 0, 0, width, height );
- glMatrixMode( GL_PROJECTION );
- glOrtho( 0, width, height, 0, -1, 1 );
- glMatrixMode( GL_MODELVIEW );
- glDisable(GL_DEPTH_TEST);
- }
- void glCircle(GLint x, GLint y, GLint r)
- {
- float step = 360.0 / (2.0*M_PI*r);
- glPushMatrix();
- glTranslatef(x, y, 0);
- glBegin(GL_TRIANGLE_FAN);
- glVertex2i(0, 0);
- for(float i=0; i<360; i+=step)
- {
- glVertex2i((GLint)(cos(RAD2DEG*i)*r), (GLint)(sin(RAD2DEG*i)*r));
- }
- glEnd();
- glPopMatrix();
- }
- int main()
- {
- init(640, 480);
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- glCircle(200, 200, 100);
- SDL_GL_SwapBuffers();
- SDL_Delay(2000);
- }
阅读(6156) | 评论(0) | 转发(0) |