The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
How can I optimize SDL_Texture
Jostino


Joined: 19 Jul 2015
Posts: 1
When i render a texture made by me (map_texture) the performance decreases drastically( from 350fps to 17fps), any suggestion to increase performance ?

Code:
//Create Map Texture 
   game->map.map_texture = SDL_CreateTexture(game->renderer, SDL_PIXELFORMAT_ARGB8888 ,SDL_TEXTUREACCESS_TARGET, w*32, h*32);
   SDL_SetRenderTarget(game->renderer,game->map.map_texture);
   SDL_RenderClear(game->renderer);
   
   while(++i< (game->map.h * game->map.w) ){
            
      if(game->map.array[i]!=0)
         SDL_SetRenderDrawColor(game->renderer, 255, 255, 255, 255);
      else
         SDL_SetRenderDrawColor(game->renderer, 32, 32, 128, 255);
      
      rect_map.y = 32* (i/w);
      rect_map.x = 32*(i - rect_map.y /32*w);
            
     //  SDL_RenderFillRect(game->renderer, &rect_map);
      SDL_RenderDrawRect(game->renderer, &rect_map);
      
      
      }
      SDL_RenderPresent(game->renderer);
      
      SDL_SetRenderTarget(game->renderer,NULL);


Code:
int render_game(Data *game){
   
   //set the drawing color to blue
  SDL_SetRenderDrawColor(game->renderer, 32, 32, 128, 255);

  //Clear the screen (to blue)
  SDL_RenderClear(game->renderer);
  //render map
  SDL_Rect rect = {0,0,640,480}; 
  SDL_Rect rect1 = {0,0,640,480}; 
 
  SDL_RenderCopy(game->renderer, game->map.map_texture, &rect1, &rect);      
     
   rect.x = game->p1.x;
   rect.y = game->p1.y;
   rect.w = game->p1.w;
   rect.h = game->p1.h;
   SDL_RenderCopy(game->renderer, game->p1.player_texture, NULL, &rect);
   
    SDL_RenderPresent(game->renderer);
 
   return 0;
   }
Naith


Joined: 03 Jul 2014
Posts: 158
I might be able to help you optimize your code but, I have a few questions:

1. Is the '//Create Map Texture' code executed each frame?
2. Why are you setting the 'map.map_texture' to be the current render target? I can't see that you're doing anything with it after you've set it to be the current render target.
3. What's the reason you want to render all those SDL_Rect's?'
4. The map you're rendering, is it a tile map?