2017-04-23 1 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을 사용하는 Windows 컴퓨터에서 sdl을 설치하는 방법에 대한 단계를 클릭 한 후 설치 부분에 불과합니다. 콘솔을 사용하여 오류가있는 출력 소스로 시스템 32 설치를 진행했습니다.

+0

실행 파일은 어떻게 실행하고 있습니까? 자체적으로 실행하면 상대 경로는 일반적으로 포함 디렉토리에서 가져옵니다. 그렇지 않으면 작업 디렉토리가 아무 것도 될 수 없습니다. – InternetAussie

+2

'getcwd()'와'chdir()'과 상대 경로를 일반적으로 읽는다. –

+0

이것이 작동하는지 확인하십시오 : 실행 파일을 빌드하십시오; 'SDL2_tutorials02' 디렉토리를 실행 파일과 같은 디렉토리에 두십시오; Explorer에서 실행 파일을 두 번 클릭하십시오 (IDE에서 실행하는 대신 작업 디렉토리가 변경됨). 잘하면 도움이됩니다. – InternetAussie

답변

1

InternetAussie는 이미 컴파일 된 실행 파일을 실행하는 경우 모든 경로가 실행 파일의 실행 경로와 관련이 있다고 말했습니다.

문제는 귀하의 IDE와 관련이있는 것으로 보입니다. IDE에서 코드를 빌드하고 실행할 때 작업 디렉토리는 다른 곳에서 가져옵니다. IDE가 프로그램의 디버그 버전을 빌드하는 기본 경로 일 수 있습니다. 그것은 모두를 위해 다르기 때문에 정확하게 말하기 어렵습니다.

SDL 프로젝트를 디버깅하려면 컴파일러의 작업 디렉토리를 실행 파일을 컴파일 할 디렉토리로 설정하고 읽을 파일을 저장할 위치를 지정해야합니다.

F. e. : Visual Studio를 사용하는 경우 프로젝트> 설정> 디버깅> 작업 경로 옵션을 마우스 오른쪽 버튼으로 클릭하여 설정하거나 cmake로 프로젝트를 빌드하는 경우 SET_PROPERTY(TARGET Project_name PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "path")으로 설정할 수도 있습니다.

그래도 문제가 해결되지 않으면 언제든지 질문하십시오. 또한 cmake와 VS를 사용하는 경우 VisualStudio 용 SDL2 프로젝트를 생성하고 필요한 모든 설정을 완료하는 완전한 cmakelists.txt를 제공 할 수 있습니다.

+0

VS2010 Ultimate을 설치할 때 Visual Studio에서 만든 프로젝트 폴더에 있다고 가정합니다. 이전의 C# 프로그램에서와 마찬가지로 .bmp 이미지를 프로젝트 폴더의 해당 프로젝트에 추가했습니다. – SubZero

관련 문제