C++ SDL2-假定返回True的函数的计算结果为False
我正在使用SDL2,遵循LazyFoo的教程。我已经到了加载TTF字体部分,我似乎遵循了他所做的一切,除了添加了两个额外的TTF_字体和SDL_渲染器参数C++ SDL2-假定返回True的函数的计算结果为False,c++,sdl-2,C++,Sdl 2,我正在使用SDL2,遵循LazyFoo的教程。我已经到了加载TTF字体部分,我似乎遵循了他所做的一切,除了添加了两个额外的TTF_字体和SDL_渲染器参数 bool LTexture::loadFromRenderedText(TTF_Font *&gFont, std::string textureText, SDL_Color textColor, SDL_Renderer *&gRenderer) { //Get rid of preexisting texture
bool LTexture::loadFromRenderedText(TTF_Font *&gFont, std::string textureText, SDL_Color textColor, SDL_Renderer *&gRenderer)
{
//Get rid of preexisting texture
free();
//Render text surface
SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor );
if( textSurface == NULL )
{
printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
}
else
{
//Create texture from surface pixels
mTexture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
if( mTexture == NULL )
{
printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
}
else
{
//Get image dimensions
mWidth = textSurface->w;
mHeight = textSurface->h;
}
//Get rid of old surface
SDL_FreeSurface( textSurface );
}
return 1;
}
这是函数,在这里调用它:
gFont = TTF_OpenFont( "Font/comicSans.ttf", 28 );
if( gFont == NULL )
{
printf( "Failed to load COMIC_SANS font! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
else
{
//Render text
SDL_Color textColor = { 0, 0, 0 };
if( !gTextTexture.loadFromRenderedText(gFont, "Hello I am hot", textColor, gRenderer));
{
printf( "Failed to render text texture!\n" );
success = false;
}
}
如果成功
为false,程序将打印“加载介质失败”
这是我从运行程序中获得的输出,请注意,没有实际的SDL或TTF错误,只有当loadFromRenderedText
返回false时出现的错误,它没有返回false,因为我已将其设置为返回1:
Failed to render text texture!
Failed to load media!
与我的文件和LazyFoo的文件唯一的实际区别是,我的LTexture类位于一个独立于main的文件中,这就是为什么出于某种原因,我不能使用gFont
和grender
(全局变量)
你知道是什么引起的吗
完整代码:
main.cpp:
/*This source code copyrighted by Lazy Foo' Productions (2004-2019)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>
#include <string>
#include "LTexture.h"
bool init();
bool loadMedia();
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
TTF_Font *gFont = NULL;
LTexture gTextTexture;
//Screen dimension constants
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
int main(int argc, char* args[])
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event event;
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &event ) != 0 )
{
//User requests quit
if( event.type == SDL_QUIT )
{
quit = true;
}
}
//Clear screen
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear( gRenderer );
gTextTexture.render(gRenderer, (SCREEN_WIDTH - gTextTexture.getWidth()) / 2, (SCREEN_HEIGHT - gTextTexture.getHeight()) / 2);
//Update screen
SDL_RenderPresent( gRenderer );
}
}
}
//Free resources and close SDL
close();
return 0;
}
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
}
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Open the font
gFont = TTF_OpenFont( "Font/comicSans.ttf", 28 );
if( gFont == NULL )
{
printf( "Failed to load COMIC_SANS font! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
else
{
//Render text
SDL_Color textColor = { 0, 0, 0 };
if( !gTextTexture.loadFromRenderedText(gFont, "Hello I am hot", textColor, gRenderer));
{
printf( "Failed to render text texture!\n" );
success = false;
}
}
return success;
}
void close()
{
//Free loaded images
gTextTexture.free();
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
TTF_CloseFont(gFont);
gWindow = NULL;
gRenderer = NULL;
gFont = NULL;
//Quit SDL subsystems
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
问题是if语句末尾的分号
if( !gTextTexture.loadFromRenderedText(gFont, "Hello I am hot", textColor, gRenderer));
{
printf( "Failed to render text texture!\n" );
success = false;
}
该分号将为该if块创建一个空语句,它将导致
{
printf( "Failed to render text texture!\n" );
success = false;
}
总是执行。就是这样,
if(.....)
;
{
....
}
问题是if语句末尾的分号
if( !gTextTexture.loadFromRenderedText(gFont, "Hello I am hot", textColor, gRenderer));
{
printf( "Failed to render text texture!\n" );
success = false;
}
该分号将为该if块创建一个空语句,它将导致
{
printf( "Failed to render text texture!\n" );
success = false;
}
总是执行。就是这样,
if(.....)
;
{
....
}
如果返回true,则输出“未能渲染文本纹理”。这就是它所做的,它不会返回错误。哦,天哪,我没看到!非常感谢你!我知道if的声明有问题,该死的,非常感谢!如果返回true,则输出“未能渲染文本纹理”。这就是它所做的,它不会返回错误。哦,天哪,我没看到!非常感谢你!我知道if的声明有问题,该死的,非常感谢!