3D并不是必需的,但是3D世界中常见的painter算法可能会恕我直言为您节省一些工作:
绘制器算法的工作原理是先绘制最远的对象,然后再绘制更靠近相机的对象.在您的情况下,它将归结为生成精灵的缓冲区,将其绘制到缓冲区上,找到下一个依赖的sprite-part(即装甲或其他东西),绘制该对象,找到下一个依赖的sprite-part(即特殊的标记在盔甲上),依此类推.当没有更多的依赖零件时,可以将完整的生成的精灵绘制到用户看到的显示器上.
合并的部件应具有Alpha通道(RGBA而不是RGB),以便您仅合并Alpha值设置为所选值的部件.如果由于某种原因无法做到这一点,只需坚持使用一种将被视为透明的RGB组合即可.
使用3D可能会使您更容易地组合各部分,并且您甚至不必使用屏幕外缓冲区或编写像素组合代码.不利的一面是,如果您还不了解3D,则需要学习一些3D. ?
编辑以回答评论:
组合部分的工作原理如下(在C中,Java将非常相似-请注意,我没有通过编译器运行以下代码):
//
// @param dependant_textures is a vector of textures where
// texture n+1 depends on texture n.
// @param combimed_tex is the output of all textures combined
void Sprite::combineTextures (vector const& dependant_textures,
Texture& combined_tex) {
vector< Texture >::iterator iter = dependant_textures.begin();
combined_tex = *iter;
if (dependant_textures.size() > 1)
for (iter++; iter != dependant_textures.end(); iter++) {
Texture& current_tex = *iter;
// Go through each pixel, painting:
for (unsigned char pixel_index = 0;
pixel_index < current_tex.numPixels(); pixel_index++) {
// Assuming that Texture had a method to export the raw pixel data
// as an array of chars - to illustrate, check Alpha value:
int const BYTESPERPIXEL = 4; // RGBA
if (!current_tex.getRawData()[pixel_index * BYTESPERPIXEL + 3])
for (int copied_bytes = 0; copied_bytes < 3; copied_bytes++)
{
int index = pixel_index * BYTESPERPIXEL + copied_bytes;
combined_tex.getRawData()[index] =
current_tex.getRawData()[index];
}
}
}
}
要回答有关3D解决方案的问题,您只需在彼此之间绘制具有各自纹理(具有alpha通道)的矩形即可.您可以将系统设置为以正交模式显示(对于OpenGL:gluOrtho2D()).