用C++編寫:我試圖讓一匹騎士穿過屏幕運行(不知名的「傢伙」)。騎士目前在我的目錄中存在2個.png文件,以模擬一些動畫不佳的舞動。當他是.bmp時,我能夠讓他出現,但我想利用png文件的透明度 - 我也嘗試過並且打開tif文件失敗。如何更改我的代碼以便讓他從正確加載到SDL中的.png中出現?這裏是我的.cpp文件:使用C++在SDL中顯示.png文件
#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);
}
正如我現在有它,騎士不會出現,我收到任何錯誤故障檢查,如
if(!guy) {
cout << "IMG_LoadPNG_RW: %s\n" << IMG_GetError();
}
(SDL的屏幕是開放的後果是什麼?)
也沒有收到任何結果。我的.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
因爲傢伙= NULL,你可以嘗試看看是否加載的surfa ce看起來和預期的一樣: printf(「Surface%s:w:%dh:%d bpp:%d \ n」,「guy」,guy-> w,guy-> h,guy-> format-> BitsPerPixel) ; – Dirk
也許檢查SDL_BlitSurface返回碼? – nemasu
出於好奇:爲什麼在使用C++編碼時使用SDL?那SFML不是更好的選擇嗎?由於SDL基於C++上的C和SFML。 – Skalli