diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 8f30052602..15da6b7c74 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -21,6 +21,7 @@ namespace OpenRa.Game string TileSuffix; const string mapName = "scm12ea.ini"; + const string shaderName = "diffuse.fx"; Dictionary> tileMapping = new Dictionary>(); @@ -29,6 +30,11 @@ namespace OpenRa.Game Dictionary drawBatches = new Dictionary(); + Effect effect; + IntPtr textureParameter; + SpriteHelper spriteHelper; + FontHelper fontHelper; + void LoadTextures() { List tempSheets = new List(); @@ -62,6 +68,22 @@ namespace OpenRa.Game s.LoadTexture(device); } + float U(SheetRectangle s, float u) + { + float u0 = (float)s.origin.X / (float)s.sheet.bitmap.Width; + float u1 = (float)(s.origin.X + s.size.Width) / (float)s.sheet.bitmap.Width; + + return (1 - u) * u0 + u * u1; + } + + float V(SheetRectangle s, float v) + { + float v0 = (float)s.origin.Y / (float)s.sheet.bitmap.Height; + float v1 = (float)(s.origin.Y + s.size.Height) / (float)s.sheet.bitmap.Height; + + return (1 - v) * v0 + v * v1; + } + void LoadVertexBuffer() { Dictionary> indexMap = new Dictionary>(); @@ -74,10 +96,10 @@ namespace OpenRa.Game ushort offset = (ushort)(4 * (i * 128 + j)); - vertices[offset] = new Vertex(24 * i, 24 * j, 0, 0, 0); - vertices[offset + 1] = new Vertex(24 + 24 * i, 24 * j, 0, 1, 0); - vertices[offset + 2] = new Vertex(24 * i, 24 + 24 * j, 0, 0, 1); - vertices[offset + 3] = new Vertex(24 + 24 * i, 24 + 24 * j, 0, 1, 1); + vertices[offset] = new Vertex(24 * i, 24 * j, 0, U(tile,0), V(tile,0)); + vertices[offset + 1] = new Vertex(24 + 24 * i, 24 * j, 0, U(tile,1), V(tile,0)); + vertices[offset + 2] = new Vertex(24 * i, 24 + 24 * j, 0, U(tile,0), V(tile,1)); + vertices[offset + 3] = new Vertex(24 + 24 * i, 24 + 24 * j, 0, U(tile,1), V(tile,1)); List indexList; if (!indexMap.TryGetValue(tile.sheet, out indexList)) @@ -120,6 +142,12 @@ namespace OpenRa.Game LoadTextures(); LoadVertexBuffer(); + + effect = new Effect(device, File.OpenRead("../../../" + shaderName)); + textureParameter = effect.GetHandle("DiffuseTexture"); + + spriteHelper = new SpriteHelper(device); + fontHelper = new FontHelper(device, "Tahoma", 10, false); } internal void Run() @@ -134,21 +162,32 @@ namespace OpenRa.Game void Frame() { device.Begin(); - device.Clear(0); - - // render something :) + device.Clear( Color.Red.ToArgb(), Surfaces.Color ); vertexBuffer.Bind(0); + effect.Quality = ShaderQuality.Low; + effect.Begin(); + effect.BeginPass(0); + foreach (KeyValuePair batch in drawBatches) { - //todo: bind texture! + effect.SetTexture(textureParameter, batch.Key.texture); + effect.Commit(); batch.Value.Bind(); + device.DrawIndexedPrimitives(PrimitiveType.TriangleList, vertexBuffer.Size, batch.Value.Size / 3); } + effect.EndPass(); + effect.End(); + + spriteHelper.Begin(); + fontHelper.Draw(spriteHelper, "fps: 1337", 0, 0, Color.White.ToArgb()); + spriteHelper.End(); + device.End(); device.Present(); } diff --git a/diffuse.fx b/diffuse.fx new file mode 100644 index 0000000000..797cd0a25c --- /dev/null +++ b/diffuse.fx @@ -0,0 +1,53 @@ +// OpenRA test shader +// Author: C. Forbes +//-------------------------------------------------------- + +shared texture DiffuseTexture; + +sampler s_DiffuseTexture = sampler_state { + Texture = ; + MinFilter = None; + MagFilter = None; + MipFilter = None; + + AddressU = Wrap; + AddressV = Wrap; + AddressW = Wrap; +}; + +struct VertexIn { + float4 Position: POSITION; + float2 Tex0: TEXCOORD0; +}; + +struct VertexOut { + float4 Position: POSITION; + float2 Tex0: TEXCOORD0; +}; + +struct FragmentIn { + float2 Tex0: TEXCOORD0; +}; + +VertexOut Simple_vp(VertexIn v) { + VertexOut o; + o.Position = float4( v.Position.x / 320.0f - 0.5f - 2, 5-v.Position.y / 240.0f, 0, 1 ); + o.Tex0 = v.Tex0; + return o; +} + +float4 Simple_fp(FragmentIn f) : COLOR0 { + float4 color = tex2D(s_DiffuseTexture, f.Tex0); + return color; +} + +technique low_quality { + pass p0 { + AlphaBlendEnable = false; + ZWriteEnable = true; + ZEnable = false; + CullMode = None; + VertexShader = compile vs_2_0 Simple_vp(); + PixelShader = compile ps_2_0 Simple_fp(); + } +}