Created
April 6, 2020 02:56
-
-
Save jnrdrgz/6ac20e91f30bdf7f58d2a02a7066134c to your computer and use it in GitHub Desktop.
Create an SDL2 OpenGL 2 context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#include <GL/glu.h> | |
#include <stdio.h> | |
//compile g++ -Wall -Wextra -g -O3 -lGL -lGLU -lSDL2 main.cpp -o opengl_sdl | |
int main(int argc, char* args[]) | |
{ | |
SDL_GLContext context; | |
const int screenWidth = 640; | |
const int screenHeight = 480; | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); | |
SDL_Window* window = SDL_CreateWindow( "SDL OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN ); | |
SDL_GLContext myopengl_context = SDL_GL_CreateContext(window); | |
if(myopengl_context == NULL) printf( "Error creating OpenGL context, SDL Error: %s\n", SDL_GetError()); | |
if(SDL_GL_SetSwapInterval(1)<0) printf( "Error setting vsync, SDL_error: %s\n", SDL_GetError()); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glClearColor(0.0f,0.0f,0.0f,1.0f); | |
bool running = true; | |
SDL_Event event; | |
while(running){ | |
while(SDL_PollEvent(&event)){ | |
if(event.type == SDL_QUIT){ | |
running = false; | |
} | |
} | |
glClear(GL_COLOR_BUFFER_BIT); | |
glBegin(GL_QUADS); | |
glVertex2f( -0.5f, -0.5f ); | |
glVertex2f( 0.5f, -0.5f ); | |
glVertex2f( 0.5f, 0.5f ); | |
glVertex2f( -0.5f, 0.5f ); | |
glEnd(); | |
SDL_GL_SwapWindow(window); | |
} | |
SDL_DestroyWindow(window); | |
window = nullptr; | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment