I have a font file/texture I’d like to use across windows, there might be 3+ windows, is it possible? What if I used gl?
I have zero experience with multiple windows, but I guess that no, it is not possible.
try this:
- save your font file/texture to a buffer you own, such as with e.g.
malloc(width*height*sizeof (uint32_t))
assuming you are using some pixel format that is 32-bit per pixel - create separate textures for each renderer
- if you create with
SDL_TEXTUREACCESS_STREAMING
- lock the texture
memcpy
one or more times your buffer into it, paying attention to pitch- unlock the texture and use it
- if you create with
SDL_TEXTUREACCESS_STATIC
- use
SDL_UpdateTexture
to set the texture
- if you create with
- if you destroy windows and create new windows etc., you still have your original buffer untouched, copying is data is kinda quick
- …
- profit?
good luck
This is one of the downsides of using an opaque structure like SDL_Textures; the data of the texture is basically stored in the GPU and can only be accessed through renderer requests to download images back into a CPU buffer.
I don’t think anyone has a work-around to share a texture between windows.
The best option right now [An option] is to share data between windows rather than textures. This can be done either as an array or buffer of data as Elefantinho describes, or it might be more convenient to render your texture down to a surface using SDL_RenderReadPixels(). Then SDL_LockTextureToSurface() to update your streaming textures in the other windows.
Since this is a one-time transfer, I might even use SDL_CreateTextureFromSurface() to create a static texture from the surface. (I’m not certain if there’s any kind of performance boost or not for drawing static textures instead of target or streaming textures, but I tend to aim for them if I don’t intend to further modify the contents.)
SDL_RenderReadPixels() is considered a “slow” function [not ideal for a game loop], but if you only need to run it once at start-up or at level-changes then it is not too bad.
The simplest solution is to load two copies of the same texture, for two renderers. The problem of sharing it will be eliminated and there will be no need to transfer anything between CPU and GPU. The only downside is twice as much GPU memory usage.
My old question rendering - SDL2: Share renderer between multiple windows - Stack Overflow.
Now I would use a hash map to store textures for each window to not recreate them. In general, it depends on how often you need to share them (I had an issue when every frame I had to draw on two screens a huge background and recreating takes too long).