SDL2中使用OpenGL绘图
Posted CodeMIRACLE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDL2中使用OpenGL绘图相关的知识,希望对你有一定的参考价值。
原文地址http://www.zhieng.com/sdl2-with-opengl/
1 // OpenGL headers 2 #define GLEW_STATIC 3 #include <GL/glew.h> 4 #include <GL/glu.h> 5 #include <GL/gl.h> 6 7 // SDL headers 8 #include <SDL_main.h> 9 #include <SDL.h> 10 #include <SDL_opengl.h> 11 12 bool quit; 13 14 SDL_Window* window; 15 SDL_GLContext glContext; 16 SDL_Event sdlEvent; 17 18 int main(int argc, char *argv[]) 19 { 20 quit = false; 21 22 //Use OpenGL 3.1 core 23 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 24 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); 25 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 26 27 // Initialize video subsystem 28 if(SDL_Init(SDL_INIT_VIDEO) < 0) 29 { 30 // Display error message 31 printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); 32 return false; 33 } 34 else 35 { 36 // Create window 37 window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN ); 38 if( window == NULL ) 39 { 40 // Display error message 41 printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); 42 return false; 43 } 44 else 45 { 46 // Create OpenGL context 47 glContext = SDL_GL_CreateContext(window); 48 49 if( glContext == NULL ) 50 { 51 // Display error message 52 printf( "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() ); 53 return false; 54 } 55 else 56 { 57 // Initialize glew 58 glewInit(); 59 } 60 } 61 } 62 63 // Game loop 64 while (!quit) 65 { 66 while(SDL_PollEvent(&sdlEvent) != 0) 67 { 68 // Esc button is pressed 69 if(sdlEvent.type == SDL_QUIT) 70 { 71 quit = true; 72 } 73 } 74 75 // Set background color as cornflower blue 76 glClearColor(0.39f, 0.58f, 0.93f, 1.f); 77 // Clear color buffer 78 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 79 // Update window with OpenGL rendering 80 SDL_GL_SwapWindow(window); 81 } 82 83 //Destroy window 84 SDL_DestroyWindow(window); 85 window = NULL; 86 87 //Quit SDL subsystems 88 SDL_Quit(); 89 90 return 0; 91 }
以上是关于SDL2中使用OpenGL绘图的主要内容,如果未能解决你的问题,请参考以下文章