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;
|
2007-06-26 09:09:54 +00:00
|
|
|
using SlimDX.Direct3D;
|
2007-07-07 09:40:34 +00:00
|
|
|
using SlimDX.Direct3D9;
|
2007-06-26 09:09:54 +00:00
|
|
|
using SlimDX;
|
2007-07-07 09:40:34 +00:00
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
2007-09-17 21:22:46 +00:00
|
|
|
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
|
|
|
|
{
|
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
|
|
|
|
|
2007-10-20 12:34:27 +00:00
|
|
|
private const byte DOUBLESIDED_LINE_ALPHA = 130;
|
|
|
|
private const float FSAA_BLEND_FACTOR = 0.6f;
|
2007-10-21 22:41:46 +00:00
|
|
|
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 = 2f;
|
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
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Rendertargets
|
2007-11-11 13:51:01 +00:00
|
|
|
private Texture backtex;
|
2007-10-23 05:08:02 +00:00
|
|
|
private Texture structtex;
|
|
|
|
private Texture thingstex;
|
|
|
|
|
|
|
|
// Locking data
|
|
|
|
private LockedRect structlocked;
|
|
|
|
private Surface thingssurface;
|
|
|
|
|
|
|
|
// Rendertarget sizes
|
|
|
|
private Size windowsize;
|
|
|
|
private Size structsize;
|
|
|
|
private Size thingssize;
|
2007-11-11 13:51:01 +00:00
|
|
|
private Size backsize;
|
2007-10-23 05:08:02 +00:00
|
|
|
|
|
|
|
// Geometry plotter
|
2007-10-19 14:27:46 +00:00
|
|
|
private Plotter plotter;
|
2007-10-23 05:08:02 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Batch buffer for things rendering
|
2007-10-21 04:07:36 +00:00
|
|
|
private VertexBuffer thingsvertices;
|
2007-10-23 05:08:02 +00:00
|
|
|
private int maxthings, numthings;
|
2007-10-21 04:07:36 +00:00
|
|
|
|
2007-10-21 22:41:46 +00:00
|
|
|
// Render settings
|
|
|
|
private bool thingsfront;
|
2007-10-22 09:00:54 +00:00
|
|
|
private int vertexsize;
|
|
|
|
|
2007-10-21 04:07:36 +00:00
|
|
|
// Images
|
|
|
|
private ResourceImage thingtexture;
|
|
|
|
|
2007-06-24 18:56:43 +00:00
|
|
|
// View settings (world coordinates)
|
|
|
|
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;
|
|
|
|
private float lastgridx;
|
|
|
|
private float lastgridy;
|
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 Scale { get { return scale; } }
|
2007-06-15 18:50:18 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
2007-09-17 21:22:46 +00:00
|
|
|
|
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
|
|
|
{
|
|
|
|
// Initialize
|
2007-10-21 04:07:36 +00:00
|
|
|
thingtexture = new ResourceImage("Thing2D.png");
|
|
|
|
thingtexture.LoadImage();
|
|
|
|
thingtexture.CreateTexture();
|
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Create rendertargets
|
|
|
|
CreateRendertargets();
|
2007-09-17 21:22:46 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2007-10-23 05:08:02 +00:00
|
|
|
// Destroy rendertargets
|
|
|
|
DestroyRendertargets();
|
2007-10-26 18:04:54 +00:00
|
|
|
thingtexture.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
|
2007-10-23 05:08:02 +00:00
|
|
|
|
2007-10-19 14:27:46 +00:00
|
|
|
// This draws the image on screen
|
|
|
|
public void Present()
|
|
|
|
{
|
|
|
|
// Start drawing
|
2007-10-31 20:34:09 +00:00
|
|
|
if(graphics.StartRendering(true, General.Colors.Background.ToInt(), graphics.BackBuffer, graphics.DepthBuffer))
|
2007-10-19 14:27:46 +00:00
|
|
|
{
|
2007-10-23 05:08:02 +00:00
|
|
|
// Renderstates that count for this whole sequence
|
|
|
|
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
|
|
|
|
graphics.Device.SetRenderState(RenderState.ZEnable, false);
|
2007-11-11 14:42:04 +00:00
|
|
|
graphics.Shaders.Display2D.Begin();
|
2007-10-23 05:08:02 +00:00
|
|
|
|
2007-11-10 19:24:52 +00:00
|
|
|
// Render a background image?
|
2007-11-11 13:51:01 +00:00
|
|
|
if((backimageverts != null) && (General.Map.Grid.Background.Texture != null))
|
2007-11-10 19:24:52 +00:00
|
|
|
{
|
|
|
|
// Set renderstates
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
|
|
|
|
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_BLEND_FACTOR, 1f);
|
|
|
|
|
2007-11-11 13:51:01 +00:00
|
|
|
// Draw the background image
|
|
|
|
graphics.Shaders.Display2D.BeginPass(1);
|
|
|
|
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, backimageverts);
|
2007-11-10 19:24:52 +00:00
|
|
|
graphics.Shaders.Display2D.EndPass();
|
|
|
|
}
|
|
|
|
|
2007-11-11 13:51:01 +00:00
|
|
|
// From here on only using screen vertices
|
|
|
|
graphics.Device.SetStreamSource(0, screenverts, 0, sizeof(FlatVertex));
|
|
|
|
|
2007-10-21 22:41:46 +00:00
|
|
|
// Render things in back?
|
2007-10-23 05:08:02 +00:00
|
|
|
if(!thingsfront) PresentThings(THINGS_BACK_ALPHA);
|
2007-11-11 13:51:01 +00:00
|
|
|
|
|
|
|
// Set renderstates
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
|
|
|
|
graphics.Device.SetTexture(0, backtex);
|
|
|
|
graphics.Shaders.Display2D.Texture1 = backtex;
|
|
|
|
graphics.Shaders.Display2D.SetSettings(1f / backsize.Width, 1f / backsize.Height, 0f, 1f);
|
|
|
|
|
|
|
|
// Draw the background grid
|
|
|
|
graphics.Shaders.Display2D.BeginPass(1);
|
|
|
|
//graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, backverts);
|
|
|
|
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
|
|
|
|
graphics.Shaders.Display2D.EndPass();
|
2007-10-21 22:41:46 +00:00
|
|
|
|
2007-10-21 04:07:36 +00:00
|
|
|
// Set renderstates
|
2007-10-20 12:34:27 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
|
2007-10-23 05:08:02 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
|
2007-10-20 12:34:27 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
|
|
|
|
graphics.Device.SetRenderState(RenderState.DestBlend, Blend.InvSourceAlpha);
|
2007-10-23 05:08:02 +00:00
|
|
|
graphics.Device.SetTexture(0, structtex);
|
|
|
|
graphics.Shaders.Display2D.Texture1 = structtex;
|
|
|
|
graphics.Shaders.Display2D.SetSettings(1f / structsize.Width, 1f / structsize.Height, FSAA_BLEND_FACTOR, 1f);
|
2007-10-19 17:05:21 +00:00
|
|
|
|
2007-10-21 04:07:36 +00:00
|
|
|
// Draw the lines and vertices texture
|
2007-10-23 05:08:02 +00:00
|
|
|
graphics.Shaders.Display2D.BeginPass(0);
|
2007-10-26 18:04:54 +00:00
|
|
|
//try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, structverts); } catch(Exception) { }
|
2007-11-11 13:51:01 +00:00
|
|
|
//graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, structverts);
|
|
|
|
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
|
2007-10-23 05:08:02 +00:00
|
|
|
graphics.Shaders.Display2D.EndPass();
|
2007-10-21 22:41:46 +00:00
|
|
|
|
2007-10-22 09:00:54 +00:00
|
|
|
// Render things in front?
|
2007-10-23 05:08:02 +00:00
|
|
|
if(thingsfront) PresentThings(1f);
|
2007-10-21 22:41:46 +00:00
|
|
|
|
|
|
|
// Done
|
2007-11-11 14:42:04 +00:00
|
|
|
graphics.Shaders.Display2D.End();
|
2007-10-31 21:33:06 +00:00
|
|
|
graphics.FinishRendering();
|
|
|
|
graphics.Present();
|
2007-10-21 22:41:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This presents the things
|
2007-10-23 05:08:02 +00:00
|
|
|
private void PresentThings(float alpha)
|
2007-10-21 22:41:46 +00:00
|
|
|
{
|
2007-10-23 05:08:02 +00:00
|
|
|
// Set renderstates
|
|
|
|
//graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
|
|
|
|
//graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
|
|
|
|
//graphics.Device.SetRenderState(RenderState.AlphaFunc, Compare.GreaterEqual);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
|
|
|
|
graphics.Device.SetRenderState(RenderState.DestBlend, Blend.InvSourceAlpha);
|
|
|
|
graphics.Device.SetTexture(0, thingstex);
|
|
|
|
graphics.Shaders.Display2D.Texture1 = thingstex;
|
|
|
|
graphics.Shaders.Display2D.SetSettings(1f / thingssize.Width, 1f / thingssize.Height, FSAA_BLEND_FACTOR, alpha);
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// Draw the things texture
|
2007-10-23 05:08:02 +00:00
|
|
|
graphics.Shaders.Display2D.BeginPass(0);
|
2007-10-26 18:04:54 +00:00
|
|
|
//try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, thingsverts); } catch(Exception) { }
|
2007-11-11 13:51:01 +00:00
|
|
|
//graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, thingsverts);
|
|
|
|
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
|
2007-10-23 05:08:02 +00:00
|
|
|
graphics.Shaders.Display2D.EndPass();
|
2007-10-19 14:27:46 +00:00
|
|
|
}
|
2007-10-23 05:08:02 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Management
|
2007-10-19 14:27:46 +00:00
|
|
|
|
|
|
|
// This is called before a device is reset
|
|
|
|
// (when resized or display adapter was changed)
|
2008-01-05 18:11:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// DO NOT USE.
|
|
|
|
/// </summary>
|
2007-10-19 14:27:46 +00:00
|
|
|
public override void UnloadResource()
|
|
|
|
{
|
2007-10-23 05:08:02 +00:00
|
|
|
// Destroy rendertargets
|
|
|
|
DestroyRendertargets();
|
2007-10-19 14:27:46 +00:00
|
|
|
}
|
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
// This is called resets when the device is reset
|
|
|
|
// (when resized or display adapter was changed)
|
2008-01-05 18:11:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// DO NOT USE.
|
|
|
|
/// </summary>
|
2007-10-19 14:27:46 +00:00
|
|
|
public override void ReloadResource()
|
2007-06-24 18:56:43 +00:00
|
|
|
{
|
2007-10-23 05:08:02 +00:00
|
|
|
// Re-create rendertargets
|
|
|
|
CreateRendertargets();
|
2007-10-19 14:27:46 +00:00
|
|
|
}
|
2007-09-24 19:54:47 +00:00
|
|
|
|
2007-10-19 14:27:46 +00:00
|
|
|
// This resets the graphics
|
|
|
|
public override void Reset()
|
|
|
|
{
|
|
|
|
UnloadResource();
|
|
|
|
ReloadResource();
|
2007-06-24 18:56:43 +00:00
|
|
|
}
|
2007-09-17 21:22:46 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// This destroys the rendertargets
|
2008-01-02 21:49:43 +00:00
|
|
|
public void DestroyRendertargets()
|
2007-10-23 05:08:02 +00:00
|
|
|
{
|
|
|
|
// Trash rendertargets
|
|
|
|
if(structtex != null) structtex.Dispose();
|
|
|
|
if(thingstex != null) thingstex.Dispose();
|
2007-11-11 13:51:01 +00:00
|
|
|
if(backtex != null) backtex.Dispose();
|
|
|
|
if(screenverts != null) screenverts.Dispose();
|
2007-10-23 05:08:02 +00:00
|
|
|
structtex = null;
|
|
|
|
thingstex = null;
|
2007-11-11 13:51:01 +00:00
|
|
|
backtex = null;
|
|
|
|
screenverts = null;
|
2007-10-23 05:08:02 +00:00
|
|
|
|
|
|
|
// 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;
|
2007-10-23 05:08:02 +00:00
|
|
|
}
|
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
// Allocates new image memory to render on
|
2007-10-23 05:08:02 +00:00
|
|
|
public void CreateRendertargets()
|
2007-06-15 22:38:42 +00:00
|
|
|
{
|
2007-10-19 14:27:46 +00:00
|
|
|
SurfaceDescription sd;
|
2007-11-11 13:51:01 +00:00
|
|
|
DataStream stream;
|
|
|
|
FlatVertex[] verts;
|
2007-10-19 14:27:46 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Destroy rendertargets
|
|
|
|
DestroyRendertargets();
|
2007-09-17 21:22:46 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Get new width and height
|
|
|
|
windowsize.Width = graphics.RenderTarget.ClientSize.Width;
|
|
|
|
windowsize.Height = graphics.RenderTarget.ClientSize.Height;
|
2007-10-19 14:27:46 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Create rendertargets textures
|
2007-10-31 20:34:09 +00:00
|
|
|
structtex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
|
2007-10-23 05:08:02 +00:00
|
|
|
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);
|
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Get the real surface sizes
|
|
|
|
sd = structtex.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;
|
|
|
|
|
|
|
|
// 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();
|
2007-10-23 05:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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].w = 1f;
|
2007-10-22 09:00:54 +00:00
|
|
|
screenverts[0].c = -1;
|
2007-10-23 05:08:02 +00:00
|
|
|
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].w = 1f;
|
2007-10-22 09:00:54 +00:00
|
|
|
screenverts[1].c = -1;
|
2007-10-23 05:08:02 +00:00
|
|
|
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;
|
2007-10-23 05:08:02 +00:00
|
|
|
screenverts[2].y = texturesize.Height - 1.5f;
|
2007-10-22 09:00:54 +00:00
|
|
|
screenverts[2].c = -1;
|
2007-10-21 04:07:36 +00:00
|
|
|
screenverts[2].w = 1f;
|
2007-10-23 05:08:02 +00:00
|
|
|
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;
|
2007-10-21 04:07:36 +00:00
|
|
|
screenverts[3].w = 1f;
|
2007-10-22 09:00:54 +00:00
|
|
|
screenverts[3].c = -1;
|
2007-10-23 05:08:02 +00:00
|
|
|
screenverts[3].u = 1f - 1f / texturesize.Width;
|
|
|
|
screenverts[3].v = 1f - 1f / texturesize.Height;
|
|
|
|
return screenverts;
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
2007-06-24 18:56:43 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
#endregion
|
2007-06-15 22:38:42 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
#region ================== Coordination
|
|
|
|
|
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-07-07 09:40:34 +00:00
|
|
|
|
|
|
|
// Recalculate linedefs (normal lengths must be adjusted)
|
2007-10-05 11:17:58 +00:00
|
|
|
foreach(Linedef l in General.Map.Map.Linedefs) l.NeedUpdate();
|
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;
|
2007-10-23 05:08:02 +00:00
|
|
|
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;
|
2007-10-22 09:00:54 +00:00
|
|
|
vertexsize = (int)(1.7f * scale + 0.5f);
|
|
|
|
if(vertexsize < 0) vertexsize = 0;
|
|
|
|
if(vertexsize > 4) vertexsize = 4;
|
2007-10-20 19:50:03 +00:00
|
|
|
}
|
|
|
|
|
2007-07-07 09:40:34 +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
|
|
|
|
|
2007-10-21 04:07:36 +00:00
|
|
|
#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;
|
|
|
|
|
2007-10-21 14:11:32 +00:00
|
|
|
// Do we need to make changes?
|
2007-10-21 04:07:36 +00:00
|
|
|
if((newnumthings > maxthings) || !preserve)
|
|
|
|
{
|
|
|
|
// Read old things data if we want to keep it
|
|
|
|
if(preserve && (thingsvertices != null) && (numthings > 0))
|
|
|
|
{
|
2007-10-22 09:00:54 +00:00
|
|
|
stream = thingsvertices.Lock(0, numthings * 12 * FlatVertex.Stride, LockFlags.ReadOnly | LockFlags.NoSystemLock);
|
|
|
|
verts = stream.ReadRange<FlatVertex>(numthings * 12);
|
2007-10-21 04:07:36 +00:00
|
|
|
thingsvertices.Unlock();
|
|
|
|
stream.Dispose();
|
|
|
|
}
|
|
|
|
|
2007-10-21 14:11:32 +00:00
|
|
|
// 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
|
2007-10-26 18:04:54 +00:00
|
|
|
thingsvertices = new VertexBuffer(graphics.Device, newmaxthings * 12 * FlatVertex.Stride, Usage.None, VertexFormat.None, Pool.Managed);
|
2007-10-21 14:11:32 +00:00
|
|
|
maxthings = newmaxthings;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Buffer stays the same
|
|
|
|
newmaxthings = maxthings;
|
|
|
|
}
|
2007-10-21 04:07:36 +00:00
|
|
|
|
|
|
|
// Keep old things?
|
|
|
|
if(preserve && (verts != null))
|
|
|
|
{
|
|
|
|
// Write old things into new buffer
|
2007-10-22 09:00:54 +00:00
|
|
|
stream = thingsvertices.Lock(0, maxthings * 12 * FlatVertex.Stride, LockFlags.Discard | LockFlags.NoSystemLock);
|
2007-10-21 04:07:36 +00:00
|
|
|
stream.WriteRange<FlatVertex>(verts);
|
|
|
|
thingsvertices.Unlock();
|
|
|
|
stream.Dispose();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Things were trashed
|
|
|
|
numthings = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This makes vertices for a thing
|
2007-10-22 09:00:54 +00:00
|
|
|
// Returns false when not on the screen
|
|
|
|
private bool CreateThingVerts(Thing t, ref FlatVertex[] verts, int offset, PixelColor c)
|
2007-10-21 04:07:36 +00:00
|
|
|
{
|
2007-10-21 22:41:46 +00:00
|
|
|
float circlesize;
|
|
|
|
float arrowsize;
|
2007-10-22 09:00:54 +00:00
|
|
|
int color;
|
2007-10-21 22:41:46 +00:00
|
|
|
|
2007-10-21 04:07:36 +00:00
|
|
|
// Transform to screen coordinates
|
|
|
|
Vector2D screenpos = ((Vector2D)t.Position).GetTransformed(translatex, translatey, scale, -scale);
|
|
|
|
|
2007-10-21 22:41:46 +00:00
|
|
|
// Determine sizes
|
|
|
|
circlesize = (t.Size - THING_CIRCLE_SHRINK) * scale * THING_CIRCLE_SIZE;
|
|
|
|
arrowsize = (t.Size - THING_ARROW_SHRINK) * scale * THING_ARROW_SIZE;
|
|
|
|
|
2007-10-22 09:00:54 +00:00
|
|
|
// Check if the thing is actually on screen
|
2007-10-23 05:08:02 +00:00
|
|
|
if(((screenpos.x + circlesize) > 0.0f) && ((screenpos.x - circlesize) < (float)windowsize.Width) &&
|
|
|
|
((screenpos.y + circlesize) > 0.0f) && ((screenpos.y - circlesize) < (float)windowsize.Height))
|
2007-10-22 09:00:54 +00:00
|
|
|
{
|
2007-10-23 05:08:02 +00:00
|
|
|
// Get integral color
|
|
|
|
color = c.ToInt();
|
2007-10-22 09:00:54 +00:00
|
|
|
|
|
|
|
// Setup fixed rect for circle
|
|
|
|
verts[offset].x = screenpos.x - circlesize;
|
|
|
|
verts[offset].y = screenpos.y - circlesize;
|
|
|
|
verts[offset].w = 1f;
|
|
|
|
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].w = 1f;
|
|
|
|
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].w = 1f;
|
|
|
|
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].w = 1f;
|
|
|
|
verts[offset].c = color;
|
|
|
|
verts[offset].u = 0.25f - 1f / 512f;
|
|
|
|
verts[offset].v = 1f - 1f / 128f;
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
// 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].w = 1f;
|
|
|
|
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].w = 1f;
|
|
|
|
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].w = 1f;
|
|
|
|
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].w = 1f;
|
|
|
|
verts[offset].c = -1;
|
|
|
|
verts[offset].u = 0.75f + t.IconOffset;
|
|
|
|
verts[offset].v = 1f;
|
|
|
|
|
|
|
|
// Done
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not on screen
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-21 04:07:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// This draws a set of things
|
|
|
|
private void RenderThingsBatch(int offset, int count)
|
|
|
|
{
|
2007-10-26 18:04:54 +00:00
|
|
|
// Anything to render?
|
|
|
|
if(count > 0)
|
|
|
|
{
|
|
|
|
// Set renderstates for things rendering
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
|
|
|
|
graphics.Device.SetTexture(0, thingtexture.Texture);
|
|
|
|
graphics.Shaders.Things2D.Texture1 = thingtexture.Texture;
|
|
|
|
graphics.Device.SetStreamSource(0, thingsvertices, 0, FlatVertex.Stride);
|
|
|
|
|
|
|
|
// Draw the things batched
|
|
|
|
graphics.Shaders.Things2D.Begin();
|
|
|
|
graphics.Shaders.Things2D.BeginPass(0);
|
|
|
|
//try { graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, offset * 12, count * 4); } catch(Exception) { }
|
|
|
|
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, offset * 12, count * 4);
|
|
|
|
graphics.Shaders.Things2D.EndPass();
|
|
|
|
graphics.Shaders.Things2D.End();
|
|
|
|
}
|
2007-10-23 05:08:02 +00:00
|
|
|
}
|
|
|
|
|
2007-10-21 04:07:36 +00:00
|
|
|
#endregion
|
|
|
|
|
2007-10-20 19:50:03 +00:00
|
|
|
#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;
|
2007-10-21 22:41:46 +00:00
|
|
|
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
|
2007-10-21 13:51:08 +00:00
|
|
|
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;
|
2007-10-21 13:51:08 +00:00
|
|
|
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
|
|
|
|
if((l.Flags & General.Map.Config.ImpassableFlags) != 0)
|
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
|
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.WithAlpha(DOUBLESIDED_LINE_ALPHA);
|
2007-10-24 17:25:03 +00:00
|
|
|
else if((l.Flags & General.Map.Config.SoundLinedefFlags) != 0) return General.Colors.Sounds.WithAlpha(DOUBLESIDED_LINE_ALPHA);
|
2007-10-20 19:50:03 +00:00
|
|
|
else return General.Colors.Linedefs.WithAlpha(DOUBLESIDED_LINE_ALPHA);
|
|
|
|
}
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
2007-09-23 22:01:21 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2007-10-21 22:41:46 +00:00
|
|
|
#region ================== Settings
|
|
|
|
|
|
|
|
// This sets the things in front or back
|
|
|
|
public void SetThingsRenderOrder(bool front)
|
|
|
|
{
|
|
|
|
// Set things render order
|
|
|
|
this.thingsfront = front;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2007-11-10 19:24:52 +00:00
|
|
|
|
|
|
|
#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.Width, (float)General.Map.Grid.Background.Height);
|
|
|
|
|
|
|
|
// Only if a background image is set
|
|
|
|
if((General.Map.Grid.Background != null) &&
|
|
|
|
!(General.Map.Grid.Background is NullImage))
|
|
|
|
{
|
|
|
|
// Make vertices
|
2007-11-11 13:51:01 +00:00
|
|
|
backimageverts = CreateScreenVerts(windowsize);
|
2007-11-10 19:24:52 +00:00
|
|
|
|
|
|
|
// 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
|
2007-11-11 13:51:01 +00:00
|
|
|
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;
|
2007-11-10 19:24:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No background image
|
2007-11-11 13:51:01 +00:00
|
|
|
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;
|
|
|
|
LockedRect lockedrect;
|
|
|
|
|
2007-11-11 14:42:04 +00:00
|
|
|
// Do we need to redraw grid?
|
|
|
|
if((lastgridscale != scale) || (lastgridx != offsetx) || (lastgridy != offsety))
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
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);
|
2007-11-11 13:51:01 +00:00
|
|
|
gridplotter.DrawGridLineH((int)pos.y, 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);
|
2007-11-11 13:51:01 +00:00
|
|
|
gridplotter.DrawGridLineV((int)pos.x, c);
|
2007-11-10 19:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2007-10-21 22:41:46 +00:00
|
|
|
#region ================== Rendering
|
2007-09-17 21:22:46 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// This begins a drawing session
|
2007-11-21 12:50:56 +00:00
|
|
|
public unsafe bool Start(bool clearstructs, bool clearthings)
|
2007-10-23 05:08:02 +00:00
|
|
|
{
|
|
|
|
// Rendertargets available?
|
|
|
|
if((structtex != null) && (thingstex != null))
|
|
|
|
{
|
|
|
|
// Lock structures rendertarget memory
|
2007-10-31 20:34:09 +00:00
|
|
|
structlocked = structtex.LockRectangle(0, LockFlags.NoSystemLock);
|
2007-10-23 05:08:02 +00:00
|
|
|
|
|
|
|
// Create structures plotter
|
|
|
|
plotter = new Plotter((PixelColor*)structlocked.Data.DataPointer.ToPointer(), structlocked.Pitch / sizeof(PixelColor), structsize.Height, structsize.Width, structsize.Height);
|
|
|
|
if(clearstructs) plotter.Clear();
|
|
|
|
|
2007-11-10 19:24:52 +00:00
|
|
|
// Redraw grid when structures image was cleared
|
2007-11-11 13:51:01 +00:00
|
|
|
if(clearstructs) RenderBackgroundGrid();
|
2007-11-10 19:24:52 +00:00
|
|
|
|
2007-10-26 18:04:54 +00:00
|
|
|
// Always trash things batch buffer
|
|
|
|
if(thingsvertices != null) thingsvertices.Dispose();
|
|
|
|
thingsvertices = null;
|
|
|
|
numthings = 0;
|
|
|
|
maxthings = 0;
|
|
|
|
|
2007-11-10 19:24:52 +00:00
|
|
|
// Setup vertices for background image
|
|
|
|
SetupBackground();
|
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Set the rendertarget to the things texture
|
|
|
|
thingssurface = thingstex.GetSurfaceLevel(0);
|
2007-10-31 20:34:09 +00:00
|
|
|
if(graphics.StartRendering(clearthings, 0, thingssurface, null))
|
2007-10-23 05:08:02 +00:00
|
|
|
{
|
2007-10-26 18:04:54 +00:00
|
|
|
// Ready for rendering
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't render!
|
2007-11-21 12:50:56 +00:00
|
|
|
Finish();
|
2007-10-26 18:04:54 +00:00
|
|
|
return false;
|
2007-10-23 05:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't render!
|
2007-11-21 12:50:56 +00:00
|
|
|
Finish();
|
2007-10-23 05:08:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This ends a drawing session
|
2007-11-21 12:50:56 +00:00
|
|
|
public void Finish()
|
2007-10-23 05:08:02 +00:00
|
|
|
{
|
2007-10-26 18:04:54 +00:00
|
|
|
// Stop rendering
|
2007-10-31 21:33:06 +00:00
|
|
|
graphics.FinishRendering();
|
2007-10-26 18:04:54 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Release rendertarget
|
2007-10-26 18:04:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
graphics.Device.SetDepthStencilSurface(graphics.DepthBuffer);
|
|
|
|
graphics.Device.SetRenderTarget(0, graphics.BackBuffer);
|
|
|
|
}
|
|
|
|
catch(Exception) { }
|
2007-10-23 05:08:02 +00:00
|
|
|
|
2007-10-26 18:04:54 +00:00
|
|
|
// Clean up
|
|
|
|
if(structtex != null) structtex.UnlockRectangle(0);
|
|
|
|
if(structlocked.Data != null) structlocked.Data.Dispose();
|
|
|
|
if(thingssurface != null) thingssurface.Dispose();
|
|
|
|
thingssurface = null;
|
|
|
|
plotter = null;
|
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Present new image
|
|
|
|
Present();
|
|
|
|
}
|
2008-01-02 21:49:43 +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)
|
|
|
|
{
|
2007-10-22 09:00:54 +00:00
|
|
|
FlatVertex[] verts = new FlatVertex[12];
|
2007-10-21 04:07:36 +00:00
|
|
|
DataStream stream;
|
|
|
|
|
2007-10-22 09:00:54 +00:00
|
|
|
// 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
|
|
|
|
2007-10-22 09:00:54 +00:00
|
|
|
// Store vertices in buffer
|
|
|
|
stream = thingsvertices.Lock(numthings * 12 * FlatVertex.Stride, 12 * FlatVertex.Stride, LockFlags.NoSystemLock);
|
|
|
|
stream.WriteRange<FlatVertex>(verts);
|
|
|
|
thingsvertices.Unlock();
|
|
|
|
stream.Dispose();
|
2007-10-21 04:07:36 +00:00
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Thing added, render it
|
|
|
|
RenderThingsBatch(numthings, 1);
|
2007-10-22 09:00:54 +00:00
|
|
|
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)
|
|
|
|
{
|
2007-10-22 09:00:54 +00:00
|
|
|
FlatVertex[] verts = new FlatVertex[things.Count * 12];
|
2007-10-21 04:07:36 +00:00
|
|
|
DataStream stream;
|
2007-10-23 05:08:02 +00:00
|
|
|
int addcount = 0;
|
2007-10-21 04:07:36 +00:00
|
|
|
|
|
|
|
// Make sure there is enough memory reserved
|
|
|
|
ReserveThingsMemory(numthings + things.Count, true);
|
|
|
|
|
|
|
|
// Go for all things
|
|
|
|
foreach(Thing t in things)
|
|
|
|
{
|
|
|
|
// Create vertices
|
2007-10-23 05:08:02 +00:00
|
|
|
if(CreateThingVerts(t, ref verts, addcount * 12, DetermineThingColor(t)))
|
2007-10-22 09:00:54 +00:00
|
|
|
{
|
|
|
|
// Next
|
2007-10-23 05:08:02 +00:00
|
|
|
addcount++;
|
2007-10-22 09:00:54 +00:00
|
|
|
}
|
2007-10-21 04:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store vertices in buffer
|
2007-10-22 09:00:54 +00:00
|
|
|
stream = thingsvertices.Lock(numthings * 12 * FlatVertex.Stride, things.Count * 12 * FlatVertex.Stride, LockFlags.NoSystemLock);
|
2007-10-21 04:07:36 +00:00
|
|
|
stream.WriteRange<FlatVertex>(verts);
|
|
|
|
thingsvertices.Unlock();
|
|
|
|
stream.Dispose();
|
|
|
|
|
2007-10-23 05:08:02 +00:00
|
|
|
// Things added, render them
|
|
|
|
RenderThingsBatch(numthings, addcount);
|
|
|
|
numthings += addcount;
|
2007-10-21 04:07:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-20 19:50:03 +00:00
|
|
|
// This renders the linedefs of a sector with special color
|
|
|
|
public void RenderSector(Sector s, PixelColor c)
|
2007-09-17 21:22:46 +00:00
|
|
|
{
|
2007-10-20 19:50:03 +00:00
|
|
|
// Go for all sides in the sector
|
|
|
|
foreach(Sidedef sd in s.Sidedefs)
|
2007-09-23 22:01:21 +00:00
|
|
|
{
|
2007-10-20 19:50:03 +00:00
|
|
|
// Render this linedef
|
|
|
|
RenderLinedef(sd.Line, c);
|
|
|
|
|
|
|
|
// Render the two vertices on top
|
|
|
|
RenderVertex(sd.Line.Start, DetermineVertexColor(sd.Line.Start));
|
|
|
|
RenderVertex(sd.Line.End, DetermineVertexColor(sd.Line.End));
|
2007-09-23 22:01:21 +00:00
|
|
|
}
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
|
|
|
|
2007-10-20 19:50:03 +00:00
|
|
|
// This renders the linedefs of a sector
|
|
|
|
public void RenderSector(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-09-17 21:22:46 +00:00
|
|
|
{
|
2007-10-20 19:50:03 +00:00
|
|
|
// Render this linedef
|
|
|
|
RenderLinedef(sd.Line, DetermineLinedefColor(sd.Line));
|
|
|
|
|
|
|
|
// Render the two vertices on top
|
|
|
|
RenderVertex(sd.Line.Start, DetermineVertexColor(sd.Line.Start));
|
|
|
|
RenderVertex(sd.Line.End, DetermineVertexColor(sd.Line.End));
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
2007-10-20 19:50:03 +00:00
|
|
|
}
|
|
|
|
|
2008-01-02 21:49:43 +00:00
|
|
|
// This renders a simple line
|
|
|
|
public void RenderLine(Vector2D start, Vector2D end, PixelColor c)
|
|
|
|
{
|
|
|
|
// Transform coordinates
|
|
|
|
Vector2D v1 = start.GetTransformed(translatex, translatey, scale, -scale);
|
|
|
|
Vector2D v2 = end.GetTransformed(translatex, translatey, scale, -scale);
|
|
|
|
|
|
|
|
// Draw line
|
|
|
|
plotter.DrawLineSolid((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, c);
|
|
|
|
}
|
|
|
|
|
2007-10-20 19:50:03 +00:00
|
|
|
// This renders a single linedef
|
|
|
|
public void RenderLinedef(Linedef l, PixelColor c)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
plotter.DrawLineSolid((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, c);
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
(int)((v1.y + my) + (mx * l.LengthInv) * linenormalsize), c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This renders a set of linedefs
|
2007-10-21 04:07:36 +00:00
|
|
|
public void RenderLinedefSet(ICollection<Linedef> linedefs)
|
2007-10-20 19:50:03 +00:00
|
|
|
{
|
|
|
|
// Go for all linedefs
|
|
|
|
foreach(Linedef l in linedefs) RenderLinedef(l, DetermineLinedefColor(l));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This renders a single vertex
|
2007-10-21 13:51:08 +00:00
|
|
|
public void RenderVertex(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
|
2007-10-22 09:00:54 +00:00
|
|
|
plotter.DrawVertexSolid((int)nv.x, (int)nv.y, vertexsize, General.Colors.Colors[colorindex], General.Colors.BrightColors[colorindex], General.Colors.DarkColors[colorindex]);
|
2007-10-20 19:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This renders a set of vertices
|
2007-10-21 04:07:36 +00:00
|
|
|
public void RenderVerticesSet(ICollection<Vertex> vertices)
|
2007-10-20 19:50:03 +00:00
|
|
|
{
|
|
|
|
// Go for all vertices
|
|
|
|
foreach(Vertex v in vertices) RenderVertex(v, DetermineVertexColor(v));
|
2007-06-15 22:38:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|