Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <SDL.h>
  4. #include <stdio.h>
  5. #include <algorithm>
  6.  
  7. //Screen dimension constants
  8. const int SCREEN_WIDTH = 1920;
  9. const int SCREEN_HEIGHT = 1080;
  10.  
  11. SDL_Window* gWindow;
  12. SDL_Renderer* gRenderer;
  13. SDL_Texture* gTexture;
  14. SDL_Event e;
  15.  
  16. uint32_t* gPixelBuffer;
  17. void* gPixels;
  18. int gPitch;
  19.  
  20. bool gExitFlag;
  21.  
  22. Uint64 start;
  23. Uint64 end;
  24. Uint64 freq;
  25. double seconds;
  26.  
  27. int main(int argc, char* args[])
  28. {
  29.     gExitFlag = false;
  30.     gPixels = NULL;
  31.     gPixelBuffer = new uint32_t[SCREEN_WIDTH * SCREEN_HEIGHT];
  32.     std::fill_n(gPixelBuffer, SCREEN_WIDTH * SCREEN_HEIGHT, 0xFFFFFFFF);
  33.     gPitch = SCREEN_WIDTH * 4;
  34.  
  35.     //Initialize SDL
  36.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  37.     {
  38.         printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
  39.     }
  40.     else
  41.     {
  42.         //Set texture filtering to linear
  43.         if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
  44.         {
  45.             printf("Warning: Linear texture filtering not enabled!");
  46.         }
  47.  
  48.         //Create window
  49.         gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  50.         if (gWindow == NULL)
  51.         {
  52.             printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
  53.         }
  54.         else
  55.         {
  56.             //Create renderer for window
  57.             gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);// | SDL_RENDERER_PRESENTVSYNC);
  58.             if (gRenderer == NULL)
  59.             {
  60.                 printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
  61.             }
  62.             else
  63.             {
  64.                 gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
  65.                 if (gTexture == NULL)
  66.                 {
  67.                     printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError());
  68.                 }
  69.                 else
  70.                 {
  71.  
  72.                     while (!gExitFlag)
  73.                     {
  74.  
  75.  
  76.                         //Handle events on queue
  77.                         while (SDL_PollEvent(&e) != 0)
  78.                         {
  79.                             //User requests quit
  80.                             if (e.type == SDL_QUIT)
  81.                             {
  82.                                 gExitFlag = true;
  83.                             }
  84.                         }
  85.  
  86.                         start = SDL_GetPerformanceCounter();
  87.  
  88.                         SDL_RenderClear(gRenderer);
  89.                         //SDL_LockTexture(gTexture, NULL, &gPixels, &gPitch);
  90.                         //memcpy(gPixels, gPixelBuffer, gPitch * SCREEN_HEIGHT);
  91.                         //SDL_UnlockTexture(gTexture);
  92.                         SDL_UpdateTexture(gTexture, NULL, gPixelBuffer, gPitch);
  93.                         SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
  94.                         gPixels = NULL;
  95.                         SDL_RenderPresent(gRenderer);
  96.  
  97.                         end = SDL_GetPerformanceCounter();
  98.                         freq = SDL_GetPerformanceFrequency();
  99.                         seconds = (end - start) / static_cast<double>(freq);
  100.                         printf("Frame time: %fms\n", seconds * 1000.0);
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     //Destroy window
  108.     SDL_DestroyWindow(gWindow);
  109.     SDL_DestroyRenderer(gRenderer);
  110.     SDL_DestroyTexture(gTexture);
  111.  
  112.     delete gPixelBuffer;
  113.  
  114.     //Quit SDL subsystems
  115.     SDL_Quit();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement