Move IsPowerOf2 to Exts.

This commit is contained in:
Paul Chote
2013-06-08 11:46:44 +12:00
parent da8202a15e
commit cd268c11ee
2 changed files with 8 additions and 11 deletions

View File

@@ -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<T>(this IEnumerable<T> ts, string j)

View File

@@ -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;
}
}
}