Johnny Sasaki
Johnny Sasaki

Reputation: 45

SDL: Blitting BMP to window surface black screen mystery

I've written the following code to load a BMP image as a surface and then blit that image onto the window:

#include "stdafx.h"
#include "SDL.h"
#include <iostream>

int main(int argc, char *argv[])
{
    //init
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Playground", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, 0);
    std::cout << SDL_GetError() << std::endl;
    SDL_Surface* surface = SDL_GetWindowSurface(window);

    //load file and convert to texture
    SDL_Surface* bmp = SDL_LoadBMP("sample.bmp");
    std::cout << SDL_GetError() << std::endl;

    //render texture
    SDL_Rect area;
    area.x, area.y = 3;
    area.h, area.w = 25;
    SDL_BlitSurface(bmp, &area, surface, &area);
    std::cout << SDL_GetError() << std::endl;
    SDL_UpdateWindowSurface(window);
    std::cout << SDL_GetError() << std::endl;
    SDL_Delay(3000);

    //clean up
    SDL_FreeSurface(bmp);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

When I press F5 (I'm working in Visual Studio Express 2017) to build and run the program, the program created runs, creates a window, and then the window remains entirely black as the program runs. I receive no error messages from V.S., SDL_GetError(), or Windows. There appears to be no problems but the image just gets lost somewhere, it seems. Would anyone be able to help me?

P.S. Here is the bmp I am trying to display:

the bmp I am trying to display

Upvotes: 0

Views: 800

Answers (1)

Jack
Jack

Reputation: 133577

This code doesn't do what you think it does:

area.x, area.y = 3;
area.h, area.w = 25;

You should change it to

area.x = area.y = 3;
area.h = area.w = 25;

to have multiple assignments. Or even better just initialize SDL_Rect inline:

SDL_Rect area = { 3, 3, 25, 25 };

Upvotes: 2

Related Questions