Advertisement
Cepheid

GL example

Mar 17th, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2. #include <GL/glew.h>
  3.  
  4. int main(int argc, char* argv[]) {
  5.     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
  6.         return 1;
  7.     }
  8.  
  9.     //  Sets up the GL stuff to use an 8/8/8/8 RGBA framebuffer, with a 24/8 Depth/Stencil buffer.
  10.     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  11.     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  12.     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  13.     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  14.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  15.     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  16.  
  17.     //  Specifies OpenGL 2.1, lets SDL decide the profile.  Profile's only used in 3.0 or above.
  18.     //  If you use 3.0 or above, and you're not familiar with GL, stick with compatibility profile.
  19.     //  GL3.0 and above are insanely difficult to use from the get-go.
  20.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  21.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
  22.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
  23.  
  24.     //  Create your window.  Note the SDL_WINDOW_OPENGL.  This is needed.
  25.     SDL_Window* window = SDL_CreateWindow("Program Name Here", SDL_WINDOWPOS_CENTERED,
  26.                                           SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
  27.     if (!window) {
  28.         SDL_Quit();
  29.         return 1;
  30.     }
  31.  
  32.     //  Create the GL context.
  33.     SDL_GLContext context = SDL_GL_CreateContext(window);
  34.     if (!context) {
  35.         SDL_DestroyWindow(window);
  36.         SDL_Quit();
  37.         return 1;
  38.     }
  39.  
  40.     //  Optional.  If you link in GLEW(A separate library, look on google), this will
  41.     //  enable all the OpenGL extensions available for you, and wrangle all the function pointers.
  42.     //  You are advised to use GLEW.  Wrangling the pointers yourself is a nightmare to do.
  43.     if (glewInit() != GLEW_OK) {
  44.         SDL_GL_DeleteContext(context);
  45.         SDL_DestroyWindow(window);
  46.         SDL_Quit();
  47.         return 1;
  48.     }
  49.  
  50.     //  Make the context current for the window.
  51.     SDL_GL_MakeCurrent(window, context);
  52.  
  53.     //  Set up matrices.  This sets up an orthographic matrix so that everything is 2D.
  54.     glMatrixMode(GL_PROJECTION);
  55.     glLoadIdentity();
  56.     glOrtho(0, 640, 0, 480, -1, 1);
  57.  
  58.     //  Basic variables for looping.
  59.     SDL_Event event;
  60.     bool isDone = false;
  61.    
  62.     //  Loop.  Check for keypresses, if escape is down, quit.
  63.     while (!isDone) {
  64.         while (SDL_PollEvent(&event) > 0) {
  65.             switch(event.type) {
  66.                 case SDL_KEYDOWN:
  67.                 if (event.key.keysym.sym == SDLK_ESCAPE) {
  68.                     isDone = true;
  69.                 }
  70.                 break;
  71.             }
  72.         }
  73.  
  74.         //  Clear the colorbuffer used by the GL window.
  75.         glClear(GL_COLOR_BUFFER_BIT);
  76.  
  77.         //  Reset the modelview matrix.  glLoadIdentity() loads the identity 4x4 matrix into the modelview matrix.
  78.         //  glMatrixMode specifies the matrix to modify.  You have GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE and GL_COLOR.
  79.         //  You'll usually use only the first two.
  80.         glMatrixMode(GL_MODELVIEW);
  81.         glLoadIdentity();
  82.  
  83.         glColor3f(1.0, 1.0, 1.0);
  84.         glBegin(GL_TRIANGLES);
  85.             glVertex2f(25, 25);
  86.             glVertex2f(50, 50);
  87.             glVertex2f(75, 25);
  88.         glEnd();
  89.  
  90.         //  Swaps the back and front buffers for the GL window.
  91.         SDL_GL_SwapWindow(window);
  92.     }
  93.  
  94.     //  Shutdown stuffs.
  95.     SDL_GL_DeleteContext(context);
  96.     SDL_DestroyWindow(window);
  97.     SDL_Quit();
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement