C++ “我怎么能?”;“打印”;将位图图像复制到窗口

C++ “我怎么能?”;“打印”;将位图图像复制到窗口,c++,visual-studio-2015,sdl,C++,Visual Studio 2015,Sdl,当我构建程序时,它无法加载媒体,因为它找不到映像,尽管映像位于工作目录中,其中也包含“vcxproj”和我的可执行文件。这是正确的目录还是我链接不正确 Main.cpp //Using SDL and standard IO #include <SDL.h> #include <stdio.h> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480

当我构建程序时,它无法加载媒体,因为它找不到映像,尽管映像位于工作目录中,其中也包含“vcxproj”和我的可执行文件。这是正确的目录还是我链接不正确

Main.cpp

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;


bool init(); //starts SDL and creates a window

bool loadmedia(); //close function

void close(); //close function



SDL_Window* gWindow = NULL; //The window we will be rendering to

SDL_Surface* gSurface = NULL; //the surface contained by the window

SDL_Surface* gOpening = NULL; //The image we will load to screen

bool init()
{
    //Initialization flag
    bool success = true;

    //Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow("Welcome to Tenno", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (gWindow == NULL)
        {
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = false;
        }
        else
        {
            //Get window surface
            gSurface = SDL_GetWindowSurface(gWindow);
        }
    }

    return success;
}


bool loadMedia()
{
    //Loading success flag
    bool success = true;

    //Load splash image
    gOpening = SDL_LoadBMP("Project8A\Opening.bmp");
    if (gOpening == NULL)
    {
        printf("Unable to load image %s! SDL Error: %s\n", "Opening.bmp", SDL_GetError());
        success = false;
    }

    return success;
}

void close()
{
    //Deallocate surface
    SDL_FreeSurface(gOpening);
    gOpening = NULL;

    //Destroy window
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

int main( int argc, char* args[] )
{
    //Start up SDL and create window
    if (!init())
    {
        printf("Failed to initialize!\n");
    }
    else
    {
        //Load media
        if (!loadMedia())
        {
            printf("Failed to load media!\n");
        }
        else
        {
            //Apply the image
            SDL_BlitSurface(gOpening, NULL, gSurface, NULL);

            //Update the surface
            SDL_UpdateWindowSurface(gWindow);

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

    //Free resources and close SDL
    close();

    return 0;
}
//使用SDL和标准IO
#包括
#包括
//屏幕尺寸常数
屏幕宽度=640;
屏幕上的常数=480;
bool init()//启动SDL并创建一个窗口
bool loadmedia()//闭合函数
无效关闭()//闭合函数
SDL_窗口*gWindow=NULL//我们将渲染到的窗口
SDL_表面*gSurface=NULL//窗户所包含的表面
SDL_表面*gOpening=NULL//我们将加载到屏幕上的图像
boolinit()
{
//初始化标志
布尔成功=真;
//初始化SDL
if(SDL_Init(SDL_Init_视频)<0)
{
printf(“SDL无法初始化!SDL_错误:%s\n”,SDL_GetError());
成功=错误;
}
其他的
{
//创建窗口
gWindow=SDL_CreateWindow(“欢迎使用Tenno”,SDL_WINDOWPOS_未定义,SDL_WINDOWPOS_未定义,屏幕宽度,屏幕高度,显示SDL_窗口”);
if(gWindow==NULL)
{
printf(“无法创建窗口!SDL_错误:%s\n”,SDL_GetError());
成功=错误;
}
其他的
{
//获取窗口表面
gSurface=SDL_GetWindowSurface(gWindow);
}
}
回归成功;
}
bool loadMedia()
{
//加载成功标志
布尔成功=真;
//加载飞溅图像
gOpening=SDL_LoadBMP(“Project8A\Opening.bmp”);
if(gOpening==NULL)
{
printf(“无法加载图像%s!SDL错误:%s\n”,“正在打开.bmp”,SDL_GetError());
成功=错误;
}
回归成功;
}
无效关闭()
{
//释放面
SDL_自由曲面(gOpening);
gOpening=NULL;
//破坏窗口
SDL_(格温多);
gWindow=NULL;
//退出SDL子系统
SDL_退出();
}
int main(int argc,char*args[]
{
//启动SDL并创建窗口
如果(!init())
{
printf(“初始化失败!\n”);
}
其他的
{
//加载媒体
如果(!loadMedia())
{
printf(“加载媒体失败!\n”);
}
其他的
{
//应用图像
SDL_BlitSurface(gOpening,NULL,gSurface,NULL);
//更新曲面
SDL_更新内表面(gWindow);
//等两秒钟
SDL_延迟(2000年);
}
}
//释放资源并关闭SDL
close();
返回0;
}

位图路径中有一个错误的转义字符。应该是
“Project8A\\Opening.bmp”
@Sarvadi仍然没有成功:(因为它是一个相对的文件路径,你确定你的程序在正确的目录中吗?@Sarvadi如何检查?我会调用_getcwd并打印结果。我肯定有一种更好的方法我不知道。