Wiki Page Content

Differences between revisions 9 and 10
Revision 9 as of 2012-05-29 07:42:27
Size: 2332
Editor: ChrisBush
Comment: Code example
Revision 10 as of 2012-06-06 08:17:59
Size: 2519
Editor: ChrisBush
Comment: General remark about OpenGL extension loading
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Line 13: Line 12:
== Function Parameters ==
||'''window''' ||the window to associate with the context ||
Line 14: Line 15:
== Function Parameters ==
||'''window'''||the window to associate with the context||
Line 27: Line 26:
  
Line 32: Line 31:
  
Line 40: Line 39:
 
Line 42: Line 41:
  SDL_Event e;    SDL_Event e;
Line 44: Line 43:
    
Line 46: Line 45:
    
Line 48: Line 47:
    glClear(GL_COLOR_BUFFER_BIT);      glClear(GL_COLOR_BUFFER_BIT);
Line 50: Line 49:
    glBegin(GL_LINE_LOOP);          glBegin(GL_LINE_LOOP);
Line 55: Line 54:
         
Line 58: Line 57:
    SDL_GL_SwapWindow(window);         SDL_GL_SwapWindow(window);
Line 60: Line 59:
    SDL_Delay(10);                
    
  } 
  
  
    SDL_Delay(10);

  }
Line 66: Line 65:
  SDL_GL_DeleteContext(GLContext);  
  
  // Done! Close the window, clean-up and exit the program. 
  SDL_GL_DeleteContext(GLContext);

  // Close the window, clean-up and exit the program.
Line 72: Line 71:
  
Line 75: Line 74:
Line 77: Line 75:
''You can add useful comments here'' Windows users new to OpenGL should note that, for historical reasons, GL functions added after OpenGL version 1.1 are not available by default. Those functions must be loaded at run-time, either with an OpenGL extension-handling library or with [[SDL_GL_GetProcAddress]] and its related functions.
Line 80: Line 78:
 .[[SDL_GL_DeleteContext]]
 .[[SDL_GL_MakeCurrent]]
 . [[SDL_GL_DeleteContext]]
 . [[SDL_GL_MakeCurrent]]
Line 84: Line 82:
[[CategoryAPI]], [[CategoryVideo]] [[CategoryAPI]], CategoryVideo

SDL_GL_CreateContext

Use this function to create an OpenGL context for use with an OpenGL window, and make it current.

Syntax

SDL_GLContext SDL_GL_CreateContext(SDL_Window* window)

Function Parameters

window

the window to associate with the context

Return Value

Returns the OpenGL context associated with window.

Code Examples

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
int main(int argc, char* argv[]){

  SDL_Init(SDL_INIT_VIDEO); // Init SDL2

  // Create a window. Window mode MUST include SDL_WINDOW_OPENGL for use with OpenGL.
  SDL_Window *window = SDL_CreateWindow(
    "SDL2/OpenGL Demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE
  );

  // Create an OpenGL context associated with the window.
  SDL_GLContext GLContext = SDL_GL_CreateContext(window);

  // Now, regular OpenGL functions ...
  glMatrixMode(GL_PROJECTION|GL_MODELVIEW);
  glLoadIdentity();
  glOrtho(-200,200,200,-200,0,1);

  // ... can be used alongside SDL2.
  SDL_Event e;
  while(e.type!=SDL_QUIT){  // Enter main loop.

    SDL_PollEvent(&e);      // Check for events.

    glClearColor(0,0,0,1);          // Draw with OpenGL.
    glClear(GL_COLOR_BUFFER_BIT);
    glRotatef(1.0,0.0,0.0,1.0);
    glBegin(GL_LINE_LOOP);
      glColor3f(1.0,0.0,0.0);   glVertex2f(   0,100);
      glColor3f(0.0,1.0,0.0);   glVertex2f( 100,-50);
      glColor3f(1.0,0.0,1.0);   glVertex2f(-100,-50);
    glEnd();


    // Swap the window/buffer to display the result.
    SDL_GL_SwapWindow(window);
    // Pause briefly before moving on to the next cycle.
    SDL_Delay(10);

  }


  // Once finished with OpenGL functions, the SDL_GLContext can be deleted.
  SDL_GL_DeleteContext(GLContext);

  // Close the window, clean-up and exit the program.
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;

}

Remarks

Windows users new to OpenGL should note that, for historical reasons, GL functions added after OpenGL version 1.1 are not available by default. Those functions must be loaded at run-time, either with an OpenGL extension-handling library or with SDL_GL_GetProcAddress and its related functions.


CategoryAPI, CategoryVideo

None: SDL_GL_CreateContext (last edited 2018-04-05 21:55:20 by ChrisBush)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit