Last active
February 9, 2016 01:05
-
-
Save versine/d4ad1a282dc62d7bd83d to your computer and use it in GitHub Desktop.
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 <SDL2/SDL.h> | |
| #include <SDL2/SDL_opengl.h> | |
| #include <windows.h> | |
| #pragma comment(lib, "opengl32.lib") | |
| #pragma comment(lib, "SDL2.lib") | |
| #include <iostream> | |
| #include <cmath> | |
| bool running = true; | |
| constexpr int SCREEN_WIDTH = 800; | |
| constexpr int SCREEN_HEIGHT = 600; | |
| constexpr int OPENGL_MAJOR_VERSION = 2; | |
| constexpr int OPENGL_MINOR_VERSION = 1; | |
| constexpr 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); | |
| } | |
| /* function to reset our viewport after a window resize */ | |
| int setViewport(int width, int height) { | |
| /* Height / width ration */ | |
| GLfloat ratio; | |
| /* Protect against a divide by zero */ | |
| if (height == 0) { | |
| height = 1; | |
| } | |
| ratio = (GLfloat)width / (GLfloat)height; | |
| /* Setup our viewport. */ | |
| glViewport(0, 0, (GLsizei)width, (GLsizei)height); | |
| /* change to the projection matrix and set our viewing volume. */ | |
| glMatrixMode(GL_PROJECTION); | |
| glLoadIdentity(); | |
| glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); | |
| /* Set our perspective */ | |
| //perspectiveGL(45.0f, ratio, 0.1f, 100.0f); | |
| /* Make sure we're changing the model view and not the projection */ | |
| glMatrixMode(GL_MODELVIEW); | |
| /* Reset The View */ | |
| glLoadIdentity(); | |
| return 0; | |
| } | |
| int CALLBACK WinMain( | |
| HINSTANCE hInstance, | |
| HINSTANCE hPrevInstance, | |
| LPSTR lpCmdLine, | |
| int nCmdShow) { | |
| 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* window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, | |
| SCREEN_HEIGHT, SDL_WINDOW_OPENGL); | |
| if (window == nullptr) { | |
| std::cerr << "There was an error creating the window: " << SDL_GetError() << std::endl; | |
| return 1; | |
| } | |
| SDL_GLContext context = SDL_GL_CreateContext(window); | |
| if (context == nullptr) { | |
| std::cerr << "There was an error creating OpenGL context: " << SDL_GetError() << std::endl; | |
| return 1; | |
| } | |
| const unsigned char *version = glGetString(GL_VERSION); | |
| if (version == nullptr) { | |
| std::cerr << "There was an error with OpenGL configuration:" << std::endl; | |
| return 1; | |
| } | |
| SDL_GL_MakeCurrent(window, context); | |
| glShadeModel(GL_SMOOTH); | |
| glClearDepth(1.0f); | |
| glEnable(GL_DEPTH_TEST); | |
| glDepthFunc(GL_LEQUAL); | |
| glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); | |
| setViewport(SCREEN_WIDTH, SCREEN_HEIGHT); | |
| SDL_Event e; | |
| while (running) { | |
| while (SDL_PollEvent(&e)) { | |
| if (e.type == SDL_QUIT) { | |
| running = false; | |
| } | |
| } | |
| glClearColor(0.3f, 0.1f, 0.2f, 0.0f); | |
| glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); | |
| glColor4f(0.9f, 1.0f, 0.7f, 1.0); | |
| glLoadIdentity(); | |
| glBegin(GL_POLYGON); | |
| glVertex3f(20.0, 20.0, 0.0); | |
| glVertex3f(80.0, 20.0, 0.0); | |
| glVertex3f(80.0, 80.0, 0.0); | |
| glVertex3f(20.0, 80.0, 0.0); | |
| glEnd(); | |
| SDL_GL_SwapWindow(window); | |
| } | |
| SDL_GL_DeleteContext(context); | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment