GLUtil.cs 2.57 KB
Newer Older
pcy's avatar
initial  
pcy committed
1
2
3
4
5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
pcy's avatar
pcy committed
6
using System.Text;
pcy's avatar
initial  
pcy committed
7
8
9
10
11
12
13
14
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SDL2;

using static SDL2.SDL;

namespace PoroCYon.FNAGLSL {
	public static unsafe partial class GL {
pcy's avatar
pcy committed
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
		internal readonly static UTF8Encoding utf8 = new UTF8Encoding(false, true);

		static byte[] utf8buf = new byte[64];
		internal static byte[] GetNullTermUTF8(string s, bool newarr = false) {
			int nbytes = utf8.GetByteCount(s);
			byte[] ret;

			if (newarr) {
				ret = new byte[nbytes + 1];
			} else {
				if (utf8buf.Length < nbytes + 1)
					Array.Resize<byte>(ref utf8buf, nbytes + 1);
				ret = utf8buf;
			}

			utf8.GetBytes(s, 0, s.Length, ret, 0);
			ret[nbytes] = 0;
			return ret;
		}
		internal static string StrFromNullTermUTF8(byte[] s) {
			fixed (byte* b = s) {
				return StrFromNullTermUTF8((sbyte*)b, s.Length);
			}
		}
		internal static string StrFromNullTermUTF8(sbyte[] s) {
			fixed (sbyte* b = s) {
				return StrFromNullTermUTF8(b, s.Length);
			}
		}

		static long strlen(sbyte* s) {
			long r = 0;
			for (; *s != 0; ++s, ++r) ;
			return r;
		}
		internal static string StrFromNullTermUTF8(sbyte* s, int nbytes = -1) {
			if (nbytes < 0) {
				nbytes = (int)strlen(s);;
			} /*else {
				long sl = strlen(s);
				if (nbytes > sl) nbytes = (int)sl;
			}*/

			return new String(s, 0, nbytes, utf8);
		}

pcy's avatar
initial  
pcy committed
61
62
63
		public static void Throw() {
			GLError err = GetError();

pcy's avatar
pcy committed
64
			if (err != GLError.NoError) throw new GLException(err);
pcy's avatar
initial  
pcy committed
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
		}

		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;

93
94
95
96
			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
97
98
99
100
101
102
103
104
105
106
107
108

			InitProcs();

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