简体   繁体   English

使用SDL将图像添加到窗口— c ++

[英]Add image to window with SDL — c++

I want to add an image to a window. 我想将图像添加到窗口。 This is my code: 这是我的代码:

SDL_Surface* bitmap = SDL_LoadBMP("bat.bmp");

// Part of the bitmap that we want to draw
SDL_Rect source;
source.x = 24;
source.y = 63;
source.w = 65;
source.h = 44;

// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = 100;
destination.y = 100;
destination.w = 65;
destination.h = 44;

SDL_Event event;
bool gameRunning = true;

while (gameRunning)
{
    if (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
        {
            gameRunning = false;
        }
    }

    SDL_BlitSurface(bitmap, &source, screen, &destination);

    SDL_Flip(screen);
}

When the window is loaded it turns black and the image wont appear. 加载窗口后,它将变为黑色,并且不会出现图像。 What's wrong? 怎么了?

You code is fine. 您的代码很好。

Did you check if SDL_Surface* bitmap = SDL_LoadBMP("bat.bmp"); 您是否检查过SDL_Surface* bitmap = SDL_LoadBMP("bat.bmp"); is returning a proper address? 返回正确的地址?
It will return NULL on failure to load the image. 如果无法加载图像,它将返回NULL。

There might also be a problem with SDL_Surface screen but that is not shown in the code. SDL_Surface screen可能也有问题,但是代码中未显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM