From cd268c11ee62f90e8d22dfbea95190d608c9a3c0 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 8 Jun 2013 11:46:44 +1200 Subject: [PATCH] Move IsPowerOf2 to Exts. --- OpenRA.FileFormats/Exts.cs | 5 +++++ OpenRA.Renderer.SdlCommon/Texture.cs | 14 +++----------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index e58d62321c..9c071b73e0 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -137,6 +137,11 @@ namespace OpenRA return v; } + public static bool IsPowerOf2(int v) + { + return (v & (v - 1)) == 0; + } + public static Size NextPowerOf2(this Size s) { return new Size(NextPowerOf2(s.Width), NextPowerOf2(s.Height)); } public static string JoinWith(this IEnumerable ts, string j) diff --git a/OpenRA.Renderer.SdlCommon/Texture.cs b/OpenRA.Renderer.SdlCommon/Texture.cs index 82a34af274..8aadc8e740 100644 --- a/OpenRA.Renderer.SdlCommon/Texture.cs +++ b/OpenRA.Renderer.SdlCommon/Texture.cs @@ -56,7 +56,7 @@ namespace OpenRA.Renderer.SdlCommon public void SetData(byte[] colors, int width, int height) { - if (!IsPowerOf2(width) || !IsPowerOf2(height)) + if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height)) throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height)); unsafe @@ -78,7 +78,7 @@ namespace OpenRA.Renderer.SdlCommon int width = colors.GetUpperBound(1) + 1; int height = colors.GetUpperBound(0) + 1; - if (!IsPowerOf2(width) || !IsPowerOf2(height)) + if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height)) throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height)); unsafe @@ -96,11 +96,8 @@ namespace OpenRA.Renderer.SdlCommon public void SetData(Bitmap bitmap) { - if (!IsPowerOf2(bitmap.Width) || !IsPowerOf2(bitmap.Height)) - { - //throw new InvalidOperationException( "non-power-of-2-texture" ); + if (!Exts.IsPowerOf2(bitmap.Width) || !Exts.IsPowerOf2(bitmap.Height)) bitmap = new Bitmap(bitmap, bitmap.Size.NextPowerOf2()); - } var bits = bitmap.LockBits(bitmap.Bounds(), ImageLockMode.ReadOnly, @@ -112,10 +109,5 @@ namespace OpenRA.Renderer.SdlCommon ErrorHandler.CheckGlError(); bitmap.UnlockBits(bits); } - - bool IsPowerOf2(int v) - { - return (v & (v - 1)) == 0; - } } }