Cleaner attempt at rendering map previews on the fly
This commit is contained in:
@@ -45,7 +45,7 @@ namespace OpenRA.FileFormats
|
||||
public int2 BottomRight;
|
||||
public int Width { get { return BottomRight.X - TopLeft.X; } }
|
||||
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>() {
|
||||
"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])));
|
||||
}
|
||||
|
||||
Preview = Lazy.New(
|
||||
() => new Bitmap(Package.GetContent("preview.png"))
|
||||
);
|
||||
|
||||
Uid = Package.GetContent("map.uid").ReadAllText();
|
||||
}
|
||||
|
||||
|
||||
@@ -162,5 +162,43 @@ namespace OpenRA.Graphics
|
||||
(int)(bounds.Width * fx + bounds.Left),
|
||||
(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ namespace OpenRA.Widgets
|
||||
|
||||
public MapPreviewWidget() : base() { }
|
||||
|
||||
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview(stub));
|
||||
|
||||
protected MapPreviewWidget(MapPreviewWidget other)
|
||||
: base(other)
|
||||
{
|
||||
@@ -74,10 +76,11 @@ namespace OpenRA.Widgets
|
||||
|
||||
if( mapPreviewDirty )
|
||||
{
|
||||
if( mapChooserSheet == null || mapChooserSheet.Size.Width != map.Width || mapChooserSheet.Size.Height != map.Height )
|
||||
mapChooserSheet = new Sheet( Game.renderer, new Size( map.Width, map.Height ) );
|
||||
var preview = PreviewCache[map];
|
||||
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 );
|
||||
mapPreviewDirty = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user