GLUtil.cs 1.45 KB
Newer Older
pcy's avatar
initial  
pcy committed
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SDL2;

using static SDL2.SDL;

namespace PoroCYon.FNAGLSL {
	public static unsafe partial class GL {
		public static void Throw() {
			GLError err = GetError();

			if (err != GLError.NoError) throw new GLException(GetError());
		}

		internal static void CheckFNA() {
			if (typeof(Vector2).Assembly != typeof(Texture2D).Assembly) {
				throw new NotSupportedException(
					"Running XNA or MonoGame, not FNA! Can't use FNA-GLSL.");
			}
		}

		static T GetProcAddress<T>(string name)
				where T : class {
			if (!typeof(T).IsSubclassOf(typeof(Delegate))) {
				throw new ArgumentException("Type '" + typeof(T) + "' is not a delegate type!");
			}

			IntPtr fn = SDL_GL_GetProcAddress("gl"+name);
			if (fn == IntPtr.Zero) {
				throw new NotSupportedException("gl"+name);
			}

			return (T)(object)Marshal.GetDelegateForFunctionPointer(fn, typeof(T));
		}

		static bool inited = false;
		public static void Init(GraphicsDevice gd) {
			CheckFNA();
			if (inited) return;

46
47
48
49
			var nam = gd.GLDevice().GetType().Name;
			if (!nam.Contains("GLDevice"))
				throw new NotSupportedException("need OpenGL, but FNA is using"+
					" the " + nam + " backend!");
pcy's avatar
initial  
pcy committed
50
51
52
53
54
55
56
57
58
59
60
61

			InitProcs();

			inited = true;
		}
		public static void Deinit() {
			DeinitProcs();
			inited = false;
		}
	}
}