Advertisement
agmike

SDL Init 2

Oct 4th, 2011
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. // init_sdl.h
  2. #ifndef INIT_SDL_H
  3. #define INIT_SDL_H
  4.  
  5. /* Include windows.h properly on Windows */
  6. #if defined(WIN32) || defined(_WINDOWS)
  7. #  define WIN32_LEAN_AND_MEAN
  8. #  define NOMINMAX
  9. #  include <windows.h>
  10. #endif
  11.  
  12. /* SDL */
  13. #include <SDL.h>
  14. #include <SDL_opengl.h>
  15. #ifndef INIT_SDL_MAIN
  16. #  undef main
  17. #endif
  18.  
  19. #endif
  20.  
  21.  
  22.  
  23. // main.cpp
  24. #define INIT_SDL_MAIN
  25. #include "init_sdl.h"
  26.  
  27. #include <SDL_events.h>
  28.  
  29. #include <iostream>
  30.  
  31.  
  32. static void criticalError(const char* title, const char* text)
  33. {
  34.     std::cerr << "Critical error: " << title << std::endl << text << std::endl;
  35.     exit(1);
  36. }
  37.  
  38.  
  39. struct Time
  40. {
  41.     double t;
  42.     double dt;
  43. };
  44.  
  45. void init()
  46. {
  47. }
  48.  
  49. void resize(int w, int h)
  50. {
  51.     glViewport(0, 0, w, h);
  52. }
  53.  
  54. void update(Time time)
  55. {
  56. }
  57.  
  58. void render(Time time)
  59. {
  60.     glClearColor(0.3f, 0.6f, 0.9f, 1.0f);
  61.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  62. }
  63.  
  64. void mainLoop(SDL_Window* win)
  65. {
  66.     uint64_t counterOld = SDL_GetPerformanceCounter();
  67.     Time time = { 0.0, 0.0};
  68.  
  69.     int winWidth;
  70.     int winHeight;
  71.     SDL_GetWindowSize(win, &winWidth, &winHeight);
  72.    
  73.     init();
  74.     resize(winWidth, winHeight);
  75.  
  76.     bool running = true;
  77.  
  78.     while (running)
  79.     {
  80.         uint64_t counterNow = SDL_GetPerformanceCounter();
  81.         uint64_t counterFreq = SDL_GetPerformanceFrequency();
  82.         time.dt = double(counterNow - counterOld) / double(counterFreq);
  83.  
  84.         if (time.dt > 0.1)
  85.             time.dt = 1.0 / 60.0;
  86.  
  87.         time.t += time.dt;
  88.  
  89.         SDL_Event evt;
  90.         while (SDL_PollEvent(&evt))
  91.         {
  92.             switch (evt.type)
  93.             {
  94.             case SDL_QUIT:
  95.                 running = false;
  96.                 break;
  97.  
  98.             case SDL_WINDOWEVENT:
  99.                 SDL_WindowEvent winevt = evt.window;
  100.                 switch (winevt.event)
  101.                 {
  102.                 case SDL_WINDOWEVENT_RESIZED:
  103.                     winWidth = winevt.data1;
  104.                     winHeight = winevt.data2;
  105.                     resize(winWidth, winHeight);
  106.                     break;
  107.                 }
  108.                 break;
  109.             }
  110.         }
  111.  
  112.         if (time.dt > 0.0)
  113.             update(time);
  114.         render(time);
  115.  
  116.         SDL_GL_SwapWindow(win);
  117.         SDL_Delay(1);
  118.     }
  119. }
  120.  
  121.  
  122. int main(int argc, char** argv)
  123. {
  124.     SDL_Window* win;
  125.     SDL_GLContext ctx;
  126.  
  127.     const int winWidth = 800;
  128.     const int winHeight = 600;
  129.  
  130.     int result = SDL_Init(SDL_INIT_VIDEO);
  131.     if (result < 0)
  132.         criticalError("Could not initialize SDL", SDL_GetError());
  133.  
  134.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  135.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  136.     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  137.     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  138.     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  139.     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  140.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  141.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  142.  
  143.     win = SDL_CreateWindow("Hello SDL",
  144.                            SDL_WINDOWPOS_CENTERED,
  145.                            SDL_WINDOWPOS_CENTERED,
  146.                            winWidth, winHeight,
  147.                            SDL_WINDOW_OPENGL
  148.                            | SDL_WINDOW_SHOWN
  149.                            | SDL_WINDOW_RESIZABLE);
  150.     if (!win)
  151.         criticalError("Unable to create render window", SDL_GetError());
  152.  
  153.     ctx = SDL_GL_CreateContext(win);
  154.     SDL_GL_SetSwapInterval(1);
  155.  
  156.     mainLoop(win);
  157.  
  158.     SDL_GL_DeleteContext(ctx);
  159.     SDL_DestroyWindow(win);
  160.     SDL_Quit();
  161.  
  162.     return 0;
  163. }
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement