switch texture loading to resource based
[fishladder.git] / fishladder_resources.h
1 vg_tex2d tex_tile_data = { .path = "textures/tileset.png" };
2 vg_tex2d tex_tile_detail = { .path = "textures/tile_overlays.png" };
3 vg_tex2d tex_wood = { .path = "textures/wood.png" };
4 vg_tex2d tex_ball = { .path = "textures/ball.png", .flags = VG_TEXTURE_CLAMP };
5
6 vg_tex2d *texture_list[] = { &tex_tile_detail, &tex_tile_data, &tex_wood, &tex_ball };
7
8 sfx_system_t audio_system_sfx =
9 {
10 .vol = 1.f,
11 .spd = 1.f,
12 .ch = 1,
13 .cur = 0,
14 .vol_src = &g_vol_sfx,
15 .flags = 0x00,
16 .fadeout = FADEOUT_LENGTH,
17 .name = "sfx"
18 };
19
20 sfx_set_t audio_tile_mod =
21 {
22 .sources = "\
23 sound/mod_01.ogg\0\
24 sound/mod_02.ogg\0\
25 sound/mod_03.ogg\0\
26 sound/mod_04.ogg\0\
27 sound/mod_05.ogg\0\
28 sound/mod_06.ogg\0",
29 .flags = 0
30 };
31
32 static void resource_load_main(void)
33 {
34 // Textures
35 vg_tex2d_init( texture_list, vg_list_size( texture_list ) );
36
37 // Audio
38 sfx_set_init( &audio_tile_mod, NULL );
39 }
40
41 static void resource_free_main(void)
42 {
43 vg_tex2d_free( texture_list, vg_list_size( texture_list ) );
44 sfx_set_free( &audio_tile_mod );
45 }
46
47 // SHADERS
48 // ===========================================================================================================
49
50 SHADER_DEFINE( shader_tile_colour,
51
52 // VERTEX
53 "layout (location=0) in vec2 a_co;"
54 "uniform mat3 uPv;"
55 "uniform vec3 uOffset;"
56 ""
57 "void main()"
58 "{"
59 "gl_Position = vec4( uPv * vec3( a_co * uOffset.z + uOffset.xy, 1.0 ), 1.0 );"
60 "}",
61
62 // FRAGMENT
63 "out vec4 FragColor;"
64 "uniform vec4 uColour;"
65 ""
66 "void main()"
67 "{"
68 "FragColor = uColour;"
69 "}"
70 ,
71 UNIFORMS({ "uPv", "uOffset", "uColour" })
72 )
73
74 SHADER_DEFINE( shader_ball,
75 // VERTEX
76 "layout (location=0) in vec2 a_co;"
77 "uniform vec2 uOffset;"
78 "uniform mat3 uPv;"
79 ""
80 "out vec2 aTexCoords;"
81 ""
82 "void main()"
83 "{"
84 // Create texture coords
85 "aTexCoords = a_co;"
86
87 // Vertex transform
88 "vec3 worldpos = vec3( a_co * 0.5 - 0.25 + uOffset, 1.0 );"
89 "gl_Position = vec4( uPv * worldpos, 1.0 );"
90 "}",
91
92 // FRAGMENT
93 "out vec4 FragColor;"
94 ""
95 "uniform sampler2D uTexMain;"
96 "uniform vec3 uColour;"
97 ""
98 "in vec2 aTexCoords;"
99 ""
100 "void main()"
101 "{"
102 "vec4 glyph = texture( uTexMain, aTexCoords );"
103 "FragColor = vec4( uColour + glyph.rgb * 0.2, glyph.a );"
104 "}"
105 ,
106 UNIFORMS({ "uTexMain", "uColour", "uOffset", "uPv" })
107 )
108
109 SHADER_DEFINE( shader_tile_main,
110 // VERTEX
111 "layout (location=0) in vec2 a_co;"
112 "uniform vec4 uOffset;" // Tile x/y, uv x/y
113 "uniform mat3 uPv;"
114 "uniform mat2 uSubTransform;"
115 ""
116 "out vec4 aTexCoords;"
117 "out vec2 aWorldCoords;"
118 ""
119 "vec2 hash22(vec2 p)"
120 "{"
121 "vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));"
122 "p3 += dot(p3, p3.yzx+33.33);"
123 "return fract((p3.xx+p3.yz)*p3.zy);"
124 "}"
125 ""
126 "void main()"
127 "{"
128 // Vertex transform
129 "vec2 subtransform = uSubTransform * (a_co-0.5) + 0.5;"
130 "vec3 worldpos = vec3( subtransform + uOffset.xy, 1.0 );"
131 "gl_Position = vec4( uPv * worldpos, 1.0 );"
132
133 // Create texture coords
134 "vec2 random_offset = floor(hash22(uOffset.xy) * 4.0) * 0.25;"
135 "vec2 edge_safe_coords = a_co * 0.98 + 0.01;"
136 "aTexCoords = vec4((edge_safe_coords + uOffset.zw) * 0.25, edge_safe_coords * 0.25 + random_offset );"
137 "aWorldCoords = worldpos.xy;"
138 "}",
139
140 // FRAGMENT
141 "out vec4 FragColor;"
142 ""
143 "uniform sampler2D uTexGlyphs;"
144 "uniform sampler2D uTexWood;"
145 "uniform float uGhost;"
146 "uniform vec2 uMousePos;"
147 "uniform vec4 uColour;"
148 ""
149 "in vec4 aTexCoords;"
150 "in vec2 aWorldCoords;"
151 ""
152 "void main()"
153 "{"
154 "vec3 shadowing_colour = vec3( 0.93, 0.88536, 0.8184 );"
155 "vec4 glyph = texture( uTexGlyphs, aTexCoords.xy );"
156 "vec4 wood = texture( uTexWood, aTexCoords.zw );"
157 "vec4 wood_secondary = texture( uTexWood, aTexCoords.zw + 0.25 );"
158 "vec3 wood_comp = mix( wood_secondary.rgb * shadowing_colour, wood.rgb, clamp( glyph.b * 2.0 - 1.0, 0.0, 1.0 ) );"
159
160 "vec3 shadows = mix( vec3( 0.85, 0.7344, 0.561 ), vec3(1.0,1.0,1.0), glyph.r );"
161
162 "vec4 output_regular = vec4( wood_comp * shadows, glyph.b );"
163
164 "float ghost_dist = clamp( 1.5 - distance(uMousePos, aWorldCoords), 0.0, 1.0 );"
165 "vec4 output_ghost = vec4( 1.0, 1.0, 1.0, glyph.g * ghost_dist );"
166
167 "FragColor = mix( output_regular, output_ghost, uGhost ) * uColour;"
168 "}"
169 ,
170 UNIFORMS({ "uPv", "uOffset", "uTexGlyphs", "uTexWood", "uSubTransform", "uGhost", "uMousePos", "uColour" })
171 )
172
173 void vg_register(void)
174 {
175 SHADER_INIT( shader_tile_colour );
176 SHADER_INIT( shader_tile_main );
177 SHADER_INIT( shader_ball );
178 }