#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using xTile.Dimensions;
using xTile.Display;
using xTile.Layers;
using xTile.Tiles;
using Rectangle = xTile.Dimensions.Rectangle;
namespace StardewModdingAPI.Framework.Rendering
{
/// A map display device which reimplements the default logic.
/// This is an exact copy of , except that private fields are protected and all methods are virtual.
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Field naming deliberately matches " + nameof(XnaDisplayDevice) + " to minimize differences.")]
internal class SXnaDisplayDevice : IDisplayDevice
{
/*********
** Fields
*********/
protected readonly ContentManager m_contentManager;
protected readonly GraphicsDevice m_graphicsDevice;
protected SpriteBatch m_spriteBatchAlpha;
protected SpriteBatch m_spriteBatchAdditive;
protected readonly Dictionary m_tileSheetTextures;
protected Vector2 m_tilePosition;
protected Microsoft.Xna.Framework.Rectangle m_sourceRectangle;
protected readonly Color m_modulationColour;
/*********
** Public methods
*********/
/// Construct an instance.
/// The content manager through which to load tiles.
/// The graphics device with which to render tiles.
public SXnaDisplayDevice(ContentManager contentManager, GraphicsDevice graphicsDevice)
{
this.m_contentManager = contentManager;
this.m_graphicsDevice = graphicsDevice;
this.m_spriteBatchAlpha = new SpriteBatch(graphicsDevice);
this.m_spriteBatchAdditive = new SpriteBatch(graphicsDevice);
this.m_tileSheetTextures = new Dictionary();
this.m_tilePosition = new Vector2();
this.m_sourceRectangle = new Microsoft.Xna.Framework.Rectangle();
this.m_modulationColour = Color.White;
}
/// Load a tilesheet texture.
/// The tilesheet instance.
public virtual void LoadTileSheet(TileSheet tileSheet)
{
Texture2D texture2D = this.m_contentManager.Load(tileSheet.ImageSource);
this.m_tileSheetTextures[tileSheet] = texture2D;
}
/// Unload a tilesheet texture.
/// The tilesheet instance.
public virtual void DisposeTileSheet(TileSheet tileSheet)
{
this.m_tileSheetTextures.Remove(tileSheet);
}
/// Prepare to render to the screen.
/// The sprite batch being rendered.
public virtual void BeginScene(SpriteBatch b)
{
this.m_spriteBatchAlpha = b;
}
/// Set the clipping region.
/// The clipping region.
public virtual void SetClippingRegion(Rectangle clippingRegion)
{
int backBufferWidth = this.m_graphicsDevice.PresentationParameters.BackBufferWidth;
int backBufferHeight = this.m_graphicsDevice.PresentationParameters.BackBufferHeight;
int x = this.Clamp(clippingRegion.X, 0, backBufferWidth);
int y = this.Clamp(clippingRegion.Y, 0, backBufferHeight);
int num1 = this.Clamp(clippingRegion.X + clippingRegion.Width, 0, backBufferWidth);
int num2 = this.Clamp(clippingRegion.Y + clippingRegion.Height, 0, backBufferHeight);
int width = num1 - x;
int height = num2 - y;
this.m_graphicsDevice.Viewport = new Viewport(x, y, width, height);
}
/// Draw a tile to the screen.
/// The tile to draw.
/// The tile position to draw.
/// The layer depth at which to draw.
public virtual void DrawTile(Tile tile, Location location, float layerDepth)
{
if (tile == null)
return;
Rectangle tileImageBounds = tile.TileSheet.GetTileImageBounds(tile.TileIndex);
Texture2D tileSheetTexture = this.m_tileSheetTextures[tile.TileSheet];
if (tileSheetTexture.IsDisposed)
return;
this.m_tilePosition.X = location.X;
this.m_tilePosition.Y = location.Y;
this.m_sourceRectangle.X = tileImageBounds.X;
this.m_sourceRectangle.Y = tileImageBounds.Y;
this.m_sourceRectangle.Width = tileImageBounds.Width;
this.m_sourceRectangle.Height = tileImageBounds.Height;
this.m_spriteBatchAlpha.Draw(tileSheetTexture, this.m_tilePosition, this.m_sourceRectangle, this.m_modulationColour, 0.0f, Vector2.Zero, Layer.zoom, SpriteEffects.None, layerDepth);
}
/// Finish drawing to the screen.
public virtual void EndScene() { }
/// Snap a value to the given range.
/// The value to normalize.
/// The minimum value.
/// The maximum value.
protected int Clamp(int nValue, int nMin, int nMax)
{
return Math.Min(Math.Max(nValue, nMin), nMax);
}
}
}