daily pastebin goal
63%
SHARE
TWEET

SDL_CreateTexture Example

KrunoSaho May 21st, 2014 362 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Minimap
  2. const int max_x=256;
  3. const int max_y=256;
  4. std::array<std::array<float, max_y>, max_x> zdata;
  5. // Fill up zdata
  6. ...
  7. // Draw to texture
  8. static SDL_Texture* minimap = nullptr;
  9. if (minimap == nullptr) {
  10.     minimap = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888,
  11.                                 SDL_TEXTUREACCESS_TARGET | SDL_TEXTUREACCESS_STATIC, max_x, max_y);
  12.    
  13.     if(SDL_SetRenderTarget(renderer, minimap) != 0) {
  14.         printf("%s\n", SDL_GetError());
  15.     }
  16.    
  17.     for (int i=0; i<max_x; i++) {
  18.         for (int j=0; j<max_y; j++) {
  19.                 const auto z = static_cast<int>(zdata[i][j]*255);
  20.                 SDL_SetRenderDrawColor(renderer, z, z, z, 255);
  21.                 SDL_RenderDrawPoint(renderer, i, j);
  22.         }
  23.     }
  24.    
  25.     SDL_SetRenderTarget(renderer, nullptr);
  26. }
  27.  
  28. const SDL_Rect src = {0, 0, max_x, max_y};
  29. const SDL_Rect dst = {0, 0, max_x, max_y};
  30. SDL_RenderCopy(renderer, minimap, &src, &dst);
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
 
Top