public void DrawBefore2D(GraphicsDevice gd, WorldState state) { foreach (var avatar in Blueprint.Avatars) { if ((avatar.Position.Z+0.05f)/2.95f < state.Level) avatar.Draw(gd, state); } }
public void DrawAfter2D(GraphicsDevice gd, WorldState state) { var pxOffset = state.WorldSpace.GetScreenOffset(); var _2d = state._2D; foreach (var avatar in Blueprint.Avatars) { if ((avatar.Position.Z + 0.05f) / 2.95f < state.Level) { _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(avatar.Position)); _2d.OffsetTile(avatar.Position); avatar.Draw(gd, state); } } }
/// <summary> /// Gets an object group's thumbnail provided an array of objects. /// </summary> /// <param name="objects">The object components to draw.</param> /// <param name="gd">GraphicsDevice instance.</param> /// <param name="state">WorldState instance.</param> /// <returns>Object's ID if the object was found at the given position.</returns> public Texture2D GetObjectThumb(ObjectComponent[] objects, Vector3[] positions, GraphicsDevice gd, WorldState state) { var oldZoom = state.Zoom; var oldRotation = state.Rotation; /** Center average position **/ Vector3 average = new Vector3(); for (int i = 0; i < positions.Length; i++) { average += positions[i]; } average /= positions.Length; state.SilentZoom = WorldZoom.Near; state.SilentRotation = WorldRotation.BottomRight; state.WorldSpace.Invalidate(); state.InvalidateCamera(); state.TempDraw = true; var pxOffset = new Vector2(442, 275) - state.WorldSpace.GetScreenFromTile(average); var _2d = state._2D; Promise<Texture2D> bufferTexture = null; state._2D.OBJIDMode = false; Rectangle bounds = new Rectangle(); using (var buffer = state._2D.WithBuffer(BUFFER_THUMB, ref bufferTexture)) { while (buffer.NextPass()) { for (int i=0; i<objects.Length; i++) { var obj = objects[i]; var tilePosition = positions[i]; //we need to trick the object into believing it is in a set world state. var oldObjRot = obj.Direction; obj.Direction = Direction.NORTH; state.SilentZoom = WorldZoom.Near; state.SilentRotation = WorldRotation.BottomRight; obj.OnRotationChanged(state); obj.OnZoomChanged(state); _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); //return everything to normal obj.Direction = oldObjRot; state.SilentZoom = oldZoom; state.SilentRotation = oldRotation; obj.OnRotationChanged(state); obj.OnZoomChanged(state); } bounds = _2d.GetSpriteListBounds(); } } bounds.X = Math.Max(0, Math.Min(1023, bounds.X)); bounds.Y = Math.Max(0, Math.Min(1023, bounds.Y)); if (bounds.Width + bounds.X > 1024) bounds.Width = 1024 - bounds.X; if (bounds.Height + bounds.Y > 1024) bounds.Height = 1024 - bounds.Y; //return things to normal state.WorldSpace.Invalidate(); state.InvalidateCamera(); state.TempDraw = false; var tex = bufferTexture.Get(); return TextureUtils.Clip(gd, tex, bounds); }
/// <summary> /// Gets an object's ID given an object's screen position. /// </summary> /// <param name="x">The object's X position.</param> /// <param name="y">The object's Y position.</param> /// <param name="gd">GraphicsDevice instance.</param> /// <param name="state">WorldState instance.</param> /// <returns>Object's ID if the object was found at the given position.</returns> public short GetObjectIDAtScreenPos(int x, int y, GraphicsDevice gd, WorldState state) { /** Draw all objects to a texture as their IDs **/ var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var oldCenter = state.CenterTile; var tileOff = state.WorldSpace.GetTileFromScreen(new Vector2(x, y)); state.CenterTile += tileOff; var pxOffset = state.WorldSpace.GetScreenOffset(); var _2d = state._2D; Promise<Texture2D> bufferTexture = null; state.TempDraw = true; state._2D.OBJIDMode = true; state._3D.OBJIDMode = true; using (var buffer = state._2D.WithBuffer(BUFFER_OBJID, ref bufferTexture)) { while (buffer.NextPass()) { foreach (var tile in occupiedTiles) { /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); //TODO: Level foreach (var obj in objects.Objects) { if (obj.Level != state.Level) continue; var tilePosition = obj.Position; _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); } } } state._3D.Begin(gd); foreach (var avatar in Blueprint.Avatars) { state._3D.SetObjID((short)avatar.ObjectID); avatar.Draw(gd, state); } state._3D.End(); } } state._3D.OBJIDMode = false; state._2D.OBJIDMode = false; state.TempDraw = false; state.CenterTile = oldCenter; var tex = bufferTexture.Get(); Single[] data = new float[1]; tex.GetData<Single>(data); return (short)Math.Round(data[0]*65535f); }
/// <summary> /// Paint to screen /// </summary> /// <param name="gd"></param> /// <param name="state"></param> public void Draw(GraphicsDevice gd, WorldState state) { var _2d = state._2D; _2d.ResetMatrices(gd.Viewport.Width, gd.Viewport.Height); //todo: render to texture support /** * Draw static layers */ _2d.OffsetPixel(Vector2.Zero); if (StaticTerrain != null){ _2d.DrawBasic(StaticTerrain, Vector2.Zero); } if (StaticFloor != null){ _2d.DrawBasic(StaticFloor, Vector2.Zero); } if (StaticArch != null && StaticArchDepth != null) { _2d.DrawBasicRestoreDepth(StaticArch, StaticArchDepth, Vector2.Zero); } if (StaticObjects != null && StaticObjectsDepth != null) { _2d.DrawBasicRestoreDepth(StaticObjects, StaticObjectsDepth, Vector2.Zero); } /** * Draw dynamic objects. If an object has been static for X frames move it back into the static layer */ var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var pxOffset = state.WorldSpace.GetScreenOffset(); foreach (var tile in occupiedTiles) { /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); //TODO: Level foreach (var obj in objects.Objects) { if (obj.Level > state.Level) continue; var renderInfo = GetRenderInfo(obj); if (renderInfo.Layer == WorldObjectRenderLayer.DYNAMIC) { var tilePosition = obj.Position; _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); } } } } }
/// <summary> /// Setup anything that needs a GraphicsDevice /// </summary> /// <param name="layer"></param> public override void Initialize(_3DLayer layer) { base.Initialize(layer); /** * Setup world state, this object acts as a facade * to world objects as well as providing various * state settings for the world and helper functions */ State = new WorldState(layer.Device, layer.Device.Viewport.Width/FSOEnvironment.DPIScaleFactor, layer.Device.Viewport.Height/FSOEnvironment.DPIScaleFactor, this); State.AmbientLight = new Texture2D(layer.Device, 256, 256); State._3D = new FSO.LotView.Utils._3DWorldBatch(State); State._2D = new FSO.LotView.Utils._2DWorldBatch(layer.Device, World2D.NUM_2D_BUFFERS, World2D.BUFFER_SURFACE_FORMATS, World2D.FORMAT_ALWAYS_DEPTHSTENCIL, World2D.SCROLL_BUFFER); State._2D.AmbientLight = State.AmbientLight; PPXDepthEngine.InitGD(layer.Device); if (FSOEnvironment.SoftwareDepth) PPXDepthEngine.InitScreenTargets(layer.Device); base.Camera = State.Camera; HasInitGPU = true; HasInit = HasInitGPU & HasInitBlueprint; }
public abstract void Draw(GraphicsDevice device, WorldState world);
public void DrawAfter2D(GraphicsDevice gd, WorldState state) { //gd.RasterizerState.CullMode = CullMode.CullCounterClockwiseFace; }
public void DrawBefore2D(GraphicsDevice gd, WorldState state) { }
public virtual void OnWorldChanged(WorldState world) { }
public virtual void OnScrollChanged(WorldState world) { OnWorldChanged(world); }
public virtual void OnRotationChanged(WorldState world) { OnWorldChanged(world); }
public virtual void Initialize(GraphicsDevice device, WorldState world) { OnWorldChanged(world); }
/// <summary> /// Creates a new WorldSpace instance. /// </summary> /// <param name="worldPxWidth">Width of world in pixels.</param> /// <param name="worldPxHeight">Height of world in pixels.</param> /// <param name="state">WorldState instance.</param> public WorldSpace(float worldPxWidth, float worldPxHeight, WorldState state) { this.State = state; this.WorldPxWidth = worldPxWidth; this.WorldPxHeight = worldPxHeight; }
/// <summary> /// Prep work before screen is painted /// </summary> /// <param name="gd"></param> /// <param name="state"></param> public void PreDraw(GraphicsDevice gd, WorldState state) { var damage = Blueprint.Damage; var _2d = state._2D; /** * Tasks: * If scroll, zoom or rotation has changed, redraw all static layers * If architecture has changed, redraw appropriate static layer * If there is a new object in the static layer, redraw the static layer * If an objects in the static layer has changed, redraw the static layer and move the object to the dynamic layer * If wall visibility has changed, redraw wall layer (should think about how this works with breakthrough wall mode */ var redrawTerrain = StaticTerrain == null; var redrawStaticObjects = false; var redrawFloors = false; var redrawWalls = false; WorldObjectRenderInfo info = null; foreach (var item in damage){ switch (item.Type){ case BlueprintDamageType.ROTATE: case BlueprintDamageType.ZOOM: case BlueprintDamageType.SCROLL: case BlueprintDamageType.LEVEL_CHANGED: redrawFloors = true; redrawWalls = true; redrawStaticObjects = true; redrawTerrain = true; break; case BlueprintDamageType.OBJECT_MOVE: /** Redraw if its in static layer **/ info = GetRenderInfo(item.Component); if (info.Layer == WorldObjectRenderLayer.STATIC){ redrawStaticObjects = true; } break; case BlueprintDamageType.OBJECT_GRAPHIC_CHANGE: /** Redraw if its in static layer **/ info = GetRenderInfo(item.Component); if (info.Layer == WorldObjectRenderLayer.STATIC){ redrawStaticObjects = true; info.Layer = WorldObjectRenderLayer.DYNAMIC; } break; case BlueprintDamageType.OBJECT_RETURN_TO_STATIC: info = GetRenderInfo(item.Component); if (info.Layer == WorldObjectRenderLayer.DYNAMIC) { redrawStaticObjects = true; info.Layer = WorldObjectRenderLayer.STATIC; } break; case BlueprintDamageType.WALL_CUT_CHANGED: redrawWalls = true; break; case BlueprintDamageType.FLOOR_CHANGED: redrawFloors = true; break; case BlueprintDamageType.WALL_CHANGED: redrawWalls = true; break; } } damage.Clear(); var occupiedTiles = Blueprint.GetOccupiedTiles(state.Rotation); var pxOffset = state.WorldSpace.GetScreenOffset(); if (redrawTerrain) { Promise<Texture2D> bufferTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_STATIC_TERRAIN, ref bufferTexture)){ while (buffer.NextPass()){ Blueprint.Terrain.Draw(gd, state); } } StaticTerrain = bufferTexture.Get(); } if (redrawFloors){ Promise<Texture2D> bufferTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_STATIC_FLOOR, ref bufferTexture)) { while (buffer.NextPass()) { /* foreach (var tile in occupiedTiles) { var tilePosition = new Vector3(tile.TileX, tile.TileY, 0.0f); _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(0); if ((tile.Type & BlueprintOccupiedTileType.FLOOR) == BlueprintOccupiedTileType.FLOOR) { var floor = Blueprint.GetFloor(tile.TileX, tile.TileY, 1); //TODO: levels floor.Draw(gd, state); } } */ } } StaticFloor = bufferTexture.Get(); //StaticFloor.Save("C:\\floor.png", ImageFileFormat.Png); } if (redrawWalls) { /** Draw archetecture to a texture **/ Promise<Texture2D> bufferTexture = null; Promise<Texture2D> depthTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_ARCHETECTURE_PIXEL, ref bufferTexture, BUFFER_ARCHETECTURE_DEPTH, ref depthTexture)) { while (buffer.NextPass()) { Blueprint.FloorComp.Draw(gd, state); Blueprint.WallComp.Draw(gd, state); } } StaticArch = bufferTexture.Get(); StaticArchDepth = depthTexture.Get(); } if (redrawStaticObjects){ /** Draw static objects to a texture **/ Promise<Texture2D> bufferTexture = null; Promise<Texture2D> depthTexture = null; using (var buffer = state._2D.WithBuffer(BUFFER_STATIC_OBJECTS_PIXEL, ref bufferTexture, BUFFER_STATIC_OBJECTS_DEPTH, ref depthTexture)) { while (buffer.NextPass()) { foreach (var tile in occupiedTiles) { /** Objects **/ if ((tile.Type & BlueprintOccupiedTileType.OBJECT) == BlueprintOccupiedTileType.OBJECT) { var objects = Blueprint.GetObjects(tile.TileX, tile.TileY); //TODO: Level foreach (var obj in objects.Objects) { if (obj.Level > state.Level) continue; var renderInfo = GetRenderInfo(obj); if (renderInfo.Layer == WorldObjectRenderLayer.STATIC) { var tilePosition = obj.Position; _2d.OffsetPixel(state.WorldSpace.GetScreenFromTile(tilePosition) + pxOffset); _2d.OffsetTile(tilePosition); _2d.SetObjID(obj.ObjectID); obj.Draw(gd, state); } } } } } } StaticObjects = bufferTexture.Get(); StaticObjectsDepth = depthTexture.Get(); //StaticObjects.Save("C:\\static.png", ImageFileFormat.Png); } }
public virtual void OnZoomChanged(WorldState world) { OnWorldChanged(world); }
public virtual void Update(GraphicsDevice device, WorldState world) { }
public void PreDraw(GraphicsDevice gd, WorldState state) { }
/// <summary> /// Setup anything that needs a GraphicsDevice /// </summary> /// <param name="layer"></param> public override void Initialize(_3DLayer layer) { base.Initialize(layer); /** * Setup world state, this object acts as a facade * to world objects as well as providing various * state settings for the world and helper functions */ State = new WorldState(layer.Device, layer.Device.Viewport.Width, layer.Device.Viewport.Height, this); State._3D = new FSO.LotView.Utils._3DWorldBatch(State); State._2D = new FSO.LotView.Utils._2DWorldBatch(layer.Device, World2D.NUM_2D_BUFFERS, World2D.BUFFER_SURFACE_FORMATS); base.Camera = State.Camera; HasInitGPU = true; HasInit = HasInitGPU & HasInitBlueprint; _2DWorld.Initialize(layer); }