untangling WorldRenderer from World

This commit is contained in:
Bob
2010-10-12 01:11:56 +13:00
parent 1c1483377c
commit 597dba8584
17 changed files with 162 additions and 124 deletions

View File

@@ -6,19 +6,19 @@
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Network;
using OpenRA.Server;
using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Network;
using OpenRA.Server;
using OpenRA.Support;
using OpenRA.Widgets;
@@ -32,6 +32,8 @@ namespace OpenRA
public static ModData modData;
public static World world;
public static WorldRenderer worldRenderer;
public static Viewport viewport;
public static Settings Settings;
@@ -48,6 +50,7 @@ namespace OpenRA
viewport = new Viewport(new float2(Renderer.Resolution), map.TopLeft, map.BottomRight, Renderer);
world = new World(modData.Manifest, map, orderManager);
worldRenderer = new WorldRenderer(world);
}
public static void MoveViewport(int2 loc)
@@ -92,18 +95,18 @@ namespace OpenRA
internal static int RenderFrame = 0;
internal static int LocalTick = 0;
const int NetTickScale = 3; // 120ms net tick for 40ms local tick
internal static event Action<OrderManager> ConnectionStateChanged = _ => { };
static ConnectionState lastConnectionState = ConnectionState.PreConnecting;
internal static event Action<OrderManager> ConnectionStateChanged = _ => { };
static ConnectionState lastConnectionState = ConnectionState.PreConnecting;
public static int LocalClientId { get { return orderManager.Connection.LocalClientId; } }
static void Tick( World world, OrderManager orderManager, Viewport viewPort )
{
if (orderManager.Connection.ConnectionState != lastConnectionState)
{
lastConnectionState = orderManager.Connection.ConnectionState;
ConnectionStateChanged( orderManager );
}
if (orderManager.Connection.ConnectionState != lastConnectionState)
{
lastConnectionState = orderManager.Connection.ConnectionState;
ConnectionStateChanged( orderManager );
}
int t = Environment.TickCount;
int dt = t - lastTime;
@@ -131,6 +134,7 @@ namespace OpenRA
world.OrderGenerator.Tick(world);
world.Selection.Tick(world);
world.Tick();
worldRenderer.Tick();
PerfHistory.Tick();
}
@@ -143,7 +147,7 @@ namespace OpenRA
using (new PerfSample("render"))
{
++RenderFrame;
viewport.DrawRegions(world.WorldRenderer, world);
viewport.DrawRegions(worldRenderer, world);
Sound.SetListenerPosition(viewport.Location + .5f * new float2(viewport.Width, viewport.Height));
}
@@ -174,9 +178,9 @@ namespace OpenRA
if (orderManager.GameStarted) return;
Widget.SelectedWidget = null;
LocalTick = 0;
LocalTick = 0;
orderManager.StartGame();
orderManager.StartGame();
viewport.RefreshPalette();
AfterGameStart( world );
}
@@ -221,24 +225,24 @@ namespace OpenRA
internal static void Initialize(Arguments args)
{
AppDomain.CurrentDomain.AssemblyResolve += FileSystem.ResolveAssembly;
var defaultSupport = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
+ Path.DirectorySeparatorChar + "OpenRA";
SupportDir = args.GetValue("SupportDir", defaultSupport);
Settings = new Settings(SupportDir + "settings.yaml", args);
AppDomain.CurrentDomain.AssemblyResolve += FileSystem.ResolveAssembly;
var defaultSupport = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
+ Path.DirectorySeparatorChar + "OpenRA";
SupportDir = args.GetValue("SupportDir", defaultSupport);
Settings = new Settings(SupportDir + "settings.yaml", args);
// force master server upgrade -- remove once everyone is switched over.
if (Settings.Server.MasterServer == "http://open-ra.org/master/")
Settings.Server.MasterServer = "http://master.open-ra.org/";
Settings.Save();
Log.LogPath = SupportDir + "Logs" + Path.DirectorySeparatorChar;
Log.AddChannel("perf", "perf.log");
Log.AddChannel("debug", "debug.log");
Log.AddChannel("sync", "syncreport.log");
Settings.Save();
Log.LogPath = SupportDir + "Logs" + Path.DirectorySeparatorChar;
Log.AddChannel("perf", "perf.log");
Log.AddChannel("debug", "debug.log");
Log.AddChannel("sync", "syncreport.log");
FileSystem.Mount("."); // Needed to access shaders
Renderer.Initialize( Game.Settings.Graphics.Mode );
@@ -259,13 +263,14 @@ namespace OpenRA
PerfHistory.items["render"].hasNormalTick = false;
PerfHistory.items["batches"].hasNormalTick = false;
PerfHistory.items["text"].hasNormalTick = false;
PerfHistory.items["cursor"].hasNormalTick = false;
PerfHistory.items["cursor"].hasNormalTick = false;
JoinLocal();
StartGame(modData.Manifest.ShellmapUid);
Game.AfterGameStart += world => Widget.OpenWindow("INGAME_ROOT", new Dictionary<string,object>{{"world", world}});
Game.AfterGameStart += world => Widget.OpenWindow("INGAME_ROOT", new Dictionary<string,object>{{"world", world}});
Game.ConnectionStateChanged += orderManager =>
{
Widget.CloseWindow();
@@ -309,13 +314,13 @@ namespace OpenRA
}
}
public static void Exit() { quit = true; }
public static void Exit() { quit = true; }
public static Action<Color,string,string> AddChatLine = (c,n,s) => {};
public static void Debug(string s, params object[] args)
{
public static void Debug(string s, params object[] args)
{
AddChatLine(Color.White, "Debug", String.Format(s,args));
}
public static void Disconnect()
@@ -327,25 +332,25 @@ namespace OpenRA
Widget.CloseWindow();
Widget.OpenWindow("MAINMENU_BG");
Widget.OpenWindow("MAINMENU_BG");
}
static string baseSupportDir = null;
public static string SupportDir
{
set
{
var dir = value;
// Expand paths relative to the personal directory
if (dir.ElementAt(0) == '~')
dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + dir.Substring(1);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
}
static string baseSupportDir = null;
public static string SupportDir
{
set
{
var dir = value;
// Expand paths relative to the personal directory
if (dir.ElementAt(0) == '~')
dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + dir.Substring(1);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
baseSupportDir = dir + Path.DirectorySeparatorChar;
}
}
get { return baseSupportDir; }
}
public static T CreateObject<T>( string name )
@@ -353,4 +358,4 @@ namespace OpenRA
return modData.ObjectCreator.CreateObject<T>( name );
}
}
}
}

