SDL_BlitSurface() 返回 -1 ..为什么
SDL_BlitSurface() return -1 ... Why?
SDL 文档说 SDL_BlitSurface() 在不成功时返回 -1,但我找不到它会失败的原因。
这是我的源代码:
主.cpp
#include <iostream>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "window.cpp"
#include "player.cpp"
int init();
void quit();
int main(int argc,char *args[])
{
if (init() == -1)
{
std::cerr << "Error:init()" << std::endl;
return -1;
}
window main_window("Juego","img/bg.png",800,600,32);
player player1(500,500,0,0,5,5,"img/square.png");
while (!main_window.close)
{
main_window.handle_events();
if ( main_window.draw_background() != true)
{
std::cerr << "ERROR->main_window.draw_background()" << std::endl;
main_window.close = true;
}
player1.update(main_window.screen);
SDL_Flip(main_window.screen);
SDL_Delay(60/1000);
}
quit();
return 0;
}
int init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
std::cerr << "Error while initialising SDL" << std::endl;
return -1;
}
return 0;
}
void quit()
{
SDL_Quit();
}
窗口.cpp
class window
{
private:
void load_image(std::string source,SDL_Surface *destination);
SDL_Surface *window_bg;
SDL_Event event_queue;
SDL_Rect window_rect;
public:
window(std::string SCREEN_TITLE,std::string SCREEN_BG,int sw,int sh,int sbpp);
void handle_events();
bool draw_background();
SDL_Surface *screen;
bool close;
};
window::window(std::string SCREEN_TITLE,std::string SCREEN_BG,int sw,int sh,int sbpp)
{
window_bg = NULL;
screen = NULL;
close = false;
window_rect.x = 0;
window_rect.y = 0;
window_rect.w = sw;
window_rect.h = sh;
screen = SDL_SetVideoMode(sw,sh,sbpp,SDL_SWSURFACE);
std::cout << "Screen created." << std::endl;
SDL_WM_SetCaption(SCREEN_TITLE.c_str(),NULL);
std::cout << "Window title: " << SCREEN_TITLE << std::endl;
load_image(SCREEN_BG,window_bg);
}
void window::handle_events()
{
while (SDL_PollEvent(&event_queue))
{
if (event_queue.type == SDL_QUIT)
{
close = true;
}
}
}
void window::load_image(std::string source,SDL_Surface *destination)
{
SDL_Surface *tmp_img = IMG_Load(source.c_str());
if (tmp_img != NULL)
{
if ((destination = SDL_DisplayFormat(tmp_img)) == NULL)
{
std::cerr << "ERROR->SDL_DisplayFormat()" << std::endl;
}
std::cout << source << " Loaded." << std::endl;
SDL_FreeSurface(tmp_img);
}
else
{
std::cerr << "Could not load: " << source << std::endl;
}
}
bool window::draw_background()
{
int error_check = SDL_BlitSurface(window_bg,&window_rect,screen,NULL);
if (error_check != 0)
{
std::cerr << "SDL_BlitSurface() == " << error_check << std::endl;
return false;
}
return true;
}
错误在窗口中.cpp(我认为):
bool window::draw_background()
{
int error_check = SDL_BlitSurface(window_bg,NULL,screen,NULL);
if (error_check != 0)
{
std::cerr << "SDL_BlitSurface() == " << error_check << std::endl;
return false;
}
return true;
}
我的程序的输出是:
Screen created.
Window title: Juego
img/bg.png Loaded.
img/square.png Loaded
SDL_BlitSurface() == -1
ERROR->main_window.draw_background()
窗口构造函数中的load_image(SCREEN_BG,window_bg);
加载到临时SDL_Surface中,并且从不填充destination
,我假设这是一个输出参数。 那么当draw_background()
使用 window_bg
时,它仍然是 NULL。
任何事情都
可能是造成这种情况的原因(我无法查看您的代码,请参阅 http://sscce.org/),您可以通过打印错误返回值的SDL_GetError()
来调试问题所在。
相关文章:
- 为什么 Serial.println(<char[]>);返回随机字符?
- 为什么模板类中的对象不能返回值
- 为什么(-1)%vector::size()总是返回0
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 为什么我可以通过引用修改常量返回
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 为什么返回类型中需要typename?C++
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- C 参考class对象的返回 - 为什么不工作
- C 参考和类对象的非参考返回 - 为什么输出相同
- c 参考class对象的返回 - 为什么未调用复制构造函数
- SDL_BlitSurface() 返回 -1 ..为什么
- 将5计算为3的幂,但它返回0.为什么