gpt4 book ai didi

c++ - 当我尝试在 C++ 中使用 SDL_RenderCopy 复制纹理时出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:31:02 25 4
gpt4 key购买 nike

这是我的 .cpp 文件:

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

bool runningOnEmpty = false;
const char* title;
int xpos, ypos, width, height;
bool fullscreen = false;
SDL_Window *window;
SDL_Renderer *renderer;

void init(){
int flags = 0;
if(fullscreen){
flags = SDL_WINDOW_FULLSCREEN;
}

if(!SDL_Init(SDL_INIT_EVERYTHING)){
std::cout << "Sdl initialised!!\n";

window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if(window){
std::cout << "window created!!\n";
}

renderer = SDL_CreateRenderer(window, -1, 0);
if(renderer){
std::cout << "renderer created!!\n";

SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}

runningOnEmpty = true;
} else {
runningOnEmpty = false;
}
if(TTF_Init()==-1) {
std::cout << "cannot init TTF: " << TTF_GetError() << "\n";
} else {
std::cout << "TTF initialized!" << "\n";
}
}

void render(){
SDL_RenderClear(renderer);
TTF_Font* rFont = TTF_OpenFont("./fonts/roboto.ttf", 20);
if(!rFont){
std::cout << "Cannot open font: " << TTF_GetError() << "\n";
}
SDL_Color White = {0, 0, 0};
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(rFont, "Score: ", White);

if(surfaceMessage == NULL)
std::cout << "Cannot make surface!" << SDL_GetError() << "\n";

SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);

SDL_Rect mr;
mr.x = 50, mr.y = 50, mr.w = 100, mr.h = 100;

if(Message = NULL)
std::cout << "Cannot make texture!" << SDL_GetError() << "\n";

if(SDL_RenderCopy(renderer, Message, NULL, &mr))
std::cout << "Cannot render text!" << SDL_GetError() << "\n";

SDL_FreeSurface(surfaceMessage);
SDL_RenderPresent(renderer);
}

void clean(){
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Game cleaned!\n";
}

void handleEvents(){
SDL_Event event;
SDL_PollEvent(&event);
switch(event.type){
case SDL_QUIT:
runningOnEmpty = false;
break;

default:
break;
}
}

int main(){
title = "Test";
xpos = SDL_WINDOWPOS_CENTERED;
ypos = SDL_WINDOWPOS_CENTERED;

width = 800, height = 900;
fullscreen = false;

init();
render();

while(runningOnEmpty){
handleEvents();
}

clean();

return 0;
}

它编译没有错误,当我运行可执行文件时,它呈现一个白色窗口,上面没有文本(即使它应该有一些文本)。

当我尝试使用 SDL_RenderCopy 时出现错误,正如我使用 SDL_Error() 确认的那样,显然纹理无效,即使我在使用它之前检查它也是如此。

这是我在运行代码时在控制台中得到的确切信息:

Sdl initialised!!
window created!!
renderer created!!
TTF initialized!
Cannot render text!Invalid texture
Game cleaned!

最佳答案

if(Message = NULL) 是一个赋值,而不是比较,所以你以前的 Message 值丢失了,它在之后是 NULL那,那个结果被用作逻辑值(NULL == 0 == false,就此而言)。启用编译器警告应该会产生一条消息,指出这个特定问题,例如-Wall 为 gcc 提供

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(Message = NULL)

要比较值,请使用 == 运算符。

如果没有 DestroyTexture,您的纹理也会超出范围,这显然是资源泄漏。

关于c++ - 当我尝试在 C++ 中使用 SDL_RenderCopy 复制纹理时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52612133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com