View File

@@ -111,7 +111,7 @@ namespace OpenRA.Graphics
public void RefreshPalette()
{
Game.world.WorldRenderer.palette.Update(
Game.worldRenderer.palette.Update(
Game.world.WorldActor.TraitsImplementing<IPaletteModifier>());
}

View File

@@ -31,6 +31,9 @@ namespace OpenRA.Graphics
terrainRenderer = new TerrainRenderer(world, this);
uiOverlay = new UiOverlay();
palette = new HardwarePalette(world.Map);
foreach( var pal in world.traitDict.ActorsWithTraitMultiple<IPalette>( world ) )
pal.Trait.InitPalette( this );
}
public int GetPaletteIndex(string name) { return palette.GetPaletteIndex(name); }

View File

@@ -63,7 +63,6 @@ namespace OpenRA
PlayerRef = pr;
RegisterPlayerColor(world, Palette);
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) });
}
@@ -85,18 +84,9 @@ namespace OpenRA
ClientIndex = client.Index;
PlayerRef = pr;
RegisterPlayerColor(world, Palette);
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) });
}
public void RegisterPlayerColor(World world, string palette)
{
var info = Rules.Info["world"].Traits.Get<PlayerColorPaletteInfo>();
var newpal = new Palette(world.WorldRenderer.GetPalette(info.BasePalette),
new PlayerColorRemap(Color, Color2, info.PaletteFormat));
world.WorldRenderer.AddPalette(palette, newpal);
}
public void GiveAdvice(string advice)
{
Sound.PlayToPlayer(this, advice);

View File

@@ -105,6 +105,7 @@ namespace OpenRA.Traits
public interface IDamageModifier { float GetDamageModifier( WarheadInfo warhead ); }
public interface ISpeedModifier { float GetSpeedModifier(); }
public interface IFirepowerModifier { float GetFirepowerModifier(); }
public interface IPalette { void InitPalette( WorldRenderer wr ); }
public interface IPaletteModifier { void AdjustPalette(Dictionary<string,Palette> b); }
public interface IPips { IEnumerable<PipType> GetPips(Actor self); }
public interface ITags { IEnumerable<TagType> GetTags(); }

View File

@@ -9,13 +9,34 @@
#endregion
using OpenRA.FileFormats;
using OpenRA.Graphics;
namespace OpenRA.Traits
{
public class PlayerColorPaletteInfo : TraitInfo<PlayerColorPalette>
public class PlayerColorPaletteInfo : ITraitInfo
{
public readonly string BasePalette = null;
public readonly string BaseName = "player";
public readonly PaletteFormat PaletteFormat = PaletteFormat.ra;
public object Create( ActorInitializer init ) { return new PlayerColorPalette( init.self.Owner, this ); }
}
public class PlayerColorPalette {}
public class PlayerColorPalette : IPalette
{
readonly Player owner;
readonly PlayerColorPaletteInfo info;
public PlayerColorPalette( Player owner, PlayerColorPaletteInfo info )
{
this.owner = owner;
this.info = info;
}
public void InitPalette( WorldRenderer wr )
{
var paletteName = "{0}{1}".F( info.BaseName, owner.Index );
var newpal = new Palette(wr.GetPalette(info.BasePalette),
new PlayerColorRemap(owner.Color, owner.Color2, info.PaletteFormat));
wr.AddPalette(paletteName, newpal);
}
}
}

View File

@@ -61,7 +61,7 @@ namespace OpenRA.Widgets
cachedFrame = frame;
}
Game.Renderer.WorldSpriteRenderer.DrawSprite(sprite,RenderOrigin, Game.world.WorldRenderer, palette);
Game.Renderer.WorldSpriteRenderer.DrawSprite(sprite,RenderOrigin, Game.worldRenderer, palette);
}
}
}

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Widgets
public static void DrawSHP(Sprite s, float2 pos)
{
Game.Renderer.WorldSpriteRenderer.DrawSprite(s,pos, Game.world.WorldRenderer, "chrome");
Game.Renderer.WorldSpriteRenderer.DrawSprite(s,pos, Game.worldRenderer, "chrome");
}
public static void DrawPanel(string collection, Rectangle Bounds)

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Widgets
Game.Renderer.LineRenderer.DrawLine(a, a + c, Color.White, Color.White);
foreach (var u in SelectActorsInBox(world, selbox.Value.First, selbox.Value.Second))
world.WorldRenderer.DrawSelectionBox(u, Color.Yellow);
Game.worldRenderer.DrawSelectionBox(u, Color.Yellow);
}
float2 dragStart, dragEnd;

View File

@@ -57,8 +57,6 @@ namespace OpenRA
public readonly Map Map;
public readonly TileSet TileSet;
public readonly WorldRenderer WorldRenderer;
IOrderGenerator orderGenerator_;
public IOrderGenerator OrderGenerator
{
@@ -97,8 +95,6 @@ namespace OpenRA
TileSet = Rules.TileSets[Map.Tileset];
TileSet.LoadTiles();
WorldRenderer = new WorldRenderer(this);
WorldActor = CreateActor( "World", new TypeDictionary() );
Queries = new AllQueries(this);
@@ -170,8 +166,6 @@ namespace OpenRA
Game.viewport.Tick();
while (frameEndActions.Count != 0)
frameEndActions.Dequeue()(this);
WorldRenderer.Tick();
}
public IEnumerable<Actor> Actors { get { return actors; } }