More refactoring and a TODO on custom terrain perf

This commit is contained in:
Paul Chote
2010-07-22 01:13:03 +12:00
parent 2d2362a1a6
commit db9cfb9ddd
2 changed files with 28 additions and 17 deletions

View File

@@ -18,22 +18,11 @@ using OpenRA.Traits;
namespace OpenRA.Graphics namespace OpenRA.Graphics
{ {
class Minimap class Minimap
{ {
static int NextPowerOf2(int v)
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
++v;
return v;
}
public static Bitmap RenderTerrainBitmap(Map map) public static Bitmap RenderTerrainBitmap(Map map)
{ {
var tileset = Rules.TileSets[map.Tileset]; var tileset = Rules.TileSets[map.Tileset];
var size = NextPowerOf2(Math.Max(map.Width, map.Height)); var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height));
Bitmap terrain = new Bitmap(size, size); Bitmap terrain = new Bitmap(size, size);
var bitmapData = terrain.LockBits(new Rectangle(0, 0, terrain.Width, terrain.Height), var bitmapData = terrain.LockBits(new Rectangle(0, 0, terrain.Width, terrain.Height),
@@ -108,9 +97,12 @@ namespace OpenRA.Graphics
{ {
var mapX = x + map.TopLeft.X; var mapX = x + map.TopLeft.X;
var mapY = y + map.TopLeft.Y; var mapY = y + map.TopLeft.Y;
// This is extremely slow (~7ms). TODO: cache this information and only update when it changes
var customTerrain = world.WorldActor.traits.WithInterface<ITerrainTypeModifier>() var customTerrain = world.WorldActor.traits.WithInterface<ITerrainTypeModifier>()
.Select( t => t.GetTerrainType(new int2(mapX, mapY)) ) .Select( t => t.GetTerrainType(new int2(mapX, mapY)) )
.FirstOrDefault( t => t != null ); .FirstOrDefault( t => t != null );
if (customTerrain == null) continue; if (customTerrain == null) continue;
*(c + (y * bitmapData.Stride >> 2) + x) = world.TileSet.Terrain[customTerrain].Color.ToArgb(); *(c + (y * bitmapData.Stride >> 2) + x) = world.TileSet.Terrain[customTerrain].Color.ToArgb();
@@ -120,7 +112,7 @@ namespace OpenRA.Graphics
bitmap.UnlockBits(bitmapData); bitmap.UnlockBits(bitmapData);
return bitmap; return bitmap;
} }
public static Bitmap AddActors(World world, Bitmap terrain) public static Bitmap AddActors(World world, Bitmap terrain)
{ {
var map = world.Map; var map = world.Map;
@@ -151,13 +143,12 @@ namespace OpenRA.Graphics
continue; continue;
} }
if (!world.LocalPlayer.Shroud.IsVisible(mapX,mapY)) if (!world.LocalPlayer.Shroud.IsVisible(mapX,mapY))
{ {
*(c + (y * bitmapData.Stride >> 2) + x) = Util.Lerp(fogOpacity, Color.FromArgb(*(c + (y * bitmapData.Stride >> 2) + x)), Color.Black).ToArgb(); *(c + (y * bitmapData.Stride >> 2) + x) = Util.LerpARGBColor(fogOpacity, *(c + (y * bitmapData.Stride >> 2) + x), shroud);
continue; continue;
} }
var b = world.WorldActor.traits.Get<BuildingInfluence>().GetBuildingAt(new int2(mapX, mapY)); var b = world.WorldActor.traits.Get<BuildingInfluence>().GetBuildingAt(new int2(mapX, mapY));
if (b != null) if (b != null)
*(c + (y * bitmapData.Stride >> 2) + x) = b.Owner.Color.ToArgb(); *(c + (y * bitmapData.Stride >> 2) + x) = b.Owner.Color.ToArgb();
} }

View File

@@ -112,10 +112,30 @@ namespace OpenRA.Graphics
LerpChannel(t, a.G, b.G), LerpChannel(t, a.G, b.G),
LerpChannel(t, a.B, b.B)); LerpChannel(t, a.B, b.B));
} }
public static int LerpARGBColor(float t, int c1, int c2)
{
int a = LerpChannel(t, (c1 >> 24) & 255, (c2 >> 24) & 255);
int r = LerpChannel(t, (c1 >> 16) & 255, (c2 >> 16) & 255);
int g = LerpChannel(t, (c1 >> 8) & 255, (c2 >> 8) & 255);
int b = LerpChannel(t, c1 & 255, c2 & 255);
return (a << 24) | (r << 16) | (g << 8) | b;
}
public static int LerpChannel(float t, int a, int b) public static int LerpChannel(float t, int a, int b)
{ {
return (int)((1 - t) * a + t * b); 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;
}
} }
} }