简体   繁体   中英

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"); is returning a proper address?
It will return NULL on failure to load the image.

There might also be a problem with SDL_Surface screen but that is not shown in the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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