在 C++ 中尝试使用 SDL_RenderCopy 复制纹理时出现错误
I have an error when I'm trying to copy a texture with SDL_RenderCopy, in C++
这是我的 .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;
}
它编译没有错误,当我 运行 可执行文件时它呈现白色 window,上面没有文本(即使它应该有一些文本)。
当我尝试使用 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 的情况下超出了范围,这显然是资源泄漏。
这是我的 .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;
}
它编译没有错误,当我 运行 可执行文件时它呈现白色 window,上面没有文本(即使它应该有一些文本)。
当我尝试使用 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 的情况下超出了范围,这显然是资源泄漏。