/********************************************/
/*★ Girl.bmp でアニメーション 前田 稔 ★*/
/********************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
namespace DXDraw
{
public class DXDraw : Form
{
String ImgFile = "C:\\Data\\Test\\girl.bmp";
const int NH = 7; // Sprite の枚数
Device draw = null; // DrawDevice object.
Surface primary = null; // primary destination surface.
Surface image = null; // Image surface.
Surface offscreen = null; // offscreen surface
Rectangle dest,sou,win; // 受取側と送り側とウインドウの矩形領域
int width, height; // Image Size
int sp_no= 0; // Sprite Number
public DXDraw()
{
Text = "DXDraw";
Bitmap bmp = new Bitmap(ImgFile);
width = bmp.Width;
height = bmp.Height;
bmp.Dispose();
ClientSize = new System.Drawing.Size(width/NH, height);
CreateSurfaces();
}
// This function Draws the offscreen bitmap surface to the primary visible surface.
private void Draw()
{
if (null == primary) return;
if (FormWindowState.Minimized == WindowState) return;
// offscreen に Sprite を描画
int tickCurrent = (Environment.TickCount/200)%7;
if (tickCurrent==sp_no) return;
sp_no= tickCurrent;
sou.X= sp_no*width/NH;
offscreen.ColorFill(Color.White);
offscreen.DrawFast(dest.X, dest.Y, image, sou, DrawFastFlags.DoNotWait | DrawFastFlags.SourceColorKey);
try
{
win = new Rectangle(PointToScreen(new Point(0, 0)), ClientSize);
primary.Draw(win, offscreen, DrawFlags.Wait);
}
catch(SurfaceLostException)
{
CreateSurfaces(); // Surface was lost. Recreate them.
}
}
// This function is where the surfaces and clipper object are created.
private void CreateSurfaces()
{
// Create new DrawDevice. coop level normal windowed mode.
draw = new Device();
draw.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);
// Create primary surface.
SurfaceDescription description = new SurfaceDescription();
description.SurfaceCaps.PrimarySurface = true;
primary = new Surface(description, draw);
// Create image surface.
description.Clear();
image = new Surface(ImgFile, description, draw);
// Create a new colorkey.
ColorKey ck = new ColorKey();
image.SetColorKey(ColorKeyFlags.SourceDraw, ck);
// Create offscreen surface.
description.Clear();
description.SurfaceCaps.OffScreenPlain = true;
description.Width = this.Width;
description.Height = this.Height;
offscreen = new Surface(description, draw);
offscreen.ColorFill(Color.White);
// 一番目の Sprite を描画
sou = new Rectangle(0, 0, width / NH, height);
dest = new Rectangle(10, 20, width / NH, height);
offscreen.DrawFast(dest.X, dest.Y, image, sou, DrawFastFlags.DoNotWait | DrawFastFlags.SourceColorKey);
}
//☆ Main() メソッド
static void Main()
{
// using で資源の解放を確実に行う
using (DXDraw frm = new DXDraw())
{
frm.CreateSurfaces(); // Create Draw Surfaces
frm.Show();
// メッセージループ
while(frm.Created)
{
frm.Draw();
Application.DoEvents();
}
}
}
}
}
|