UltimateZoneBuilder/Source/Rendering/Renderer2D.cs

192 lines
4.2 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 Microsoft.DirectX.Direct3D;
using System.ComponentModel;
2007-06-15 22:38:42 +00:00
using CodeImp.DoomBuilder.Map;
using Microsoft.DirectX;
2007-06-15 18:50:18 +00:00
#endregion
namespace CodeImp.DoomBuilder.Rendering
{
internal class Renderer2D : IDisposable
{
#region ================== Constants
#endregion
#region ================== Variables
2007-06-15 22:38:42 +00:00
// Owner
private Graphics graphics;
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
// Disposing
private bool isdisposed = false;
#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
public bool IsDisposed { get { return isdisposed; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
2007-06-15 22:38:42 +00:00
public Renderer2D(Graphics graphics)
2007-06-15 18:50:18 +00:00
{
// Initialize
2007-06-15 22:38:42 +00:00
this.graphics = graphics;
2007-06-15 18:50:18 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Diposer
public void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
2007-06-15 22:38:42 +00:00
2007-06-15 18:50:18 +00:00
// Done
isdisposed = true;
}
}
#endregion
#region ================== Methods
2007-06-24 18:56:43 +00:00
// This rebuilds matrices according to view settings
private void SetupMatrices()
{
Matrix proj;
float width, height, left, top, right, bottom;
// Build projection matrix
width = (float)graphics.RenderTarget.ClientSize.Width / scale;
height = (float)graphics.RenderTarget.ClientSize.Height / scale;
left = offsetx - width * 0.5f;
top = offsety - height * 0.5f;
right = offsetx + width * 0.5f;
bottom = offsety + height * 0.5f;
proj = Matrix.OrthoOffCenterLH(left, right, top, bottom, -1f, 1f);
// Apply matrices
graphics.Device.Transform.Projection = proj;
graphics.Device.Transform.View = Matrix.Identity;
graphics.Device.Transform.World = Matrix.Identity;
}
2007-06-15 22:38:42 +00:00
// This begins a drawing session
public bool StartRendering()
{
2007-06-24 18:56:43 +00:00
// Can we render?
if(graphics.StartRendering())
2007-06-15 22:38:42 +00:00
{
2007-06-24 18:56:43 +00:00
// Setup matrices
SetupMatrices();
// Success
2007-06-15 22:38:42 +00:00
return true;
}
else
{
2007-06-24 18:56:43 +00:00
// Cannot render
2007-06-15 22:38:42 +00:00
return false;
}
}
// This ends a drawing session
public void FinishRendering()
{
2007-06-24 18:56:43 +00:00
// Finish and present our drawing
graphics.FinishRendering();
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-06-15 22:38:42 +00:00
// This renders a set of Linedefs
2007-06-24 18:56:43 +00:00
public void RenderLinedefs(ICollection<Linedef> linedefs)
2007-06-15 22:38:42 +00:00
{
2007-06-24 18:56:43 +00:00
PTVertex[] verts = new PTVertex[linedefs.Count * 4];
int i = 0;
2007-06-15 22:38:42 +00:00
2007-06-26 06:01:11 +00:00
// Any linedefs?
if(linedefs.Count > 0)
2007-06-15 22:38:42 +00:00
{
2007-06-26 06:01:11 +00:00
graphics.Device.RenderState.TextureFactor = -1;
// Go for all linedefs
foreach(Linedef l in linedefs)
{
// Make vertices
verts[i++] = l.LineVertices[0];
verts[i++] = l.LineVertices[1];
verts[i++] = l.LineVertices[2];
verts[i++] = l.LineVertices[3];
}
// Draw lines
graphics.Device.DrawUserPrimitives(PrimitiveType.LineList, linedefs.Count * 2, verts);
2007-06-15 22:38:42 +00:00
}
2007-06-24 18:56:43 +00:00
}
2007-06-15 22:38:42 +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-06-15 22:38:42 +00:00
}
2007-06-15 18:50:18 +00:00
#endregion
}
}