UltimateZoneBuilder/Source/Rendering/Renderer2D.cs

1581 lines
53 KiB
C#
Raw Normal View History

2007-06-15 18:50:18 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Drawing;
using System.ComponentModel;
2007-06-15 22:38:42 +00:00
using CodeImp.DoomBuilder.Map;
using SlimDX.Direct3D9;
using SlimDX;
using CodeImp.DoomBuilder.Geometry;
using System.Drawing.Imaging;
2007-10-21 04:07:36 +00:00
using CodeImp.DoomBuilder.Data;
2007-11-10 19:24:52 +00:00
using CodeImp.DoomBuilder.Editing;
2007-06-15 18:50:18 +00:00
#endregion
namespace CodeImp.DoomBuilder.Rendering
{
/* This renders a 2D presentation of the map. This is done in several
* layers which each are optimized for a different purpose. Set the
* PresentationLayer(s) to specify how to present these layers.
*/
2008-01-02 21:49:43 +00:00
internal unsafe sealed class Renderer2D : Renderer, IRenderer2D
2007-06-15 18:50:18 +00:00
{
#region ================== Constants
private const float FSAA_FACTOR = 0.6f;
private const float THING_ARROW_SIZE = 1.5f;
private const float THING_ARROW_SHRINK = 2f;
private const float THING_CIRCLE_SIZE = 1f;
private const float THING_CIRCLE_SHRINK = 0f;
2007-10-21 04:07:36 +00:00
private const int THING_BUFFER_STEP = 100;
2007-11-10 19:24:52 +00:00
private const float THINGS_BACK_ALPHA = 0.3f;
2007-10-20 12:34:27 +00:00
2008-04-17 05:36:48 +00:00
private const string FONT_NAME = "Verdana";
private const int FONT_WIDTH = 0;
private const int FONT_HEIGHT = 0;
private const int THING_SHINY = 1;
private const int THING_SQUARE = 2;
private const int NUM_THING_TEXTURES = 4;
internal const int NUM_VIEW_MODES = 4;
2008-04-17 05:36:48 +00:00
2007-06-15 18:50:18 +00:00
#endregion
#region ================== Variables
// Rendertargets
2007-11-11 13:51:01 +00:00
private Texture backtex;
private Texture plottertex;
private Texture thingstex;
private Texture overlaytex;
private Texture surfacetex;
// Locking data
private DataRectangle plotlocked;
private Surface targetsurface;
// Rendertarget sizes
private Size windowsize;
private Size structsize;
private Size thingssize;
private Size overlaysize;
2007-11-11 13:51:01 +00:00
private Size backsize;
2008-04-17 05:36:48 +00:00
// Font
private SlimDX.Direct3D9.Font font;
// Geometry plotter
private Plotter plotter;
// Vertices to present the textures
2007-11-11 13:51:01 +00:00
private VertexBuffer screenverts;
private FlatVertex[] backimageverts;
2007-10-21 04:07:36 +00:00
// Batch buffer for things rendering
2007-10-21 04:07:36 +00:00
private VertexBuffer thingsvertices;
private int maxthings, numthings;
2007-10-21 04:07:36 +00:00
// Render settings
private bool thingsfront;
private int vertexsize;
private RenderLayers renderlayer = RenderLayers.None;
2007-10-21 04:07:36 +00:00
// Images
private ResourceImage whitetexture;
private ResourceImage[] thingtexture;
2007-10-21 04:07:36 +00:00
2007-06-24 18:56:43 +00:00
// View settings (world coordinates)
private ViewMode viewmode;
2007-06-24 18:56:43 +00:00
private float scale;
2007-10-20 19:50:03 +00:00
private float scaleinv;
2007-06-24 18:56:43 +00:00
private float offsetx;
private float offsety;
2007-10-20 19:50:03 +00:00
private float translatex;
private float translatey;
private float linenormalsize;
2007-11-11 14:42:04 +00:00
private float lastgridscale = -1f;
2008-06-10 08:35:14 +00:00
private int lastgridsize;
2007-11-11 14:42:04 +00:00
private float lastgridx;
private float lastgridy;
// Presentation
private Presentation present;
2007-10-20 19:50:03 +00:00
2007-06-15 18:50:18 +00:00
#endregion
#region ================== Properties
2007-06-24 18:56:43 +00:00
public float OffsetX { get { return offsetx; } }
public float OffsetY { get { return offsety; } }
public float TranslateX { get { return translatex; } }
public float TranslateY { get { return translatey; } }
2007-06-24 18:56:43 +00:00
public float Scale { get { return scale; } }
public int VertexSize { get { return vertexsize; } }
public ViewMode ViewMode { get { return viewmode; } }
2007-06-15 18:50:18 +00:00
#endregion
#region ================== Constructor / Disposer
2007-06-15 18:50:18 +00:00
// Constructor
2008-01-02 21:49:43 +00:00
internal Renderer2D(D3DDevice graphics) : base(graphics)
2007-06-15 18:50:18 +00:00
{
// Load thing textures
thingtexture = new ResourceImage[NUM_THING_TEXTURES];
for(int i = 0; i < NUM_THING_TEXTURES; i++)
{
thingtexture[i] = new ResourceImage("Thing2D_" + i.ToString(CultureInfo.InvariantCulture) + ".png");
thingtexture[i].UseColorCorrection = false;
thingtexture[i].LoadImage();
thingtexture[i].CreateTexture();
}
// Load white texture
whitetexture = new ResourceImage("White.png");
whitetexture.UseColorCorrection = false;
whitetexture.LoadImage();
whitetexture.CreateTexture();
2007-10-21 04:07:36 +00:00
// Create rendertargets
CreateRendertargets();
2007-06-15 18:50:18 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
2008-01-02 21:49:43 +00:00
// Disposer
internal override void Dispose()
2007-06-15 18:50:18 +00:00
{
// Not already disposed?
if(!isdisposed)
{
// Destroy rendertargets
DestroyRendertargets();
foreach(ResourceImage i in thingtexture) i.Dispose();
whitetexture.Dispose();
2007-06-15 22:38:42 +00:00
2007-06-15 18:50:18 +00:00
// Done
2007-09-24 19:54:47 +00:00
base.Dispose();
2007-06-15 18:50:18 +00:00
}
}
#endregion
2007-11-10 19:24:52 +00:00
#region ================== Presenting
// This sets the presentation to use
public void SetPresentation(Presentation present)
{
this.present = new Presentation(present);
}
// This draws the image on screen
public void Present()
{
// Start drawing
if(graphics.StartRendering(true, General.Colors.Background.ToColorValue(), graphics.BackBuffer, graphics.DepthBuffer))
{
// Renderstates that count for this whole sequence
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
graphics.Device.SetStreamSource(0, screenverts, 0, sizeof(FlatVertex));
2008-05-18 11:43:28 +00:00
graphics.Device.SetTransform(TransformState.World, Matrix.Identity);
2007-11-11 14:42:04 +00:00
graphics.Shaders.Display2D.Begin();
// Go for all layers
foreach(PresentLayer layer in present.layers)
2007-11-10 19:24:52 +00:00
{
int aapass;
// Set blending mode
switch(layer.blending)
{
case BlendingMode.None:
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
break;
case BlendingMode.Mask:
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
break;
case BlendingMode.Alpha:
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
graphics.Device.SetRenderState(RenderState.TextureFactor, (new Color4(layer.alpha, 1f, 1f, 1f)).ToArgb());
break;
case BlendingMode.Additive:
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.One);
graphics.Device.SetRenderState(RenderState.TextureFactor, (new Color4(layer.alpha, 1f, 1f, 1f)).ToArgb());
break;
}
// Check which pass to use
if(layer.antialiasing) aapass = 0; else aapass = 1;
// Render layer
switch(layer.layer)
{
// BACKGROUND
case RendererLayer.Background:
if((backimageverts == null) || (General.Map.Grid.Background.Texture == null)) break;
graphics.Device.SetTexture(0, General.Map.Grid.Background.Texture);
graphics.Shaders.Display2D.Texture1 = General.Map.Grid.Background.Texture;
graphics.Shaders.Display2D.SetSettings(1f / windowsize.Width, 1f / windowsize.Height, FSAA_FACTOR, layer.alpha, false);
graphics.Shaders.Display2D.BeginPass(aapass);
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, backimageverts);
graphics.Shaders.Display2D.EndPass();
graphics.Device.SetStreamSource(0, screenverts, 0, sizeof(FlatVertex));
break;
// GRID
case RendererLayer.Grid:
graphics.Device.SetTexture(0, backtex);
graphics.Shaders.Display2D.Texture1 = backtex;
graphics.Shaders.Display2D.SetSettings(1f / backsize.Width, 1f / backsize.Height, FSAA_FACTOR, layer.alpha, false);
graphics.Shaders.Display2D.BeginPass(aapass);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass();
break;
// GEOMETRY
case RendererLayer.Geometry:
graphics.Device.SetTexture(0, plottertex);
graphics.Shaders.Display2D.Texture1 = plottertex;
graphics.Shaders.Display2D.SetSettings(1f / structsize.Width, 1f / structsize.Height, FSAA_FACTOR, layer.alpha, false);
graphics.Shaders.Display2D.BeginPass(aapass);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass();
break;
// THINGS
case RendererLayer.Things:
graphics.Device.SetTexture(0, thingstex);
graphics.Shaders.Display2D.Texture1 = thingstex;
graphics.Shaders.Display2D.SetSettings(1f / thingssize.Width, 1f / thingssize.Height, FSAA_FACTOR, layer.alpha, false);
graphics.Shaders.Display2D.BeginPass(aapass);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass();
break;
// OVERLAY
case RendererLayer.Overlay:
graphics.Device.SetTexture(0, overlaytex);
graphics.Shaders.Display2D.Texture1 = overlaytex;
graphics.Shaders.Display2D.SetSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false);
graphics.Shaders.Display2D.BeginPass(aapass);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass();
break;
// SURFACE
case RendererLayer.Surface:
graphics.Device.SetTexture(0, surfacetex);
graphics.Shaders.Display2D.Texture1 = surfacetex;
graphics.Shaders.Display2D.SetSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false);
graphics.Shaders.Display2D.BeginPass(aapass);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass();
break;
}
2007-11-10 19:24:52 +00:00
}
2007-11-11 13:51:01 +00:00
// Done
2007-11-11 14:42:04 +00:00
graphics.Shaders.Display2D.End();
graphics.FinishRendering();
graphics.Present();
// Release binds
graphics.Device.SetTexture(0, null);
graphics.Shaders.Display2D.Texture1 = null;
2008-05-18 11:43:28 +00:00
graphics.Device.SetStreamSource(0, null, 0, 0);
}
else
{
// Request delayed redraw
General.MainWindow.DelayedRedraw();
}
}
#endregion
#region ================== Management
// This is called before a device is reset
// (when resized or display adapter was changed)
public override void UnloadResource()
{
// Destroy rendertargets
DestroyRendertargets();
}
// This is called resets when the device is reset
// (when resized or display adapter was changed)
public override void ReloadResource()
2007-06-24 18:56:43 +00:00
{
// Re-create rendertargets
CreateRendertargets();
}
2007-09-24 19:54:47 +00:00
// This resets the graphics
public override void Reset()
{
UnloadResource();
ReloadResource();
2007-06-24 18:56:43 +00:00
}
// This destroys the rendertargets
2008-01-02 21:49:43 +00:00
public void DestroyRendertargets()
{
// Trash rendertargets
if(plottertex != null) plottertex.Dispose();
if(thingstex != null) thingstex.Dispose();
if(overlaytex != null) overlaytex.Dispose();
if(surfacetex != null) surfacetex.Dispose();
2007-11-11 13:51:01 +00:00
if(backtex != null) backtex.Dispose();
if(screenverts != null) screenverts.Dispose();
plottertex = null;
thingstex = null;
2007-11-11 13:51:01 +00:00
backtex = null;
screenverts = null;
overlaytex = null;
surfacetex = null;
// Trash things batch buffer
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
numthings = 0;
maxthings = 0;
2007-11-11 14:42:04 +00:00
lastgridscale = -1f;
2008-06-10 08:35:14 +00:00
lastgridsize = 0;
2008-04-17 05:36:48 +00:00
// Trash font
if(font != null) font.Dispose();
font = null;
}
// Allocates new image memory to render on
public void CreateRendertargets()
2007-06-15 22:38:42 +00:00
{
SurfaceDescription sd;
2007-11-11 13:51:01 +00:00
DataStream stream;
FlatVertex[] verts;
// Destroy rendertargets
DestroyRendertargets();
// Get new width and height
windowsize.Width = graphics.RenderTarget.ClientSize.Width;
windowsize.Height = graphics.RenderTarget.ClientSize.Height;
// Create rendertargets textures
plottertex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
thingstex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
2007-11-11 13:51:01 +00:00
backtex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
overlaytex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
surfacetex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
2007-11-11 13:51:01 +00:00
// Get the real surface sizes
sd = plottertex.GetLevelDescription(0);
structsize.Width = sd.Width;
structsize.Height = sd.Height;
sd = thingstex.GetLevelDescription(0);
thingssize.Width = sd.Width;
thingssize.Height = sd.Height;
2007-11-11 13:51:01 +00:00
sd = backtex.GetLevelDescription(0);
backsize.Width = sd.Width;
backsize.Height = sd.Height;
sd = overlaytex.GetLevelDescription(0);
overlaysize.Width = sd.Width;
overlaysize.Height = sd.Height;
// Clear rendertargets
StartPlotter(true); Finish();
StartThings(true); Finish();
StartOverlay(true); Finish();
2008-04-17 05:36:48 +00:00
// Create font
font = new SlimDX.Direct3D9.Font(graphics.Device, FONT_WIDTH, FONT_HEIGHT, FontWeight.Bold, 1, false, CharacterSet.Ansi, Precision.Default, FontQuality.Antialiased, PitchAndFamily.Default, FONT_NAME);
2008-04-17 05:36:48 +00:00
2007-11-11 13:51:01 +00:00
// Create vertex buffers
screenverts = new VertexBuffer(graphics.Device, 4 * sizeof(FlatVertex), Usage.Dynamic | Usage.WriteOnly, VertexFormat.None, Pool.Default);
// Make screen vertices
stream = screenverts.Lock(0, 4 * sizeof(FlatVertex), LockFlags.Discard | LockFlags.NoSystemLock);
verts = CreateScreenVerts(structsize);
stream.WriteRange<FlatVertex>(verts);
screenverts.Unlock();
stream.Dispose();
}
// This makes screen vertices for display
private FlatVertex[] CreateScreenVerts(Size texturesize)
{
FlatVertex[] screenverts = new FlatVertex[4];
2007-10-21 04:07:36 +00:00
screenverts[0].x = 0.5f;
screenverts[0].y = 0.5f;
screenverts[0].c = -1;
screenverts[0].u = 1f / texturesize.Width;
screenverts[0].v = 1f / texturesize.Height;
screenverts[1].x = texturesize.Width - 1.5f;
2007-10-21 04:07:36 +00:00
screenverts[1].y = 0.5f;
screenverts[1].c = -1;
screenverts[1].u = 1f - 1f / texturesize.Width;
screenverts[1].v = 1f / texturesize.Height;
2007-10-21 04:07:36 +00:00
screenverts[2].x = 0.5f;
screenverts[2].y = texturesize.Height - 1.5f;
screenverts[2].c = -1;
screenverts[2].u = 1f / texturesize.Width;
screenverts[2].v = 1f - 1f / texturesize.Height;
screenverts[3].x = texturesize.Width - 1.5f;
screenverts[3].y = texturesize.Height - 1.5f;
screenverts[3].c = -1;
screenverts[3].u = 1f - 1f / texturesize.Width;
screenverts[3].v = 1f - 1f / texturesize.Height;
return screenverts;
}
2007-06-24 18:56:43 +00:00
#endregion
2007-06-15 22:38:42 +00:00
#region ================== View
// This changes view mode
public void SetViewMode(ViewMode mode)
{
viewmode = mode;
}
2007-06-24 18:56:43 +00:00
// This changes view position
public void PositionView(float x, float y)
{
// Change position in world coordinates
offsetx = x;
offsety = y;
2007-10-20 19:50:03 +00:00
UpdateTransformations();
2007-06-24 18:56:43 +00:00
}
// This changes zoom
public void ScaleView(float scale)
{
// Change zoom scale
this.scale = scale;
2007-10-20 19:50:03 +00:00
UpdateTransformations();
2007-09-24 19:54:47 +00:00
// Show zoom on main window
General.MainWindow.UpdateZoom(scale);
2007-06-24 18:56:43 +00:00
}
2007-10-20 19:50:03 +00:00
// This updates some maths
private void UpdateTransformations()
{
scaleinv = 1f / scale;
translatex = -offsetx + (windowsize.Width * 0.5f) * scaleinv;
translatey = -offsety - (windowsize.Height * 0.5f) * scaleinv;
2007-10-20 19:50:03 +00:00
linenormalsize = 10f * scaleinv;
vertexsize = (int)(1.7f * scale + 0.5f);
if(vertexsize < 0) vertexsize = 0;
if(vertexsize > 4) vertexsize = 4;
2008-05-18 11:43:28 +00:00
Matrix scaling = Matrix.Scaling((1f / (float)windowsize.Width) * 2f, (1f / (float)windowsize.Height) * -2f, 1f);
Matrix translate = Matrix.Translation(-(float)windowsize.Width * 0.5f, -(float)windowsize.Height * 0.5f, 0f);
graphics.Device.SetTransform(TransformState.View, Matrix.Multiply(translate, scaling));
graphics.Device.SetTransform(TransformState.Projection, Matrix.Identity);
2008-05-18 11:43:28 +00:00
}
// This sets the world matrix for transformation
private void SetWorldTransformation(bool transform)
{
if(transform)
{
Matrix translate = Matrix.Translation(translatex, translatey, 0f);
Matrix scaling = Matrix.Scaling(scale, -scale, 1f);
graphics.Device.SetTransform(TransformState.World, Matrix.Multiply(translate, scaling));
}
else
{
graphics.Device.SetTransform(TransformState.World, Matrix.Identity);
}
2007-10-20 19:50:03 +00:00
}
// This unprojects mouse coordinates into map coordinates
public Vector2D GetMapCoordinates(Vector2D mousepos)
2007-06-15 22:38:42 +00:00
{
2007-10-20 19:50:03 +00:00
return mousepos.GetInvTransformed(-translatex, -translatey, scaleinv, -scaleinv);
}
#endregion
#region ================== Colors
2007-10-21 04:07:36 +00:00
// This returns the color for a thing
public PixelColor DetermineThingColor(Thing t)
{
// Determine color
2007-12-01 18:29:58 +00:00
if(t.Selected) return General.Colors.Selection;
else return t.Color;
2007-10-21 04:07:36 +00:00
}
2007-10-20 19:50:03 +00:00
// This returns the color for a vertex
public int DetermineVertexColor(Vertex v)
2007-10-20 19:50:03 +00:00
{
// Determine color
2007-12-01 18:29:58 +00:00
if(v.Selected) return ColorCollection.SELECTION;
else return ColorCollection.VERTICES;
2007-10-20 19:50:03 +00:00
}
// This returns the color for a linedef
public PixelColor DetermineLinedefColor(Linedef l)
{
2007-12-27 01:24:11 +00:00
// Impassable lines
2008-05-31 19:31:45 +00:00
if(l.IsFlagSet(General.Map.Config.ImpassableFlag))
2007-10-20 19:50:03 +00:00
{
// Determine color
2007-12-01 18:29:58 +00:00
if(l.Selected) return General.Colors.Selection;
2007-10-20 19:50:03 +00:00
else if(l.Action != 0) return General.Colors.Actions;
else return General.Colors.Linedefs;
}
else
{
// Determine color
byte a = (byte)(General.Settings.DoubleSidedAlpha * 255.0f);
2007-12-01 18:29:58 +00:00
if(l.Selected) return General.Colors.Selection;
else if(l.Action != 0) return General.Colors.Actions.WithAlpha(a);
else if(l.IsFlagSet(General.Map.Config.SoundLinedefFlag)) return General.Colors.Sounds.WithAlpha(a);
else return General.Colors.Linedefs.WithAlpha(a);
2007-10-20 19:50:03 +00:00
}
}
#endregion
#region ================== Start / Finish
2007-11-10 19:24:52 +00:00
// This begins a drawing session
public unsafe bool StartPlotter(bool clear)
2007-11-10 19:24:52 +00:00
{
if(renderlayer != RenderLayers.None) throw new InvalidOperationException("Renderer starting called before finished previous layer. Call Finish() first!");
renderlayer = RenderLayers.Plotter;
2008-12-05 08:13:39 +00:00
try { graphics.Device.SetRenderState(RenderState.FogEnable, false); } catch(Exception) { }
// Rendertargets available?
if(plottertex != null)
2007-11-10 19:24:52 +00:00
{
// Lock structures rendertarget memory
plotlocked = plottertex.LockRectangle(0, LockFlags.NoSystemLock);
2007-11-10 19:24:52 +00:00
// Create structures plotter
plotter = new Plotter((PixelColor*)plotlocked.Data.DataPointer.ToPointer(), plotlocked.Pitch / sizeof(PixelColor), structsize.Height, structsize.Width, structsize.Height);
if(clear) plotter.Clear();
// Redraw grid when structures image was cleared
if(clear)
{
RenderBackgroundGrid();
SetupBackground();
}
2007-11-10 19:24:52 +00:00
// Ready for rendering
2008-05-18 11:43:28 +00:00
UpdateTransformations();
return true;
2007-11-10 19:24:52 +00:00
}
else
{
// Can't render!
Finish();
return false;
}
}
// This begins a drawing session
public unsafe bool StartThings(bool clear)
{
if(renderlayer != RenderLayers.None) throw new InvalidOperationException("Renderer starting called before finished previous layer. Call Finish() first!");
renderlayer = RenderLayers.Things;
2008-12-05 08:13:39 +00:00
try { graphics.Device.SetRenderState(RenderState.FogEnable, false); } catch(Exception) { }
// Rendertargets available?
if(thingstex != null)
{
// Always trash things batch buffer
if(thingsvertices != null) thingsvertices.Dispose();
thingsvertices = null;
numthings = 0;
maxthings = 0;
// Set the rendertarget to the things texture
targetsurface = thingstex.GetSurfaceLevel(0);
if(graphics.StartRendering(clear, General.Colors.Background.WithAlpha(0).ToColorValue(), targetsurface, null))
{
// Ready for rendering
2008-05-18 11:43:28 +00:00
UpdateTransformations();
return true;
}
else
{
// Can't render!
Finish();
return false;
}
}
else
{
// Can't render!
Finish();
return false;
}
}
// This begins a drawing session
public unsafe bool StartOverlay(bool clear)
{
if(renderlayer != RenderLayers.None) throw new InvalidOperationException("Renderer starting called before finished previous layer. Call Finish() first!");
renderlayer = RenderLayers.Overlay;
2008-12-05 08:13:39 +00:00
try { graphics.Device.SetRenderState(RenderState.FogEnable, false); } catch(Exception) { }
// Rendertargets available?
if(overlaytex != null)
{
// Set the rendertarget to the things texture
targetsurface = overlaytex.GetSurfaceLevel(0);
if(graphics.StartRendering(clear, General.Colors.Background.WithAlpha(0).ToColorValue(), targetsurface, null))
{
// Ready for rendering
2008-05-18 11:43:28 +00:00
UpdateTransformations();
return true;
}
else
{
// Can't render!
Finish();
return false;
}
}
else
{
// Can't render!
Finish();
return false;
}
}
// This ends a drawing session
public void Finish()
{
// Clean up plotter
if(renderlayer == RenderLayers.Plotter)
{
if(plottertex != null) plottertex.UnlockRectangle(0);
if(plotlocked.Data != null) plotlocked.Data.Dispose();
plotter = null;
}
// Clean up things / overlay
if((renderlayer == RenderLayers.Things) || (renderlayer == RenderLayers.Overlay) || (renderlayer == RenderLayers.Surface))
{
// Stop rendering
graphics.FinishRendering();
// Release rendertarget
try
{
graphics.Device.DepthStencilSurface = graphics.DepthBuffer;
graphics.Device.SetRenderTarget(0, graphics.BackBuffer);
}
catch(Exception) { }
if(targetsurface != null) targetsurface.Dispose();
targetsurface = null;
}
// Done
renderlayer = RenderLayers.None;
}
#endregion
#region ================== Background
// This sets up background image vertices
private void SetupBackground()
{
Vector2D ltpos, rbpos;
Vector2D backoffset = new Vector2D((float)General.Map.Grid.BackgroundX, (float)General.Map.Grid.BackgroundY);
Vector2D backimagesize = new Vector2D((float)General.Map.Grid.Background.ScaledWidth, (float)General.Map.Grid.Background.ScaledHeight);
Vector2D backimagescale = new Vector2D(General.Map.Grid.BackgroundScaleX, General.Map.Grid.BackgroundScaleY);
// Scale the background image size
backimagesize *= backimagescale;
// Only if a background image is set
if((General.Map.Grid.Background != null) &&
!(General.Map.Grid.Background is UnknownImage))
{
// Make vertices
backimageverts = CreateScreenVerts(windowsize);
// Determine map coordinates for view window
ltpos = GetMapCoordinates(new Vector2D(0f, 0f));
rbpos = GetMapCoordinates(new Vector2D(windowsize.Width, windowsize.Height));
// Offset by given background offset
ltpos -= backoffset;
rbpos -= backoffset;
// Calculate UV coordinates
// NOTE: backimagesize.y is made negative to match Doom's coordinate system
backimageverts[0].u = ltpos.x / backimagesize.x;
backimageverts[0].v = ltpos.y / -backimagesize.y;
backimageverts[1].u = rbpos.x / backimagesize.x;
backimageverts[1].v = ltpos.y / -backimagesize.y;
backimageverts[2].u = ltpos.x / backimagesize.x;
backimageverts[2].v = rbpos.y / -backimagesize.y;
backimageverts[3].u = rbpos.x / backimagesize.x;
backimageverts[3].v = rbpos.y / -backimagesize.y;
}
else
{
// No background image
backimageverts = null;
}
}
2007-11-10 19:24:52 +00:00
2007-11-11 13:51:01 +00:00
// This renders all grid
private void RenderBackgroundGrid()
{
Plotter gridplotter;
DataRectangle lockedrect;
2007-11-11 13:51:01 +00:00
2007-11-11 14:42:04 +00:00
// Do we need to redraw grid?
2008-06-10 08:35:14 +00:00
if((lastgridsize != General.Map.Grid.GridSize) || (lastgridscale != scale) ||
(lastgridx != offsetx) || (lastgridy != offsety))
2007-11-11 14:42:04 +00:00
{
// Lock background rendertarget memory
lockedrect = backtex.LockRectangle(0, LockFlags.NoSystemLock);
// Create a plotter
gridplotter = new Plotter((PixelColor*)lockedrect.Data.DataPointer.ToPointer(), lockedrect.Pitch / sizeof(PixelColor), backsize.Height, backsize.Width, backsize.Height);
gridplotter.Clear();
2007-11-11 13:51:01 +00:00
2007-11-11 14:42:04 +00:00
// Render normal grid
RenderGrid(General.Map.Grid.GridSize, General.Colors.Grid, gridplotter);
2007-11-11 13:51:01 +00:00
2007-11-11 14:42:04 +00:00
// Render 64 grid
if(General.Map.Grid.GridSize <= 64) RenderGrid(64f, General.Colors.Grid64, gridplotter);
// Done
backtex.UnlockRectangle(0);
lockedrect.Data.Dispose();
lastgridscale = scale;
2008-06-10 08:35:14 +00:00
lastgridsize = General.Map.Grid.GridSize;
2007-11-11 14:42:04 +00:00
lastgridx = offsetx;
lastgridy = offsety;
}
2007-11-11 13:51:01 +00:00
}
2007-11-10 19:24:52 +00:00
// This renders the grid
2007-11-11 13:51:01 +00:00
private void RenderGrid(float size, PixelColor c, Plotter gridplotter)
2007-11-10 19:24:52 +00:00
{
Vector2D ltpos, rbpos;
Vector2D pos = new Vector2D();
float sizeinv = 1f / size;
// Only render grid when not screen-filling
if((size * scale) > 6f)
{
// Determine map coordinates for view window
ltpos = GetMapCoordinates(new Vector2D(0, 0));
rbpos = GetMapCoordinates(new Vector2D(windowsize.Width, windowsize.Height));
// Clip to nearest grid
ltpos = GridSetup.SnappedToGrid(ltpos, size, sizeinv);
rbpos = GridSetup.SnappedToGrid(rbpos, size, sizeinv);
// Draw all horizontal grid lines
for(float y = ltpos.y + size; y > rbpos.y - size; y -= size)
{
pos.y = y;
pos = pos.GetTransformed(translatex, translatey, scale, -scale);
2008-04-17 05:36:48 +00:00
gridplotter.DrawGridLineH((int)pos.y, ref c);
2007-11-10 19:24:52 +00:00
}
// Draw all vertical grid lines
for(float x = ltpos.x - size; x < rbpos.x + size; x += size)
{
pos.x = x;
pos = pos.GetTransformed(translatex, translatey, scale, -scale);
2008-04-17 05:36:48 +00:00
gridplotter.DrawGridLineV((int)pos.x, ref c);
2007-11-10 19:24:52 +00:00
}
}
}
#endregion
#region ================== Things
// This ensures there is enough place in the things buffer
private void ReserveThingsMemory(int newnumthings, bool preserve)
{
int newmaxthings;
DataStream stream;
FlatVertex[] verts = null;
// Do we need to make changes?
if((newnumthings > maxthings) || !preserve)
{
// Read old things data if we want to keep it
if(preserve && (thingsvertices != null) && (numthings > 0))
{
stream = thingsvertices.Lock(0, numthings * 12 * FlatVertex.Stride, LockFlags.ReadOnly | LockFlags.NoSystemLock);
verts = stream.ReadRange<FlatVertex>(numthings * 12);
thingsvertices.Unlock();
stream.Dispose();
}
// Buffer needs to be reallocated?
if(newnumthings > maxthings)
{
// Calculate new size
newmaxthings = newnumthings + THING_BUFFER_STEP;
// Trash old buffer
if(thingsvertices != null) thingsvertices.Dispose();
// Create new buffer
thingsvertices = new VertexBuffer(graphics.Device, newmaxthings * 12 * FlatVertex.Stride, Usage.None, VertexFormat.None, Pool.Managed);
maxthings = newmaxthings;
}
else
{
// Buffer stays the same
newmaxthings = maxthings;
}
// Keep old things?
if(preserve && (verts != null))
{
// Write old things into new buffer
stream = thingsvertices.Lock(0, maxthings * 12 * FlatVertex.Stride, LockFlags.Discard | LockFlags.NoSystemLock);
stream.WriteRange<FlatVertex>(verts);
thingsvertices.Unlock();
stream.Dispose();
}
else
{
// Things were trashed
numthings = 0;
}
}
}
// This makes vertices for a thing
// Returns false when not on the screen
private bool CreateThingVerts(Thing t, ref FlatVertex[] verts, int offset, PixelColor c)
{
float circlesize;
float arrowsize;
int color;
// Transform to screen coordinates
Vector2D screenpos = ((Vector2D)t.Position).GetTransformed(translatex, translatey, scale, -scale);
// Determine sizes
if(t.FixedSize && (scale > 1.0f))
{
circlesize = (t.Size - THING_CIRCLE_SHRINK) * THING_CIRCLE_SIZE;
arrowsize = (t.Size - THING_ARROW_SHRINK) * THING_ARROW_SIZE;
}
else
{
circlesize = (t.Size - THING_CIRCLE_SHRINK) * scale * THING_CIRCLE_SIZE;
arrowsize = (t.Size - THING_ARROW_SHRINK) * scale * THING_ARROW_SIZE;
}
// Check if the thing is actually on screen
if(((screenpos.x + circlesize) > 0.0f) && ((screenpos.x - circlesize) < (float)windowsize.Width) &&
((screenpos.y + circlesize) > 0.0f) && ((screenpos.y - circlesize) < (float)windowsize.Height))
{
// Get integral color
color = c.ToInt();
// Setup fixed rect for circle
verts[offset].x = screenpos.x - circlesize;
verts[offset].y = screenpos.y - circlesize;
verts[offset].c = color;
verts[offset].u = 1f / 512f;
verts[offset].v = 1f / 128f;
offset++;
verts[offset].x = screenpos.x + circlesize;
verts[offset].y = screenpos.y - circlesize;
verts[offset].c = color;
verts[offset].u = 0.25f - 1f / 512f;
verts[offset].v = 1f / 128f;
offset++;
verts[offset].x = screenpos.x - circlesize;
verts[offset].y = screenpos.y + circlesize;
verts[offset].c = color;
verts[offset].u = 1f / 512f;
verts[offset].v = 1f - 1f / 128f;
offset++;
verts[offset] = verts[offset - 2];
offset++;
verts[offset] = verts[offset - 2];
offset++;
verts[offset].x = screenpos.x + circlesize;
verts[offset].y = screenpos.y + circlesize;
verts[offset].c = color;
verts[offset].u = 0.25f - 1f / 512f;
verts[offset].v = 1f - 1f / 128f;
offset++;
2007-11-10 19:24:52 +00:00
// Setup rotated rect for arrow
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle - Angle2D.PI * 0.25f) * arrowsize;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle - Angle2D.PI * 0.25f) * arrowsize;
verts[offset].c = -1;
verts[offset].u = 0.50f + t.IconOffset;
verts[offset].v = 0f;
offset++;
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle + Angle2D.PI * 0.25f) * arrowsize;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle + Angle2D.PI * 0.25f) * arrowsize;
verts[offset].c = -1;
verts[offset].u = 0.75f + t.IconOffset;
verts[offset].v = 0f;
offset++;
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle - Angle2D.PI * 0.75f) * arrowsize;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle - Angle2D.PI * 0.75f) * arrowsize;
verts[offset].c = -1;
verts[offset].u = 0.50f + t.IconOffset;
verts[offset].v = 1f;
offset++;
verts[offset] = verts[offset - 2];
offset++;
verts[offset] = verts[offset - 2];
offset++;
verts[offset].x = screenpos.x + (float)Math.Sin(t.Angle + Angle2D.PI * 0.75f) * arrowsize;
verts[offset].y = screenpos.y + (float)Math.Cos(t.Angle + Angle2D.PI * 0.75f) * arrowsize;
verts[offset].c = -1;
verts[offset].u = 0.75f + t.IconOffset;
verts[offset].v = 1f;
// Done
return true;
}
else
{
// Not on screen
return false;
}
}
// This draws a set of things
private void RenderThingsBatch(int offset, int count, float alpha)
{
int thingtextureindex = 0;
// Anything to render?
if(count > 0)
{
// Make alpha color
Color4 alphacolor = new Color4(alpha, 1.0f, 1.0f, 1.0f);
// Set renderstates for things rendering
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, alphacolor.ToArgb());
graphics.Device.SetStreamSource(0, thingsvertices, 0, FlatVertex.Stride);
// Determine things texture to use
if(General.Settings.QualityDisplay) thingtextureindex |= THING_SHINY;
if(General.Settings.SquareThings) thingtextureindex |= THING_SQUARE;
graphics.Device.SetTexture(0, thingtexture[thingtextureindex].Texture);
graphics.Shaders.Things2D.Texture1 = thingtexture[thingtextureindex].Texture;
2008-05-18 11:43:28 +00:00
SetWorldTransformation(false);
graphics.Shaders.Things2D.SetSettings(alpha);
// Draw the things batched
graphics.Shaders.Things2D.Begin();
graphics.Shaders.Things2D.BeginPass(0);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, offset * 12, count * 4);
graphics.Shaders.Things2D.EndPass();
graphics.Shaders.Things2D.End();
2007-10-26 18:04:54 +00:00
}
}
2007-10-21 04:07:36 +00:00
// This adds a thing in the things buffer for rendering
public void RenderThing(Thing t, PixelColor c, float alpha)
2007-10-21 04:07:36 +00:00
{
FlatVertex[] verts = new FlatVertex[12];
2007-10-21 04:07:36 +00:00
DataStream stream;
// Create vertices
if(CreateThingVerts(t, ref verts, 0, c))
{
// Make sure there is enough memory reserved
ReserveThingsMemory(numthings + 1, true);
2007-10-21 04:07:36 +00:00
// Store vertices in buffer
if(thingsvertices != null)
{
stream = thingsvertices.Lock(numthings * 12 * FlatVertex.Stride, 12 * FlatVertex.Stride, LockFlags.NoSystemLock);
stream.WriteRange<FlatVertex>(verts);
thingsvertices.Unlock();
stream.Dispose();
}
// Thing added, render it
RenderThingsBatch(numthings, 1, alpha);
numthings++;
}
2007-10-21 04:07:36 +00:00
}
// This adds a thing in the things buffer for rendering
public void RenderThingSet(ICollection<Thing> things, float alpha)
2007-10-21 04:07:36 +00:00
{
// Anything to do?
if(things.Count > 0)
{
FlatVertex[] verts = new FlatVertex[things.Count * 12];
// Make sure there is enough memory reserved
ReserveThingsMemory(numthings + things.Count, true);
2007-10-21 04:07:36 +00:00
// Go for all things
int addcount = 0;
foreach(Thing t in things)
{
// Create vertices
if(CreateThingVerts(t, ref verts, addcount * 12, DetermineThingColor(t)))
{
// Next
addcount++;
}
}
// Store vertices in buffer
if(thingsvertices != null)
{
DataStream stream = thingsvertices.Lock(numthings * 12 * FlatVertex.Stride, things.Count * 12 * FlatVertex.Stride, LockFlags.NoSystemLock);
stream.WriteRange<FlatVertex>(verts);
thingsvertices.Unlock();
stream.Dispose();
}
// Things added, render them
RenderThingsBatch(numthings, addcount, alpha);
numthings += addcount;
}
2007-10-21 04:07:36 +00:00
}
#endregion
#region ================== Surface
// This redraws the surface
public void RedrawSurface()
{
if(renderlayer != RenderLayers.None) throw new InvalidOperationException("Renderer starting called before finished previous layer. Call Finish() first!");
renderlayer = RenderLayers.Surface;
// Rendertargets available?
if(surfacetex != null)
{
// Set the rendertarget to the surface texture
targetsurface = surfacetex.GetSurfaceLevel(0);
if(graphics.StartRendering(true, General.Colors.Background.WithAlpha(0).ToColorValue(), targetsurface, null))
{
// Set transformations
UpdateTransformations();
// Render what must be rendered
switch(viewmode)
{
case ViewMode.Brightness: RenderSectorBrightness(General.Map.Map.Sectors); break;
case ViewMode.FloorTextures: RenderSectorFloors(General.Map.Map.Sectors); break;
case ViewMode.CeilingTextures: RenderSectorCeilings(General.Map.Map.Sectors); break;
}
}
}
// Done
Finish();
}
// This renders all sector floors
private void RenderSectorFloors(ICollection<Sector> sectors)
{
// Set states
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
SetWorldTransformation(true);
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Render all sectors
foreach(Sector s in sectors)
RenderSectorSurface(s, s.FlatFloorBuffer, s.LongFloorTexture);
}
// This renders all sector ceilings
private void RenderSectorCeilings(ICollection<Sector> sectors)
{
// Set states
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
SetWorldTransformation(true);
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Render all sectors
foreach(Sector s in sectors)
RenderSectorSurface(s, s.FlatCeilingBuffer, s.LongCeilTexture);
}
// This renders all sector brightness levels
private void RenderSectorBrightness(ICollection<Sector> sectors)
{
// Set states
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
SetWorldTransformation(true);
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Render all sectors
foreach(Sector s in sectors)
RenderSectorSurface(s, s.FlatFloorBuffer, 0);
}
// This renders the geometry and tecture of the sector
private void RenderSectorSurface(Sector s, VertexBuffer buffer, long longimagename)
{
Texture t = null;
if((buffer != null) && (s.FlatVertices != null) && (s.FlatVertices.Length > 0))
{
if(longimagename == 0)
{
t = whitetexture.Texture;
}
else
{
ImageData img = General.Map.Data.GetFlatImage(longimagename);
if(img != null)
{
// Texture unknown?
if(img is UnknownImage)
{
General.Map.Data.UnknownTexture3D.CreateTexture();
t = General.Map.Data.UnknownTexture3D.Texture;
}
// Is the texture loaded?
else if(img.IsImageLoaded && !img.LoadFailed)
{
if(img.Texture == null) img.CreateTexture();
t = img.Texture;
}
else
{
t = whitetexture.Texture;
}
}
else
{
t = whitetexture.Texture;
}
}
// Set renderstates for rendering
graphics.Shaders.Display2D.Texture1 = t;
graphics.Device.SetTexture(0, t);
graphics.Device.SetStreamSource(0, buffer, 0, FlatVertex.Stride);
// Draw
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(1);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, s.FlatVertices.Length / 3);
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
}
}
#endregion
#region ================== Overlay
// This renders geometry
// The geometry must be a triangle list
2008-05-18 11:43:28 +00:00
public void RenderGeometry(FlatVertex[] vertices, ImageData texture, bool transformcoords)
{
Texture t = null;
if(vertices.Length > 0)
{
if(texture != null)
{
// Make sure the texture is loaded
if(!texture.IsImageLoaded) texture.LoadImage();
if(texture.Texture == null) texture.CreateTexture();
t = texture.Texture;
}
else
{
t = whitetexture.Texture;
}
// Set renderstates for rendering
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
2008-09-09 05:52:11 +00:00
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.Texture1 = t;
graphics.Device.SetTexture(0, t);
2008-05-18 11:43:28 +00:00
SetWorldTransformation(transformcoords);
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Draw
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.Begin();
2008-09-09 05:52:11 +00:00
graphics.Shaders.Display2D.BeginPass(1);
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleList, 0, vertices.Length / 3, vertices);
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
}
}
2008-04-17 05:36:48 +00:00
// This renders text
public void RenderText(TextLabel text)
2008-04-17 05:36:48 +00:00
{
// Update the text if needed
text.Update(translatex, translatey, scale, -scale);
2008-04-17 05:36:48 +00:00
// Text is created?
if(text.VertexBuffer != null)
{
// Set renderstates for rendering
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.Texture1 = graphics.FontTexture;
SetWorldTransformation(false);
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, true);
graphics.Device.SetTexture(0, graphics.FontTexture);
graphics.Device.SetStreamSource(0, text.VertexBuffer, 0, FlatVertex.Stride);
// Draw
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(2);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, text.NumFaces >> 1);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, text.NumFaces);
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
}
2008-04-17 05:36:48 +00:00
}
// This renders a rectangle with given border size and color
public void RenderRectangle(RectangleF rect, float bordersize, PixelColor c, bool transformrect)
{
FlatQuad[] quads = new FlatQuad[4];
/*
* Rectangle setup:
*
* --------------------------
* |___________0____________|
* | | | |
* | | | |
* | | | |
* | 2| |3 |
* | | | |
* | | | |
* |__|__________________|__|
* | 1 |
* --------------------------
*
* Don't you just love ASCII art?
*/
// Calculate positions
Vector2D lt = new Vector2D(rect.Left, rect.Top);
Vector2D rb = new Vector2D(rect.Right, rect.Bottom);
if(transformrect)
{
lt = lt.GetTransformed(translatex, translatey, scale, -scale);
rb = rb.GetTransformed(translatex, translatey, scale, -scale);
}
// Make quads
quads[0] = new FlatQuad(PrimitiveType.TriangleStrip, lt.x, lt.y, rb.x, lt.y - bordersize);
quads[1] = new FlatQuad(PrimitiveType.TriangleStrip, lt.x, rb.y + bordersize, rb.x, rb.y);
quads[2] = new FlatQuad(PrimitiveType.TriangleStrip, lt.x, lt.y - bordersize, lt.x + bordersize, rb.y + bordersize);
quads[3] = new FlatQuad(PrimitiveType.TriangleStrip, rb.x - bordersize, lt.y - bordersize, rb.x, rb.y + bordersize);
quads[0].SetColors(c.ToInt());
quads[1].SetColors(c.ToInt());
quads[2].SetColors(c.ToInt());
2008-05-18 11:43:28 +00:00
quads[3].SetColors(c.ToInt());
// Set renderstates for rendering
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
2008-05-18 11:43:28 +00:00
SetWorldTransformation(false);
graphics.Device.SetTexture(0, whitetexture.Texture);
graphics.Shaders.Display2D.Texture1 = whitetexture.Texture;
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Draw
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(1);
quads[0].Render(graphics);
quads[1].Render(graphics);
quads[2].Render(graphics);
quads[3].Render(graphics);
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
}
2008-04-17 05:36:48 +00:00
// This renders a filled rectangle with given color
public void RenderRectangleFilled(RectangleF rect, PixelColor c, bool transformrect)
{
// Calculate positions
Vector2D lt = new Vector2D(rect.Left, rect.Top);
Vector2D rb = new Vector2D(rect.Right, rect.Bottom);
if(transformrect)
{
lt = lt.GetTransformed(translatex, translatey, scale, -scale);
rb = rb.GetTransformed(translatex, translatey, scale, -scale);
}
// Make quad
FlatQuad quad = new FlatQuad(PrimitiveType.TriangleStrip, lt.x, lt.y, rb.x, rb.y);
2008-04-17 05:36:48 +00:00
quad.SetColors(c.ToInt());
// Set renderstates for rendering
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
2008-05-18 11:43:28 +00:00
SetWorldTransformation(false);
graphics.Device.SetTexture(0, whitetexture.Texture);
graphics.Shaders.Display2D.Texture1 = whitetexture.Texture;
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
2008-04-17 05:36:48 +00:00
// Draw
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(1);
2008-04-17 05:36:48 +00:00
quad.Render(graphics);
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
2008-04-17 05:36:48 +00:00
}
// This renders a line with given color
public void RenderLine(Vector2D start, Vector2D end, float thickness, PixelColor c, bool transformcoords)
{
FlatVertex[] verts = new FlatVertex[4];
// Calculate positions
if(transformcoords)
{
start = start.GetTransformed(translatex, translatey, scale, -scale);
end = end.GetTransformed(translatex, translatey, scale, -scale);
}
// Calculate offsets
Vector2D delta = end - start;
Vector2D dn = delta.GetNormal() * thickness;
// Make vertices
verts[0].x = start.x - dn.x + dn.y;
verts[0].y = start.y - dn.y - dn.x;
verts[0].z = 0.0f;
verts[0].c = c.ToInt();
verts[1].x = start.x - dn.x - dn.y;
verts[1].y = start.y - dn.y + dn.x;
verts[1].z = 0.0f;
verts[1].c = c.ToInt();
verts[2].x = end.x + dn.x + dn.y;
verts[2].y = end.y + dn.y - dn.x;
verts[2].z = 0.0f;
verts[2].c = c.ToInt();
verts[3].x = end.x + dn.x - dn.y;
verts[3].y = end.y + dn.y + dn.x;
verts[3].z = 0.0f;
verts[3].c = c.ToInt();
// Set renderstates for rendering
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
graphics.Device.SetRenderState(RenderState.ZEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
graphics.Device.SetRenderState(RenderState.FogEnable, false);
2008-05-18 11:43:28 +00:00
SetWorldTransformation(false);
graphics.Device.SetTexture(0, whitetexture.Texture);
graphics.Shaders.Display2D.Texture1 = whitetexture.Texture;
graphics.Shaders.Display2D.SetSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Draw
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(0);
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, verts);
2008-05-18 11:43:28 +00:00
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
}
#endregion
#region ================== Geometry
2007-10-20 19:50:03 +00:00
// This renders the linedefs of a sector with special color
public void PlotSector(Sector s, PixelColor c)
{
2007-10-20 19:50:03 +00:00
// Go for all sides in the sector
foreach(Sidedef sd in s.Sidedefs)
{
2007-10-20 19:50:03 +00:00
// Render this linedef
PlotLinedef(sd.Line, c);
2007-10-20 19:50:03 +00:00
// Render the two vertices on top
PlotVertex(sd.Line.Start, DetermineVertexColor(sd.Line.Start));
PlotVertex(sd.Line.End, DetermineVertexColor(sd.Line.End));
}
}
2007-10-20 19:50:03 +00:00
// This renders the linedefs of a sector
public void PlotSector(Sector s)
2007-06-24 18:56:43 +00:00
{
2007-10-20 19:50:03 +00:00
// Go for all sides in the sector
foreach(Sidedef sd in s.Sidedefs)
{
2007-10-20 19:50:03 +00:00
// Render this linedef
PlotLinedef(sd.Line, DetermineLinedefColor(sd.Line));
2007-10-20 19:50:03 +00:00
// Render the two vertices on top
PlotVertex(sd.Line.Start, DetermineVertexColor(sd.Line.Start));
PlotVertex(sd.Line.End, DetermineVertexColor(sd.Line.End));
}
2007-10-20 19:50:03 +00:00
}
2008-01-02 21:49:43 +00:00
// This renders a simple line
public void PlotLine(Vector2D start, Vector2D end, PixelColor c)
2008-01-02 21:49:43 +00:00
{
// Transform coordinates
Vector2D v1 = start.GetTransformed(translatex, translatey, scale, -scale);
Vector2D v2 = end.GetTransformed(translatex, translatey, scale, -scale);
// Draw line
2008-04-17 05:36:48 +00:00
plotter.DrawLineSolid((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, ref c);
2008-01-02 21:49:43 +00:00
}
2007-10-20 19:50:03 +00:00
// This renders a single linedef
public void PlotLinedef(Linedef l, PixelColor c)
2007-10-20 19:50:03 +00:00
{
// Transform vertex coordinates
Vector2D v1 = l.Start.Position.GetTransformed(translatex, translatey, scale, -scale);
Vector2D v2 = l.End.Position.GetTransformed(translatex, translatey, scale, -scale);
// Draw line
2008-04-17 05:36:48 +00:00
plotter.DrawLineSolid((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, ref c);
2007-10-20 19:50:03 +00:00
// Calculate normal indicator
float mx = (v2.x - v1.x) * 0.5f;
float my = (v2.y - v1.y) * 0.5f;
// Draw normal indicator
plotter.DrawLineSolid((int)(v1.x + mx), (int)(v1.y + my),
(int)((v1.x + mx) - (my * l.LengthInv) * linenormalsize),
2008-04-17 05:36:48 +00:00
(int)((v1.y + my) + (mx * l.LengthInv) * linenormalsize), ref c);
2007-10-20 19:50:03 +00:00
}
// This renders a set of linedefs
public void PlotLinedefSet(ICollection<Linedef> linedefs)
2007-10-20 19:50:03 +00:00
{
// Go for all linedefs
foreach(Linedef l in linedefs) PlotLinedef(l, DetermineLinedefColor(l));
2007-10-20 19:50:03 +00:00
}
// This renders a single vertex
public void PlotVertex(Vertex v, int colorindex)
2007-10-20 19:50:03 +00:00
{
// Transform vertex coordinates
Vector2D nv = v.Position.GetTransformed(translatex, translatey, scale, -scale);
// Draw pixel here
2008-04-17 05:36:48 +00:00
plotter.DrawVertexSolid((int)nv.x, (int)nv.y, vertexsize, ref General.Colors.Colors[colorindex], ref General.Colors.BrightColors[colorindex], ref General.Colors.DarkColors[colorindex]);
2007-10-20 19:50:03 +00:00
}
// This renders a single vertex at specified coordinates
public void PlotVertexAt(Vector2D v, int colorindex)
{
// Transform vertex coordinates
Vector2D nv = v.GetTransformed(translatex, translatey, scale, -scale);
// Draw pixel here
2008-04-17 05:36:48 +00:00
plotter.DrawVertexSolid((int)nv.x, (int)nv.y, vertexsize, ref General.Colors.Colors[colorindex], ref General.Colors.BrightColors[colorindex], ref General.Colors.DarkColors[colorindex]);
}
2007-10-20 19:50:03 +00:00
// This renders a set of vertices
public void PlotVerticesSet(ICollection<Vertex> vertices)
2007-10-20 19:50:03 +00:00
{
// Go for all vertices
foreach(Vertex v in vertices) PlotVertex(v, DetermineVertexColor(v));
2007-06-15 22:38:42 +00:00
}
2007-06-15 18:50:18 +00:00
#endregion
}
}