SDL 使用 SDL_TEXTUREACCESS_STREAMING 传输纹理

问题描述 投票:0回答:0

我正在使用 SDL2 和

SDL_TEXTUREACCESS_STREAMING
,没有 OpenGL,我想将一些纹理从我的函数传输/组合到我的主循环。因此,我决定将我的访问类型从
STREAMING 
更改为
TARGET
,将
SDL_SetRenderTarget
SDL_RenderCopy
一起使用,并再次将访问类型设置为
STREAMING
,但是当我使用
SDL_RenderPresent
预览它时,并未显示发送的纹理。

这是我打开纹理的函数:

TUserFunc(
    void, OpenTTF, Reference font, int32_t align, int32_t x, int32_t y, Reference txt, int32_t scl)
{    
    int tgt_w = g_w;
    int tgt_h = g_h; 
 
    TTF_Font * ttfFont = TTF_OpenFont(pu->refToAstr(CP_THREAD_ACP, font).c_str(), scl);
    if(ttfFont == nullptr) { 
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SDL ERROR", "Error cargando la direccion de las fuentes", NULL);
        quitGame();
    } 
    
    SDL_Surface * surfaceText = TTF_RenderText_Blended(ttfFont, (pu->refToAstr(CP_THREAD_ACP, txt).c_str()), color);
    if(surfaceText == nullptr){
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SDL ERROR", "Error creando Superficie", NULL);
        quitGame();
    } 
    
    textureText = SDL_CreateTextureFromSurface(g_renderer,surfaceText); 
    if(textureText == nullptr){
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SDL ERROR", "Error creando Texturas", NULL);
        quitGame();
    } 

    SDL_Rect rectangle;
    rectangle.x = x;
    rectangle.y = y;
    rectangle.w = surfaceText->w;
    rectangle.h = surfaceText->h;

//The Problem Start from here

    unlockTarget(); 
    g_target = SDL_CreateTexture(g_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, tgt_w, tgt_h);

    SDL_SetRenderTarget(g_renderer, g_target);
    SDL_RenderCopy(g_renderer, textureText, NULL, &rectangle);
    SDL_SetRenderTarget(g_renderer, NULL);
     
    g_target = SDL_CreateTexture(g_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, tgt_w, tgt_h);
    lockTarget();
}

lockTarget()
unlockTarget()
是锁定和解锁纹理的简单方法,你可以看到,我使用
CreateTexture
更改为
TARGET
,放入我的
SetRenderTarget
RenderCopy
,然后再次使用
CreateTexture
更改回
STREAMING

这是我显示图形的主循环:

TUserFunc(void, Flip)
{       

    if(g_target){
        unlockTarget();
        SDL_RenderCopy(g_renderer, g_target, nullptr, nullptr); 
        lockTarget();
    }     
    SDL_RenderPresent(g_renderer);
}

但是如果我添加这个,则会显示纹理,但不是相同的纹理,我的意思是,不与

g_target

组合
TUserFunc(void, Flip)
{       

    if(g_target){
        unlockTarget();
        SDL_RenderCopy(g_renderer, g_target, nullptr, nullptr); 
        lockTarget();
    }    
    if(textureText){
        unlockTarget();
        SDL_RenderCopy(g_renderer, textureText, nullptr, nullptr); 
        lockTarget();
    }
    SDL_RenderPresent(g_renderer);
}

在这里您可以查看其他功能,以便更好地了解问题:

TUserFunc(bool, Init...)
{
...
        // This is the main and unique call to g_target
    g_target = SDL_CreateTexture(g_renderer, SDL_PIXELFORMAT_ARGB8888,
            SDL_TEXTUREACCESS_STREAMING, w, h);
    SDL_SetTextureBlendMode(g_target, SDL_BLENDMODE_NONE);
...
}

void lockTarget()
{
    if(g_target) SDL_LockTexture(g_target, nullptr, (void**)&g_pix, &g_pitch);
    if(textureText) SDL_LockTexture(textureText, nullptr, (void**)&g_pix, &g_pitch);
}

void unlockTarget()
{
    if(g_target) SDL_UnlockTexture(g_target);
    if(textureText) SDL_UnlockTexture(textureText);
}
c++ sdl-2 sdl-ttf
© www.soinside.com 2019 - 2024. All rights reserved.