Wiki Page Content

Differences between revisions 16 and 17
Revision 16 as of 2013-07-06 08:33:11
Size: 3142
Comment: Corrected includes.
Revision 17 as of 2013-08-26 05:28:16
Size: 1565
Editor: RyanGordon
Comment: Cut down code example.
Deletions are marked like this. Additions are marked like this.
Line 21: Line 21:
#include "SDL.h"
#include "SDL_opengl.h"
Line 24: Line 22:
int main(int argc, char* argv[]){ // Window mode MUST include SDL_WINDOW_OPENGL for use with OpenGL.
SDL_Window *window = SDL_CreateWindow(
    "SDL2/OpenGL Demo", 0, 0, 640, 480,
    SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  
// Create an OpenGL context associated with the window.
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
Line 26: Line 30:
  SDL_Init(SDL_INIT_VIDEO); // Init SDL2 (you should check for errors)
  
  // 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 you can make GL calls.
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
Line 36: Line 35:
  // Now, regular OpenGL functions ...
  glMatrixMode(GL_PROJECTION|GL_MODELVIEW);
  glLoadIdentity();
  glOrtho(-320,320,240,-240,0,1);
 
  // ... can be used alongside SDL2.
  float x = 0.0, y = 30.0;

  SDL_Event event;
  Uint8 done = 0;
  while(!done) // Enter main loop.
  {
    while(SDL_PollEvent(&event)) // Check for events.
    {
        if(event.type == SDL_QUIT || event.type == SDL_QUIT)
            done = 1;
    }

    // Draw:
    glClearColor(0,0,0,1); // Use OpenGL commands, see the OpenGL reference.
    glClear(GL_COLOR_BUFFER_BIT); // clearing screen
    glRotatef(10.0,0.0,0.0,1.0); // rotating everything
    // Note that the glBegin() ... glEnd() OpenGL style used below is actually
    // obsolete, but it will do for example purposes. For more information, see
    // SDL_GL_GetProcAddress() or find an OpenGL extension loading library.
    glBegin(GL_TRIANGLES); // drawing a multicolored triangle
      glColor3f(1.0,0.0,0.0); glVertex2f(x, y+90.0);
      glColor3f(0.0,1.0,0.0); glVertex2f(x+90.0, y-90.0);
      glColor3f(0.0,0.0,1.0); glVertex2f(x-90.0, y-90.0);
    glEnd();
    
    SDL_GL_SwapWindow(window); // Swap the window/buffer to display the result.
    SDL_Delay(10); // Pause briefly before moving on to the next cycle.
    
  }

  // Once finished with OpenGL functions, the SDL_GLContext can be deleted.
  SDL_GL_DeleteContext(glcontext);
  
  // Done! Close the window, clean-up and exit the program.
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
  
}
// Once finished with OpenGL functions, the SDL_GLContext can be deleted.
SDL_GL_DeleteContext(glcontext);

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 or NULL on error; call SDL_GetError() for more details.

Code Examples

// Window mode MUST include SDL_WINDOW_OPENGL for use with OpenGL.
SDL_Window *window = SDL_CreateWindow(
    "SDL2/OpenGL Demo", 0, 0, 640, 480, 
    SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  
// Create an OpenGL context associated with the window.
SDL_GLContext glcontext = SDL_GL_CreateContext(window);

// now you can make GL calls.
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);

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

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