我想從文件中加載圖像,而不使用整個文件路徑。就像在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安裝程序作爲錯誤輸出源。
你是如何運行可執行文件的?如果您自己運行它,則通常會從包含的目錄中獲取相對路徑。否則,工作目錄可能是任何東西。 – InternetAussie
通常閱讀'getcwd()'和'chdir()'和相對路徑。 –
看看是否有效:構建可執行文件;將SDL2_tutorials02'目錄放置在與可執行文件相同的目錄中;並雙擊Explorer中的可執行文件,而不是從IDE(它改變工作目錄)運行它。希望有幫助。 – InternetAussie