Search code examples
c++sdlblit

Image not rendering .bmp image C++ SDL


I am learning SDL, and I am trying to render a .bmp image, but it just displays a black screen. Here is the code:

#include <SDL.h>
#include "engine.cpp"
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
    //Init image's pointers
    bool quit = false;
    //Render a window
    SDL_Event event;
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Surface *imageSurface = NULL;
    SDL_Surface* windowSurface = NULL;

    SDL_Window* window = SDL_CreateWindow("SDL2 Displaying Image", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    windowSurface = SDL_GetWindowSurface(window);
    imageSurface = SDL_LoadBMP("hello.bmp");



    while (!quit) {
        SDL_WaitEvent(&event);
        switch (event.type) {
        case SDL_QUIT:
            quit = true;
            break;
        }
    }

    SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);

    SDL_UpdateWindowSurface(window);
    
    SDL_Delay(2000);
    SDL_Quit();
    return 0;
}

What I am trying to do is render a .bmp image. I am currently only bug testing, so please don't roast me for not using SDL_Quit(). I have looked at any solution to this issue I could find, and none of them have worked. Please help!


Solution

  • You told the computer to wait until you quit the program, and then draw the picture.

    You probably want to draw the picture straight away, before you quit the program.