-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaders.py
40 lines (35 loc) · 1.43 KB
/
shaders.py
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
import raylib
import os
import pickle
shaders = {}
shaders_enabled = True
shader_cache_file = 'shaders/shader_cache.pkl'
def load_shader_with_error_check(vs_path, fs_path):
if fs_path not in shaders:
shader = raylib.LoadShader(vs_path, fs_path)
if shader.id == 0:
raise ValueError(f"Failed to load shader from {fs_path}")
shaders[fs_path] = shader
save_shader_cache()
return shaders[fs_path]
def save_shader_cache():
with open(shader_cache_file, 'wb') as cache_file:
pickle.dump(list(shaders.keys()), cache_file)
def load_shader_cache():
if os.path.exists(shader_cache_file):
with open(shader_cache_file, 'rb') as cache_file:
cached_shaders = pickle.load(cache_file)
for fs_path in cached_shaders:
if isinstance(fs_path, str):
fs_path = fs_path.encode('utf-8')
shader = raylib.LoadShader(b"", fs_path)
shaders[fs_path] = shader
def load_shaders():
load_shader_cache()
try:
shaders["lava"] = load_shader_with_error_check(b"", b"shaders/lava.fs")
shaders["background"] = load_shader_with_error_check(b"", b"shaders/background.fs")
shaders["main_menu_background"] = load_shader_with_error_check(b"", b"shaders/main_menu_background.fs")
shaders["lighting"] = load_shader_with_error_check(b"", b"shaders/lighting.fs")
except Exception as e:
raise e