started factoring bits out; added map chooser preview.

This commit is contained in:
Chris Forbes
2010-01-26 18:29:38 +13:00
parent 9b71a85f84
commit ad94d05ac7
2 changed files with 70 additions and 32 deletions

View File

@@ -74,6 +74,10 @@ namespace OpenRa
static float2 powerOrigin = new float2(42, 205); // Relative to radarOrigin static float2 powerOrigin = new float2(42, 205); // Relative to radarOrigin
static Size powerSize = new Size(138,5); static Size powerSize = new Size(138,5);
// mapchooser
Sheet mapChooserSheet;
Sprite mapChooserSprite;
public Chrome(Renderer r) public Chrome(Renderer r)
{ {
this.renderer = r; this.renderer = r;
@@ -137,6 +141,8 @@ namespace OpenRa
ready = new Animation("pips"); ready = new Animation("pips");
ready.PlayRepeating("ready"); ready.PlayRepeating("ready");
clock = new Animation("clock"); clock = new Animation("clock");
mapChooserSheet = new Sheet(r, new Size(128, 128));
} }
public void Tick() public void Tick()
@@ -226,15 +232,13 @@ namespace OpenRa
class MapInfo class MapInfo
{ {
public readonly string Filename; public readonly string Filename;
readonly Map Map; public readonly Map Map;
public MapInfo(string filename) public MapInfo(string filename)
{ {
Filename = filename.ToLowerInvariant(); Filename = filename.ToLowerInvariant();
Map = new Map(new IniFile(FileSystem.Open(Filename))); Map = new Map(new IniFile(FileSystem.Open(Filename)));
} }
public string Name { get { return Map.Title; } }
}; };
Lazy<List<MapInfo>> mapList = Lazy.New( Lazy<List<MapInfo>> mapList = Lazy.New(
@@ -247,6 +251,7 @@ namespace OpenRa
bool showMapChooser = false; bool showMapChooser = false;
MapInfo currentMap; MapInfo currentMap;
bool mapPreviewDirty = true;
void AddUiButton(int2 pos, string text, Action<bool> a) void AddUiButton(int2 pos, string text, Action<bool> a)
{ {
@@ -258,7 +263,7 @@ namespace OpenRa
public void DrawMapChooser() public void DrawMapChooser()
{ {
var w = 600; var w = 800;
var h = 600; var h = 600;
var r = new Rectangle( (Game.viewport.Width - w) / 2, (Game.viewport.Height - h) / 2, w, h ); var r = new Rectangle( (Game.viewport.Width - w) / 2, (Game.viewport.Height - h) / 2, w, h );
DrawDialogBackground(r, optionsSprites, true); DrawDialogBackground(r, optionsSprites, true);
@@ -280,17 +285,34 @@ namespace OpenRa
showMapChooser = false; showMapChooser = false;
}); });
if (mapPreviewDirty)
{
var b = Minimap.RenderTerrainBitmap(currentMap.Map, Game.world.TileSet); // tileset -> hack
mapChooserSheet.Texture.SetData(b);
mapChooserSprite = new Sprite(mapChooserSheet,
Minimap.MakeMinimapBounds(currentMap.Map), TextureChannel.Alpha);
mapPreviewDirty = false;
}
var mapRect = new Rectangle(r.Right - 280, r.Top + 30, 256, 256);
DrawDialogBackground(mapRect, panelSprites, false);
rgbaRenderer.DrawSprite(mapChooserSprite,
new float2(mapRect.Location) + new float2(4, 4),
PaletteType.Chrome,
new float2(mapRect.Size) - new float2(8, 8));
rgbaRenderer.Flush();
var y = r.Top + 50; var y = r.Top + 50;
foreach (var map in mapList.Value) foreach (var map in mapList.Value)
{ {
var itemRect = new Rectangle(r.Left + 50, y - 2, r.Width - 100, 20); var itemRect = new Rectangle(r.Left + 50, y - 2, r.Width - 340, 20);
if (map == currentMap) if (map == currentMap)
DrawDialogBackground(itemRect, panelSprites, false); DrawDialogBackground(itemRect, panelSprites, false);
renderer.DrawText(map.Name, new int2(r.Left + 60, y), Color.White); renderer.DrawText(map.Map.Title, new int2(r.Left + 60, y), Color.White);
var closureMap = map; var closureMap = map;
AddButton(itemRect, _ => currentMap = closureMap); AddButton(itemRect, _ => { currentMap = closureMap; mapPreviewDirty = true; });
y += 20; y += 20;
} }
@@ -342,6 +364,7 @@ namespace OpenRa
showMapChooser = true; showMapChooser = true;
currentMap = mapList.Value.Single( currentMap = mapList.Value.Single(
m => m.Filename == Game.LobbyInfo.GlobalSettings.Map.ToLowerInvariant()); m => m.Filename == Game.LobbyInfo.GlobalSettings.Map.ToLowerInvariant());
mapPreviewDirty = true;
} }
}); });
} }

View File

