2009-02-12 21:34:11 +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 CodeImp.DoomBuilder.IO;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
using System.IO;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using CodeImp.DoomBuilder.Compilers;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.ZDoom
|
|
|
|
{
|
|
|
|
public sealed class PatchStructure
|
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Declaration
|
|
|
|
private string name;
|
|
|
|
private int offsetx;
|
|
|
|
private int offsety;
|
|
|
|
private bool flipx;
|
|
|
|
private bool flipy;
|
2009-02-15 23:58:28 +00:00
|
|
|
private float alpha;
|
2009-02-12 21:34:11 +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; } }
|
2009-02-15 23:58:28 +00:00
|
|
|
public float Alpha { get { return alpha; } }
|
2009-02-12 21:34:11 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
internal PatchStructure(TexturesParser parser)
|
|
|
|
{
|
|
|
|
string tokenstr;
|
|
|
|
|
|
|
|
// Initialize
|
2009-02-15 23:58:28 +00:00
|
|
|
alpha = 1.0f;
|
2009-02-12 21:34:11 +00:00
|
|
|
|
|
|
|
// There should be 3 tokens separated by 2 commas now:
|
|
|
|
// Name, Width, Height
|
|
|
|
|
|
|
|
// First token is the class name
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
name = parser.ReadToken();
|
|
|
|
if(string.IsNullOrEmpty(name))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected patch name");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we should find a comma
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(tokenstr != ",")
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected a comma");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next is the patch width
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, out offsetx))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected offset in pixels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we should find a comma again
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(tokenstr != ",")
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected a comma");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next is the patch height
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, out offsety))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected offset in pixels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next token should be the beginning of the texture scope
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(tokenstr != "{")
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected begin of structure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now parse the contents of texture structure
|
|
|
|
while(parser.SkipWhitespace(true))
|
|
|
|
{
|
|
|
|
string token = parser.ReadToken();
|
|
|
|
token = token.ToLowerInvariant();
|
|
|
|
if(token == "flipx")
|
|
|
|
{
|
|
|
|
flipx = true;
|
|
|
|
}
|
|
|
|
else if(token == "flipy")
|
|
|
|
{
|
|
|
|
flipy = true;
|
|
|
|
}
|
2009-02-15 23:58:28 +00:00
|
|
|
else if(token == "alpha")
|
|
|
|
{
|
|
|
|
if(!ReadTokenFloat(parser, token, out alpha)) return;
|
|
|
|
alpha = General.Clamp(alpha, 0.0f, 1.0f);
|
|
|
|
}
|
2009-02-12 21:34:11 +00:00
|
|
|
else if(token == "}")
|
|
|
|
{
|
|
|
|
// Patch scope ends here,
|
|
|
|
// break out of this parse loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2009-02-15 23:58:28 +00:00
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This reads the next token and sets a floating point value, returns false when failed
|
|
|
|
private bool ReadTokenFloat(TexturesParser parser, string propertyname, out float value)
|
|
|
|
{
|
|
|
|
// 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, out value))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected numeric value for property '" + propertyname + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't find the property value!
|
|
|
|
parser.ReportError("Expected a value for property '" + propertyname + "'");
|
|
|
|
value = 0.0f;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This reads the next token and sets an integral value, returns false when failed
|
|
|
|
private bool ReadTokenInt(TexturesParser parser, string propertyname, out int value)
|
|
|
|
{
|
|
|
|
// 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, out value))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected integral value for property '" + propertyname + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't find the property value!
|
|
|
|
parser.ReportError("Expected a value for property '" + propertyname + "'");
|
|
|
|
value = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2009-02-12 21:34:11 +00:00
|
|
|
}
|
|
|
|
}
|