From 9a9452c3fadc3f3a295065687be5f1e2559753f4 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Thu, 15 Oct 2009 19:15:40 +1300 Subject: [PATCH] what a hack, but the cursors work and the terrain doesnt break anymore --- OpenRa.Game/Cursor.cs | 6 ++++-- OpenRa.Game/Graphics/CursorSequence.cs | 6 ++++++ OpenRa.Game/Graphics/SequenceProvider.cs | 2 ++ OpenRa.Game/Graphics/SheetBuilder.cs | 6 ++++++ OpenRa.Game/Graphics/TerrainRenderer.cs | 2 ++ OpenRa.Game/Graphics/Viewport.cs | 12 ++++++++++++ OpenRa.Game/MainWindow.cs | 18 +++++++++++++++--- sequences.xml | 2 +- 8 files changed, 48 insertions(+), 6 deletions(-) diff --git a/OpenRa.Game/Cursor.cs b/OpenRa.Game/Cursor.cs index 4363dd7fbb..67d68444a2 100644 --- a/OpenRa.Game/Cursor.cs +++ b/OpenRa.Game/Cursor.cs @@ -7,15 +7,17 @@ using System.IO; namespace OpenRa.Game { - public class Cursor + class Cursor { CursorSequence sequence; Cursor(string cursor) { sequence = SequenceProvider.GetCursorSequence(cursor); - } + public Sprite GetSprite(int frame) { return sequence.GetSprite(frame); } + public int2 GetHotspot() { return sequence.Hotspot; } + public static Cursor Default { get { return new Cursor("default"); } diff --git a/OpenRa.Game/Graphics/CursorSequence.cs b/OpenRa.Game/Graphics/CursorSequence.cs index c49ba51f28..55209762da 100644 --- a/OpenRa.Game/Graphics/CursorSequence.cs +++ b/OpenRa.Game/Graphics/CursorSequence.cs @@ -14,7 +14,10 @@ namespace OpenRa.Game.Graphics public int End { get { return start + length; } } public int Length { get { return length; } } + public readonly int2 Hotspot; + Sprite[] sprites; + public CursorSequence(string cursorSrc, XmlElement e) { sprites = CursorSheetBuilder.LoadAllSprites(cursorSrc, ".shp"); @@ -29,6 +32,9 @@ namespace OpenRa.Game.Graphics length = int.Parse(e.GetAttribute("end")) - start; else length = 1; + + int.TryParse( e.GetAttribute("x"), out Hotspot.X ); + int.TryParse( e.GetAttribute("y"), out Hotspot.Y ); } public Sprite GetSprite(int frame) diff --git a/OpenRa.Game/Graphics/SequenceProvider.cs b/OpenRa.Game/Graphics/SequenceProvider.cs index 7d0b9fb357..79cfad0b69 100644 --- a/OpenRa.Game/Graphics/SequenceProvider.cs +++ b/OpenRa.Game/Graphics/SequenceProvider.cs @@ -29,6 +29,8 @@ namespace OpenRa.Game.Graphics foreach (XmlElement eSequence in eCursor.SelectNodes("./sequence")) cursors.Add(eSequence.GetAttribute("name"), new CursorSequence(cursorSrc, eSequence)); + + Log.Write("* LoadSequencesForCursor() done"); } public static void ForcePrecache() { } // force static ctor to run diff --git a/OpenRa.Game/Graphics/SheetBuilder.cs b/OpenRa.Game/Graphics/SheetBuilder.cs index 480288e2c0..7e62b9b9e0 100644 --- a/OpenRa.Game/Graphics/SheetBuilder.cs +++ b/OpenRa.Game/Graphics/SheetBuilder.cs @@ -25,6 +25,12 @@ namespace OpenRa.Game.Graphics return Add(data, size); } + public static void ForceNewSheet() + { + current = NewSheet(); + channel = NextChannel(null); + } + static Sheet NewSheet() { return new Sheet(renderer, new Size(512, 512)); } static Renderer renderer; diff --git a/OpenRa.Game/Graphics/TerrainRenderer.cs b/OpenRa.Game/Graphics/TerrainRenderer.cs index 4d9de120dc..6620bafab0 100644 --- a/OpenRa.Game/Graphics/TerrainRenderer.cs +++ b/OpenRa.Game/Graphics/TerrainRenderer.cs @@ -32,6 +32,8 @@ namespace OpenRa.Game.Graphics Size tileSize = new Size( Game.CellSize, Game.CellSize ); + SheetBuilder.ForceNewSheet(); + var tileMapping = new Cache( x => SheetBuilder.Add(tileSet.GetBytes(x), tileSize)); diff --git a/OpenRa.Game/Graphics/Viewport.cs b/OpenRa.Game/Graphics/Viewport.cs index f68b979ce5..c7749d80d2 100644 --- a/OpenRa.Game/Graphics/Viewport.cs +++ b/OpenRa.Game/Graphics/Viewport.cs @@ -17,6 +17,11 @@ namespace OpenRa.Game.Graphics public int Width { get { return (int)size.X; } } public int Height { get { return (int)size.Y; } } + public Cursor cursor = Cursor.Move; + SpriteRenderer cursorRenderer; + int2 mousePos; + float cursorFrame = 0f; + public void Scroll(float2 delta) { scrollPosition = (scrollPosition + delta).Constrain(float2.Zero, mapSize); @@ -27,6 +32,7 @@ namespace OpenRa.Game.Graphics this.size = size; this.mapSize = Game.CellSize * mapSize - size + new float2(128, 0); this.renderer = renderer; + cursorRenderer = new SpriteRenderer(renderer, true); } List regions = new List(); @@ -42,6 +48,9 @@ namespace OpenRa.Game.Graphics foreach (Region region in regions) region.Draw(renderer); + cursorFrame += 0.01f; + cursorRenderer.DrawSprite(cursor.GetSprite((int)cursorFrame), mousePos + Location - cursor.GetHotspot(), 0); + cursorRenderer.Flush(); renderer.EndFrame(); } @@ -49,6 +58,9 @@ namespace OpenRa.Game.Graphics Region dragRegion = null; public void DispatchMouseInput(MouseInput mi) { + if (mi.Event == MouseInputEvent.Move) + mousePos = mi.Location; + if (dragRegion != null) { dragRegion.HandleMouseInput( mi ); if (mi.Event == MouseInputEvent.Up) dragRegion = null; diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 883bd8b541..0e18e5e419 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -6,7 +6,8 @@ using OpenRa.TechTree; namespace OpenRa.Game { - using GRegion = OpenRa.Game.Graphics.Region; + using GRegion = OpenRa.Game.Graphics.Region; +using System.Runtime.InteropServices; class MainWindow : Form { @@ -21,7 +22,10 @@ namespace OpenRa.Game return new Size(settings.GetValue("width", desktopResolution.Width), settings.GetValue("height", desktopResolution.Height)); - } + } + + [DllImport("user32")] + static extern int ShowCursor([MarshalAs(UnmanagedType.Bool)] bool visible); public MainWindow(Settings settings) { @@ -58,7 +62,9 @@ namespace OpenRa.Game sidebar = new Sidebar(renderer, game); - renderer.BuildPalette(game.map); + renderer.BuildPalette(game.map); + + ShowCursor(false); } internal void Run() @@ -66,6 +72,12 @@ namespace OpenRa.Game while (Created && Visible) { game.Tick(); + + // rude hack + game.viewport.cursor = (game.controller.orderGenerator is UnitOrderGenerator) + && (game.controller.orderGenerator as UnitOrderGenerator).selection.Count > 0 + ? OpenRa.Game.Cursor.Move : OpenRa.Game.Cursor.Default; + Application.DoEvents(); } } diff --git a/sequences.xml b/sequences.xml index 59e6f56358..32bf1239f4 100644 --- a/sequences.xml +++ b/sequences.xml @@ -323,7 +323,7 @@ - +