grimfang4/SDL_FontCache

Question

EvilPudding opened this issue · 6 comments

I'm avoiding using SDL renderer to render my scene, is there anyway to render the result to an already bound opengl framebuffer or screen, or get the result in an opengl texture so I can render it in my shaders manually?

There's not yet a simple way to interact with raw OpenGL objects. I'd welcome any contribution to do it, though.

SDL_FontCache has two renderer options: SDL_Renderer and SDL_gpu. Currently, the easiest way to render to an OpenGL buffer would be to use SDL_gpu (from which you can get the OpenGL texture handle). Define FC_USE_SDL_GPU to do that, though it may require more refactoring in your code than you want.

If you do want to try implementing an OpenGL renderer, then search for FC_USE_SDL_GPU in the sources to find the places that need to be changed.

Also, I should note that SDL_gpu has a shader interface so you can potentially use its GPU_Image objects in custom shaders.

Will take a look into that.

Could you create function skeletons for one function to return a SDL_Surface or SDL_Texture, and one to return an OpenGL texture? I'm trying to make them, but I really don't know where they would fit or how they would be called.

SDL_FontCache stores several textures for the glyph cache. You can get them like so:

int i;
for(i = 0; i < FC_GetNumCacheLevels(font); ++i)
{
    SDL_Texture* tex = FC_GetGlyphCacheLevel(font, i);
    // Do stuff with it
}

(this is only is you want access to the raw cache buffer)

Oddly, I think the only way to get an SDL_Surface from an SDL_Texture is to use SDL_SetRenderTarget() and then SDL_RenderReadPixels(), and your texture must be loaded as a render target (SDL_FontCache does this for the glyph levels).

SDL does have a function SDL_GL_BindTexture(), though it may be of limited use without more info about the texture.

With SDL_gpu, replace the SDL_Texture in the code above with GPU_Image*. You can get the SDL_Surface with GPU_CopySurfaceFromImage().

Currently, to get the actual GL texture handle, you need to force a specific renderer (say, OpenGL 3.x), include the proper header for the cast you need (SDL_gpu_OpenGL_3.h), then get the handle from the casted data pointer:

GLuint handle = ((ImageData_OpenGL_3*)image->data)->handle;

If you want to get a printed string, then you'd have to render to a texture first, then do stuff with that texture as above.