Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL2/SDL.h>
- #include <GL/glew.h>
- int main(int argc, char* argv[]) {
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
- return 1;
- }
- // Sets up the GL stuff to use an 8/8/8/8 RGBA framebuffer, with a 24/8 Depth/Stencil buffer.
- SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- // Specifies OpenGL 2.1, lets SDL decide the profile. Profile's only used in 3.0 or above.
- // If you use 3.0 or above, and you're not familiar with GL, stick with compatibility profile.
- // GL3.0 and above are insanely difficult to use from the get-go.
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
- // Create your window. Note the SDL_WINDOW_OPENGL. This is needed.
- SDL_Window* window = SDL_CreateWindow("Program Name Here", SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
- if (!window) {
- SDL_Quit();
- return 1;
- }
- // Create the GL context.
- SDL_GLContext context = SDL_GL_CreateContext(window);
- if (!context) {
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 1;
- }
- // Optional. If you link in GLEW(A separate library, look on google), this will
- // enable all the OpenGL extensions available for you, and wrangle all the function pointers.
- // You are advised to use GLEW. Wrangling the pointers yourself is a nightmare to do.
- if (glewInit() != GLEW_OK) {
- SDL_GL_DeleteContext(context);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 1;
- }
- // Make the context current for the window.
- SDL_GL_MakeCurrent(window, context);
- // Set up matrices. This sets up an orthographic matrix so that everything is 2D.
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 640, 0, 480, -1, 1);
- // Basic variables for looping.
- SDL_Event event;
- bool isDone = false;
- // Loop. Check for keypresses, if escape is down, quit.
- while (!isDone) {
- while (SDL_PollEvent(&event) > 0) {
- switch(event.type) {
- case SDL_KEYDOWN:
- if (event.key.keysym.sym == SDLK_ESCAPE) {
- isDone = true;
- }
- break;
- }
- }
- // Clear the colorbuffer used by the GL window.
- glClear(GL_COLOR_BUFFER_BIT);
- // Reset the modelview matrix. glLoadIdentity() loads the identity 4x4 matrix into the modelview matrix.
- // glMatrixMode specifies the matrix to modify. You have GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE and GL_COLOR.
- // You'll usually use only the first two.
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_TRIANGLES);
- glVertex2f(25, 25);
- glVertex2f(50, 50);
- glVertex2f(75, 25);
- glEnd();
- // Swaps the back and front buffers for the GL window.
- SDL_GL_SwapWindow(window);
- }
- // Shutdown stuffs.
- SDL_GL_DeleteContext(context);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement