//スプライト描画
void sprite(IVideoDriver *driver, char* filename,
int x, int y, int w, int h, int u, int v, int r,
float sx, float sy, SColor color,
bool bColorKey)
{
matrix4 rotate, pos, Ortho, scale, world;
SColor col = color;
char *file = filename;
dimension2d<s32> screen = driver->getScreenSize();
//テクスチャ読み込み
ITexture* Texture;
Texture = driver->getTexture(file);
//[注意]
//黒色が存在する状態で、ほかの色を指定すると抜いても黒くなる
//描画のたびにカラーキー指定すると遅くなる(描画とは別が望ましい)
//アルファを指定する場合は0x80以上だけが有効、それ以外は透明
if(bColorKey)
{
//カラーキー(色指定)
driver->makeColorKeyTexture(Texture, 0x00000000);
//カラーキー(座標指定)
//driver->makeColorKeyTexture(Texture,position2d<s32>(0,0));
}
dimension2d<s32> tex = Texture->getOriginalSize();
//マテリアル設定
SMaterial Material;
Material.Lighting = false;
if(bColorKey)
Material.MaterialType = EMT_TRANSPARENT_ALPHA_CHANNEL;//画像アルファ
else
Material.MaterialType = EMT_TRANSPARENT_VERTEX_ALPHA;//頂点アルファ
Material.TextureLayer[0].Texture = Texture;
//拡大縮小設定
scale.setScale(vector3df(sx,sy,1));//Z軸での拡大縮小はダメ
//位置設定(左上を基準に設定)
float px = ((x)+w/2*sx)/((double)screen.Width/2) - 1.0;
float py = 1.0 - ((y)+h/2*sy)/((double)screen.Height/2);
pos.setTranslation(vector3df(px,py,0));//Z座標での位置指定はダメ
//回転設定(中心で回転)
float rad = (float)(PI/180);//1度を設定
float rz = (r)%360 * rad;
rotate.setRotationRadians(vector3df(0,0,rz));//XY座標での回転はダメ
//ポリゴン作成
u16 list[] = {0,1,2, 2,1,3};
S3DVertex ver[4];
float tx = w/2;
float ty = h/2;
float tw = (float)w/tex.Width;
float th = (float)h/tex.Height;
float tu = (float)(u)/(float)tex.Width;
float tv = (float)(v)/(float)tex.Height;
ver[0] = S3DVertex(-tx, ty, 0, 0,0,0, col, tu, tv );//左上
ver[1] = S3DVertex( tx, ty, 0, 0,0,0, col, tu+tw,tv );//右上
ver[2] = S3DVertex(-tx,-ty, 0, 0,0,0, col, tu, tv+th);//左下
ver[3] = S3DVertex( tx,-ty, 0, 0,0,0, col, tu+tw,tv+th);//右下
//スクリーン設定
//初期値以外を書き換え(W,0,0,0,0,H,0,0,0,0,1,0,0,0,0,1)
Ortho(0,0) = (double)2/(double)screen.Width;
Ortho(1,1) = (double)2/(double)screen.Height;
//ワールドに反映
driver->setTransform(ETS_VIEW, world);
driver->setTransform(ETS_PROJECTION, world);
world = pos*Ortho*rotate*scale;
driver->setTransform(ETS_WORLD, world);
//描画
driver->setMaterial(Material);
driver->drawIndexedTriangleList(&ver[0], 4, &list[0], 2);
}
|
void makeScene(IVideoDriver *driver)
{
SColor colors[8] =
{
0xFFFFFFFF, 0xFF0000FF, 0xFF00FF00, 0xFFFF0000,
0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00, 0xFF000000
};
sprite(driver,"icon.bmp",mouse.X,mouse.Y,32,32,TV*32,0,
RZ,SCALE,SCALE,colors[COL],true);
}
|