-
-
Save FyiurAmron/8858d5357bc9f2d3dd096b578cd4901e to your computer and use it in GitHub Desktop.
Very basic SDL2 OpenGL application
This file contains hidden or 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 <iostream> | |
#define _USE_MATH_DEFINES | |
#include <cmath> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
const int SCREEN_WIDTH = 800; | |
const int SCREEN_HEIGHT = 600; | |
const int OPENGL_MAJOR_VERSION = 2; | |
const int OPENGL_MINOR_VERSION = 1; | |
const SDL_GLprofile OPENGL_PROFILE = SDL_GLprofile::SDL_GL_CONTEXT_PROFILE_CORE; | |
void perspectiveGL(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar) { | |
GLdouble fW, fH; | |
fH = tan(fovY / 360 * M_PI) * zNear; | |
fW = fH * aspect; | |
glFrustum(-fW, fW, -fH, fH, zNear, zFar); | |
} | |
void Display_InitGL() { | |
glShadeModel( GL_SMOOTH); | |
glClearColor(0.2f, 0.2f, 0.2f, 0.2f); | |
glEnable( GL_DEPTH_TEST); | |
} | |
int Display_SetViewport(int width, int height) { | |
if (height == 0) { | |
height = 1; | |
} | |
GLfloat ratio = (GLfloat) width / (GLfloat) height; | |
glViewport(0, 0, (GLsizei) width, (GLsizei) height); | |
glMatrixMode( GL_PROJECTION ); | |
glLoadIdentity(); | |
perspectiveGL(45.0f, ratio, 0.1f, 100.0f); | |
glMatrixMode( GL_MODELVIEW ); | |
glLoadIdentity(); | |
return 1; | |
} | |
void Display_Render(SDL_Window* displayWindow) { | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
glLoadIdentity(); | |
glTranslatef(-1.5f, 0.0f, -6.0f); | |
glBegin( GL_TRIANGLES ); | |
glColor4f(0, 1, 0, 1); | |
glVertex3f(0.0f, 1.0f, 0.0f); /* Top */ | |
glColor4f(0, 0, 1, 1); | |
glVertex3f(-1.0f, -1.0f, 0.0f); /* Bottom Left */ | |
glColor4f(1, 0, 0, 1); | |
glVertex3f(1.0f, -1.0f, 0.0f); /* Bottom Right */ | |
glEnd(); | |
glTranslatef(3.0f, 0.0f, 0.0f); | |
glBegin( GL_QUADS ); | |
glColor4f(0, 1, 0, 1); | |
glVertex3f(-1.0f, 1.0f, 0.0f); /* Top Left */ | |
glColor4f(1, 1, 1, 1); | |
glVertex3f(1.0f, 1.0f, 0.0f); /* Top Right */ | |
glColor4f(1, 0, 0, 1); | |
glVertex3f(1.0f, -1.0f, 0.0f); /* Bottom Right */ | |
glColor4f(0, 0, 0, 1); | |
glVertex3f(-1.0f, -1.0f, 0.0f); /* Bottom Left */ | |
glEnd(); | |
SDL_GL_SwapWindow(displayWindow); | |
} | |
int main(int argc, char *argv[]) { | |
if (SDL_Init( SDL_INIT_VIDEO) < 0) { | |
std::cerr << "There was an error initing SDL2: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, OPENGL_PROFILE); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, OPENGL_MAJOR_VERSION); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, OPENGL_MINOR_VERSION); | |
SDL_Window* displayWindow = SDL_CreateWindow("SDL2 OpenGL test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, | |
SCREEN_HEIGHT, SDL_WINDOW_OPENGL); | |
if (displayWindow == NULL) { | |
std::cerr << "There was an error creating the window: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
SDL_GLContext context = SDL_GL_CreateContext(displayWindow); | |
if (context == NULL) { | |
std::cerr << "There was an error creating OpenGL context: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
const unsigned char *version = glGetString(GL_VERSION); | |
if (version == NULL) { | |
std::cerr << "There was an error with OpenGL configuration:" << std::endl; | |
return 1; | |
} | |
SDL_GL_MakeCurrent(displayWindow, context); | |
Display_InitGL(); | |
Display_SetViewport(SCREEN_WIDTH, SCREEN_HEIGHT); | |
Display_Render(displayWindow); | |
SDL_Event event; | |
while( true ) { | |
while( SDL_PollEvent(&event) ) { | |
if ( event.type == SDL_QUIT ) { | |
SDL_GL_DeleteContext(context); | |
SDL_Quit(); | |
return 0; | |
} | |
} | |
SDL_Delay(10); | |
Display_Render(displayWindow); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment