我正在 SDL2 中制作游戲,我決定添加一個 FPS 計數器。計數器的數字需要每秒更新一次,這就是我如何完成的
bool updateFPS() {
if (fpsDTime == 1) { // time between update has been one second
frameCounterS ; // add an extra frame
fpsTime = currentTime; // reset timer
// My attempt at converting my int to a char to display
char SframeCounterS[MAX_DIGITS sizeof(char)];
std::to_chars(SframeCounterS, SframeCounterS MAX_DIGITS, frameCounterS);
// Rendering the text
message = TTF_RenderText_Blended(HPusab, SframeCounterS, White); // Loading text to a variable
std::cout << SframeCounterS << std::endl; // Console Debugging
if (message == NULL) {
std::cout << "Could not create message! " << SDL_GetError() << std::endl;
return false;
// code will stop running here because I used a return statement
} // Error Checking Function
slotEight = SDL_CreateTextureFromSurface(gRenderer, message); // Creating a texture from my surface
if (slotEight == NULL) {
std::cout << "Could not create Message! " << SDL_GetError() << std::endl;
return false;
// code will stop running here because I used a return statement
} // More error checking!
rslotEight.x = 30; // controls the rect's x coordinate
rslotEight.y = 10; // controls the rect's y coordinte
rslotEight.w = message->w; // controls the width of the rect
rslotEight.h = message->h; // controls the height of the rect
frameCounterS = 0; // Reset the FPS counter
}
我嘗試轉換為 char 而不是字串的原因是該TTF_RenderText方法需要 char 作為文本的輸入。它確實顯示了我想要的數字,但最后有一堆亂碼。
這是我的控制臺輸出:
uj5u.com熱心網友回復:
我懷疑您只是從SframeCounterS. 您應該使用 a 清除緩沖區memset(SframeCounterS, 0, sizeof(SframeCounterS));,或者使用字串,這也將簡化代碼:
TTF_RenderText_Blended(HPusab, std::to_string(frameCounterS).c_str(), White);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488526.html
