2012-04-23 11 views
1

SDLドキュメントは、SDL_BlitSurface()が成功しなかった場合は-1を返しますが、なぜ失敗するのかはわかりません。ここでSDL_BlitSurface()は-1を返します...なぜですか?

は、私のソースコードです:

main.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(); 
} 

window.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; 
} 

エラーが(私は思う)window.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() 

答えて

0

load_image(SCREEN_BG,window_bg);は、一時的なSDL_Surfaceにロードされ、destinationは決して出力されません。それでdraw_background()window_bgを使用するとき、それはまだNULLです。

0

ものは、あなたがエラーの戻り値にSDL_GetError()を印刷して何が間違ってデバッグすることができます(私は、あなたのコードに目を通すhttp://sscce.org/を見るためにarsedすることができませんでした)この原因である可能性があります。

+0

申し訳ありませんが、私はちょうどそれをコピーして貼り付けたので、私のコードは大きかったと思った...とにかく、私は私のウィンドウクラスのコンストラクタで、私はエラーを修正したと思うwindow_bg = NULL;私はそれを消去し、今SDL_BlitSurface()はうまくいくようです。しかし、何も画面に描画されません。 – juiko

関連する問題