SDL2 TTF 文本呈现随机消失

SDL2 TTF text renders randomly disappearing

我正在使用 SDL2,并且刚刚掌握了 SDL TTF 的一些基本概念,但是由于某种原因,在应用程序 运行 一段时间后,我的文本元素消失了,似乎没有任何原因,可以有人请告诉我我需要做什么来阻止我的文字渲染消失?这里出了什么问题?

#include <iostream>
#include <iomanip>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include "AdviLib.h"

#define SDLKCM SDLK_COMMA
#define SDLKPR SDLK_PERIOD
#define SDLKSP SDLK_SPACE
#define SDLKES SDLK_ESCAPE

void Screenshot(SDL_Renderer* r, const char* name, int w, int h)
{
const uint32_t format{ SDL_PIXELFORMAT_ARGB8888 };
SDL_Surface* sur{ SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, format) };
SDL_RenderReadPixels(r, NULL, format, sur->pixels, sur->pitch);
SDL_SaveBMP(sur, name);
SDL_FreeSurface(sur);
std::cout << name << " captured!\n";
}

int main(int args, char* argc[])
{
// before SDL init
std::cout << "enter the name of the session: "; string sesh{ sInput() };
std::cout << "enter the name of the local, non .bmp, image: "; string image{ sInput() };

// SDL init
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
TTF_Init();
const int W{ 800 }; const int H{ 750 };
SDL_Window* win{ SDL_CreateWindow(sesh.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN) };
SDL_Renderer* ren{ SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) };
SDL_Surface* img{ IMG_Load(image.c_str()) };
TTF_Font* font{ TTF_OpenFont("CRYSRG__.ttf", 40) };
SDL_Color color{ 205, 99, 35 };
SDL_Texture* tex{ SDL_CreateTextureFromSurface(ren, img) };
SDL_Event e;

// buffer variables
string Txt; string Tx2; string Tx3; string Tx4; string Tx5; string Tx6; string Tx7;
SDL_Surface* ts1 = nullptr; SDL_Surface* ts2 = nullptr; SDL_Surface* ts3 = nullptr; SDL_Surface* ts4 = nullptr; SDL_Surface* ts5 = nullptr; SDL_Surface* ts6 = nullptr; SDL_Surface* ts7 = nullptr;
SDL_Texture* tb1 = nullptr; SDL_Texture* tb2 = nullptr; SDL_Texture* tb3 = nullptr; SDL_Texture* tb4 = nullptr; SDL_Texture* tb5 = nullptr; SDL_Texture* tb6 = nullptr; SDL_Texture* tb7 = nullptr;
int w{ img->w }; int h{ img->h }; int x{ randomWithinRange(W - w) }; int y{ randomWithinRange(H - h) };
int tbW{ 0 }; int tbH{ 0 }; int X{ 2 }; int tbY{ 1 }; int tb2Y{ 32 }; int tb3Y{ 63 };  int tb4Y{ 94 }; int tb5Y{ 125 }; int tb6Y{ 156 }; int tb7Y{ 187 };
int ms{ 1 };
bool q{ false };
double ANG{ 0 }; int ANGI{ 0 };

// before main loop

