使用C ++在SDL中显示.png文件

时间:2014-03-28 06:56:13

标签: c++ png sdl

用C ++写作:我试图让骑士骑着马(不可思议的名字"家伙")穿过屏幕。骑士目前作为2个.png文件存在于我的目录中,以模拟一些动画不佳的舞动。当他是.bmp时,我能够让他出现,但我想利用png文件的透明度 - 我也试过打开tif文件而失败了。我怎样才能改变我的代码,以便让他从.png正确加载到SDL中?这是我的.cpp文件:

#include "SDL/SDL.h"
#include <iostream>
#include "source_sdl.h"
# include <string>

using namespace std;

int source_sdl( int argc, char* args[] ) {

int switch = 1;
int running = 1;

// surface & rect declarations
SDL_Surface* ground = NULL;
SDL_Surface* guy = NULL;
SDL_Surface* guy2 = NULL;
SDL_Rect guylocationRect;
SDL_Rect groundlocationRect;

// initialize SDL & the screen
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode( 875, 625, 32, SDL_SWSURFACE );

// open .png files
SDL_RWops* guy_rwop;
SDL_RWops* guy2_rwop;
guy_rwop = SDL_RWFromFile("tiffKnight.png", "rb");
guy2_rwop = SDL_RWFromFile("tiffKnight2.png", "rb");
guy = IMG_LoadPNG_RW(guy_rwop);
guy2 = IMG_LoadPNG_RW(guy2_rwop);
guylocationRect.x = 300;
guylocationRect.y = 300;
groundlocationRect.x = 300;
groundlocationRect.y = 300;

SDL_Event occur;

// animation loop (currently endless)
while (running == 1){

    SDL_Flip( screen );
    SDL_Delay( 300 );

    if (gallop > 89) gallop=0;

    // draw the ground
    for( int yu = 0; yu<35; yu++){
         groundlocationRect.x=25*yu;
         groundlocationRect.y=5*25;
         SDL_BlitSurface( ground, NULL, screen, &groundlocationRect );
    }
    // draw the gallopping
    guylocationRect.x=10*gallop;
    guylocationRect.y=5*25;
    if( switch ){
        SDL_BlitSurface( guy, NULL, screen, &guylocationRect );
    }else{
        SDL_BlitSurface( guy2, NULL, screen, &guylocationRect );
    }
    gallop++;
    switch = (switch+1)%2;
    for(int u = 6; u < 25; u++){
        for(int yu = 0; yu < 35; yu++){ 
            groundlocationRect.x = 25*yu;
            groundlocationRect.y = 25*u;
            SDL_BlitSurface( ground, NULL, screen, &groundlocationRect );
        }
    }
}
SDL_FreeSurface( guy );
SDL_FreeSurface( guy2 );
SDL_FreeSurface( ground );
}

我目前拥有它,骑士没有出现,我没有收到任何错误(SDL屏幕被打开的结果?)失败检查,如

if(!guy) {
    cout << "IMG_LoadPNG_RW: %s\n" << IMG_GetError();
}

也没有产生任何结果。我的.h文件很简单:

#include "SDL/SDL.h"
#include <iostream>

using namespace std;
 int source_sdl( int argc, char* args[] );

我的main.cpp:

#include "SDL/SDL.h"
#include <iostream>
#include "source_sdl.h"
using namespace std;

int main( int argc, char* args[] ){

    source_sdl( argc, args );

}

任何见解都会很精彩,因为这位骑士最终应该成为塔防游戏中的敌人!提前谢谢!

以下是我从中获取有关如何将不同文件类型加载到SDL的信息的页面:http://www.libsdl.org/projects/SDL_image/docs/SDL_image.html#SEC24

1 个答案:

答案 0 :(得分:2)

如果您真的想在C ++中坚持使用SDL,我建议您使用SDL_image library并使用如下函数:

SDL_Surface * load_image(std::string const & filename)
{
    SDL_Surface * tmp;
    SDL_Surface * img;

    img = NULL;
    if ((tmp = IMG_Load(filename.c_str())))
    {
      img = SDL_DisplayFormatAlpha(tmp);
      SDL_FreeSurface(tmp);
    }
    return img;
}

如果您想要使用该库的更现代和更安全的方法:

surface_ptr load_image (std::string const & filename)
{
    using surface_ptr = std::unique_ptr<SDL_Surface, decltype(SDL_FreeSurface) *>;

    surface_ptr img { nullptr, SDL_FreeSurface };
    surface_ptr tmp { IMG_Load(filename.c_str()), SDL_FreeSurface };

    if (tmp)
        img.reset(SDL_DisplayFormatAlpha(tmp.get()));

    return img;
}