Home > Net >  Can't draw simple bitmap to window in SDL2
Can't draw simple bitmap to window in SDL2

Time:11-19

I'm currently trying to set up a few C libraries for a future project. Namely, SDL2. Here's my code:

#include <iostream>
#include <fstream>
#include <SDL.h>

int SCREEN_WIDTH = 457;
int SCREEN_HEIGHT = 497;

const char* imgpath = "Sprite.bmp";

std::string errmsg;

SDL_Window* window = NULL;
SDL_Surface* screensurface = NULL;
SDL_Surface* image = NULL;
SDL_Rect rect;

struct {
    bool wdown;
    bool adown;
    bool sdown;
    bool ddown;
    bool edown;
    bool escdown;
} kpresses;

void clearkpresses() {
    kpresses.wdown = false;
    kpresses.adown = false;
    kpresses.sdown = false;
    kpresses.ddown = false;
    kpresses.edown = false;
    kpresses.escdown = false;
}
void setrect() {
    rect.x = 0;
    rect.y = 0;
    rect.w = 457;
    rect.h = 497;
}
bool gameinit() {
    std::ofstream errfile;
    errfile.open("errlog.txt");
    clearkpresses();
    setrect();
    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0 ) {
        errmsg = "SDL could not initialize! SDL_Error:";
        std::cout << errmsg << SDL_GetError() << std::endl;
        errfile << errmsg << SDL_GetError() << std::endl;
        errfile.std::ofstream::close();
        
        return false;
    }
    screensurface = SDL_GetWindowSurface(window);
    window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    if( window == NULL ){
        errmsg = "Window could not be created! SDL_Error:";
        std::cout << errmsg << SDL_GetError() << std::endl;
        errfile << errmsg << SDL_GetError() << std::endl;
        errfile.std::ofstream::close();

        return false;
    }
    image = SDL_LoadBMP(imgpath);
    if (image == NULL) {
        errmsg = "Media unable to load! IMG Error: ";
        std::cout << errmsg << SDL_GetError() << std::endl;
        errfile << errmsg << SDL_GetError() << std::endl;
        errfile.std::ofstream::close();

        return false;
    }
    return true;
}

void gamehalt()
{
    SDL_DestroyWindow(window);
    window = NULL;
    SDL_Quit();
}

int main(int argc, char* args[]) {
    if (!gameinit()) {
        gamehalt();
        return 0;
    }
    bool quit = false;
    SDL_Event event;
    while (!quit) {
        while (SDL_PollEvent(&event) != 0) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
            switch(event.type) {
                case SDLK_w:
                    kpresses.wdown = true;
                case SDLK_a:
                    kpresses.adown = true;
                case SDLK_s:
                    kpresses.sdown = true;
                case SDLK_d:
                    kpresses.ddown = true;
                case SDLK_e:
                    kpresses.edown = true;
                case SDLK_ESCAPE:
                    kpresses.escdown = true;
            }
        }
        //TODOUpdate
        //TODORender
        SDL_BlitSurface(image, NULL, screensurface, &rect);
        SDL_UpdateWindowSurface(window);
        //reset
        clearkpresses();
    }
    gamehalt();
    return 0;
}

Compiled, assembled, and linked with this windows cmd command:

g   Main.cpp -static-libgcc -static-libstdc   -I..\include\SDL2\include\SDL2 -I..\include\SDL2_image\include\SDL2 -L..\include\SDL2\lib -L..\include\SDL2_image\lib -w -lmingw32 -lSDL2main -lSDL2 -o ../Transcend.exe

It compiles and runs with no errors, but only displays a blank screen. Not really sure what's going on here, any help is appreciated. Thanks

CodePudding user response:

The problem is that you are trying to create a surface from the window, but the window isn't already created.

Please swap those two lines:

screensurface = SDL_GetWindowSurface(window);
window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

This way:

window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
screensurface = SDL_GetWindowSurface(window);
  • Related