SDL 窗口不显示。

SDL Window doesn't appear.

本文关键字:显示 窗口 SDL      更新时间:2023-10-16

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

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

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

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

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

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