| 1 |
/****************************************************************************** |
|---|
| 2 |
x42view.net - .x42 viewer and auditing tool |
|---|
| 3 |
Copyright (C) 2007 HermitWorks Entertainment Corporation |
|---|
| 4 |
|
|---|
| 5 |
This program is free software; you can redistribute it and/or modify it |
|---|
| 6 |
under the terms of the GNU General Public License as published by the Free |
|---|
| 7 |
Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 8 |
any later version. |
|---|
| 9 |
|
|---|
| 10 |
This program is distributed in the hope that it will be useful, but |
|---|
| 11 |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 12 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|---|
| 13 |
for more details. |
|---|
| 14 |
|
|---|
| 15 |
You should have received a copy of the GNU General Public License along |
|---|
| 16 |
with this program; if not, write to the |
|---|
| 17 |
|
|---|
| 18 |
Free Software Foundation, Inc. |
|---|
| 19 |
51 Franklin Street, Fifth Floor |
|---|
| 20 |
Boston, MA 02110-1301, USA. |
|---|
| 21 |
******************************************************************************/ |
|---|
| 22 |
|
|---|
| 23 |
using System; |
|---|
| 24 |
using System.Drawing; |
|---|
| 25 |
|
|---|
| 26 |
namespace x42view |
|---|
| 27 |
{ |
|---|
| 28 |
internal static partial class Helpers |
|---|
| 29 |
{ |
|---|
| 30 |
public static void DisposeAndClear<T>( ref T obj ) |
|---|
| 31 |
{ |
|---|
| 32 |
IDisposable idisp = obj as IDisposable; |
|---|
| 33 |
if( idisp != null ) |
|---|
| 34 |
idisp.Dispose(); |
|---|
| 35 |
|
|---|
| 36 |
obj = default( T ); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
public static unsafe void ZeroMemory( IntPtr mem, long size ) |
|---|
| 40 |
{ |
|---|
| 41 |
long intBlocks = size / sizeof( IntPtr ); |
|---|
| 42 |
long remBytes = size - intBlocks * sizeof( IntPtr ); |
|---|
| 43 |
|
|---|
| 44 |
IntPtr *pImem = (IntPtr*)mem; |
|---|
| 45 |
while( intBlocks-- != 0 ) |
|---|
| 46 |
*pImem++ = IntPtr.Zero; |
|---|
| 47 |
|
|---|
| 48 |
byte* pBmem = (byte*)pImem; |
|---|
| 49 |
while( remBytes-- != 0 ) |
|---|
| 50 |
*pBmem++ = 0; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
public static float Clamp( float value, float min, float max ) |
|---|
| 54 |
{ |
|---|
| 55 |
if( value < min ) |
|---|
| 56 |
return min; |
|---|
| 57 |
|
|---|
| 58 |
if( value > max ) |
|---|
| 59 |
return max; |
|---|
| 60 |
|
|---|
| 61 |
return value; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
public static int Clamp( int value, int min, int max ) |
|---|
| 65 |
{ |
|---|
| 66 |
if( value < min ) |
|---|
| 67 |
return min; |
|---|
| 68 |
|
|---|
| 69 |
if( value > max ) |
|---|
| 70 |
return max; |
|---|
| 71 |
|
|---|
| 72 |
return value; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
public static float Saturate( float value ) |
|---|
| 76 |
{ |
|---|
| 77 |
return Clamp( value, 0, 1 ); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
public static float Max( float a, float b ) |
|---|
| 81 |
{ |
|---|
| 82 |
return Math.Max( a, b ); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
public static float Max( float a, float b, float c ) |
|---|
| 86 |
{ |
|---|
| 87 |
return Math.Max( a, Math.Max( b, c ) ); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
public static float Max( float a, float b, float c, float d ) |
|---|
| 91 |
{ |
|---|
| 92 |
return Math.Max( a, Math.Max( b, Math.Max( c, d ) ) ); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
public static float Max( params float[] values ) |
|---|
| 96 |
{ |
|---|
| 97 |
if( values == null || values.Length == 0 ) |
|---|
| 98 |
throw new ArgumentException(); |
|---|
| 99 |
|
|---|
| 100 |
float v = values[0]; |
|---|
| 101 |
for( int i = 1; i < values.Length; i++ ) |
|---|
| 102 |
v = Math.Max( v, values[i] ); |
|---|
| 103 |
|
|---|
| 104 |
return v; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
public static Color Lerp( Color a, Color b, float t ) |
|---|
| 108 |
{ |
|---|
| 109 |
float it = 1.0F - t; |
|---|
| 110 |
return Color.FromArgb( |
|---|
| 111 |
(int)((float)a.A * it + (float)b.A * t), |
|---|
| 112 |
(int)((float)a.R * it + (float)b.R * t), |
|---|
| 113 |
(int)((float)a.G * it + (float)b.G * t), |
|---|
| 114 |
(int)((float)a.B * it + (float)b.B * t) ); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
public static Color Lerp( Color a, Color b, float t, int alpha ) |
|---|
| 118 |
{ |
|---|
| 119 |
float it = 1.0F - t; |
|---|
| 120 |
return Color.FromArgb( |
|---|
| 121 |
alpha, |
|---|
| 122 |
(int)((float)a.R * it + (float)b.R * t), |
|---|
| 123 |
(int)((float)a.G * it + (float)b.G * t), |
|---|
| 124 |
(int)((float)a.B * it + (float)b.B * t) ); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
public static void Swap<T>( ref T a, ref T b ) |
|---|
| 128 |
{ |
|---|
| 129 |
T tmp = a; |
|---|
| 130 |
a = b; |
|---|
| 131 |
b = tmp; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
public static Color ScaleColor( Color color, float factor ) |
|---|
| 135 |
{ |
|---|
| 136 |
int r = Clamp( (int)(color.R * factor), 0, 0xFF ); |
|---|
| 137 |
int g = Clamp( (int)(color.G * factor), 0, 0xFF ); |
|---|
| 138 |
int b = Clamp( (int)(color.B * factor), 0, 0xFF ); |
|---|
| 139 |
|
|---|
| 140 |
return Color.FromArgb( r, g, b ); |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
public static Microsoft.DirectX.Vector3 ColorToVector3( Color cl ) |
|---|
| 144 |
{ |
|---|
| 145 |
const float conv = 1.0F / 0xFF; |
|---|
| 146 |
return new Microsoft.DirectX.Vector3( cl.R * conv, cl.G * conv, cl.B * conv ); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
public static IntPtr OffsetIntPtr( IntPtr ptr, long offset ) |
|---|
| 150 |
{ |
|---|
| 151 |
return new IntPtr( ptr.ToInt64() + offset ); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
public static Microsoft.DirectX.Vector3 ConvertToDX( Libx42.Math.Vector3 v ) |
|---|
| 155 |
{ |
|---|
| 156 |
return new Microsoft.DirectX.Vector3( v.x, v.y, v.z ); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
public static Microsoft.DirectX.Matrix ConvertToDX( Libx42.Math.Affine a ) |
|---|
| 160 |
{ |
|---|
| 161 |
Microsoft.DirectX.Matrix ret = new Microsoft.DirectX.Matrix(); |
|---|
| 162 |
|
|---|
| 163 |
ret.M11 = a.c00; |
|---|
| 164 |
ret.M12 = a.c01; |
|---|
| 165 |
ret.M13 = a.c02; |
|---|
| 166 |
ret.M14 = 0; |
|---|
| 167 |
|
|---|
| 168 |
ret.M21 = a.c10; |
|---|
| 169 |
ret.M22 = a.c11; |
|---|
| 170 |
ret.M23 = a.c12; |
|---|
| 171 |
ret.M24 = 0; |
|---|
| 172 |
|
|---|
| 173 |
ret.M31 = a.c20; |
|---|
| 174 |
ret.M32 = a.c21; |
|---|
| 175 |
ret.M33 = a.c22; |
|---|
| 176 |
ret.M34 = 0; |
|---|
| 177 |
|
|---|
| 178 |
ret.M41 = a.c30; |
|---|
| 179 |
ret.M42 = a.c31; |
|---|
| 180 |
ret.M43 = a.c32; |
|---|
| 181 |
ret.M44 = 1; |
|---|
| 182 |
|
|---|
| 183 |
return ret; |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
public static Microsoft.DirectX.Direct3D.PrimitiveType ConvertToDX( Libx42.PrimitiveType primType ) |
|---|
| 187 |
{ |
|---|
| 188 |
switch( primType ) |
|---|
| 189 |
{ |
|---|
| 190 |
case Libx42.PrimitiveType.PointList: |
|---|
| 191 |
return Microsoft.DirectX.Direct3D.PrimitiveType.PointList; |
|---|
| 192 |
|
|---|
| 193 |
case Libx42.PrimitiveType.LineList: |
|---|
| 194 |
return Microsoft.DirectX.Direct3D.PrimitiveType.LineList; |
|---|
| 195 |
|
|---|
| 196 |
case Libx42.PrimitiveType.LineStrip: |
|---|
| 197 |
return Microsoft.DirectX.Direct3D.PrimitiveType.LineStrip; |
|---|
| 198 |
|
|---|
| 199 |
case Libx42.PrimitiveType.TriangleList: |
|---|
| 200 |
return Microsoft.DirectX.Direct3D.PrimitiveType.TriangleList; |
|---|
| 201 |
|
|---|
| 202 |
case Libx42.PrimitiveType.TriangleStrip: |
|---|
| 203 |
return Microsoft.DirectX.Direct3D.PrimitiveType.TriangleStrip; |
|---|
| 204 |
|
|---|
| 205 |
case Libx42.PrimitiveType.TriangleFan: |
|---|
| 206 |
return Microsoft.DirectX.Direct3D.PrimitiveType.TriangleFan; |
|---|
| 207 |
|
|---|
| 208 |
default: |
|---|
| 209 |
throw new ArgumentException(); |
|---|
| 210 |
} |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
public static void InitArray<T>( ref T[] array, int length ) |
|---|
| 214 |
{ |
|---|
| 215 |
if( array == null || array.Length != length ) |
|---|
| 216 |
array = new T[length]; |
|---|
| 217 |
else |
|---|
| 218 |
Array.Clear( array, 0, length ); |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
public static Microsoft.DirectX.Vector3 GetIncenter( |
|---|
| 222 |
Microsoft.DirectX.Vector3 A, |
|---|
| 223 |
Microsoft.DirectX.Vector3 B, |
|---|
| 224 |
Microsoft.DirectX.Vector3 C |
|---|
| 225 |
) |
|---|
| 226 |
{ |
|---|
| 227 |
float a = (B - C).Length(); |
|---|
| 228 |
float b = (C - A).Length(); |
|---|
| 229 |
float c = (A - B).Length(); |
|---|
| 230 |
|
|---|
| 231 |
float div = 1.0F / (a + b + c); |
|---|
| 232 |
|
|---|
| 233 |
return (a * A + b * B + c * C) * div; |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
public static string FormatFileSize( long p ) |
|---|
| 237 |
{ |
|---|
| 238 |
return string.Format( "{0:#,#} KB", (p + 1023) / 1024 ); |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
public static float ToRadians( float degrees ) |
|---|
| 242 |
{ |
|---|
| 243 |
return degrees * (float)(Math.PI / 180.0); |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
} |
|---|