2016-06-24 70 views
0

我使用的是Code :: Blocks 16.01,以及LazyFoo的SDL2.0教程。我無法使用SDL_LoadBMP()加載圖像。無法在CodeBlocks中使用SDL_LoadBMP加載bmp文件

這是我第一次在這裏問一個問題。我做了Google的回答並搜索了這裏的現有問題/答案,但無法解決問題。

這是我的代碼:

/*This source code copyrighted by Lazy Foo' Productions (2004-2015) 
and may not be redistributed without written permission.*/ 

//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 - Lesson 02", 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("/02_getting_an_image_on_the_screen/hello_world.bmp"); 
    if(gHelloWorld == NULL) 
    { 
     printf("Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/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); 
     } 
    } 

    //Free resources and close SDL 
    close(); 

    return 0; 
} 

此代碼工作正確的,當我指定的完整路徑。但是有沒有一種更簡單的方法來做到這一點,而不是總是指定整個路徑?

我在哪裏放置圖像,以及如何指定它的路徑?

我將不勝感激您提供的任何幫助。

回答

0

刪除/02_getting_an_image_on_the_screen/hello_world.bmp。 如果您不在IDE之外,則必須將該文件放置在項目文件夾中/與可執行文件位於同一目錄中。

+0

我把我的文件放在我的項目文件夾(02_getting_an_image_on_the_screen),但它不工作。我刪除/但它不起作用。 – FKH

+0

如果「02_getting_an_image_on_the_screen」是您的項目名稱,您必須將文件放入其中並打開它,只需命名您的文件名。 「hello_world.bmp」 – Shiro

+0

謝謝。我刪除(/ 02_getting_an_image_on_the_screen /),並且正常工作。 – FKH

0

我將/02_getting_an_image_on_the_screen/hello_world.bmp替換爲../02_getting_an_image_on_the_screen/hello_world.bmp,它的工作原理也是正確的。 :)

+0

但這似乎有點多餘。 – Shiro