Split the radar into multiple bitmaps

This commit is contained in:
Paul Chote
2010-07-22 22:54:12 +12:00
parent d21e9fe093
commit ac526f9762
3 changed files with 64 additions and 33 deletions

View File

@@ -19,7 +19,7 @@ namespace OpenRA.Graphics
{
class Minimap
{
public static Bitmap RenderTerrainBitmap(Map map)
public static Bitmap TerrainBitmap(Map map)
{
var tileset = Rules.TileSets[map.Tileset];
var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height));
@@ -81,10 +81,11 @@ namespace OpenRA.Graphics
return terrain;
}
public static Bitmap AddCustomTerrain(World world, Bitmap terrainBitmap)
public static Bitmap CustomTerrainBitmap(World world)
{
var map = world.Map;
var bitmap = new Bitmap(terrainBitmap);
var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height));
Bitmap bitmap = new Bitmap(size, size);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
@@ -107,16 +108,14 @@ namespace OpenRA.Graphics
return bitmap;
}
public static Bitmap AddActors(World world, Bitmap terrain)
public static Bitmap ActorsBitmap(World world)
{
var map = world.Map;
var bitmap = new Bitmap(terrain);
var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height));
Bitmap bitmap = new Bitmap(size, size);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
var shroud = Color.Black.ToArgb();
var fogOpacity = 0.5f;
unsafe
{
int* c = (int*)bitmapData.Scan0;
@@ -127,23 +126,36 @@ namespace OpenRA.Graphics
foreach( var cell in t.Trait.RadarSignatureCells(t.Actor))
*(c + ((cell.Y - world.Map.TopLeft.Y)* bitmapData.Stride >> 2) + cell.X - world.Map.TopLeft.X) = color.ToArgb();
}
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public static Bitmap ShroudBitmap(World world)
{
var map = world.Map;
var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height));
Bitmap bitmap = new Bitmap(size, size);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
var shroud = Color.Black.ToArgb();
var fog = Color.FromArgb(128, Color.Black).ToArgb();
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var x = 0; x < map.Width; x++)
for (var y = 0; y < map.Height; y++)
{
var mapX = x + map.TopLeft.X;
var mapY = y + map.TopLeft.Y;
if (!world.LocalPlayer.Shroud.IsExplored(mapX, mapY))
{
*(c + (y * bitmapData.Stride >> 2) + x) = shroud;
continue;
}
if (!world.LocalPlayer.Shroud.IsVisible(mapX,mapY))
{
*(c + (y * bitmapData.Stride >> 2) + x) = Util.LerpARGBColor(fogOpacity, *(c + (y * bitmapData.Stride >> 2) + x), shroud);
continue;
}
else if (!world.LocalPlayer.Shroud.IsVisible(mapX,mapY))
*(c + (y * bitmapData.Stride >> 2) + x) = fog;
}
}
@@ -153,7 +165,7 @@ namespace OpenRA.Graphics
public static Bitmap RenderMapPreview(Map map)
{
Bitmap terrain = RenderTerrainBitmap(map);
Bitmap terrain = TerrainBitmap(map);
return AddStaticResources(map, terrain);
}
}

View File

@@ -73,7 +73,10 @@ namespace OpenRA.Traits
for (int x = map.XOffset; x < map.XOffset + map.Width; x++)
for (int y = map.YOffset; y < map.YOffset + map.Height; y++)
if (content[x, y].type != null)
{
content[x, y].density = GetIdealDensity(x, y);
w.Map.CustomTerrain[x, y] = content[x, y].type.info.TerrainType;
}
}
Sprite[] ChooseContent(ResourceType t)

View File

@@ -27,8 +27,7 @@ namespace OpenRA.Widgets
bool radarAnimating = false;
bool hasRadar = false;
Sheet radarSheet;
Sprite radarSprite;
string radarCollection;
@@ -37,7 +36,12 @@ namespace OpenRA.Widgets
float previewScale = 0;
RectangleF mapRect = Rectangle.Empty;
int2 previewOrigin;
Bitmap terrainBitmap = null;
Sprite terrainSprite;
Sprite customTerrainSprite;
Sprite actorSprite;
Sprite shroudSprite;
public void SetWorld(World world)
{
this.world = world;
@@ -48,9 +52,18 @@ namespace OpenRA.Widgets
previewOrigin = new int2(9 + (int)(radarOpenOrigin.X + previewScale * (size - world.Map.Width)/2), (int)(radarOpenOrigin.Y + previewScale * (size - world.Map.Height)/2));
mapRect = new RectangleF(previewOrigin.X, previewOrigin.Y, (int)(world.Map.Width * previewScale), (int)(world.Map.Height * previewScale));
terrainBitmap = Minimap.RenderTerrainBitmap(world.Map);
radarSheet = new Sheet(new Size( terrainBitmap.Width, terrainBitmap.Height ) );
radarSprite = new Sprite( radarSheet, new Rectangle( 0, 0, world.Map.Width, world.Map.Height ), TextureChannel.Alpha );
// Only needs to be done once
var terrainBitmap = Minimap.TerrainBitmap(world.Map);
var r = new Rectangle( 0, 0, world.Map.Width, world.Map.Height );
var s = new Size( terrainBitmap.Width, terrainBitmap.Height );
terrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
terrainSprite.sheet.Texture.SetData(terrainBitmap);
// Data is set in Tick()
customTerrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
actorSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
shroudSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
}
public override string GetCursor(int2 pos)
@@ -120,14 +133,17 @@ namespace OpenRA.Widgets
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "bottom"), radarOrigin + new float2(0, 192), "chrome");
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "bg"), radarOrigin + new float2(9, 0), "chrome");
Game.Renderer.RgbaSpriteRenderer.Flush();
// Don't draw the radar if the tray is moving
if (radarAnimationFrame >= radarSlideAnimationLength)
{
Game.Renderer.RgbaSpriteRenderer.DrawSprite( radarSprite,
new float2(mapRect.Location.X, mapRect.Location.Y + world.Map.Height * previewScale * (1 - radarMinimapHeight)/2),
"chrome",
new float2(mapRect.Size.Width, mapRect.Size.Height*radarMinimapHeight) );
var o = new float2(mapRect.Location.X, mapRect.Location.Y + world.Map.Height * previewScale * (1 - radarMinimapHeight)/2);
var s = new float2(mapRect.Size.Width, mapRect.Size.Height*radarMinimapHeight);
Game.Renderer.RgbaSpriteRenderer.DrawSprite(terrainSprite, o, "chrome", s);
Game.Renderer.RgbaSpriteRenderer.DrawSprite(customTerrainSprite, o, "chrome", s);
Game.Renderer.RgbaSpriteRenderer.DrawSprite(actorSprite, o, "chrome", s);
Game.Renderer.RgbaSpriteRenderer.DrawSprite(shroudSprite, o, "chrome", s);
}
}
@@ -145,9 +161,9 @@ namespace OpenRA.Widgets
if (hasRadar)
{
// Build the radar image
var custom = Minimap.AddCustomTerrain(world,terrainBitmap);
var final = Minimap.AddActors(world, custom);
radarSheet.Texture.SetData(final);
customTerrainSprite.sheet.Texture.SetData(Minimap.CustomTerrainBitmap(world));
actorSprite.sheet.Texture.SetData(Minimap.ActorsBitmap(world));
shroudSprite.sheet.Texture.SetData(Minimap.ShroudBitmap(world));
}
if (!radarAnimating)