3c519b57137bc99d19707c80b07c421e1c178079
[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 #include "common.h"
14
15 VG_STATIC void vg_loader_start( void(*pfn)(void) );
16 VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) );
17
18 static struct vg_shader _shader_loader =
19 {
20 .name = "[vg] loader",
21 .link = NULL,
22
23 /* This is the new foreground shader */
24 .vs =
25 {
26 .orig_file = NULL,
27 .static_src = ""
28 "layout (location=0) in vec2 a_co;"
29 "out vec2 aUv;"
30 "void main()"
31 "{"
32 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
33 "aUv = a_co;"
34 "}"
35 },
36 .fs =
37 {
38 .orig_file = NULL,
39 .static_src =
40
41 "out vec4 FragColor;"
42 "uniform float uTime;"
43 "uniform float uRatio;"
44 "uniform float uOpacity;"
45 "in vec2 aUv;"
46
47 "float eval_zero( vec2 uv )"
48 "{"
49 "vec4 vsines = sin( (uTime+uv.y*80.0) * vec4(1.1,2.0234,3.73,2.444) );"
50 "float gradient = min( uv.y, 0.0 );"
51 "float offset = vsines.x*vsines.y*vsines.z*vsines.w*gradient;"
52
53 "vec2 vpos = uv + vec2( offset, 0.0 );"
54 "float dist = dot( vpos, vpos );"
55
56 "float fring = step(0.1*0.1,dist) * step(dist,0.15*0.15);"
57 "return max( 0.0, fring * 1.0+gradient*6.0 );"
58 "}"
59
60 "void main()"
61 "{"
62
63 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
64
65 "vec2 uvx = aUv - vec2( 0.5 );"
66 "uvx.x *= uRatio;"
67 "uvx.y *= 0.75;"
68
69 "float zero = eval_zero( uvx );"
70
71 "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
72 "float fmt1 = step( 0.5, zero*zero + dither )*0.8+0.2;"
73
74 "FragColor = vec4(vec3(fmt1),uOpacity);"
75 "}"
76 }
77 };
78
79 static struct vg_loader
80 {
81 /* Shutdown steps */
82 struct loader_free_step
83 {
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 = 0.8f;
188 }
189
190
191 VG_STATIC void vg_load_full(void);
192
193 VG_STATIC int _vg_loader_thread(void *pfn)
194 {
195 if( setjmp( vg.env_loader_exit ) )
196 return 0;
197
198 /* Run client loader */
199 vg_info( "Starting client loader thread @%p\n", pfn );
200 void (*call_func)(void) = pfn;
201 call_func();
202
203 SDL_SemPost( vg.sem_loader );
204 vg.thread_id_loader = 0;
205
206 return 0;
207 }
208
209 VG_STATIC void vg_loader_start( void(*pfn)(void) )
210 {
211 SDL_SemWait( vg.sem_loader );
212 SDL_CreateThread( _vg_loader_thread, "Loader thread", pfn );
213 }
214
215 /*
216 * Schedule something to be ran now, freed later. Checks in with engine status
217 */
218 VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) )
219 {
220 if( fn_load )
221 fn_load();
222
223 if( fn_free ){
224 struct loader_free_step step;
225 step.fn_free = fn_free;
226
227 if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
228 vg_fatal_error( "Too many free steps" );
229
230 vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
231 }
232
233 /* TODO: There was a quit checker here, re-add this? */
234 }
235
236 #endif /* VG_LOADER_H */