Advertisement
Guest User

SDL Project

a guest
Feb 25th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include "include/SDL2/SDL.h"
  4. #include "include/GL/glew.h"
  5. #include <thread>
  6. #include <chrono>
  7.  
  8. int main(int argc, char** argv) {
  9.  
  10.     if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
  11.         std::cerr << "Error initializing SDL.\n";
  12.         exit(1);
  13.     }
  14.  
  15.     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  16.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  17.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
  18.     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  19.     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  20.     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  21.     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  22.     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  23.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  24.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  25.  
  26.     glewExperimental = GL_TRUE;
  27.  
  28.     SDL_Window* window = SDL_CreateWindow("RenderSystem", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  29.  
  30.     if(!window) {
  31.         std::cerr << "Window creation failed.\n";
  32.         SDL_Quit();
  33.         exit(2);
  34.     }
  35.  
  36.     SDL_GLContext context = SDL_GL_CreateContext(window);
  37.  
  38.     if(!context) {
  39.         std::cerr << "OpenGL Context creation failed.\n";
  40.         SDL_DestroyWindow(window);
  41.         SDL_Quit();
  42.         exit(3);
  43.     }
  44.  
  45.     SDL_GL_MakeCurrent(window, context);
  46.  
  47.     std::fstream logFile;
  48.     logFile.open("RenderSystem.log", std::ios::in | std::ios::out | std::ios::trunc);
  49.  
  50.     if(!logFile) {
  51.         std::cerr << "Failed to open log file.\n";
  52.     }
  53.  
  54.     int GLAttribVal;
  55.     SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &GLAttribVal);
  56.     logFile << "SDL_GL_ACCELERATED_VISUAL: " << std::boolalpha << (bool)(GLAttribVal) << "\n";
  57.     SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &GLAttribVal);
  58.     logFile << "SDL_GL_CONTEXT_MAJOR_VERSION: " << GLAttribVal << "\n";
  59.     SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &GLAttribVal);
  60.     logFile << "SDL_GL_CONTEXT_MINOR_VERSION: " << GLAttribVal << "\n";;
  61.     SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &GLAttribVal);
  62.     logFile << "SDL_GL_RED_SIZE (red bits): " << GLAttribVal << "\n";
  63.     SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &GLAttribVal);
  64.     logFile << "SDL_GL_GREEN_SIZE (green bits): " << GLAttribVal << "\n";
  65.     SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &GLAttribVal);
  66.     logFile << "SDL_GL_BLUE_SIZE (blue bits): " << GLAttribVal << "\n";
  67.     SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &GLAttribVal);
  68.     logFile << "SDL_GL_ALPHA_SIZE (alpha bits): " << GLAttribVal << "\n";
  69.     SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &GLAttribVal);
  70.     logFile << "SDL_GL_DEPTH_SIZE: " << GLAttribVal << "\n";
  71.     SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &GLAttribVal);
  72.     logFile << "SDL_GL_DOUBLEBUFFER: " << std::boolalpha << (bool)(GLAttribVal) << "\n";
  73.  
  74.     int success = glewInit();
  75.  
  76.     if(success != GLEW_OK) {
  77.         SDL_GL_DeleteContext(context);
  78.         SDL_DestroyWindow(window);
  79.         SDL_Quit();
  80.         exit(4);
  81.     }
  82.  
  83.     logFile << "\n\nOpenGL version: " << glGetString(GL_VERSION) << "\n";
  84.     logFile << "OpenGL vendor: " << glGetString(GL_VENDOR) << "\n";
  85.     logFile << "OpenGL renderer: " << glGetString(GL_RENDERER) << "\n\n\n";
  86.  
  87.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  88.  
  89.     SDL_Event evt;
  90.     bool run = true;
  91.  
  92.     while(run) {
  93.  
  94.         SDL_PollEvent(&evt);
  95.  
  96.         switch(evt.type) {
  97.             case SDL_KEYDOWN:
  98.                 if(evt.key.keysym.sym == SDLK_ESCAPE) {
  99.                     run = false;
  100.                 }
  101.                 break;
  102.  
  103.             case SDL_QUIT:
  104.                 run = false;
  105.                 break;
  106.         }
  107.  
  108.         SDL_GL_SwapWindow(window);
  109.  
  110.     }
  111.  
  112.     SDL_GL_DeleteContext(context);
  113.     SDL_DestroyWindow(window);
  114.     SDL_Quit();
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement