-1

I got stuck in some kind of "logical" routine. My code receives live preview pictures from my camera, which are converted to RWops struct and finally be loaded as texture to SDL. Now my question is: How can i update the texture without destroying and rebuilding it? Some code im using atm:

for(x=0; x<100; x++){
    capture_to_memory(camera, context, (const char**)&data, &size);

    rw = SDL_RWFromConstMem(data, size);

    // This part is building the texture through SDL_image
    sdlTexture = IMG_LoadTexture_RW(sdlRenderer, rw, 0);

    SDL_RenderClear(sdlRenderer);
    SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
    SDL_RenderPresent(sdlRenderer);

    SDL_DestroyTexture(sdlTexture);
}

As u see, and how i guess, thats totally inefficient. What i notice is that for the 100 pictures it takes me round 12 seconds to display them (jpg). If i just let the capture and conversion run, its done in 4.5 sec. So it seams my routine with building and destroying the texture is slowing all down.

Any suggestions maybe?

genpfault
  • 51,148
  • 11
  • 85
  • 139
ayk
  • 1,330
  • 2
  • 15
  • 24
  • SDL_image doesn't provide this functionality. If you really have to, you could use libpng/libjpeg/whatever_your_format_is directly. However, slow part is most likely decoding image, not allocating memory for it, so it wouldn't give you much benefit. Where are you getting data from and what actual format/resolution/framerate is? – keltar Nov 13 '13 at 03:23
  • Yes, thats what i have noticed, too. The decoding takes most of the time. I have to say, im working on a Raspberry PI. Want to use SDL as dont wanna use x11 server. My data is coming from a DSLR (libgphoto2), which is JPG format on a resolution of 640x400, so not that big. Im much more interested in how to handle fast changing pictures in SDL. I cant always destroy and rebuild the texture! – ayk Nov 13 '13 at 08:18

1 Answers1

0

After quick look at SDL_image it doesn't seem to have API to decode image into existing texture.

If you can't request image in uncompressed format - you'll have to use libjpeg directly. Or reconsider the whole idea of how you getting images - maybe reading directly from video device (v4l?) will be better?

For quickly-changing textures, SDL have SDL_TEXTUREACCESS_STREAMING flag for SDL_CreateTexture. Then, when you need to modify data, lock texture with SDL_LockTexture, modify returned memory and unlock it back.

keltar
  • 17,711
  • 2
  • 37
  • 42