我正在爲我的一個小型項目製作紋理管理和動畫解決方案。儘管該項目使用Allegro進行渲染和輸入,但我的問題主要圍繞着C和內存管理。我想在這裏發佈它,以獲得對這種方法的想法和洞察力,因爲在指針方面我很糟糕。紋理管理/指針問題
基本上我想要做的是將所有我的紋理資源加載到中央管理器(textureManager) - 這實質上是一個包含ALLEGRO_BITMAP對象的結構數組。存儲在textureManager中的紋理大多是完整的精靈表。
從那裏,我有一個anim(ation)結構,其中包含特定於動畫的信息(以及指向textureManager中相應紋理的指針)。
爲了給你一個想法,這裏是我如何設置和參賽的棋手「走」的動畫:
createAnimation(&player.animations[0], "media/characters/player/walk.png", player.w, player.h);
playAnimation(&player.animations[0], 10);
渲染動畫當前幀僅僅是塊傳輸存儲在精靈表的特定區域的情況下, textureManager。
僅供參考,以下是anim.h和anim.c的代碼。我確信我在這裏做的事情可能是一個可怕的方法,原因有很多。我想聽聽他們的消息!我是否面臨任何陷阱?這會按我希望的方式工作嗎?
anim.h
#ifndef ANIM_H
#define ANIM_H
#define ANIM_MAX_FRAMES 10
#define MAX_TEXTURES 50
struct texture {
bool active;
ALLEGRO_BITMAP *bmp;
};
struct texture textureManager[MAX_TEXTURES];
typedef struct tAnim {
ALLEGRO_BITMAP **sprite;
int w, h;
int curFrame, numFrames, frameCount;
float delay;
} anim;
void setupTextureManager(void);
int addTexture(char *filename);
int createAnimation(anim *a, char *filename, int w, int h);
void playAnimation(anim *a, float delay);
void updateAnimation(anim *a);
#endif
anim.c
void setupTextureManager() {
int i = 0;
for(i = 0; i < MAX_TEXTURES; i++) {
textureManager[i].active = false;
}
}
int addTextureToManager(char *filename) {
int i = 0;
for(i = 0; i < MAX_TEXTURES; i++) {
if(!textureManager[i].active) {
textureManager[i].bmp = al_load_bitmap(filename);
textureManager[i].active = true;
if(!textureManager[i].bmp) {
printf("Error loading texture: %s", filename);
return -1;
}
return i;
}
}
return -1;
}
int createAnimation(anim *a, char *filename, int w, int h) {
int textureId = addTextureToManager(filename);
if(textureId > -1) {
a->sprite = textureManager[textureId].bmp;
a->w = w;
a->h = h;
a->numFrames = al_get_bitmap_width(a->sprite)/w;
printf("Animation loaded with %i frames, given resource id: %i\n", a->numFrames, textureId);
} else {
printf("Texture manager full\n");
return 1;
}
return 0;
}
void playAnimation(anim *a, float delay) {
a->curFrame = 0;
a->frameCount = 0;
a->delay = delay;
}
void updateAnimation(anim *a) {
a->frameCount ++;
if(a->frameCount >= a->delay) {
a->frameCount = 0;
a->curFrame ++;
if(a->curFrame >= a->numFrames) {
a->curFrame = 0;
}
}
}
ALLEGRO_BITMAP不是一個指針,但你是正確的。由於他使用的是精靈表(一個包含多個圖片的位圖),因此他只需要一個指向單個位圖的指針。 – Matthew 2010-03-16 17:35:43