From be47fb55e8feb3935b015d91a08657432d6dcda8 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 15 Sep 2010 17:13:18 +1200 Subject: [PATCH] Initial support for custom tile sizes --- OpenRA.Editor/Form1.cs | 14 ++--- OpenRA.Editor/Surface.cs | 68 ++++++++++++------------ OpenRA.FileFormats/Map/Terrain.cs | 14 +++-- OpenRA.FileFormats/Map/TileSet.cs | 7 +-- OpenRA.Game/Graphics/TerrainRenderer.cs | 4 +- OpenRA.Game/Graphics/WorldRenderer.cs | 4 +- OpenRA.Game/Traits/World/ScreenShaker.cs | 2 +- OpenRA.TilesetBuilder/Form1.cs | 42 ++++++++------- OpenRA.TilesetBuilder/Program.cs | 3 +- 9 files changed, 81 insertions(+), 77 deletions(-) diff --git a/OpenRA.Editor/Form1.cs b/OpenRA.Editor/Form1.cs index 990dc4a993..b9a387ac59 100644 --- a/OpenRA.Editor/Form1.cs +++ b/OpenRA.Editor/Form1.cs @@ -201,7 +201,7 @@ namespace OpenRA.Editor var template = ts.Templates[n]; var tile = ts.Tiles[n]; - var bitmap = new Bitmap(24 * template.Size.X, 24 * template.Size.Y); + var bitmap = new Bitmap(ts.TileSize * template.Size.X, ts.TileSize * template.Size.Y); var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); @@ -215,15 +215,15 @@ namespace OpenRA.Editor if (tile.TileBitmapBytes[u + v * template.Size.X] != null) { var rawImage = tile.TileBitmapBytes[u + v * template.Size.X]; - for (var i = 0; i < 24; i++) - for (var j = 0; j < 24; j++) - q[(v * 24 + j) * stride + u * 24 + i] = p.GetColor(rawImage[i + 24 * j]).ToArgb(); + for (var i = 0; i < ts.TileSize; i++) + for (var j = 0; j < ts.TileSize; j++) + q[(v * ts.TileSize + j) * stride + u * ts.TileSize + i] = p.GetColor(rawImage[i + ts.TileSize * j]).ToArgb(); } else { - for (var i = 0; i < 24; i++) - for (var j = 0; j < 24; j++) - q[(v * 24 + j) * stride + u * 24 + i] = Color.Transparent.ToArgb(); + for (var i = 0; i < ts.TileSize; i++) + for (var j = 0; j < ts.TileSize; j++) + q[(v * ts.TileSize + j) * stride + u * ts.TileSize + i] = Color.Transparent.ToArgb(); } } diff --git a/OpenRA.Editor/Surface.cs b/OpenRA.Editor/Surface.cs index da10896c8c..f95265782d 100755 --- a/OpenRA.Editor/Surface.cs +++ b/OpenRA.Editor/Surface.cs @@ -346,7 +346,7 @@ namespace OpenRA.Editor Bitmap RenderChunk(int u, int v) { - var bitmap = new Bitmap(ChunkSize * 24, ChunkSize * 24); + var bitmap = new Bitmap(ChunkSize * TileSet.TileSize, ChunkSize * TileSet.TileSize); bitmap.SetPixel(0, 0, Color.Green); var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), @@ -364,9 +364,9 @@ namespace OpenRA.Editor var tile = TileSet.Tiles[tr.type]; var index = (tr.image < tile.TileBitmapBytes.Count) ? tr.image : (byte)0; var rawImage = tile.TileBitmapBytes[index]; - for (var x = 0; x < 24; x++) - for (var y = 0; y < 24; y++) - p[(j * 24 + y) * stride + i * 24 + x] = Palette.GetColor(rawImage[x + 24 * y]).ToArgb(); + for (var x = 0; x < TileSet.TileSize; x++) + for (var y = 0; y < TileSet.TileSize; y++) + p[(j * TileSet.TileSize + y) * stride + i * TileSet.TileSize + x] = Palette.GetColor(rawImage[x + TileSet.TileSize * y]).ToArgb(); if (Map.MapResources[u * ChunkSize + i, v * ChunkSize + j].type != 0) { @@ -377,12 +377,12 @@ namespace OpenRA.Editor int* q = (int*)srcdata.Scan0.ToPointer(); var srcstride = srcdata.Stride >> 2; - for (var x = 0; x < 24; x++) - for (var y = 0; y < 24; y++) + for (var x = 0; x < TileSet.TileSize; x++) + for (var y = 0; y < TileSet.TileSize; y++) { var c = q[y * srcstride + x]; if ((c & 0xff000000) != 0) /* quick & dirty, i cbf doing real alpha */ - p[(j * 24 + y) * stride + i * 24 + x] = c; + p[(j * TileSet.TileSize + y) * stride + i * TileSet.TileSize + x] = c; } resourceImage.UnlockBits(srcdata); @@ -398,16 +398,16 @@ namespace OpenRA.Editor { var vX = (int)Math.Floor((MousePos.X - Offset.X) / Zoom); var vY = (int)Math.Floor((MousePos.Y - Offset.Y) / Zoom); - return new int2(vX / 24, vY / 24); + return new int2(vX / TileSet.TileSize, vY / TileSet.TileSize); } void DrawActor(System.Drawing.Graphics g, int2 p, ActorTemplate t) { - float OffsetX = t.Centered ? t.Bitmap.Width / 2 - 12 : 0; - float DrawX = 24 * p.X * Zoom + Offset.X - OffsetX; + float OffsetX = t.Centered ? t.Bitmap.Width / 2 - TileSet.TileSize/2 : 0; + float DrawX = TileSet.TileSize * p.X * Zoom + Offset.X - OffsetX; - float OffsetY = t.Centered ? t.Bitmap.Height / 2 - 12 : 0; - float DrawY = 24 * p.Y * Zoom + Offset.Y - OffsetY; + float OffsetY = t.Centered ? t.Bitmap.Height / 2 - TileSet.TileSize/2 : 0; + float DrawY = TileSet.TileSize * p.Y * Zoom + Offset.Y - OffsetY; float width = t.Bitmap.Width * Zoom; float height = t.Bitmap.Height * Zoom; @@ -418,11 +418,11 @@ namespace OpenRA.Editor void DrawImage(System.Drawing.Graphics g, Bitmap bmp, int2 location) { - float OffsetX = bmp.Width / 2 - 12; - float DrawX = 24 * location.X * Zoom + Offset.X - OffsetX; + float OffsetX = bmp.Width / 2 - TileSet.TileSize / 2; + float DrawX = TileSet.TileSize * location.X * Zoom + Offset.X - OffsetX; - float OffsetY = bmp.Height / 2 - 12; - float DrawY = 24 * location.Y * Zoom + Offset.Y - OffsetY; + float OffsetY = bmp.Height / 2 - TileSet.TileSize / 2; + float DrawY = TileSet.TileSize * location.Y * Zoom + Offset.Y - OffsetY; float width = bmp.Width * Zoom; float height = bmp.Height * Zoom; @@ -433,11 +433,11 @@ namespace OpenRA.Editor void DrawActorBorder(System.Drawing.Graphics g, int2 p, ActorTemplate t) { - float OffsetX = t.Centered ? t.Bitmap.Width / 2 - 12 : 0; - float DrawX = 24 * p.X * Zoom + Offset.X - OffsetX; + float OffsetX = t.Centered ? t.Bitmap.Width / 2 - TileSet.TileSize / 2 : 0; + float DrawX = TileSet.TileSize * p.X * Zoom + Offset.X - OffsetX; - float OffsetY = t.Centered ? t.Bitmap.Height / 2 - 12 : 0; - float DrawY = 24 * p.Y * Zoom + Offset.Y - OffsetY; + float OffsetY = t.Centered ? t.Bitmap.Height / 2 - TileSet.TileSize / 2 : 0; + float DrawY = TileSet.TileSize * p.Y * Zoom + Offset.Y - OffsetY; float width = t.Bitmap.Width * Zoom; float height = t.Bitmap.Height * Zoom; @@ -461,32 +461,32 @@ namespace OpenRA.Editor Bitmap bmp = Chunks[x]; - float DrawX = 24.0f * (float)ChunkSize * (float)x.X * Zoom + Offset.X; - float DrawY = 24.0f * (float)ChunkSize * (float)x.Y * Zoom + Offset.Y; + float DrawX = TileSet.TileSize* 1f * (float)ChunkSize * (float)x.X * Zoom + Offset.X; + float DrawY = TileSet.TileSize * 1f * (float)ChunkSize * (float)x.Y * Zoom + Offset.Y; RectangleF sourceRect = new RectangleF(0, 0, bmp.Width, bmp.Height); RectangleF destRect = new RectangleF(DrawX, DrawY, bmp.Width * Zoom, bmp.Height * Zoom); e.Graphics.DrawImage(bmp, destRect, sourceRect, GraphicsUnit.Pixel); } e.Graphics.DrawRectangle(CordonPen, - Map.XOffset * 24 * Zoom + Offset.X, - Map.YOffset * 24 * Zoom + Offset.Y, - Map.Width * 24 * Zoom, - Map.Height * 24 * Zoom); + Map.XOffset * TileSet.TileSize * Zoom + Offset.X, + Map.YOffset * TileSet.TileSize * Zoom + Offset.Y, + Map.Width * TileSet.TileSize * Zoom, + Map.Height * TileSet.TileSize * Zoom); foreach (var ar in Map.Actors) DrawActor(e.Graphics, ar.Value.Location(), ActorTemplates[ar.Value.Type]); foreach (var wp in Map.Waypoints) e.Graphics.DrawRectangle(Pens.LimeGreen, - 24 * wp.Value.X * Zoom + Offset.X + 4, - 24 * wp.Value.Y * Zoom + Offset.Y + 4, - 16 * Zoom, 16 * Zoom); + TileSet.TileSize * wp.Value.X * Zoom + Offset.X + 4, + TileSet.TileSize * wp.Value.Y * Zoom + Offset.Y + 4, + (TileSet.TileSize - 8) * Zoom, (TileSet.TileSize - 8) * Zoom); if (Brush != null) e.Graphics.DrawImage(Brush.Bitmap, - 24 * GetBrushLocation().X * Zoom + Offset.X, - 24 * GetBrushLocation().Y * Zoom + Offset.Y, + TileSet.TileSize * GetBrushLocation().X * Zoom + Offset.X, + TileSet.TileSize * GetBrushLocation().Y * Zoom + Offset.Y, Brush.Bitmap.Width * Zoom, Brush.Bitmap.Height * Zoom); @@ -498,9 +498,9 @@ namespace OpenRA.Editor if (Waypoint != null) e.Graphics.DrawRectangle(Pens.LimeGreen, - 24 * GetBrushLocation().X * Zoom + Offset.X + 4, - 24 * GetBrushLocation().Y * Zoom + Offset.Y + 4, - 16 * Zoom, 16 * Zoom); + TileSet.TileSize * GetBrushLocation().X * Zoom + Offset.X + 4, + TileSet.TileSize * GetBrushLocation().Y * Zoom + Offset.Y + 4, + (TileSet.TileSize - 8) * Zoom, (TileSet.TileSize - 8) * Zoom); if (Brush == null && Actor == null && Resource == null) { diff --git a/OpenRA.FileFormats/Map/Terrain.cs b/OpenRA.FileFormats/Map/Terrain.cs index 6cfd340113..c431b69701 100644 --- a/OpenRA.FileFormats/Map/Terrain.cs +++ b/OpenRA.FileFormats/Map/Terrain.cs @@ -17,15 +17,15 @@ namespace OpenRA.FileFormats { public readonly List TileBitmapBytes = new List(); - public Terrain( Stream stream ) + public Terrain( Stream stream, int size ) { // Try loading as a cnc .tem BinaryReader reader = new BinaryReader( stream ); int Width = reader.ReadUInt16(); int Height = reader.ReadUInt16(); - if( Width != 24 || Height != 24 ) - throw new InvalidDataException( string.Format( "{0}x{1}", Width, Height ) ); + if( Width != size || Height != size ) + throw new InvalidDataException( string.Format( "Expected tile of size {0}x{1}, got {1}x{2]", size, size, Width, Height ) ); /*NumTiles = */reader.ReadUInt16(); /*Zero1 = */reader.ReadUInt16(); @@ -46,9 +46,7 @@ namespace OpenRA.FileFormats reader = new BinaryReader( stream ); Width = reader.ReadUInt16(); Height = reader.ReadUInt16(); - if( Width != 24 || Height != 24 ) - throw new InvalidDataException( string.Format( "{0}x{1}", Width, Height ) ); - + /*NumTiles = */reader.ReadUInt16(); reader.ReadUInt16(); /*XDim = */reader.ReadUInt16(); @@ -67,8 +65,8 @@ namespace OpenRA.FileFormats { if (b != 255) { - stream.Position = ImgStart + b * 24 * 24; - TileBitmapBytes.Add(new BinaryReader(stream).ReadBytes(24 * 24)); + stream.Position = ImgStart + b * size * size; + TileBitmapBytes.Add(new BinaryReader(stream).ReadBytes(size * size)); } else TileBitmapBytes.Add(null); diff --git a/OpenRA.FileFormats/Map/TileSet.cs b/OpenRA.FileFormats/Map/TileSet.cs index cde06ea959..a356d44bff 100644 --- a/OpenRA.FileFormats/Map/TileSet.cs +++ b/OpenRA.FileFormats/Map/TileSet.cs @@ -74,11 +74,12 @@ namespace OpenRA.FileFormats public string Name; public string Id; public string Palette; + public int TileSize = 24; public string[] Extensions; public Dictionary Terrain = new Dictionary(); public Dictionary Tiles = new Dictionary(); public Dictionary Templates = new Dictionary(); - static List fields = new List() {"Name", "Id", "Palette", "Extensions"}; + static List fields = new List() {"Name", "TileSize", "Id", "Palette", "Extensions"}; public TileSet() {} public TileSet( string filepath ) @@ -103,7 +104,7 @@ namespace OpenRA.FileFormats using( Stream s = FileSystem.OpenWithExts(t.Value.Image, Extensions) ) { if( !Tiles.ContainsKey( t.Key ) ) - Tiles.Add( t.Key, new Terrain( s ) ); + Tiles.Add( t.Key, new Terrain( s, TileSize ) ); } } @@ -144,7 +145,7 @@ namespace OpenRA.FileFormats if( Tiles.TryGetValue( r.type, out tile ) ) return tile.TileBitmapBytes[ r.image ]; - byte[] missingTile = new byte[ 24 * 24 ]; + byte[] missingTile = new byte[ TileSize * TileSize ]; for( int i = 0 ; i < missingTile.Length ; i++ ) missingTile[ i ] = 0x36; diff --git a/OpenRA.Game/Graphics/TerrainRenderer.cs b/OpenRA.Game/Graphics/TerrainRenderer.cs index c60e9dddb2..23259235ab 100644 --- a/OpenRA.Game/Graphics/TerrainRenderer.cs +++ b/OpenRA.Game/Graphics/TerrainRenderer.cs @@ -68,9 +68,9 @@ namespace OpenRA.Graphics int indicesPerRow = map.Width * 6; int verticesPerRow = map.Width * 4; - int visibleRows = (int)(viewport.Height / 24.0f + 2); + int visibleRows = (int)(viewport.Height * 1f / Game.CellSize + 2); - int firstRow = (int)((viewport.Location.Y) / 24.0f - map.YOffset); + int firstRow = (int)(viewport.Location.Y * 1f / Game.CellSize - map.YOffset); int lastRow = firstRow + visibleRows; if (lastRow < 0 || firstRow > map.Height) diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index 6780e2ff40..2a35a12032 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -142,8 +142,8 @@ namespace OpenRA.Graphics for (var j = 0; j < world.Map.MapSize.Y; j += world.WorldActor.Info.Traits.Get().BinSize) { - Game.Renderer.LineRenderer.DrawLine(new float2(0, j * 24), new float2(world.Map.MapSize.X * 24, j * 24), Color.Black, Color.Black); - Game.Renderer.LineRenderer.DrawLine(new float2(j * 24, 0), new float2(j * 24, world.Map.MapSize.Y * 24), Color.Black, Color.Black); + Game.Renderer.LineRenderer.DrawLine(new float2(0, j * Game.CellSize), new float2(world.Map.MapSize.X * Game.CellSize, j * Game.CellSize), Color.Black, Color.Black); + Game.Renderer.LineRenderer.DrawLine(new float2(j * Game.CellSize, 0), new float2(j * Game.CellSize, world.Map.MapSize.Y * Game.CellSize), Color.Black, Color.Black); } } diff --git a/OpenRA.Game/Traits/World/ScreenShaker.cs b/OpenRA.Game/Traits/World/ScreenShaker.cs index 2ccf33a4b9..e1f7a15323 100644 --- a/OpenRA.Game/Traits/World/ScreenShaker.cs +++ b/OpenRA.Game/Traits/World/ScreenShaker.cs @@ -51,7 +51,7 @@ namespace OpenRA.Traits var cp = Game.viewport.Location + .5f * new float2(Game.viewport.Width, Game.viewport.Height); - var intensity = 24 * 24 * 100 * shakeEffects.Sum( + var intensity = Game.CellSize * Game.CellSize * 100 * shakeEffects.Sum( e => e.Intensity / (e.Position - cp).LengthSquared); return Math.Min(intensity, 10); diff --git a/OpenRA.TilesetBuilder/Form1.cs b/OpenRA.TilesetBuilder/Form1.cs index 9cf5176806..4b8ed60c49 100644 --- a/OpenRA.TilesetBuilder/Form1.cs +++ b/OpenRA.TilesetBuilder/Form1.cs @@ -23,14 +23,16 @@ namespace OpenRA.TilesetBuilder public partial class Form1 : Form { string srcfile; - public Form1( string src ) + int TileSize; + public Form1( string src, int size ) { srcfile = src; - + TileSize = size; InitializeComponent(); + surface1.TileSize = TileSize; surface1.Image = (Bitmap)Image.FromFile(src); - surface1.TerrainTypes = new int[surface1.Image.Width / 24, surface1.Image.Height / 24]; /* all passable by default */ + surface1.TerrainTypes = new int[surface1.Image.Width / size, surface1.Image.Height / size]; /* all passable by default */ surface1.Templates = new List