2017-04-23 75 views
0

我想从文件中加载图像,而不使用整个文件路径。就像在C#中一样,但是当我们尝试加载图像时,只是在控制台中引发我的sdl错误。从文件sdl_loadbmp不使用整个文件路径

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

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

     return success; 
    } 

这是我的,我从一个在线教程中学原代码的一部分,试图教自己SDL和更多关于C++

编辑为InternetAussie,这是什么,我认为我在我的整个源代码从教程中学低于:

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

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

//Starts up SDL and creates window 
bool init(); 

//Loads media 
bool loadMedia(); 

//Frees media and shuts down SDL 
void close(); 

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

//The surface contained by the window 
SDL_Surface* gScreenSurface = NULL; 

//The image we will load and show on the screen 
SDL_Surface* gHelloWorld = NULL; 

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("SDL Tutorial", 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 
     gScreenSurface = SDL_GetWindowSurface(gWindow); 
    } 
} 

return success; 
} 

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

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

return success; 
} 

void close() 
{ 
//Deallocate surface 
SDL_FreeSurface(gHelloWorld); 
gHelloWorld = 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(gHelloWorld, NULL, gScreenSurface, NULL); 

     //Update the surface 
     SDL_UpdateWindowSurface(gWindow); 

     //Wait two seconds 
     SDL_Delay(2000); 
    } 
} 
    //system halt for testing 
system("pause"); 

//Free resources and close SDL 
close(); 

return 0; 
} 

,如果它可以帮助我跟着在懒惰富”制作教程1中的说明。我希望我已经适当添加的超链接仅仅是点击如何在使用visual studio 2010 ultimate的windows计算机上设置sdl的步骤之后的设置部分。我使用控制台将系统32安装程序作为错误输出源。

+0

你是如何运行可执行文件的?如果您自己运行它,则通常会从包含的目录中获取相对路径。否则,工作目录可能是任何东西。 – InternetAussie

+2

通常阅读'getcwd()'和'chdir()'和相对路径。 –

+0

看看是否有效:构建可执行文件;将SDL2_tutorials02'目录放置在与可执行文件相同的目录中;并双击Explorer中的可执行文件,而不是从IDE(它改变工作目录)运行它。希望有帮助。 – InternetAussie

回答

1

正如InternetAussie已经说过,如果你正在运行编译可执行所有的路径是相对于你的可执行文件从运行的目录。

您的问题似乎与您的IDE有关。当您从IDE构建并运行代码时,工作目录将从其他位置获取。它可以是IDE构建程序调试版本的默认路径。很难说清楚,因为它们都是不同的。

如果你要调试SDL的项目,你应该设置工作目录编译器正在编译您的可执行文件到该目录中,并在那里你也有你想读保存您的文件。

F.即:如果您正在使用visual studio,您可以通过右键单击项目>设置>调试>工作路径选项来设置此项,或者如果使用cmake构建项目,也可以使用SET_PROPERTY(TARGET Project_name PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "path")进行设置。

如果这不能解决您的问题,请随时提问。另外,如果您使用的是cmake和VS,我可以为您提供完整的cmakelists.txt,可为VisualStudio生成SDL2项目并设置所有必需的设置。

+0

我认为它是在我开始安装VS2010 Ultimate时在视觉工作室创建的项目文件夹中。就像在我之前的c#程序中,我将.bmp图像添加到项目文件夹中的相应项目中。 – SubZero