SDL窗口不会出现。

时间:2016-03-06 14:09:44

标签: c++ sdl sdl-2

这是我在SDL的第二个项目。我尝试加载位置图像(“C:/Users/user/Desktop/learning_sdl/bpn.bmp”)并将其显示在SDL窗口中。该程序编译没有错误。但是,我只能看到控制台屏幕。 plzz帮助。

#include<iostream>
#include<sdl.h>
using namespace std;

bool init(); //initializes window and surface.
bool loadimage(); //loads image
void close(); //free surface and destroy pointer

SDL_Window* w; //window pointer
SDL_Surface*img; //image pointter
SDL_Surface*surf; //surface pointer
const int scrWidth=640;
const int scrLength=400;

int main(int argc, char*args[])
{
    if(!init)
    {
        cout<<"Sorry, Cannot Initialize the window."<<SDL_GetError();
    }
    else
    {
        if(!loadimage())
        {
            cout<<"Cannot load images."<<SDL_GetError();
        }
        else
        {
             SDL_BlitSurface( img, NULL, surf, NULL );
             SDL_UpdateWindowSurface(w);
             SDL_Delay(10000);
        }
    }

    close();
}



bool init()
{
    bool status=true;
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
       {
           cout<<"Cannot initialize SDL"<<SDL_GetError();
           status=false;
       }
    else
    {
        w=SDL_CreateWindow("bipin",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,scrWidth,scrLength,SDL_WINDOW_SHOWN);
        if (w==NULL)
        {
            cout<<"Cannot create sdl window"<<SDL_GetError();
            status=false;
        }
        else
        {
            surf=SDL_GetWindowSurface(w);
            if (surf==NULL) status=false;
        }
    }
    return status;
}



bool loadimage()
{
    bool status=true;
    img=SDL_LoadBMP("C:/Users/user/Desktop/learning_sdl/bpn.bmp");
    if (img==NULL)
    {
        cout<<"Unable to find image."<<SDL_GetError();
       status=false;
    }
    return status;
}




void close()
{
    SDL_FreeSurface(img);
    SDL_FreeSurface(surf);
    img=NULL;
    surf=NULL;
    SDL_DestroyWindow(w);
    w=NULL;
    SDL_Quit();
}

1 个答案:

答案 0 :(得分:0)

在你的代码中,在main函数上:

if(!init)
{
    cout<<"Sorry, Cannot Initialize the window."<<SDL_GetError();
}

请注意 init 是一个函数,但不是函数调用,而是使用:

if(!init())
{
    cout<<"Sorry, Cannot Initialize the window."<<SDL_GetError();
}