c++ - 为什么我的图像无法渲染?

标签 c++ sdl render sdl-1.2

所以我正在使用 SDL(在 Visual Studio 2013 上)学习 C++。我正在关注 Lazy Foo 的教程(特别是:http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php)。

我的图像无法呈现。与教程(有效)相反,我没有全局表面变量。相反,我在 main 中声明了它们并传递了指针。

代码:

#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;
}

最佳答案

您的程序展示了 undefined behavior ,因为 main 函数中的指针初始化。

当您将它们传递给 initialiseSDL 函数时,您按值传递它们,这意味着它们被复制并且该函数仅对拷贝而不是原始指针进行操作,原始指针在调用后仍将未初始化.

您需要做的是通过引用传递指针:

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

您当然需要对 loadMedia 函数执行相同的操作。

关于c++ - 为什么我的图像无法渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138717/

相关文章:

javascript - 解析从 python(tornado) 到 javascript 的路径变量

java - 如何最好地用Java实现 war 迷雾?

c++ - 在程序集名称中创建具有特殊字符的 NuGet 包

java - 使用 Java 将模拟的 Windows 键盘事件发送到使用 SDL 的 C 程序

python - 在 Python 中加载 C++ 类

c++ - main 已经在 main.obj 中定义

image - SDL_image PNG 透明度错误

android - CSS + Phonegap = 渲染不正确

c++ - Halcon 9.0 在 Windows 10(但不是 Windows 8.1)下生成错误

c++ - 虚拟基类析构函数调用顺序?