diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 3df3eef37f..a39000bc4d 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -99,10 +99,10 @@ namespace OpenRa.Game void Frame() { - PointF r1 = new PointF(2.0f / viewport.ClientSize.Width, -2.0f / viewport.ClientSize.Height); - PointF r2 = new PointF(-1, 1); + float2 r1 = new float2(2, -2) / viewport.Size; + float2 r2 = new float2(-1, 1); - renderer.BeginFrame(r1, r2, viewport.ScrollPosition); + renderer.BeginFrame(r1, r2, viewport.Location); renderer.Device.EnableScissor(0, 0, viewport.ClientSize.Width - 128, viewport.ClientSize.Height); terrain.Draw(viewport); diff --git a/OpenRa.Game/Renderer.cs b/OpenRa.Game/Renderer.cs index 4815c58b55..537778d391 100644 --- a/OpenRa.Game/Renderer.cs +++ b/OpenRa.Game/Renderer.cs @@ -8,7 +8,7 @@ using System.IO; namespace OpenRa.Game { - public class Renderer + class Renderer { readonly GraphicsDevice device; readonly Effect shader; @@ -40,10 +40,9 @@ namespace OpenRa.Game public GraphicsDevice Device { get { return device; } } - public void BeginFrame( PointF r1, PointF r2, PointF scroll ) + public void BeginFrame( float2 r1, float2 r2, float2 scroll ) { device.Begin(); - //device.Clear(Color.Gray.ToArgb(), Surfaces.Color); shader.SetValue(scrollHandle, scroll); shader.SetValue(r1Handle, r1); diff --git a/OpenRa.Game/Sidebar.cs b/OpenRa.Game/Sidebar.cs index 48cec2ff38..3ed95bc27d 100644 --- a/OpenRa.Game/Sidebar.cs +++ b/OpenRa.Game/Sidebar.cs @@ -42,13 +42,13 @@ namespace OpenRa.Game } } - void DrawSprite(Sprite s, ref PointF p) + void DrawSprite(Sprite s, ref float2 p) { spriteRenderer.DrawSprite(s, p, 0); p.Y += 48; } - void Fill(Size clientSize, PointF p) + void Fill(Size clientSize, float2 p) { while (p.Y < clientSize.Height) DrawSprite(blank, ref p); @@ -56,8 +56,8 @@ namespace OpenRa.Game public void Paint(Viewport viewport) { - PointF buildPos = new PointF(viewport.ClientSize.Width - 128 + viewport.ScrollPosition.X, viewport.ScrollPosition.Y); - PointF unitPos = new PointF(viewport.ClientSize.Width - 64 + viewport.ScrollPosition.X, viewport.ScrollPosition.Y); + float2 buildPos = viewport.Location + new float2(viewport.ClientSize.Width - 128, 0); + float2 unitPos = viewport.Location + new float2(viewport.ClientSize.Width - 64, 0); foreach (Item i in techTree.BuildableItems) { diff --git a/OpenRa.Game/Sprite.cs b/OpenRa.Game/Sprite.cs index e58737d7af..52a29b5cf7 100644 --- a/OpenRa.Game/Sprite.cs +++ b/OpenRa.Game/Sprite.cs @@ -30,11 +30,11 @@ namespace OpenRa.Game } } - public PointF MapTextureCoords(PointF p) + public float2 MapTextureCoords(float2 p) { RectangleF uv = TextureCoords; - return new PointF( + return new float2( p.X > 0 ? uv.Right : uv.Left, p.Y > 0 ? uv.Bottom : uv.Top); } diff --git a/OpenRa.Game/SpriteRenderer.cs b/OpenRa.Game/SpriteRenderer.cs index 82b47abb50..6ca78320bc 100644 --- a/OpenRa.Game/SpriteRenderer.cs +++ b/OpenRa.Game/SpriteRenderer.cs @@ -52,7 +52,7 @@ namespace OpenRa.Game } } - public void DrawSprite(Sprite s, PointF location, int palette) + public void DrawSprite(Sprite s, float2 location, int palette) { if (s.sheet != currentSheet) Flush(); diff --git a/OpenRa.Game/TerrainRenderer.cs b/OpenRa.Game/TerrainRenderer.cs index 6448c900f2..7ea704b36d 100644 --- a/OpenRa.Game/TerrainRenderer.cs +++ b/OpenRa.Game/TerrainRenderer.cs @@ -44,7 +44,7 @@ namespace OpenRa.Game terrainSheet = tile.sheet; - Util.CreateQuad(vertices, indices, new PointF(24 * i, 24 * j), tile, 0); + Util.CreateQuad(vertices, indices, 24 * new float2(i,j), tile, 0); } vertexBuffer = new FvfVertexBuffer(renderer.Device, vertices.Count, Vertex.Format); diff --git a/OpenRa.Game/Util.cs b/OpenRa.Game/Util.cs index 429875b420..8c0165d371 100644 --- a/OpenRa.Game/Util.cs +++ b/OpenRa.Game/Util.cs @@ -14,7 +14,7 @@ namespace OpenRa.Game return x < range.Start ? range.Start : x > range.End ? range.End : x; } - static PointF EncodeVertexAttributes(TextureChannel channel, int paletteLine) + static float2 EncodeVertexAttributes(TextureChannel channel, int paletteLine) { Converter channelEncoder = delegate(TextureChannel c) { @@ -29,15 +29,13 @@ namespace OpenRa.Game } }; - return new PointF(paletteLine / 16.0f, channelEncoder(channel)); + return new float2(paletteLine / 16.0f, channelEncoder(channel)); } - public static Vertex MakeVertex(PointF o, PointF uv, Sprite r, int palette) + public static Vertex MakeVertex(float2 o, float2 uv, Sprite r, int palette) { - PointF farCorner = new PointF(o.X + r.bounds.Width, o.Y + r.bounds.Height); - return new Vertex( - Lerp( o, farCorner, uv ), + Lerp( o, o + new float2(r.bounds.Size), uv ), r.MapTextureCoords(uv), EncodeVertexAttributes(r.channel, palette)); } @@ -47,26 +45,26 @@ namespace OpenRa.Game return (1 - t) * a + t * b; } - static PointF Lerp(PointF a, PointF b, PointF t) + static float2 Lerp(float2 a, float2 b, float2 t) { - return new PointF( + return new float2( Lerp(a.X, b.X, t.X), Lerp(a.Y, b.Y, t.Y)); } - static PointF[] uv = + static float2[] uv = { - new PointF( 0,0 ), - new PointF( 1,0 ), - new PointF( 0,1 ), - new PointF( 1,1 ), + new float2( 0,0 ), + new float2( 1,0 ), + new float2( 0,1 ), + new float2( 1,1 ), }; - public static void CreateQuad(List vertices, List indices, PointF o, Sprite r, int palette) + public static void CreateQuad(List vertices, List indices, float2 o, Sprite r, int palette) { ushort offset = (ushort)vertices.Count; - foreach( PointF p in uv ) + foreach( float2 p in uv ) vertices.Add(Util.MakeVertex(o, p, r, palette)); indices.Add(offset); diff --git a/OpenRa.Game/Vertex.cs b/OpenRa.Game/Vertex.cs index c679fc7651..1872148b8c 100644 --- a/OpenRa.Game/Vertex.cs +++ b/OpenRa.Game/Vertex.cs @@ -13,7 +13,7 @@ namespace OpenRa.Game public float x, y, z, u, v; public float p, c; - public Vertex(PointF xy, PointF uv, PointF pc) + public Vertex(float2 xy, float2 uv, float2 pc) { this.x = xy.X; this.y = xy.Y; this.z = 0; this.u = uv.X; this.v = uv.Y; diff --git a/OpenRa.Game/Viewport.cs b/OpenRa.Game/Viewport.cs index 85358da855..35be9c271f 100644 --- a/OpenRa.Game/Viewport.cs +++ b/OpenRa.Game/Viewport.cs @@ -7,7 +7,7 @@ using BluntDirectX.Direct3D; namespace OpenRa.Game { - public delegate void Renderable(Renderer renderer, Viewport viewport); + delegate void Renderable(Renderer renderer, Viewport viewport); class Viewport { readonly Size clientSize; @@ -18,6 +18,9 @@ namespace OpenRa.Game public PointF ScrollPosition { get { return scrollPosition.ToPointF(); } } public Size ClientSize { get { return clientSize; } } + public float2 Location { get { return scrollPosition; } } + public float2 Size { get { return scrollPosition + new float2(ClientSize); } } + public void Scroll(float2 delta) { scrollPosition = (scrollPosition + delta).Constrain(new Range(float2.Zero, mapSize)); diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index 90bfb3e346..6c1e1cd403 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -12,17 +12,14 @@ namespace OpenRa.Game List actors = new List(); SpriteRenderer spriteRenderer; - public World(Renderer renderer) - { - spriteRenderer = new SpriteRenderer(renderer, true); - } + public World(Renderer renderer) { spriteRenderer = new SpriteRenderer(renderer, true); } public void Add(Actor a) { actors.Add(a); } public void Draw(Renderer renderer, Viewport viewport) { - Range xr = new Range(viewport.ScrollPosition.X, viewport.ScrollPosition.X + viewport.ClientSize.Width); - Range yr = new Range(viewport.ScrollPosition.Y, viewport.ScrollPosition.Y + viewport.ClientSize.Height); + Range range = new Range(viewport.Location, viewport.Size); + foreach (Actor a in actors) { Sprite[] images = a.CurrentImages; @@ -30,14 +27,14 @@ namespace OpenRa.Game if (images == null) continue; - if (a.location.X > xr.End || a.location.X < xr.Start - images[0].bounds.Width) + if (a.location.X > range.End.X || a.location.X < range.Start.X - images[0].bounds.Width) continue; - if (a.location.Y > yr.End || a.location.Y < yr.Start - images[0].bounds.Height) + if (a.location.Y > range.End.Y || a.location.Y < range.Start.Y - images[0].bounds.Height) continue; foreach (Sprite image in images) - spriteRenderer.DrawSprite(image, a.location.ToPointF(), a.palette); + spriteRenderer.DrawSprite(image, a.location, a.palette); } spriteRenderer.Flush(); diff --git a/OpenRa.Game/float2.cs b/OpenRa.Game/float2.cs index a20dbdac19..5bea593388 100644 --- a/OpenRa.Game/float2.cs +++ b/OpenRa.Game/float2.cs @@ -43,5 +43,10 @@ namespace OpenRa.Game } public static readonly float2 Zero = new float2(0, 0); + + public static float2 operator /(float2 a, float2 b) + { + return new float2(a.X / b.X, a.Y / b.Y); + } } }