
![]()
/*****************************************/
/*★ 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();
}
}
}
}
}
|
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\\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();
}
|
![]()
|
0x808080 0x404040 0x00FF00 |
|
sprite.Draw(texture, Vector3.Empty, new Vector3(80,60,0), 0xffffff); sprite.Draw(texture, new Vector3(40,20,0), Vector3.Empty, 0xffffff); sprite.Draw(texture, new Rectangle(10, 30, 100, 60), Vector3.Empty, new Vector3(80, 60, 0), 0xffffff); |
![]()
| パラメータ | 説明 |
|---|---|
| texture | 描画する texture |
| Vector3.Empty | 画像の中心座標 |
| Vector3.Empty | 描画する座標 |
| 0xffffff | Color 値(RGB) |
![]()