@@ -4,6 +4,7 @@ using System.Linq;
using OpenRa.Traits; using OpenRa.Traits;
using OpenRa.FileFormats; using OpenRa.FileFormats;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using IjwFramework.Collections;
namespace OpenRa.Graphics namespace OpenRa.Graphics
{ {
@@ -35,50 +36,63 @@ namespace OpenRa.Graphics
mapOnlySprite = new Sprite(mapOnlySheet, rect, TextureChannel.Alpha); mapOnlySprite = new Sprite(mapOnlySheet, rect, TextureChannel.Alpha);
} }
Color[] terrainTypeColors; public static Rectangle MakeMinimapBounds(Map m)
{
var size = Math.Max(m.Width, m.Height);
var dw = (size - m.Width) / 2;
var dh = (size - m.Height) / 2;
return new Rectangle(m.Offset.X - dw, m.Offset.Y - dh, size, size);
}
static Cache<string, Color[]> terrainTypeColors = new Cache<string, Color[]>(
theater =>
{
var pal = new Palette(FileSystem.Open(theater + ".pal"));
return new[] {
theater == "snow" ? 0xe3 :0x1a,
0x63, 0x2f, 0x1f, 0x14, 0x64, 0x1f, 0x68, 0x6b, 0x6d, 0x88 }
.Select(a => Color.FromArgb(alpha, pal.GetColor(a))).ToArray();
});
Color[] playerColors; Color[] playerColors;
Color shroudColor; static Color shroudColor;
string theater; string theater;
public void InvalidateOre() { oreLayer = null; } public void InvalidateOre() { oreLayer = null; }
public static Bitmap RenderTerrainBitmap(Map map, TileSet tileset)
{
var colors = terrainTypeColors[map.Theater.ToLowerInvariant()];
var terrain = new Bitmap(128, 128);
for (var y = 0; y < 128; y++)
for (var x = 0; x < 128; x++)
terrain.SetPixel(x, y, map.IsInMap(x, y)
? colors[tileset.GetWalkability(map.MapTiles[x, y])]
: shroudColor);
return terrain;
}
public void Update() public void Update()
{ {
if (world.Map.Theater != theater)
{
terrainTypeColors = null;
theater = world.Map.Theater;
}
if (terrainTypeColors == null) if (terrainTypeColors == null)
{ {
var pal = new Palette(FileSystem.Open(world.Map.Theater + ".pal"));
terrainTypeColors = new[] {
theater.ToLowerInvariant() == "snow" ? 0xe3 :0x1a,
0x63, 0x2f, 0x1f, 0x14, 0x64, 0x1f, 0x68, 0x6b, 0x6d, 0x88 }
.Select( a => Color.FromArgb(alpha, pal.GetColor(a) )).ToArray();
playerColors = Util.MakeArray<Color>( 8, b => Color.FromArgb(alpha, Chat.paletteColors[b]) ); playerColors = Util.MakeArray<Color>( 8, b => Color.FromArgb(alpha, Chat.paletteColors[b]) );
shroudColor = Color.FromArgb(alpha, Color.Black); shroudColor = Color.FromArgb(alpha, Color.Black);
} }
if (terrain == null) if (terrain == null)
{ terrain = RenderTerrainBitmap(world.Map, world.TileSet);
terrain = new Bitmap(128, 128);
for (var y = 0; y < 128; y++)
for (var x = 0; x < 128; x++)
terrain.SetPixel(x, y, world.Map.IsInMap(x, y)
? terrainTypeColors[world.TileSet.GetWalkability(world.Map.MapTiles[x, y])]
: shroudColor);
}
if (oreLayer == null) if (oreLayer == null)
{ {
var colors = terrainTypeColors[world.Map.Theater.ToLowerInvariant()];
oreLayer = new Bitmap(terrain); oreLayer = new Bitmap(terrain);
for (var y = 0; y < 128; y++) for (var y = 0; y < 128; y++)
for (var x = 0; x < 128; x++) for (var x = 0; x < 128; x++)
if (world.Map.ContainsResource(new int2(x, y))) if (world.Map.ContainsResource(new int2(x, y)))
oreLayer.SetPixel(x, y, terrainTypeColors[(int)TerrainMovementType.Ore]); oreLayer.SetPixel(x, y, colors[(int)TerrainMovementType.Ore]);
} }
mapOnlySheet.Texture.SetData(oreLayer); mapOnlySheet.Texture.SetData(oreLayer);
@@ -92,6 +106,7 @@ namespace OpenRa.Graphics
unsafe unsafe
{ {
var colors = terrainTypeColors[world.Map.Theater.ToLowerInvariant()];
int* c = (int*)bitmapData.Scan0; int* c = (int*)bitmapData.Scan0;
for (var y = 0; y < 128; y++) for (var y = 0; y < 128; y++)
@@ -100,7 +115,7 @@ namespace OpenRa.Graphics
var b = world.BuildingInfluence.GetBuildingAt(new int2(x, y)); var b = world.BuildingInfluence.GetBuildingAt(new int2(x, y));
if (b != null) if (b != null)
*(c + (y * bitmapData.Stride >> 2) + x) = *(c + (y * bitmapData.Stride >> 2) + x) =
(b.Owner != null ? playerColors[(int)b.Owner.Palette] : terrainTypeColors[4]).ToArgb(); (b.Owner != null ? playerColors[(int)b.Owner.Palette] : colors[4]).ToArgb();
} }
foreach (var a in world.Actors.Where(a => a.traits.Contains<Unit>())) foreach (var a in world.Actors.Where(a => a.traits.Contains<Unit>()))