/*****************************************/
/*★ Sprite で Image を表示 前田 稔 ★*/
/*****************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DeviceTutorial
{
public class CreateDevice : Form
{
Device device = null; // Direct Device の定義
Sprite sprite = null; // Sprite Object Class
Texture texture; // Texture Image
// CreateDevice の Constructor
public CreateDevice()
{
// caption の設定
this.Text = "Sprite Image File";
}
public bool InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed=true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
sprite = new Sprite(device);
texture = TextureLoader.FromFile(device, "c:\\data\\test\\kishi.jpg");
return true;
}
catch (DirectXException)
{
return false;
}
}
// Frame の描画メソッド
private void Render()
{
if (device == null) return;
//Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
//Begin the scene
device.BeginScene();
sprite.Begin(SpriteFlags.None);
sprite.Draw(texture, Vector3.Empty, Vector3.Empty, 0xffffff);
sprite.End();
//End the scene
device.EndScene();
device.Present();
}
// Window の描画 OnPaint() をオーバーロード
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render(); // Render on painting
}
// OnKeyPress() をオーバーロード
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Close(); // Esc was pressed
}
//☆ Main() メソッド
static void Main()
{
// using で資源の解放を確実に行う
using (CreateDevice frm = new CreateDevice())
{
if (!frm.InitializeGraphics()) // Initialize Direct3D
{
MessageBox.Show("Could not initialize Direct3D");
return;
}
frm.Show();
// メッセージループ
while(frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}
}
}
|