SDL will only draw a black screen when trying to print a picture C++

class Game {

public:
    Game(int width, int height) {
        gwidth = width;
        gheight = height;
    }
    ~Game() {



    }
    bool initGame() {
        bool sucsess = true;
        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            sucsess = false;
            }

            //gTetrisSurface = SDL_LoadBMP("TetrisBg.bmp");

        gWindow = SDL_CreateWindow("Petris V1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, gwidth, gheight, SDL_WINDOW_SHOWN);
        gScreenSurface = SDL_GetWindowSurface(gWindow);
        gMainBG = SDL_LoadBMP("test.bmp");

        /**if (gMainBG == NULL) {
            return false;
        }**/
        bool running = true;
        //SDL_SetWindowFullscreen(gWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
        while (running == true) {
            SDL_BlitSurface(gMainBG, NULL, gScreenSurface, NULL);
            SDL_UpdateWindowSurface( gWindow );

        }
        return sucsess;

    }

protected:
    int gwidth, gheight;
    SDL_Window* gWindow = NULL;
    SDL_Surface* gScreenSurface = NULL;
    SDL_Surface* gTetrisSurface = NULL;
    SDL_Surface* gPongSurface = NULL;
    SDL_Surface* gMainBG = NULL;

};

The thought behind this, is that this should be one huge surface, containing 2 other surfaces for 2 other games. Problem is, it wont seem to draw the BMP that i try to draw on it. Because of this my development got kinda stuck. Anyone see a possible sulotion for this problem? Thanks!

EDIT: I made it so it flushes the events, BUT, i did a bit of debugging, the problem seems to be that the img file returns NULL, why? It should be loaded with a bmp... (No error messages at all..)

Upvotes: 0

Views: 2127

Answers (2)

The whole problem was that the BMP file was in the wrong folder.

If anyone have the same problem, the picture should either be in the same directory as the src (when debugging) or the solution (if building).

Hope this might help someone in the future, either way, thanks to those who tried coming up with other possibilites.

(Using visual studio)

Upvotes: 0

Joseph Paul
Joseph Paul

Reputation: 346

From the documentation of SDL2: https://wiki.libsdl.org/SDL_GetWindowSurface

This surface will be invalidated if the window is resized. After resizing a window this function must be called again to return a valid surface. You may not combine this with 3D or the rendering API on this window.

My advice is to avoid using the window surface directly. Instead, you should use a renderer and copy your own main surface to your background texture.

The SDL2 migration guide explains clearly the right way to copy surfaces and textures on the screen, especially the paragraph "If your game wants to do both".

Upvotes: 2

Related Questions