diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index f23ef1b56c..a18d923f16 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -154,5 +154,16 @@ namespace OpenRA { for(;;) { yield return t; t = f(t); } } + + public static int NextPowerOf2(int v) + { + --v; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + ++v; + return v; + } } } diff --git a/OpenRA.FileFormats/Graphics/VqaReader.cs b/OpenRA.FileFormats/Graphics/VqaReader.cs index da0aaa48bb..2e88134f53 100644 --- a/OpenRA.FileFormats/Graphics/VqaReader.cs +++ b/OpenRA.FileFormats/Graphics/VqaReader.cs @@ -84,7 +84,7 @@ namespace OpenRA.FileFormats /*var unknown3 = */reader.ReadChars(14); - var frameSize = NextPowerOf2(Math.Max(Width,Height)); + var frameSize = Exts.NextPowerOf2(Math.Max(Width,Height)); cbf = new byte[Width*Height]; cbp = new byte[Width*Height]; palette = new uint[numColors]; @@ -258,37 +258,34 @@ namespace OpenRA.FileFormats } int cachedFrame = -1; - public uint[,] FrameData { get + + void DecodeFrameData( int frame ) { - if (cachedFrame != currentFrame) + cachedFrame = currentFrame; + for (var y = 0; y < blocks.Y; y++) + for (var x = 0; x < blocks.X; x++) + { + var px = origData[x + y*blocks.X]; + var mod = origData[x + (y + blocks.Y)*blocks.X]; + for (var j = 0; j < blockHeight; j++) + for (var i = 0; i < blockWidth; i++) + { + var cbfi = (mod*256 + px)*8 + j*blockWidth + i; + byte color = (mod == 0x0f) ? px : cbf[cbfi]; + frameData[y*blockHeight + j, x*blockWidth + i] = palette[color]; + } + } + } + + public uint[,] FrameData + { + get { - cachedFrame = currentFrame; - for (var y = 0; y < blocks.Y; y++) - for (var x = 0; x < blocks.X; x++) - { - var px = origData[x + y*blocks.X]; - var mod = origData[x + (y + blocks.Y)*blocks.X]; - for (var j = 0; j < blockHeight; j++) - for (var i = 0; i < blockWidth; i++) - { - var cbfi = (mod*256 + px)*8 + j*blockWidth + i; - byte color = (mod == 0x0f) ? px : cbf[cbfi]; - frameData[y*blockHeight + j, x*blockWidth + i] = palette[color]; - } - } + if (cachedFrame != currentFrame) + DecodeFrameData(currentFrame); + + return frameData; } - return frameData; - }} - - int NextPowerOf2(int v) - { - --v; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - ++v; - return v; } } } diff --git a/OpenRA.Game/Graphics/Minimap.cs b/OpenRA.Game/Graphics/Minimap.cs index 04426e5477..b9b7a1a60e 100644 --- a/OpenRA.Game/Graphics/Minimap.cs +++ b/OpenRA.Game/Graphics/Minimap.cs @@ -11,10 +11,10 @@ using System; using System.Drawing; using System.Drawing.Imaging; +using System.IO; using System.Linq; using OpenRA.FileFormats; using OpenRA.Traits; -using System.IO; namespace OpenRA.Graphics { @@ -33,10 +33,10 @@ namespace OpenRA.Graphics if (!actualSize) { - width = height = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); + width = height = Exts.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); } - Bitmap terrain = new Bitmap(width, height); + var terrain = new Bitmap(width, height); var bitmapData = terrain.LockBits(new Rectangle(0, 0, terrain.Width, terrain.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); @@ -57,10 +57,11 @@ namespace OpenRA.Graphics *(c + (y * bitmapData.Stride >> 2) + x) = tileset.Terrain[type].Color.ToArgb(); } } + terrain.UnlockBits(bitmapData); return terrain; } - + // Add the static resources defined in the map; if the map lives // in a world use AddCustomTerrain instead public static Bitmap AddStaticResources(Map map, Bitmap terrainBitmap) @@ -92,6 +93,7 @@ namespace OpenRA.Graphics *(c + (y * bitmapData.Stride >> 2) + x) = tileset.Terrain[res].Color.ToArgb(); } } + terrain.UnlockBits(bitmapData); return terrain; @@ -100,8 +102,8 @@ namespace OpenRA.Graphics public static Bitmap CustomTerrainBitmap(World world) { var map = world.Map; - var size = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); - Bitmap bitmap = new Bitmap(size, size); + var size = Exts.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); + var bitmap = new Bitmap(size, size); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); @@ -120,6 +122,7 @@ namespace OpenRA.Graphics *(c + (y * bitmapData.Stride >> 2) + x) = world.TileSet.Terrain[custom].Color.ToArgb(); } } + bitmap.UnlockBits(bitmapData); return bitmap; } @@ -127,8 +130,8 @@ namespace OpenRA.Graphics public static Bitmap ActorsBitmap(World world) { var map = world.Map; - var size = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); - Bitmap bitmap = new Bitmap(size, size); + var size = Exts.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); + var bitmap = new Bitmap(size, size); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); @@ -155,8 +158,8 @@ namespace OpenRA.Graphics public static Bitmap ShroudBitmap(World world) { var map = world.Map; - var size = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); - Bitmap bitmap = new Bitmap(size, size); + var size = Exts.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); + var bitmap = new Bitmap(size, size); if (world.LocalShroud.Disabled) return bitmap; diff --git a/OpenRA.Game/Graphics/Util.cs b/OpenRA.Game/Graphics/Util.cs index a10a836447..a0038900ae 100644 --- a/OpenRA.Game/Graphics/Util.cs +++ b/OpenRA.Game/Graphics/Util.cs @@ -102,16 +102,5 @@ namespace OpenRA.Graphics { return (int)((1 - t) * a + t * b); } - - public static int NextPowerOf2(int v) - { - --v; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - ++v; - return v; - } } } diff --git a/OpenRA.Game/Widgets/VqaPlayerWidget.cs b/OpenRA.Game/Widgets/VqaPlayerWidget.cs index 874a43c3bf..32fd64f469 100644 --- a/OpenRA.Game/Widgets/VqaPlayerWidget.cs +++ b/OpenRA.Game/Widgets/VqaPlayerWidget.cs @@ -55,7 +55,7 @@ namespace OpenRA.Widgets invLength = video.Framerate*1f/video.Frames; var size = Math.Max(video.Width, video.Height); - var textureSize = OpenRA.Graphics.Util.NextPowerOf2(size); + var textureSize = Exts.NextPowerOf2(size); videoSprite = new Sprite(new Sheet(new Size(textureSize,textureSize)), new Rectangle( 0, 0, video.Width, video.Height ), TextureChannel.Alpha); videoSprite.sheet.Texture.SetData(video.FrameData); diff --git a/OpenRA.Renderer.Cg/Texture.cs b/OpenRA.Renderer.Cg/Texture.cs index 6d9f387e88..a8cd5a339f 100644 --- a/OpenRA.Renderer.Cg/Texture.cs +++ b/OpenRA.Renderer.Cg/Texture.cs @@ -12,6 +12,7 @@ using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; +using OpenRA.FileFormats; using OpenRA.FileFormats.Graphics; using Tao.OpenGl; @@ -94,7 +95,7 @@ namespace OpenRA.Renderer.Cg if (!IsPowerOf2(bitmap.Width) || !IsPowerOf2(bitmap.Height)) { //throw new InvalidOperationException( "non-power-of-2-texture" ); - bitmap = new Bitmap(bitmap, new Size(NextPowerOf2(bitmap.Width), NextPowerOf2(bitmap.Height))); + bitmap = new Bitmap(bitmap, new Size(Exts.NextPowerOf2(bitmap.Width), Exts.NextPowerOf2(bitmap.Height))); } var bits = bitmap.LockBits( @@ -114,16 +115,5 @@ namespace OpenRA.Renderer.Cg { return (v & (v - 1)) == 0; } - - int NextPowerOf2(int v) - { - --v; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - ++v; - return v; - } } } diff --git a/OpenRA.Renderer.Gl/Texture.cs b/OpenRA.Renderer.Gl/Texture.cs index cb0d4f65f8..1568074d99 100644 --- a/OpenRA.Renderer.Gl/Texture.cs +++ b/OpenRA.Renderer.Gl/Texture.cs @@ -12,6 +12,7 @@ using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; +using OpenRA.FileFormats; using OpenRA.FileFormats.Graphics; using Tao.OpenGl; @@ -120,7 +121,7 @@ namespace OpenRA.Renderer.Glsl if (!IsPowerOf2(bitmap.Width) || !IsPowerOf2(bitmap.Height)) { //throw new InvalidOperationException( "non-power-of-2-texture" ); - bitmap = new Bitmap(bitmap, new Size(NextPowerOf2(bitmap.Width), NextPowerOf2(bitmap.Height))); + bitmap = new Bitmap(bitmap, new Size(Exts.NextPowerOf2(bitmap.Width), Exts.NextPowerOf2(bitmap.Height))); } var bits = bitmap.LockBits( @@ -141,16 +142,5 @@ namespace OpenRA.Renderer.Glsl { return (v & (v - 1)) == 0; } - - int NextPowerOf2(int v) - { - --v; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - ++v; - return v; - } } }