SHARE
TWEET

Untitled

a guest Feb 28th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <SDL.h>
  4. #define GLM_FORCE_RADIANS 1
  5. #include <glm/glm.hpp>
  6. #include <glm/gtc/matrix_transform.hpp>
  7. #include <glm/gtc/type_ptr.hpp>
  8.  
  9.  
  10. #include "glad/glad.h"
  11.  
  12. static const int SCREEN_FULLSCREEN = 1;
  13. static const int SCREEN_WIDTH  = 960;
  14. static const int SCREEN_HEIGHT = 540;
  15. static SDL_Window *window = NULL;
  16. static SDL_GLContext maincontext;
  17.  
  18. static void sdl_die(const char * message) {
  19.   fprintf(stderr, "%s: %s\n", message, SDL_GetError());
  20.   exit(2);
  21. }
  22.  
  23. void init_screen(const char * caption) {
  24.   // Initialize SDL
  25.   if (SDL_Init(SDL_INIT_VIDEO) < 0)
  26.   sdl_die("Couldn't initialize SDL");
  27.   atexit (SDL_Quit);
  28.   SDL_GL_LoadLibrary(NULL); // Default OpenGL is fine.
  29.  
  30.   // Request an OpenGL 4.5 context (should be core)
  31.   SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  32.   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  33.   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
  34.   // Also request a depth buffer
  35.   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  36.   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  37.  
  38.   // Create the window
  39.   if (SCREEN_FULLSCREEN) {
  40.     window = SDL_CreateWindow(
  41.       caption,
  42.       SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  43.       0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_OPENGL
  44.     );
  45.   } else {
  46.     window = SDL_CreateWindow(
  47.       caption,
  48.       SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  49.       SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL
  50.     );
  51.   }
  52.   if (window == NULL) sdl_die("Couldn't set video mode");
  53.  
  54.   maincontext = SDL_GL_CreateContext(window);
  55.   if (maincontext == NULL)
  56.     sdl_die("Failed to create OpenGL context");
  57.  
  58.   // Check OpenGL properties
  59.   printf("OpenGL loaded\n");
  60.   gladLoadGLLoader(SDL_GL_GetProcAddress);
  61.   printf("Vendor:   %s\n", glGetString(GL_VENDOR));
  62.   printf("Renderer: %s\n", glGetString(GL_RENDERER));
  63.   printf("Version:  %s\n", glGetString(GL_VERSION));
  64.  
  65.   // Use v-sync
  66.   SDL_GL_SetSwapInterval(1);
  67.  
  68.   // Disable depth test and face culling.
  69.   glDisable(GL_DEPTH_TEST);
  70.   glDisable(GL_CULL_FACE);
  71.  
  72.   int w,h;
  73.   SDL_GetWindowSize(window, &w, &h);
  74.   glViewport(0, 0, w, h);
  75.   glClearColor(0.0f, 0.5f, 1.0f, 0.0f);
  76. }
  77.  
  78. #undef main
  79. int main() {
  80.   init_screen("OpenGL 4.5");
  81.   SDL_Event event;
  82.  
  83.   bool quit = false;
  84.   while (!quit) {
  85.     SDL_GL_SwapWindow(window);
  86.     while (SDL_PollEvent(&event)) {
  87.       if (event.type == SDL_QUIT) {
  88.         quit = true;
  89.       }
  90.     }
  91.   }
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top