From 5bf38488d2c4dd00b50a6e5cc83e6761d2a4a6e2 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@993157c7-ee19-0410-b2c4-bb4e9862e678> Date: Tue, 24 Jul 2007 07:59:41 +0000 Subject: [PATCH] git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1339 993157c7-ee19-0410-b2c4-bb4e9862e678 --- OpenRa.DataStructures/float2.cs | 2 + OpenRa.DataStructures/int2.cs | 3 ++ OpenRa.Game/Game.cs | 3 ++ OpenRa.Game/MainWindow.cs | 6 +-- OpenRa.Game/Network/Network.cs | 6 +-- OpenRa.Game/OpenRa.Game.csproj | 1 + OpenRa.Game/Region.cs | 27 +++++------ OpenRa.Game/Sidebar.cs | 86 ++++++++++++++++----------------- OpenRa.Game/SidebarItem.cs | 37 ++++++++++++++ sequences.xml | 6 +++ 10 files changed, 113 insertions(+), 64 deletions(-) create mode 100644 OpenRa.Game/SidebarItem.cs diff --git a/OpenRa.DataStructures/float2.cs b/OpenRa.DataStructures/float2.cs index 06fc49b828..dd80f7fc70 100644 --- a/OpenRa.DataStructures/float2.cs +++ b/OpenRa.DataStructures/float2.cs @@ -68,5 +68,7 @@ namespace OpenRa public float2 Sign() { return new float2(Math.Sign(X), Math.Sign(Y)); } public static float Dot(float2 a, float2 b) { return a.X * b.X + a.Y * b.Y; } public float2 Round() { return new float2((float)Math.Round(X), (float)Math.Round(Y)); } + + public override string ToString() { return string.Format("({0},{1})", X, Y); } } } diff --git a/OpenRa.DataStructures/int2.cs b/OpenRa.DataStructures/int2.cs index ca013a248a..5aab4b0a71 100644 --- a/OpenRa.DataStructures/int2.cs +++ b/OpenRa.DataStructures/int2.cs @@ -33,5 +33,8 @@ namespace OpenRa int2 o = (int2)obj; return o == this; } + + public static readonly int2 Zero = new int2(0, 0); + public Point ToPoint() { return new Point(X, Y); } } } diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index d51889c171..541031a69f 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.Text; using OpenRa.FileFormats; +using BluntDirectX.Direct3D; +using System.Drawing; namespace OpenRa.Game { @@ -65,6 +67,7 @@ namespace OpenRa.Game public void Tick() { viewport.DrawRegions(this); + Queue stuffFromOtherPlayers = network.Tick(); // todo: actually use the orders! } public void Issue(IOrder order) diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 911263ad76..7a1de14d28 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -66,12 +66,12 @@ namespace OpenRa.Game } } - float2 lastPos; + int2 lastPos; protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); - lastPos = new float2(e.Location); + lastPos = new int2(e.Location); if (e.Button == MouseButtons.Left) foreach (Region region in game.viewport.Regions) @@ -85,7 +85,7 @@ namespace OpenRa.Game if (e.Button == MouseButtons.Right) { - float2 p = new float2(e.Location); + int2 p = new int2(e.Location); game.viewport.Scroll(lastPos - p); lastPos = p; } diff --git a/OpenRa.Game/Network/Network.cs b/OpenRa.Game/Network/Network.cs index 884b6d194e..bcf6fc5629 100644 --- a/OpenRa.Game/Network/Network.cs +++ b/OpenRa.Game/Network/Network.cs @@ -18,7 +18,7 @@ namespace OpenRa.Game int currentFrame = 0; public int CurrentFrame { get { return currentFrame; } } - public int RemainingNetSyncTime { get { return Math.Min(0, Environment.TickCount - nextSyncTime); } } + public int RemainingNetSyncTime { get { return Math.Max(0, nextSyncTime - Environment.TickCount); } } Queue incomingPackets = new Queue(); @@ -36,10 +36,8 @@ namespace OpenRa.Game Packet packet = Packet.FromReceivedData(sender, data); lock (this) - { if (currentFrame <= packet.Frame) incomingPackets.Enqueue(packet); - } } }); @@ -76,7 +74,9 @@ namespace OpenRa.Game if (p.Frame == currentFrame) toProcess.Enqueue(p); } + ++currentFrame; + nextSyncTime = Environment.TickCount + netSyncInterval; } return toProcess; diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index af135ab1d3..edac3ad24f 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -68,6 +68,7 @@ + diff --git a/OpenRa.Game/Region.cs b/OpenRa.Game/Region.cs index de40eabb3f..9f787b8ff7 100644 --- a/OpenRa.Game/Region.cs +++ b/OpenRa.Game/Region.cs @@ -25,19 +25,19 @@ namespace OpenRa.Game Action drawFunction; MouseEventHandler mouseHandler; - RectangleF rect; + Rectangle rect; - static float2 MakeSize(Viewport v, DockStyle d, float size) + static int2 MakeSize(Viewport v, DockStyle d, int size) { switch (d) { case DockStyle.Top: case DockStyle.Bottom: - return new float2(v.Width, size); + return new int2(v.Width, size); case DockStyle.Left: case DockStyle.Right: - return new float2(size, v.Height); + return new int2(size, v.Height); default: throw new NotImplementedException(); @@ -46,42 +46,39 @@ namespace OpenRa.Game public void Clicked(MouseEventArgs e) { - mouseHandler(this, e); + mouseHandler(this, new MouseEventArgs(e.Button, e.Clicks, e.X - rect.Left, e.Y - rect.Top, e.Delta)); } - public static Region Create(Viewport v, DockStyle d, float size, Action f, MouseEventHandler m) + public static Region Create(Viewport v, DockStyle d, int size, Action f, MouseEventHandler m) { - float2 s = MakeSize(v, d, size); + int2 s = MakeSize(v, d, size); switch (d) { case DockStyle.Top: case DockStyle.Left: - return new Region(new float2(0,0), s, v, f, m); + return new Region(int2.Zero, s, v, f, m); case DockStyle.Right: case DockStyle.Bottom: - return new Region(new float2( v.Width - s.X, v.Height - s.Y ), s, v, f, m); + return new Region(new int2( v.Width - s.X, v.Height - s.Y ), s, v, f, m); default: throw new NotImplementedException(); } } - Region(float2 location, float2 size, Viewport viewport, Action drawFunction, MouseEventHandler mouseHandler) + Region(int2 location, int2 size, Viewport viewport, Action drawFunction, MouseEventHandler mouseHandler) { this.location = location; this.size = size; this.drawFunction = drawFunction; this.viewport = viewport; this.mouseHandler = mouseHandler; - rect = new RectangleF(location.ToPointF(), new SizeF(size.ToPointF())); + rect = new Rectangle(location.X, location.Y, size.X, size.Y); } - public bool Contains(float2 point) - { - return rect.Contains(point.ToPointF()); - } + public bool Contains(int2 point) { return rect.Contains(point.ToPoint()); } public void Draw(Renderer renderer) { diff --git a/OpenRa.Game/Sidebar.cs b/OpenRa.Game/Sidebar.cs index 647d5d456d..821396bd5d 100644 --- a/OpenRa.Game/Sidebar.cs +++ b/OpenRa.Game/Sidebar.cs @@ -14,24 +14,20 @@ namespace OpenRa.Game { TechTree.TechTree techTree; - SpriteRenderer spriteRenderer; + SpriteRenderer spriteRenderer, clockRenderer; Sprite blank; Game game; readonly Region region; - public Region Region - { - get { return region; } - } + Animation clockAnimation = new Animation("clock"); + + public Region Region { get { return region; } } + public float Width { get { return spriteWidth * 2; } } Dictionary sprites = new Dictionary(); - const float spriteWidth = 64, spriteHeight = 48; - - public float Width - { - get { return spriteWidth * 2; } - } + const int spriteWidth = 64, spriteHeight = 48; + List items = new List(); public Sidebar( Race race, Renderer renderer, Game game ) { @@ -42,17 +38,20 @@ namespace OpenRa.Game techTree.CurrentRace = race; techTree.Build("FACT", true); spriteRenderer = new SpriteRenderer(renderer, false); + clockRenderer = new SpriteRenderer(renderer, true); LoadSprites("buildings.txt"); LoadSprites("units.txt"); blank = SheetBuilder.Add(new Size((int)spriteWidth, (int)spriteHeight), 16); + + clockAnimation.PlayRepeating("idle"); } - public void Build(string key) + public void Build(SidebarItem item) { - if (string.IsNullOrEmpty(key)) return; - game.world.orderGenerator = new PlaceBuilding( 1, key.ToLowerInvariant() ); + if (item != null) + game.world.orderGenerator = new PlaceBuilding( 1, item.techTreeItem.tag.ToLowerInvariant() ); } void LoadSprites(string filename) @@ -76,53 +75,54 @@ namespace OpenRa.Game DrawSprite(blank, ref p); } - void Paint() + void PopulateItemList() { - float2 buildPos = region.Location + new float2(region.Size.X - spriteWidth * 2, 0); - float2 unitPos = region.Location + new float2(region.Size.X - spriteWidth, 0); - + int buildPos = 0, unitPos = 0; + + items.Clear(); + foreach (Item i in techTree.BuildableItems) { Sprite sprite; if (!sprites.TryGetValue(i.tag, out sprite)) continue; + items.Add(new SidebarItem(sprite, i, i.IsStructure ? buildPos : unitPos)); + if (i.IsStructure) - DrawSprite( sprite, ref buildPos ); + buildPos += spriteHeight; else - DrawSprite( sprite, ref unitPos ); + unitPos += spriteHeight; } - - Fill( region.Location.Y + region.Size.Y, buildPos ); - Fill( region.Location.Y + region.Size.Y, unitPos ); - - spriteRenderer.Flush(); } - public string FindSpriteAtPoint(float2 point) + void Paint() { - float y1 = 0, y2 = 0; - foreach (Item i in techTree.BuildableItems) - { - RectangleF rect; - if (i.IsStructure) - { - rect = new RectangleF(region.Location.X, region.Location.Y + y1, spriteWidth, spriteHeight); - y1 += 48; - } - else - { - rect = new RectangleF(region.Location.X + spriteWidth, region.Location.Y + y2, spriteWidth, spriteHeight); - y2 += 48; - } - if (rect.Contains(point.ToPointF())) return i.tag; - } + PopulateItemList(); // todo: do this less often, just when things actually change! + + foreach (SidebarItem i in items) + i.Paint(spriteRenderer, region.Location); + + spriteRenderer.Flush(); //todo: fix filling + + clockRenderer.DrawSprite( clockAnimation.Images[0], region.Location, 0 ); + clockAnimation.Tick(1); + + clockRenderer.Flush(); + } + + public SidebarItem GetItem(float2 point) + { + foreach (SidebarItem i in items) + if (i.Clicked(point)) + return i; + return null; } void MouseHandler(object sender, MouseEventArgs e) { float2 point = new float2(e.Location); - Build(FindSpriteAtPoint(point)); + Build(GetItem(point)); } } diff --git a/OpenRa.Game/SidebarItem.cs b/OpenRa.Game/SidebarItem.cs new file mode 100644 index 0000000000..92b8fda8f8 --- /dev/null +++ b/OpenRa.Game/SidebarItem.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OpenRa.TechTree; + +namespace OpenRa.Game +{ + class SidebarItem + { + public readonly Item techTreeItem; + public readonly float2 location; + readonly Sprite sprite; + + public SidebarItem(Sprite s, Item item, int y) + { + this.techTreeItem = item; + this.sprite = s; + location = new float2(item.IsStructure ? 0 : 64, y); + } + + public bool Clicked(float2 p) + { + if (p.X < location.X || p.Y < location.Y) + return false; + + if (p.X > location.X + 64 || p.Y > location.Y + 48) + return false; + + return true; + } + + public void Paint(SpriteRenderer renderer, float2 offset) + { + renderer.DrawSprite(sprite, location + offset, 0); + } + } +} diff --git a/sequences.xml b/sequences.xml index aa4acfa829..570edfef2f 100644 --- a/sequences.xml +++ b/sequences.xml @@ -130,4 +130,10 @@ + + + + + +