glsltest.cs 1.55 KB
Newer Older
pcy's avatar
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
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.Core;
using Terraria.ModLoader.Engine;

namespace glsltest {
	public class glsltest : Mod {
		public glsltest() { }

		// creating GLSL shaders needs to happen on the main graphics thread,
		// but mod loading happens on a background thread...
		// GLCallLocker.InvokeAsync can be used to call things on the main
		// thread, but it's internal, so we have to do this instead
		static MethodInfo glcl_InvokeAsync_mi = null;
		static Task<T> GLCallLocker_InvokeAsync<T>(Func<T> f) {
			if (glcl_InvokeAsync_mi == null) {
				glcl_InvokeAsync_mi = typeof(GLCallLocker).GetMethods(
						BindingFlags.InvokeMethod|BindingFlags.Static|BindingFlags.NonPublic)
					.First(mi => mi.Name == "InvokeAsync");
			}

			return (Task<T>)glcl_InvokeAsync_mi.MakeGenericMethod(new[]{typeof(T)}).Invoke(null, new[]{f});
		}

		public override void Load() {
			GLCallLocker_InvokeAsync(() => {
				GLSLBackground.Load(Main.instance.GraphicsDevice);
				return 0;
			}).Wait();
		}
		public override void Unload() {
			GLCallLocker_InvokeAsync(() => {
				GLSLBackground.Unload();
				return 0;
			}).Wait();
		}

		// need some way to keep track of time
		public override void UpdateUI(GameTime gt) {
			GLSLBackground.totalElapsed += gt.ElapsedGameTime;
		}
	}
}