/
Spritebatch.cs
176 lines (156 loc) · 6.59 KB
/
Spritebatch.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace TKPlatformer
{
class Spritebatch
{
private static GameWindow window;
private static Size ScreenSize
{
get
{
return window.ClientSize;
}
}
public static void Initialize(GameWindow gameWindow)
{
//Enable Textures and alpha blending
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
//Set how the alpha blending function works
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
window = gameWindow;
}
/// <summary>
/// Generally called before Spritebatch.Begin()
/// </summary>
public static void Clear(ClearBufferMask mask)
{
GL.Clear(mask);
}
public static void Begin(float depthMax = 4.0f, View view = null)
{
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-window.ClientSize.Width / 2f, window.ClientSize.Width / 2f, window.ClientSize.Height / 2f, -window.ClientSize.Height / 2f, 0.0, depthMax);
if (view != null)
{
view.ApplyTransform();
}
}
public static void End()
{
//Doesn't do anything at the moment. window.SwapBuffers() is done externally now
}
/// <summary>
///
/// </summary>
/// <param name="rotation">In Radians</param>
public static void Draw(int Texture, Vector2 position, Vector2 size)
{
Draw(Texture, position, size, 0f, Color.White, Vector2.Zero, 0f);
}
/// <summary>
///
/// </summary>
/// <param name="rotation">In Radians</param>
public static void Draw(int Texture, Vector2 position, Vector2 size, float rotation)
{
Draw(Texture, position, size, rotation, Color.White, Vector2.Zero, 0f);
}
/// <summary>
///
/// </summary>
/// <param name="rotation">In Radians</param>
public static void Draw(int Texture, Vector2 position, Vector2 size, float rotation, Color color)
{
Draw(Texture, position, size, rotation, color, Vector2.Zero, 0f);
}
/// <summary>
///
/// </summary>
/// <param name="rotation">In Radians</param>
public static void Draw(int Texture, Vector2 position, Vector2 size, float rotation, Color color, Vector2 origin)
{
Draw(Texture, position, size, rotation, color, origin, 0f);
}
/// <summary>
///
/// </summary>
/// <param name="rotation">In Radians</param>
public static void Draw(int Texture, Vector2 position, Vector2 size, float rotation, Color color, Vector2 origin, float depth)
{
GL.BindTexture(TextureTarget.Texture2D, Texture);
GL.Begin(PrimitiveType.Quads);
Vector2[] p = new Vector2[4]{Vector2.Zero, Vector2.Zero ,Vector2.Zero, Vector2.Zero};
p[0] = new Vector2(0, 0);
p[1] = new Vector2(1, 0);
p[2] = new Vector2(1, 1);
p[3] = new Vector2(0, 1);
Vector2[] tC = new Vector2[4]{Vector2.Zero, Vector2.Zero ,Vector2.Zero, Vector2.Zero};;
p.CopyTo(tC, 0);
//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
for (int i = 0; i < 4; i++)
{
p[i] = new Vector2(p[i].X * size.X + position.X, p[i].Y * size.Y + position.Y);
p[i] -= origin;
if (rotation != 0)
{
p[i] = Calc.RotateAround(p[i], origin + position, rotation);
}
//p[i] = p[i] + new Vector2(ScreenSize.Width / 2f, ScreenSize.Height / 2f);
//p[i] = new Vector2(p[i].X / (ScreenSize.Width / 2f), p[i].Y / (ScreenSize.Height / 2f));
GL.Color4(color);
GL.TexCoord2(tC[i]);
//GL.Vertex2(p[i]);
GL.Vertex3(new Vector3(p[i].X, p[i].Y, depth));
}
GL.End();
}
public static void DrawRectangle(RectangleF rec, Color color)
{
DrawRectangle(new Vector2(rec.X, rec.Y), new Vector2(rec.Width, rec.Height), color);
}
public static void DrawRectangle(Vector2 position, Vector2 size, Color color)
{
DrawRectangle(position.X, position.Y, size.X, size.Y, color, 0, Vector2.Zero, 0);
}
public static void DrawRectangle(Vector2 position, Vector2 size, Color color, float rotation)
{
DrawRectangle(position.X, position.Y, size.X, size.Y, color, rotation, Vector2.Zero, 0);
}
public static void DrawRectangle(Vector2 position, Vector2 size, Color color, float rotation, Vector2 origin)
{
DrawRectangle(position.X, position.Y, size.X, size.Y, color, rotation, origin, 0);
}
public static void DrawRectangle(Vector2 position, Vector2 size, Color color, float rotation, Vector2 origin, float depth)
{
DrawRectangle(position.X, position.Y, size.X, size.Y, color, rotation, origin, depth);
}
public static void DrawRectangle(float x, float y, float width, float height, Color color)
{
Draw(My.dot.Id, new Vector2(x, y), new Vector2(width, height), 0, color, Vector2.Zero, 0);
}
public static void DrawRectangle(float x, float y, float width, float height, Color color, float rotation)
{
Draw(My.dot.Id, new Vector2(x, y), new Vector2(width, height), rotation, color, Vector2.Zero, 0);
}
public static void DrawRectangle(float x, float y, float width, float height, Color color, float rotation, Vector2 origin)
{
Draw(My.dot.Id, new Vector2(x, y), new Vector2(width, height), rotation, color, origin, 0);
}
public static void DrawRectangle(float x, float y, float width, float height, Color color, float rotation, Vector2 origin, float depth)
{
Draw(My.dot.Id, new Vector2(x, y), new Vector2(width, height), rotation, color, origin, depth);
}
}
}