various
[vg.git] / vg_loader.h
1 /*
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
4 *
5 * Splash / load screen
6 *
7 * -----------------------------------------------------------------------------
8 */
9
10 #ifndef VG_LOADER_H
11 #define VG_LOADER_H
12
13 #define VG_GAME
14 #include "vg/vg.h"
15 #include "vg/vg_shader.h"
16
17 VG_STATIC void vg_loader_start( void(*pfn)(void *data), void *data );
18 VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) );
19
20 static struct vg_shader _shader_loader =
21 {
22 .name = "[vg] loader",
23 .link = NULL,
24
25 /* This is the new foreground shader */
26 .vs =
27 {
28 .orig_file = NULL,
29 .static_src = ""
30 "layout (location=0) in vec2 a_co;"
31 "out vec2 aUv;"
32 "void main()"
33 "{"
34 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
35 "aUv = a_co;"
36 "}"
37 },
38 .fs =
39 {
40 .orig_file = NULL,
41 .static_src =
42
43 "out vec4 FragColor;"
44 "uniform float uTime;"
45 "uniform float uRatio;"
46 "uniform float uOpacity;"
47 "in vec2 aUv;"
48
49 "float eval_zero( vec2 uv )"
50 "{"
51 "vec4 vsines = sin( (uTime+uv.y*80.0) * vec4(1.1,2.0234,3.73,2.444) );"
52 "float gradient = min( uv.y, 0.0 );"
53 "float offset = vsines.x*vsines.y*vsines.z*vsines.w*gradient;"
54
55 "vec2 vpos = uv + vec2( offset, 0.0 );"
56 "float dist = dot( vpos, vpos );"
57
58 "float fring = step(0.1*0.1,dist) * step(dist,0.15*0.15);"
59 "return max( 0.0, fring * 1.0+gradient*6.0 );"
60 "}"
61
62 "void main()"
63 "{"
64 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
65
66 "vec2 uvx = aUv - vec2( 0.5 );"
67 "uvx.x *= uRatio;"
68 "uvx.y *= 0.75;"
69
70 "float zero = eval_zero( uvx );"
71
72 "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
73 "float fmt1 = step( 0.5, zero*zero + dither )*0.8+0.2;"
74
75 "FragColor = vec4(vec3(fmt1),uOpacity);"
76 "}"
77 }
78 };
79
80 static struct vg_loader
81 {
82 /* Shutdown steps */
83 struct loader_free_step{
84 void (*fn_free)(void);
85 }
86 step_buffer[16];
87 u32 step_count, step_action;
88
89 GLuint vao, vbo;
90 }
91 vg_loader;
92
93 VG_STATIC void _vg_loader_init(void)
94 {
95 float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
96 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };
97
98 glGenVertexArrays( 1, &vg_loader.vao );
99 glGenBuffers( 1, &vg_loader.vbo );
100 glBindVertexArray( vg_loader.vao );
101 glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo );
102 glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
103 glBindVertexArray( vg_loader.vao );
104 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 );
105 glEnableVertexAttribArray( 0 );
106
107 VG_CHECK_GL_ERR();
108
109 if( !vg_shader_compile( &_shader_loader ) )
110 vg_fatal_error( "failed to compile shader" );
111 }
112
113 VG_STATIC void _vg_loader_free(void)
114 {
115 vg_info( "vg_loader_free\n" );
116 glDeleteVertexArrays( 1, &vg_loader.vao );
117 glDeleteBuffers( 1, &vg_loader.vbo );
118
119 for( int i=0; i<vg_loader.step_count; i++ )
120 {
121 struct loader_free_step *step =
122 &vg_loader.step_buffer[vg_loader.step_count -1 -i];
123
124 vg_info( " -> %p\n", step->fn_free );
125 step->fn_free();
126 }
127 }
128
129 VG_STATIC void _vg_render_log(void)
130 {
131 #if 0
132 ui_begin( vg.window_x, vg.window_y );
133 SDL_AtomicLock( &log_print_sl );
134
135 int const fh = 14;
136 int lines_screen_max = ((vg.window_y/fh)-2),
137 lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
138 lines_to_draw = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
139
140 int ptr = vg_log.buffer_line_current;
141
142 vg_uictx.cursor[0] = 0;
143 vg_uictx.cursor[1] = lines_to_draw*fh;
144 vg_uictx.cursor[3] = fh;
145 ui_fill_x();
146
147 for( int i=0; i<lines_to_draw; i ++ ){
148 ptr --;
149
150 if( ptr < 0 )
151 ptr = vg_list_size( vg_log.buffer )-1;
152
153 ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
154 vg_uictx.cursor[1] -= fh;
155 }
156
157 SDL_AtomicUnlock( &log_print_sl );
158
159 ui_resolve();
160 ui_draw( NULL );
161 #endif
162 }
163
164 VG_STATIC void _vg_loader_render_ring( float opacity )
165 {
166 glEnable(GL_BLEND);
167 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
168 glBlendEquation(GL_FUNC_ADD);
169
170 glUseProgram( _shader_loader.id );
171 glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
172 float ratio = (float)vg.window_x / (float)vg.window_y;
173 glUniform1f( glGetUniformLocation( _shader_loader.id, "uRatio"), ratio );
174 glUniform1f( glGetUniformLocation( _shader_loader.id, "uOpacity"), opacity );
175 glBindVertexArray( vg_loader.vao );
176 glDrawArrays( GL_TRIANGLES, 0, 6 );
177 }
178
179 VG_STATIC void _vg_loader_render(void)
180 {
181 glViewport( 0,0, vg.window_x, vg.window_y );
182 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
183 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
184 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
185
186 _vg_render_log();
187 vg.loader_ring = 1.0f;
188 }
189
190
191 VG_STATIC void vg_load_full(void);
192
193 VG_STATIC int _vg_loader_thread( void *pfn ){
194 if( setjmp( vg.env_loader_exit ) )
195 return 0;
196
197 /* Run client loader */
198 vg_info( "Starting client loader thread @%p\n", pfn );
199 void (*call_func)(void *data) = pfn;
200 call_func( vg.thread_data );
201
202 SDL_SemPost( vg.sem_loader );
203 vg.thread_id_loader = 0;
204
205 return 0;
206 }
207
208 static int vg_loader_availible(void){
209 if( SDL_SemValue( vg.sem_loader ) ){
210 if( !(vg_async.start) )
211 return 1;
212 }
213
214 return 0;
215 }
216
217 VG_STATIC void vg_loader_start( void(*pfn)(void *data), void *data )
218 {
219 SDL_SemWait( vg.sem_loader );
220
221 vg.thread_data = data;
222 SDL_CreateThread( _vg_loader_thread, "Loader thread", pfn );
223 }
224
225 /*
226 * Schedule something to be ran now, freed later. Checks in with engine status
227 */
228 VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) )
229 {
230 if( fn_load )
231 fn_load();
232
233 if( fn_free ){
234 struct loader_free_step step;
235 step.fn_free = fn_free;
236
237 if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
238 vg_fatal_error( "Too many free steps" );
239
240 vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
241 }
242
243 /* TODO: There was a quit checker here, re-add this? */
244 }
245
246 #endif /* VG_LOADER_H */