Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SDL_ttf显示故障_C++_Macos_Sdl 2_Sdl Ttf - Fatal编程技术网

C++ SDL_ttf显示故障

C++ SDL_ttf显示故障,c++,macos,sdl-2,sdl-ttf,C++,Macos,Sdl 2,Sdl Ttf,我试图在SDL中使用true type字体显示字符串“hey”,但存在一些小问题。下面是应该显示的文本(我进行了屏幕录制,因此此快照的下半部分只是视频播放器的控件): 这里是一个小故障: 如您所见,字母“h”的上半部分缺失 以下是我使用的代码: #include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> #include <iostream> #include <string> int fatal( std

我试图在SDL中使用true type字体显示字符串“hey”,但存在一些小问题。下面是应该显示的文本(我进行了屏幕录制,因此此快照的下半部分只是视频播放器的控件):

这里是一个小故障:

如您所见,字母“h”的上半部分缺失

以下是我使用的代码:

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <string>

int fatal( std::string m ) {
    std::cout << "Fatal error: " << m << "\n";
    exit(-1);   
}

void drawText(TTF_Font* font,SDL_Renderer* r, std::string message) {
    SDL_Color c = {255,255,255,255};
    SDL_Surface *temp = TTF_RenderText_Blended( font, message.c_str(), c);
    if ( temp == NULL )
        fatal("failed to create surface");
    SDL_Texture *txt = SDL_CreateTextureFromSurface( r, temp );
    if ( txt == NULL )
        fatal("failed to create texture");
    SDL_FreeSurface( temp );
    SDL_Rect src;
    src.x = 0;
    src.y = 0;
    SDL_QueryTexture( txt, NULL, NULL, &src.w, &src.h );
    SDL_Rect pos;
    pos.x = 80;
    pos.y = 80;
    pos.w = src.w;
    pos.h = src.h;
    if ( SDL_RenderCopy(r,txt,&src,&pos) != 0)
        fatal("SDL_RenderCopy failed");
    SDL_DestroyTexture(txt);    
}

extern "C" int main( int argc, char** argv ) {
    SDL_Renderer *renderer;
    SDL_Window *window;
    if ( SDL_Init( SDL_INIT_VIDEO ) != 0 )
        fatal("sdl_init failed");
    if ( TTF_Init() != 0 )
        fatal("ttf_init failed");
    if ( SDL_CreateWindowAndRenderer ( 200, 200, 0, &window, &renderer ) != 0 )
        fatal("sdl_createwindowandrenderer failed");
    TTF_Font* font = TTF_OpenFont( "Arial.ttf", 30 ); 
    if ( font == NULL ) 
        fatal("failed to open font");
    SDL_Event e;
    while ( 1 ) {
        if ( SDL_PollEvent(&e) ) {
            if (e.type == SDL_QUIT)
                break;
        }
        // clear back buffer
        SDL_SetRenderDrawColor(renderer,0,0,255,255);
        SDL_RenderClear(renderer);  
        drawText(font,renderer,"hey");
        SDL_RenderPresent(renderer);
    }
    
    return 0;
} 

对于其他有相同问题的人来说,通过分别使用SDL_CreateWindow和SDL_CreateRenderer初始化窗口和渲染器,并将渲染器的标志设置为SDL_renderer_ACCELERATED | SDL_renderer_PRESENTVSYNC,初始化SDL会有很大的不同。然而,对于比上述更复杂的循环,问题在某种程度上仍然存在。如果我使用empscripten编译到webassembly中(这需要将循环放在单独的函数中),则不会发生这种情况。因此,环境因素导致了这个问题。附加注释:如果文本的纹理不是每次循环都重复创建的话,这会有很大帮助。因此,每个可见ascii字符的文本表是预先创建的,然后使用该表绘制文本。目前还不清楚为什么这是所描述的macOS环境中的一个问题,但它确实给ram带来了较小的压力。这也使低端设备(如廉价手机/平板电脑)的速度显著提高。
g++ -std=c++17 textTest.cpp -lSDL2 -lSDL2_ttf