简体   繁体   中英

Why won't my image render?

So I'm learning C++ with SDL (on Visual Studio 2013). I'm following Lazy Foo's tutorials (specifically: http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php ).

My image won't render. In contrast to the tutorial (which works) I do not have global surface variables. Instead I declared them in the main and passed the pointers around.

Code:

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>

const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 480;

//SDL init.
bool initialiseSDL(SDL_Window* gWindow, SDL_Surface* gWindowSurface)
{
    bool success = true;

    /*SDL_Init() initisalises SDL library. Returning 0 or less signifies an error.
      SDL_INIT_VIDEO flag refers to graphics sub system.*/
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialise. SDL Error: %s\n", SDL_GetError());
        success = false;
    }
    else
    {
        //Create the window
        gWindow = SDL_CreateWindow("Asteroids", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (gWindow == NULL)
        {
            printf("Could not create window. SDL Error: %s\n", SDL_GetError());
            success = false;
        }
        else
        {
            //Get the window surface.
            gWindowSurface = SDL_GetWindowSurface(gWindow);
        }
    }
    return success;
}

bool loadMedia(SDL_Surface* surface, std::string path)
{
    //Success flag
    bool success = true;

    surface = SDL_LoadBMP(path.c_str());

    if (surface == NULL)
    {
        printf("SDL surface failed to load. SDL Error: %s\n", SDL_GetError());
        success = false;
    }

    return success;
}

void close()
{

}

int main(int argc, char* argv[])
{
    SDL_Window* gWindow = NULL;
    SDL_Surface* gWindowSurface = NULL;
    SDL_Surface* gImageSurface = NULL;

    if (!initialiseSDL(gWindow, gWindowSurface))
    {
        printf("Failed to initialise.\n");
    }
    else
    {

        if (!loadMedia(gImageSurface, "hw.bmp"))
        {
            printf("Failed to load inital media.");
        }
        else
        {
            //Apply the image
            SDL_BlitSurface(gImageSurface, NULL, gWindowSurface, NULL);

            //Update the surface
            SDL_UpdateWindowSurface(gWindow);

            //Wait two seconds
            SDL_Delay(2000);
        }
    }

    return 0;
}

Your program exhibits undefined behavior , because the pointers inside the main function are not initialized.

When you pass them to the initialiseSDL function you pass them by value, meaning they are copied and the function only operates on the copies and not the original pointers, which will still be uninitialized after the call.

What you need to do is to pass the pointers by reference instead:

bool initialiseSDL(SDL_Window*& gWindow, SDL_Surface*& gWindowSurface)

You of course need to do the same with the loadMedia function.

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