2009-04-19 18:07:22 +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.Globalization;
|
|
|
|
using System.IO;
|
2016-03-16 23:26:53 +00:00
|
|
|
using CodeImp.DoomBuilder.Data;
|
2013-03-18 13:52:27 +00:00
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.ZDoom
|
|
|
|
{
|
|
|
|
public sealed class PatchStructure
|
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
2016-02-15 14:06:46 +00:00
|
|
|
// Some odd things in ZDoom
|
|
|
|
private const string IGNORE_SPRITE = "TNT1A0";
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Declaration
|
2016-02-15 14:06:46 +00:00
|
|
|
private readonly string name;
|
|
|
|
private readonly int offsetx;
|
|
|
|
private readonly int offsety;
|
|
|
|
private readonly bool flipx;
|
|
|
|
private readonly bool flipy;
|
|
|
|
private readonly float alpha;
|
|
|
|
private readonly int rotation; //mxd
|
2016-03-16 23:26:53 +00:00
|
|
|
private readonly TexturePathRenderStyle renderstyle; //mxd
|
|
|
|
private readonly PixelColor blendcolor; //mxd
|
|
|
|
private readonly TexturePathBlendStyle blendstyle; //mxd
|
2016-02-15 14:06:46 +00:00
|
|
|
private static readonly string[] renderStyles = { "copy", "translucent", "add", "subtract", "reversesubtract", "modulate", "copyalpha", "copynewalpha", "overlay" }; //mxd
|
|
|
|
private readonly bool skip; //mxd
|
2013-03-18 13:52:27 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
public string Name { get { return name; } }
|
|
|
|
public int OffsetX { get { return offsetx; } }
|
|
|
|
public int OffsetY { get { return offsety; } }
|
|
|
|
public bool FlipX { get { return flipx; } }
|
|
|
|
public bool FlipY { get { return flipy; } }
|
|
|
|
public float Alpha { get { return alpha; } }
|
2013-03-18 13:52:27 +00:00
|
|
|
public int Rotation { get { return rotation; } } //mxd
|
2016-03-16 23:26:53 +00:00
|
|
|
public TexturePathRenderStyle RenderStyle { get { return renderstyle; } } //mxd
|
|
|
|
public TexturePathBlendStyle BlendStyle { get { return blendstyle; } }
|
|
|
|
public PixelColor BlendColor { get { return blendcolor; } }//mxd
|
2016-02-15 14:06:46 +00:00
|
|
|
public bool Skip { get { return skip; } } //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
internal PatchStructure(TexturesParser parser)
|
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
alpha = 1.0f;
|
2016-03-16 23:26:53 +00:00
|
|
|
renderstyle = TexturePathRenderStyle.COPY;//mxd
|
|
|
|
blendstyle = TexturePathBlendStyle.NONE; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// There should be 3 tokens separated by 2 commas now:
|
|
|
|
// Name, Width, Height
|
|
|
|
|
|
|
|
// First token is the class name
|
|
|
|
parser.SkipWhitespace(true);
|
2016-11-09 10:07:21 +00:00
|
|
|
if(!parser.ReadTextureName(out name, "patch")) return; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
if(string.IsNullOrEmpty(name))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected patch name");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-15 14:06:46 +00:00
|
|
|
//mxd. Skip what must be skipped
|
|
|
|
skip = (name.ToUpperInvariant() == IGNORE_SPRITE);
|
|
|
|
|
2014-11-25 11:52:01 +00:00
|
|
|
//mxd
|
|
|
|
name = name.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Now we should find a comma
|
2015-12-21 14:17:47 +00:00
|
|
|
if(!parser.NextTokenIs(",")) return; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Next is the patch width
|
|
|
|
parser.SkipWhitespace(true);
|
2015-12-21 14:17:47 +00:00
|
|
|
string tokenstr = parser.ReadToken();
|
2009-04-19 18:07:22 +00:00
|
|
|
if(string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, NumberStyles.Integer, CultureInfo.InvariantCulture, out offsetx))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected offset in pixels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we should find a comma again
|
2015-12-21 14:17:47 +00:00
|
|
|
if(!parser.NextTokenIs(",")) return; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Next is the patch height
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, NumberStyles.Integer, CultureInfo.InvariantCulture, out offsety))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected offset in pixels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-21 14:17:47 +00:00
|
|
|
// Next token is the beginning of the texture scope. If not, then the patch info ends here.
|
|
|
|
if(!parser.NextTokenIs("{", false)) return; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Now parse the contents of texture structure
|
2013-12-18 09:11:04 +00:00
|
|
|
bool done = false; //mxd
|
|
|
|
while(!done && parser.SkipWhitespace(true))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
string token = parser.ReadToken();
|
|
|
|
token = token.ToLowerInvariant();
|
2013-12-18 09:11:04 +00:00
|
|
|
|
2015-12-28 15:01:53 +00:00
|
|
|
switch(token)
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2013-12-18 09:11:04 +00:00
|
|
|
case "flipx":
|
|
|
|
flipx = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "flipy":
|
|
|
|
flipy = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "alpha":
|
2015-12-28 15:01:53 +00:00
|
|
|
if(!ReadTokenFloat(parser, token, out alpha)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
alpha = General.Clamp(alpha, 0.0f, 1.0f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "rotate":
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenInt(parser, token, out rotation)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
rotation = rotation % 360; //Coalesce multiples
|
2015-12-17 10:07:28 +00:00
|
|
|
if(rotation < 0) rotation += 360; //Force positive
|
2013-12-18 09:11:04 +00:00
|
|
|
|
2015-12-17 10:07:28 +00:00
|
|
|
if(rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270)
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2016-03-08 20:41:06 +00:00
|
|
|
parser.LogWarning("Unsupported rotation (" + rotation + ") in patch \"" + name + "\"");
|
2013-12-18 09:11:04 +00:00
|
|
|
rotation = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "style": //mxd
|
2014-02-21 14:42:12 +00:00
|
|
|
string s;
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenString(parser, token, out s)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
int index = Array.IndexOf(renderStyles, s.ToLowerInvariant());
|
2016-03-16 23:26:53 +00:00
|
|
|
renderstyle = index == -1 ? TexturePathRenderStyle.COPY : (TexturePathRenderStyle) index;
|
2013-12-18 09:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "blend": //mxd
|
2013-03-18 13:52:27 +00:00
|
|
|
parser.SkipWhitespace(false);
|
2016-03-16 23:26:53 +00:00
|
|
|
PixelColor color = new PixelColor();
|
|
|
|
|
|
|
|
// Blend <string color>[,<float alpha>] block?
|
|
|
|
token = parser.ReadToken(false);
|
|
|
|
if(!parser.ReadByte(token, ref color.r))
|
|
|
|
{
|
|
|
|
if(!ZDTextParser.GetColorFromString(token, ref color))
|
|
|
|
{
|
|
|
|
parser.ReportError("Unsupported patch blend definition");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// That's Blend <int r>,<int g>,<int b>[,<float alpha>] block
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!parser.SkipWhitespace(false) ||
|
|
|
|
!parser.NextTokenIs(",", false) || !parser.SkipWhitespace(false) || !parser.ReadByte(ref color.g) ||
|
|
|
|
!parser.NextTokenIs(",", false) || !parser.SkipWhitespace(false) || !parser.ReadByte(ref color.b))
|
|
|
|
{
|
|
|
|
parser.ReportError("Unsupported patch blend definition");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
|
2016-03-16 23:26:53 +00:00
|
|
|
// Alpha block?
|
|
|
|
float blendalpha = -1f;
|
|
|
|
parser.SkipWhitespace(false);
|
|
|
|
if(parser.NextTokenIs(",", false))
|
|
|
|
{
|
2013-12-18 09:11:04 +00:00
|
|
|
parser.SkipWhitespace(false);
|
2016-03-16 23:26:53 +00:00
|
|
|
if(!ReadTokenFloat(parser, token, out blendalpha))
|
|
|
|
{
|
|
|
|
parser.ReportError("Unsupported patch blend alpha value");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blend may never be 0 when using the Tint effect
|
|
|
|
if(blendalpha > 0.0f)
|
|
|
|
{
|
|
|
|
color.a = (byte)General.Clamp((int)(blendalpha * 255), 1, 254);
|
|
|
|
blendstyle = TexturePathBlendStyle.TINT;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(blendalpha < 0.0f)
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2016-03-16 23:26:53 +00:00
|
|
|
color.a = 255;
|
|
|
|
blendstyle = TexturePathBlendStyle.BLEND;
|
2013-12-18 09:11:04 +00:00
|
|
|
}
|
2016-03-16 23:26:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ignore Blend when alpha == 0
|
|
|
|
parser.LogWarning("Blend with zero alpha will be ignored by ZDoom");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the color
|
|
|
|
blendcolor = color;
|
2013-12-18 09:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "}":
|
|
|
|
// Patch scope ends here,
|
|
|
|
// break out of this parse loop
|
|
|
|
done = true;
|
|
|
|
break;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This reads the next token and sets a floating point value, returns false when failed
|
2014-03-05 09:21:28 +00:00
|
|
|
private static bool ReadTokenFloat(TexturesParser parser, string propertyname, out float value)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Next token is the property value to set
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
string strvalue = parser.ReadToken();
|
|
|
|
if(!string.IsNullOrEmpty(strvalue))
|
|
|
|
{
|
|
|
|
// Try parsing as value
|
|
|
|
if(!float.TryParse(strvalue, NumberStyles.Float, CultureInfo.InvariantCulture, out value))
|
|
|
|
{
|
2016-03-08 20:41:06 +00:00
|
|
|
parser.ReportError("Expected numeric value for property \"" + propertyname + "\"");
|
2009-04-19 18:07:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
// Success
|
|
|
|
return true;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Can't find the property value!
|
2016-03-08 20:41:06 +00:00
|
|
|
parser.ReportError("Expected a value for property \"" + propertyname + "\"");
|
2013-12-18 09:11:04 +00:00
|
|
|
value = 0.0f;
|
|
|
|
return false;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This reads the next token and sets an integral value, returns false when failed
|
2014-03-05 09:21:28 +00:00
|
|
|
private static bool ReadTokenInt(TexturesParser parser, string propertyname, out int value)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Next token is the property value to set
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
string strvalue = parser.ReadToken();
|
|
|
|
if(!string.IsNullOrEmpty(strvalue))
|
|
|
|
{
|
|
|
|
// Try parsing as value
|
|
|
|
if(!int.TryParse(strvalue, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
|
|
|
|
{
|
2016-03-08 20:41:06 +00:00
|
|
|
parser.ReportError("Expected integral value for property \"" + propertyname + "\"");
|
2009-04-19 18:07:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Success
|
|
|
|
return true;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Can't find the property value!
|
2016-03-08 20:41:06 +00:00
|
|
|
parser.ReportError("Expected a value for property \"" + propertyname + "\"");
|
2013-12-18 09:11:04 +00:00
|
|
|
value = 0;
|
|
|
|
return false;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
//mxd. This reads the next token and sets a string value, returns false when failed
|
2014-12-03 23:15:26 +00:00
|
|
|
private static bool ReadTokenString(TexturesParser parser, string propertyname, out string value)
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
value = parser.StripTokenQuotes(parser.ReadToken());
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
if(string.IsNullOrEmpty(value))
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
// Can't find the property value!
|
2016-03-08 20:41:06 +00:00
|
|
|
parser.ReportError("Expected a value for property \"" + propertyname + "\"");
|
2013-03-18 13:52:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-17 10:07:28 +00:00
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|