1. # Demo: Fast blitting of images via textures in GPU memory with SDL2 in Raspian-Stretch
  2.  
  3. from os import listdir, path
  4. import sys
  5. import time
  6. from sdl2 import *
  7. from sdl2.sdlimage import IMG_Load as SDL_LoadImage
  8.  
  9. # Initialize SDL2
  10. SDL_Init(SDL_INIT_VIDEO)
  11.  
  12. try:
  13.   # create fullscreen display
  14.   win = SDL_CreateWindow(b'Fast Blitting', 0,0,0,0, SDL_WINDOW_FULLSCREEN)
  15.  
  16.   # create renderer
  17.   renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)
  18.  
  19.   # load all image files (BMP) in folder 'images' into textures in GPU memory
  20.   textures = []
  21.   folder = path.realpath('.')+'/fotos/'
  22.   files = listdir(folder)
  23.   for f in files:
  24.       img = SDL_LoadImage((folder+f).encode())
  25.       tex = SDL_CreateTextureFromSurface(renderer, img)
  26.       SDL_FreeSurface(img)
  27.       textures.append(tex)
  28.  
  29.   # blit 100 images
  30.   cnt = len(textures)
  31.   t_start = time.time()
  32.   for i in range(100):
  33.  
  34.     SDL_RenderCopy(renderer, textures[i % cnt], None, None)
  35.  
  36.     # Use this function to update the screen with any rendering performed since the previous call.
  37.     SDL_RenderPresent(renderer)
  38.  
  39.     # reduce fps via delay
  40.     #SDL_Delay(10)
  41.  
  42.   # show fps
  43.   sec = time.time()-t_start
  44.   print('fps: '+str(100/sec))
  45.  
  46. except:
  47.   print(sys.exc_info())
  48.  
  49. # clean up
  50. if 'win' in locals():
  51.   if 'renderer' in locals():
  52.     if 'textures' in locals():
  53.       for tex in textures: SDL_DestroyTexture(tex)
  54.     SDL_DestroyRenderer(renderer)
  55.   SDL_DestroyWindow(win)
  56. SDL_Quit()
  57.  
[raw code]