1

次のように、チュートリアルを試していました。

.png簡単なコード スニペットを使用して、Portable Network Graphics () ファイルをアプリケーション内で読み込んで表示しようとしました。

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"

#include <stdio.h>
#include <string>

//The attributes of the screen
const int screen_width = 640;
const int screen_height = 480;
const int screen_bpp = 32;              

//The surfaces that will be used
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *message = NULL;

SDL_Surface *load_image( std::string filename ) 
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    SDL_RWops *rwop;
    rwop=SDL_RWFromFile(filename.c_str(), "rb");
    if(IMG_isPNG(rwop))
        printf("%s is a PNG file.\n", filename.c_str());
    else
        printf("%s is not a PNG file, or PNG support is not available.\n", filename.c_str());

    //Load the image using SDL_image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}


void apply_surface(int x, int y, SDL_Surface *source_surface, SDL_Surface *destintion_Surface)
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect rectangle;

    //Give the offsets to the rectangle
    rectangle.x = x;
    rectangle.y = y;

    //Blit the surface
    SDL_BlitSurface(source_surface, NULL, destintion_Surface, &rectangle);
}

int main(int argc, char** argv)
{
    //Initialize all SDL subsystems
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return 1;

    //Set up the screen
    screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);

    //If there was an error in setting up the screen
    if(screen == NULL)
        return 1;

    //Set the window caption
    SDL_WM_SetCaption("Surface Bliting", NULL);

    //Load the images
    background = load_image("cute2.png");
    message = load_image("cute4.png");

    //Apply the background to the screen
    apply_surface(0, 0, background, screen);
    apply_surface(320, 0, background, screen);
    apply_surface(0, 240, background, screen);              
    apply_surface(320, 240, background, screen);

    //Apply the message to the screen
     apply_surface( 180, 140, message, screen );    

    //Update the screen
    if(SDL_Flip(screen) == -1)
        return 1;

    SDL_Delay(12000);

    SDL_FreeSurface(background);
    SDL_FreeSurface(message);

    //Quit SDL
     SDL_Quit();

    return 0;
}

現在、Visual Stdio 2008 内で、アプリケーションは非常にうまく動作しています。

しかし.exe、アプリケーションから直接実行しようとすると:

E:\SDL_sample\SDL Image Extension Libraries\Release\"SDL Image Extension Libraries.exe"

stdout.txtメッセージを表示しています:

cute2.png は PNG ファイルではないか、PNG のサポートが利用できません。
cute4.png は PNG ファイルではないか、PNG のサポートが利用できません。

何も表示/レンダリングせずにウィンドウが閉じます。

Visual Studio 2008 内でアプリケーションをビルド/実行するときにイメージが正常にロードされる方法がわかりませんが、実行する.exeとイメージがロードされず、イメージ ファイル、dll、およびすべてが同じ場所にあります。

4

2 に答える 2

0

emartelが言ったことに対する追加の回答として、 Working Directoryを変更する必要があります。Project > Properties > Configuration Properties > Debugging > Working Directoryに移動します。出力ディレクトリと同様に作業ディレクトリを変更し、そこにすべてのメディア (画像、サウンド、.etc) ファイルをコピーします。

アプリで画像が表示される理由は、VS がデフォルトでプロジェクト ディレクトリを現在の作業ディレクトリとして使用し、そこにメディア / アセット ファイルを配置すると想定しているためです。別の方法として、WinAPI SetCurrentDirectoryを使用してコード内で作業ディレクトリを設定できます。

于 2013-05-24T01:53:13.593 に答える