// main loop
while (!q)
{
    // before buffer setup
    SDL_Rect IR{ x, y, w, h };

    // buffer setup
    ANGI = ANG;
    int _TXT_RED = color.r; int _TXT_GRN = color.g; int _TXT_BLU = color.b;
    //current x pos,  current y pos,  current angle,  current speed,  text color red, text color grn, text color blue,
    char buf1[255]; char buf2[255]; char buf3[255]; char buf4[255]; char buf5[255]; char buf6[255]; char buf7[255];
    sprintf_s(buf1, "x: %i", x); sprintf_s(buf2, "y: %i", y); sprintf_s(buf3, "a: %i", ANGI); sprintf_s(buf4, "MS: %i", ms); sprintf_s(buf5, "TR: %i", _TXT_RED); sprintf_s(buf6, "TG: %i", _TXT_GRN);
    sprintf_s(buf7, "TB: %i", _TXT_BLU);
    Txt = buf1; Tx2 = buf2; Tx3 = buf3; Tx4 = buf4; Tx5 = buf5; Tx6 = buf6; Tx7 = buf7;
    ts1 = TTF_RenderText_Blended(font, Txt.c_str(), color); ts2 = TTF_RenderText_Blended(font, Tx2.c_str(), color); ts3 = TTF_RenderText_Blended(font, Tx3.c_str(), color);
    ts4 = TTF_RenderText_Blended(font, Tx4.c_str(), color); ts5 = TTF_RenderText_Blended(font, Tx5.c_str(), color); ts6 = TTF_RenderText_Blended(font, Tx6.c_str(), color);
    ts7 = TTF_RenderText_Blended(font, Tx7.c_str(), color);
    tb1 = SDL_CreateTextureFromSurface(ren, ts1); tb2 = SDL_CreateTextureFromSurface(ren, ts2); tb3 = SDL_CreateTextureFromSurface(ren, ts3); tb4 = SDL_CreateTextureFromSurface(ren, ts4);
    tb5 = SDL_CreateTextureFromSurface(ren, ts5); tb6 = SDL_CreateTextureFromSurface(ren, ts6); tb7 = SDL_CreateTextureFromSurface(ren, ts7);
    SDL_QueryTexture(tb1, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb2, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb3, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb4, NULL, NULL, &tbW, &tbH);
    SDL_QueryTexture(tb5, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb6, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb7, NULL, NULL, &tbW, &tbH);
    SDL_Rect Rct{ X, tbY, tbW, tbH };  SDL_Rect Rc2{ X, tb2Y, tbW, tbH }; SDL_Rect Rc3{ X, tb3Y, tbW, tbH }; SDL_Rect Rc4{ X, tb4Y, tbW, tbH }; SDL_Rect Rc5{ X, tb5Y, tbW, tbH }; SDL_Rect Rc6{ X, tb6Y, tbW, tbH };
    SDL_Rect Rc7{ X, tb7Y, tbW, tbH };

    // set display renders
    SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
    SDL_RenderClear(ren);
    SDL_RenderCopyEx(ren, tex, NULL, &IR, ANG, 0, SDL_FLIP_NONE);
    SDL_RenderCopy(ren, tb1, NULL, &Rct); SDL_RenderCopy(ren, tb2, NULL, &Rc2); SDL_RenderCopy(ren, tb3, NULL, &Rc3); SDL_RenderCopy(ren, tb4, NULL, &Rc4); SDL_RenderCopy(ren, tb5, NULL, &Rc5);
    SDL_RenderCopy(ren, tb6, NULL, &Rc6); SDL_RenderCopy(ren, tb7, NULL, &Rc7);
    SDL_RenderPresent(ren);
    while (SDL_PollEvent(&e))
    {
        if (e.type == SDL_QUIT) q = true;
        else if (e.type == SDL_KEYDOWN)
        {
            switch (e.key.keysym.sym)
            {
            case SDLK_w: y -= ms; break; case SDLK_s: y += ms; break;
            case SDLK_a: x -= ms; break; case SDLK_d: x += ms; break;
            case SDLK_r: if (color.r < 255) color.r += 1; else if (color.r == 255) color.r = 0; break;
            case SDLK_g: if (color.g < 255) color.g += 1; else if (color.g == 255) color.g = 0; break;
            case SDLK_b: if (color.b < 255) color.b += 1; else if (color.b == 255) color.b = 0; break;
            case SDLKCM: ANG -= 1; break;
            case SDLKPR: ANG += 1; break;
            case SDLKSP: Screenshot(ren, "capture.bmp", W, H); break;
            case SDLK_q: if (ms > 1) { ms -= 1; /*std::cout << "ms decreased to " << ms << "\n";*/ } /*else if (ms == 1) std::cout << "ms cannot decrease further\n";*/ break;
            case SDLK_e: if (ms < 10) { ms += 1; /*std::cout << "ms increased to " << ms << "\n";*/ } /*else if (ms == 10) ms = 10; std::cout << "ms cannot increase further\n";*/ break;
            case SDLKES: q = true; break;
            }
        }
    }
}

// after main loop - usually nothing

// exit code
SDL_DestroyTexture(tb1); SDL_DestroyTexture(tb2); SDL_DestroyTexture(tb3); SDL_DestroyTexture(tb4); SDL_DestroyTexture(tb5); SDL_DestroyTexture(tb6); SDL_DestroyTexture(tb7); SDL_DestroyTexture(tex);
SDL_FreeSurface(ts1); SDL_FreeSurface(ts2); SDL_FreeSurface(ts3); SDL_FreeSurface(ts4); SDL_FreeSurface(ts5); SDL_FreeSurface(ts6); SDL_FreeSurface(ts7); SDL_FreeSurface(img);
TTF_CloseFont(font);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}

这是因为您不断加载更多纹理和表面而不释放它们。您只需要加载一次。将您的缓冲区设置放在循环之前,还要检查内存使用情况,因为它 运行 我几乎可以肯定它会上升很多