1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Threading;
namespace Hmm
{
public class AForm : Form
{
Device device = null;
VertexBuffer vertexBuffer = null;
Texture GrassTexture;
Texture Arbre;
Sprite Go;
float Trans = 1.0f;
static void Main()
{
AForm form = new AForm();
form.Size = new Size(800, 600);
form.InitializeGraphics();
form.Show();
while (form.Created)
{
form.Render();
Application.DoEvents();
}
}
public void InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0,
DeviceType.Hardware,
this,
CreateFlags.HardwareVertexProcessing,
presentParams);
LoadTextures();
device.DeviceReset += new System.EventHandler(this.OnResetDevice);
OnResetDevice(device, null);
Go = new Sprite(device);
}
catch (DirectXException e)
{
MessageBox.Show(null,
"Error intializing graphics: "
+ e.Message, "Error");
Close();
}
}
private void Render()
{
if (device == null) //if there's nothing to render with
return;//then don't bother
//Set the backbuffer to a nice blue
device.Clear(ClearFlags.Target,
System.Drawing.Color.Blue,
1.0f, 0);
device.RenderState.Lighting = false;
device.BeginScene();
Go.Begin(SpriteFlags.AlphaBlend);
Go.Draw2D(GrassTexture, new PointF(0.0f, 0.0f), 0f, new PointF(0, 0), Color.White);
Go.End();
// Use alpha for transparency
device.SetRenderState(RenderStates.SourceBlend, true);
device.SetRenderState(RenderStates.DestinationBlend, true);
device.SetStreamSource(0, vertexBuffer, 0);
device.VertexFormat = CustomVertex.PositionColoredTextured.Format;
device.SetTexture(0, Arbre);
device.Transform.World = Matrix.RotationZ(0f) * Matrix.Translation(0, 0, 0) * Matrix.Scaling(12, 12, 0);
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
device.EndScene();
device.Present();
}
public void OnResetDevice(object sender, EventArgs e)
{
Device device = (Device)sender;
vertexBuffer =
new VertexBuffer(typeof(CustomVertex.PositionColoredTextured),
4,
device,
0,
CustomVertex.PositionColoredTextured.Format,
Pool.Default);
Matrix Ortho2D;
Matrix Identity;
Ortho2D = Matrix.OrthoOffCenterLH(0.0f, this.Width, 0, this.Height, -2.0f, 2.0f);
device.SetTransform(TransformType.Projection, Ortho2D);
Identity = Matrix.Identity;
device.SetTransform(TransformType.World, Identity);
device.SetTransform(TransformType.View, Identity);
device.SetRenderState(RenderStates.ZBufferWriteEnable, false);
device.SetRenderState(RenderStates.ZBufferFunction, true);
this.device.RenderState.ZBufferEnable = false;
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.CounterClockwise;
device.RenderState.AlphaBlendEnable = true;
device.RenderState.AlphaTestEnable = true;
vertexBuffer.Created +=
new System.EventHandler(this.OnCreateVertexBuffer);
this.OnCreateVertexBuffer(vertexBuffer, null);
}
public void LoadTextures()
{
try
{
System.Drawing.Bitmap grass = (System.Drawing.Bitmap)
System.Drawing.Bitmap.FromFile(@"C:\Grass.bmp");
GrassTexture = Texture.FromBitmap(device, grass, 0, Pool.Managed);
Arbre = TextureLoader.FromFile(device, "Arbre.png");
}
catch
{
MessageBox.Show(this, "An error has occured while trying to load textures");
}
}
public void OnCreateVertexBuffer(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionColoredTextured[] verts = new CustomVertex.PositionColoredTextured[4];
verts[2].Tu = 0.0f;
verts[2].Tv = 0.0f;
verts[3].Tu = 0.0f;
verts[3].Tv = 1.0f;
verts[0].Tu = 1.0f;
verts[0].Tv = 1.0f;
verts[1].Tu = 1.0f;
verts[1].Tv = 0.0f;
verts[0].Position = new Vector3(0, 0, 1.0f);
verts[1].Position = new Vector3(0, 32, 1.0f);
verts[2].Position = new Vector3(32, 32, 1.0f);
verts[3].Position = new Vector3(32, 0, 1.0f);
Color _Color = Color.FromArgb(0, Color.White);
int IColor = _Color.ToArgb();
verts[0].Color = IColor;
verts[1].Color = IColor;
verts[2].Color = IColor;
verts[3].Color = IColor;
buffer.SetData(verts, 0, LockFlags.None);
}
}
} |
Partager