-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
111 lines (92 loc) · 2.25 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using SFML.Graphics;
using SFML.System;
namespace Tiler
{
public static class Utils
{
private static Random rng = new Random();
public static float Clamp(float Value, float Min, float Max)
{
return Math.Max(Math.Min(Value, Max), Min);
}
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
public static byte RandomByte()
{
return (byte)rng.Next(0, 256);
}
public static T Pin<T>(T input)
{
GCHandle.Alloc(input, GCHandleType.Normal);
return input;
}
public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
{
if (value.CompareTo(min) < 0)
return min;
if (value.CompareTo(max) > 0)
return max;
return value;
}
public static float Lerp(float start, float end, float fraction)
{
return start * (1.0f - fraction) + end * fraction;
}
public static Vector2 LerpVector(Vector2 start, Vector2 end, float fraction)
{
return new Vector2(
Lerp(start.X, end.X, fraction),
Lerp(start.Y, end.Y, fraction)
);
}
public static Vector2i LerpVector(Vector2i start, Vector2i end, float fraction)
{
return new Vector2i(
(int)Lerp(start.X, end.X, fraction),
(int)Lerp(start.Y, end.Y, fraction)
);
}
}
public static class UtilsDrawing
{
const string LibName = "opengl32";
public const int GL_SCISSOR_TEST = 0xC11;
[DllImport(LibName)]
static extern void glEnable(int Cap);
[DllImport(LibName)]
static extern void glDisable(int Cap);
[DllImport(LibName)]
static extern void glScissor(int X, int Y, int W, int H);
static void glScissor2(int WindH, int X, int Y, int W, int H)
{
glScissor(X, WindH - Y - H, W, H);
}
public static void EnableScissor(bool Enable)
{
if (Enable)
glEnable(GL_SCISSOR_TEST);
else
glDisable(GL_SCISSOR_TEST);
}
public static void SetScissor(RenderTarget RT, int X, int Y, int W, int H)
{
View V = RT.GetView();
var offset = V.Center - V.Size / 2;
glScissor2((int)V.Size.Y, (int)(X - offset.X), (int)(Y - offset.Y), W, H);
}
}
}