English 中文(简体)
SDL_BlitSurface() 返回-1......为什么?
原标题:SDL_BlitSurface() return -1 ... Why?
  • 时间:2012-04-23 23:57:00
  •  标签:
  • c++
  • sdl

SDL文件指出,SDL_BlitSurface()在不成功的情况下返回-1,但不能找到失败的原因。

这里是我的资料来源法典:

主 席

#include <iostream>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "窗口.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;
}

The error is in 窗口.cpp (i think):

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()
最佳回答

http://www.ohchr.org。 然后在<代码>draw_background()上使用window_bg时,该代码仍为NUL。

问题回答

任何事情都可以是造成这种情况的原因(我无法通过你的法典看待,见),你可以通过打印SDL_GetError(>,说明错误的回报价值来推断错误。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签