Cleaner attempt at rendering map previews on the fly

This commit is contained in:
Paul Chote
2010-07-15 20:50:18 +12:00
parent d274c10e95
commit 45061cdf37
3 changed files with 46 additions and 9 deletions

View File

@@ -45,8 +45,8 @@ namespace OpenRA.FileFormats
public int2 BottomRight; public int2 BottomRight;
public int Width { get { return BottomRight.X - TopLeft.X; } } public int Width { get { return BottomRight.X - TopLeft.X; } }
public int Height { get { return BottomRight.Y - TopLeft.Y; } } public int Height { get { return BottomRight.Y - TopLeft.Y; } }
public Lazy<Bitmap> Preview; public Map Map { get { return new Map(Package); }}
static List<string> Fields = new List<string>() { static List<string> Fields = new List<string>() {
"Selectable", "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight" "Selectable", "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight"
}; };
@@ -64,10 +64,6 @@ namespace OpenRA.FileFormats
Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]), int.Parse(loc[1]))); Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]), int.Parse(loc[1])));
} }
Preview = Lazy.New(
() => new Bitmap(Package.GetContent("preview.png"))
);
Uid = Package.GetContent("map.uid").ReadAllText(); Uid = Package.GetContent("map.uid").ReadAllText();
} }

View File

@@ -162,5 +162,43 @@ namespace OpenRA.Graphics
(int)(bounds.Width * fx + bounds.Left), (int)(bounds.Width * fx + bounds.Left),
(int)(bounds.Height * fy + bounds.Top)); (int)(bounds.Height * fy + bounds.Top));
} }
static int NextPowerOf2(int v)
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
++v;
return v;
}
public static Bitmap RenderMapPreview(MapStub stub)
{
Map map = stub.Map;
var tileset = Rules.TileSets[map.Tileset];
var size = NextPowerOf2(Math.Max(map.Width, map.Height));
Bitmap terrain = new Bitmap(size, size);
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;
var type = tileset.GetTerrainType(map.MapTiles[mapX, mapY]);
string res = null;
if (map.MapResources[mapX, mapY].type != 0)
res = Rules.Info["world"].Traits.WithInterface<ResourceTypeInfo>()
.Where(t => t.ResourceType == map.MapResources[mapX, mapY].type)
.Select(t => t.TerrainType).FirstOrDefault();
if (res != null)
type = res;
terrain.SetPixel(x, y, tileset.Terrain[type].Color);
}
return terrain;
}
} }
} }

View File

@@ -20,6 +20,8 @@ namespace OpenRA.Widgets
public MapPreviewWidget() : base() { } public MapPreviewWidget() : base() { }
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview(stub));
protected MapPreviewWidget(MapPreviewWidget other) protected MapPreviewWidget(MapPreviewWidget other)
: base(other) : base(other)
{ {
@@ -74,10 +76,11 @@ namespace OpenRA.Widgets
if( mapPreviewDirty ) if( mapPreviewDirty )
{ {
if( mapChooserSheet == null || mapChooserSheet.Size.Width != map.Width || mapChooserSheet.Size.Height != map.Height ) var preview = PreviewCache[map];
mapChooserSheet = new Sheet( Game.renderer, new Size( map.Width, map.Height ) ); if( mapChooserSheet == null || mapChooserSheet.Size.Width != preview.Width || mapChooserSheet.Size.Height != preview.Height )
mapChooserSheet = new Sheet( Game.renderer, new Size( preview.Width, preview.Height ) );
mapChooserSheet.Texture.SetData( map.Preview.Value ); mapChooserSheet.Texture.SetData( preview );
mapChooserSprite = new Sprite( mapChooserSheet, new Rectangle( 0, 0, map.Width, map.Height ), TextureChannel.Alpha ); mapChooserSprite = new Sprite( mapChooserSheet, new Rectangle( 0, 0, map.Width, map.Height ), TextureChannel.Alpha );
mapPreviewDirty = false; mapPreviewDirty = false;
} }