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-06-15 18:50:18 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Rendering
|
|
|
|
{
|
2007-09-24 19:54:47 +00:00
|
|
|
internal unsafe class Renderer2D : Renderer
|
2007-06-15 18:50:18 +00:00
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
// Rendering memory
|
2007-10-19 14:27:46 +00:00
|
|
|
private Texture tex;
|
|
|
|
private int width, height;
|
|
|
|
private int pwidth, pheight;
|
|
|
|
private Plotter plotter;
|
|
|
|
|
2007-06-24 18:56:43 +00:00
|
|
|
// View settings (world coordinates)
|
|
|
|
private float scale;
|
|
|
|
private float offsetx;
|
|
|
|
private float offsety;
|
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
|
2007-10-19 14:27:46 +00:00
|
|
|
public Renderer2D(D3DGraphics graphics) : base(graphics)
|
2007-06-15 18:50:18 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
2007-09-17 21:22:46 +00:00
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diposer
|
2007-09-24 19:54:47 +00:00
|
|
|
public override void Dispose()
|
2007-06-15 18:50:18 +00:00
|
|
|
{
|
|
|
|
// Not already disposed?
|
|
|
|
if(!isdisposed)
|
|
|
|
{
|
|
|
|
// Clean up
|
2007-10-19 14:27:46 +00:00
|
|
|
if(tex != null) tex.Dispose();
|
|
|
|
tex = null;
|
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-07-07 09:40:34 +00:00
|
|
|
#region ================== Control
|
2007-06-24 18:56:43 +00:00
|
|
|
|
2007-10-19 14:27:46 +00:00
|
|
|
// This draws the image on screen
|
|
|
|
public void Present()
|
|
|
|
{
|
|
|
|
FlatVertex[] verts = new FlatVertex[4];
|
|
|
|
|
|
|
|
// Start drawing
|
|
|
|
if(graphics.StartRendering())
|
|
|
|
{
|
|
|
|
graphics.Device.VertexFormat = FlatVertex.Format;
|
|
|
|
|
|
|
|
// Left top
|
|
|
|
verts[0].x = -0.5f;
|
|
|
|
verts[0].y = -0.5f;
|
|
|
|
verts[0].w = 1f;
|
|
|
|
verts[0].u = 0f;
|
|
|
|
verts[0].v = 0f;
|
|
|
|
|
|
|
|
// Right top
|
|
|
|
verts[1].x = pwidth - 0.5f;
|
|
|
|
verts[1].y = -0.5f;
|
|
|
|
verts[1].w = 1f;
|
|
|
|
verts[1].u = 1f;
|
|
|
|
verts[1].v = 0f;
|
|
|
|
|
|
|
|
// Left bottom
|
|
|
|
verts[2].x = -0.5f;
|
|
|
|
verts[2].y = pheight - 0.5f;
|
|
|
|
verts[2].w = 1f;
|
|
|
|
verts[2].u = 0f;
|
|
|
|
verts[2].v = 1f;
|
|
|
|
|
|
|
|
// Right bottom
|
|
|
|
verts[3].x = pwidth - 0.5f;
|
|
|
|
verts[3].y = pheight - 0.5f;
|
|
|
|
verts[3].w = 1f;
|
|
|
|
verts[3].u = 1f;
|
|
|
|
verts[3].v = 1f;
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
graphics.Device.SetTexture(0, tex);
|
|
|
|
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
|
|
|
|
try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, verts); }
|
|
|
|
catch(Exception) { }
|
|
|
|
|
|
|
|
// Done
|
|
|
|
graphics.FinishRendering();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called before a device is reset
|
|
|
|
// (when resized or display adapter was changed)
|
|
|
|
public override void UnloadResource()
|
|
|
|
{
|
|
|
|
// Trash old texture
|
|
|
|
if(tex != null) tex.Dispose();
|
|
|
|
tex = null;
|
|
|
|
}
|
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
// This is called resets when the device is reset
|
|
|
|
// (when resized or display adapter was changed)
|
2007-10-19 14:27:46 +00:00
|
|
|
public override void ReloadResource()
|
2007-06-24 18:56:43 +00:00
|
|
|
{
|
2007-10-19 14:27:46 +00:00
|
|
|
// Re-create texture
|
|
|
|
CreateTexture();
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Allocates new image memory to render on
|
2007-10-19 14:27:46 +00:00
|
|
|
public void CreateTexture()
|
2007-06-15 22:38:42 +00:00
|
|
|
{
|
2007-10-19 14:27:46 +00:00
|
|
|
SurfaceDescription sd;
|
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
// Get new width and height
|
|
|
|
width = graphics.RenderTarget.ClientSize.Width;
|
|
|
|
height = graphics.RenderTarget.ClientSize.Height;
|
|
|
|
|
2007-10-19 14:27:46 +00:00
|
|
|
// Trash old texture
|
|
|
|
if(tex != null) tex.Dispose();
|
|
|
|
tex = null;
|
2007-09-17 21:22:46 +00:00
|
|
|
|
2007-10-19 14:27:46 +00:00
|
|
|
// Create new texture
|
|
|
|
tex = new Texture(graphics.Device, width, height, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);
|
|
|
|
|
|
|
|
// Get the real surface size
|
|
|
|
sd = tex.GetLevelDescription(0);
|
|
|
|
pwidth = sd.Width;
|
|
|
|
pheight = sd.Height;
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
2007-06-24 18:56:43 +00:00
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
// This begins a drawing session
|
|
|
|
public unsafe bool StartRendering()
|
|
|
|
{
|
2007-10-19 14:27:46 +00:00
|
|
|
LockedRect rect;
|
2007-09-17 21:22:46 +00:00
|
|
|
|
2007-10-19 14:27:46 +00:00
|
|
|
// Do we have a texture?
|
|
|
|
if(tex != null)
|
|
|
|
{
|
|
|
|
// Lock memory
|
|
|
|
rect = tex.LockRectangle(0, LockFlags.Discard);
|
|
|
|
|
|
|
|
// Create plotter
|
|
|
|
plotter = new Plotter((PixelColor*)rect.Data.DataPointer.ToPointer(), rect.Pitch / sizeof(PixelColor), pheight, width, height);
|
|
|
|
plotter.Clear();
|
|
|
|
|
|
|
|
// Ready for rendering
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't render!
|
|
|
|
return false;
|
|
|
|
}
|
2007-06-15 22:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This ends a drawing session
|
|
|
|
public void FinishRendering()
|
|
|
|
{
|
2007-09-17 21:22:46 +00:00
|
|
|
// Unlock memory
|
2007-10-19 14:27:46 +00:00
|
|
|
tex.UnlockRectangle(0);
|
|
|
|
|
|
|
|
// Present new image
|
|
|
|
Present();
|
2007-06-15 22:38:42 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This changes zoom
|
|
|
|
public void ScaleView(float scale)
|
|
|
|
{
|
|
|
|
// Change zoom scale
|
|
|
|
this.scale = scale;
|
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-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-19 14:27:46 +00:00
|
|
|
float ox = -offsetx + (width * 0.5f) / scale;
|
|
|
|
float oy = -offsety - (height * 0.5f) / scale;
|
|
|
|
return mousepos.GetTransformed(ox, oy, scale, -scale);
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
2007-09-23 22:01:21 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
#region ================== Map Rendering
|
|
|
|
|
|
|
|
// This renders a set of Linedefs
|
2007-10-19 14:27:46 +00:00
|
|
|
public void RenderLinedefs(MapSet map, ICollection<Linedef> linedefs)
|
2007-09-17 21:22:46 +00:00
|
|
|
{
|
2007-10-19 14:27:46 +00:00
|
|
|
float ox = -offsetx + (width * 0.5f) / scale;
|
|
|
|
float oy = -offsety - (height * 0.5f) / scale;
|
|
|
|
PixelColor c = PixelColor.FromColor(Color.White);
|
2007-09-23 22:01:21 +00:00
|
|
|
Vector2D v1, v2;
|
2007-10-19 14:27:46 +00:00
|
|
|
|
2007-09-23 22:01:21 +00:00
|
|
|
// Go for all linedefs
|
|
|
|
foreach(Linedef l in linedefs)
|
|
|
|
{
|
|
|
|
// Transform vertex coordinates
|
2007-10-19 14:27:46 +00:00
|
|
|
v1 = l.Start.Position.GetTransformed(ox, oy, scale, -scale);
|
|
|
|
v2 = l.End.Position.GetTransformed(ox, oy, scale, -scale);
|
|
|
|
|
2007-09-23 22:01:21 +00:00
|
|
|
// Draw line
|
2007-10-19 14:27:46 +00:00
|
|
|
plotter.DrawLineSolid((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, c);
|
2007-09-23 22:01:21 +00:00
|
|
|
}
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 18:56:43 +00:00
|
|
|
// This renders a set of Linedefs
|
|
|
|
public void RenderVertices(MapSet map, ICollection<Vertex> vertices)
|
|
|
|
{
|
2007-09-17 21:22:46 +00:00
|
|
|
Vector2D nv;
|
2007-10-19 14:27:46 +00:00
|
|
|
float ox = -offsetx + (width * 0.5f) / scale;
|
|
|
|
float oy = -offsety - (height * 0.5f) / scale;
|
|
|
|
PixelColor c = PixelColor.FromColor(Color.DeepSkyBlue);
|
2007-09-17 21:22:46 +00:00
|
|
|
int x, y;
|
|
|
|
|
|
|
|
// Go for all vertices
|
|
|
|
foreach(Vertex v in vertices)
|
|
|
|
{
|
|
|
|
// Transform vertex coordinates
|
2007-10-19 14:27:46 +00:00
|
|
|
nv = v.Position.GetTransformed(ox, oy, scale, -scale);
|
2007-09-17 21:22:46 +00:00
|
|
|
x = (int)nv.x;
|
|
|
|
y = (int)nv.y;
|
|
|
|
|
|
|
|
// Draw pixel here
|
2007-10-19 14:27:46 +00:00
|
|
|
plotter.DrawVertexSolid(x, y, 2, c);
|
2007-09-17 21:22:46 +00:00
|
|
|
}
|
2007-06-15 22:38:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|