logger update
[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 *data), void *data );
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 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
63
64 "vec2 uvx = aUv - vec2( 0.5 );"
65 "uvx.x *= uRatio;"
66 "uvx.y *= 0.75;"
67
68 "float zero = eval_zero( uvx );"
69
70 "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
71 "float fmt1 = step( 0.5, zero*zero + dither )*0.8+0.2;"
72
73 "FragColor = vec4(vec3(fmt1),uOpacity);"
74 "}"
75 }
76 };
77
78 static struct vg_loader
79 {
80 /* Shutdown steps */
81 struct loader_free_step{
82 void (*fn_free)(void);
83 }
84 step_buffer[16];
85 u32 step_count, step_action;
86
87 GLuint vao, vbo;
88 }
89 vg_loader;
90
91 VG_STATIC void _vg_loader_init(void)
92 {
93 float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
94 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };
95
96 glGenVertexArrays( 1, &vg_loader.vao );
97 glGenBuffers( 1, &vg_loader.vbo );
98 glBindVertexArray( vg_loader.vao );
99 glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo );
100 glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
101 glBindVertexArray( vg_loader.vao );
102 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 );
103 glEnableVertexAttribArray( 0 );
104
105 VG_CHECK_GL_ERR();
106
107 if( !vg_shader_compile( &_shader_loader ) )
108 vg_fatal_error( "failed to compile shader" );
109 }
110
111 VG_STATIC void _vg_loader_free(void)
112 {
113 vg_info( "vg_loader_free\n" );
114 glDeleteVertexArrays( 1, &vg_loader.vao );
115 glDeleteBuffers( 1, &vg_loader.vbo );
116
117 for( int i=0; i<vg_loader.step_count; i++ )
118 {
119 struct loader_free_step *step =
120 &vg_loader.step_buffer[vg_loader.step_count -1 -i];
121
122 vg_info( " -> %p\n", step->fn_free );
123 step->fn_free();
124 }
125 }
126
127 VG_STATIC void _vg_render_log(void)
128 {
129 #if 0
130 ui_begin( vg.window_x, vg.window_y );
131 SDL_AtomicLock( &log_print_sl );
132
133 int const fh = 14;
134 int lines_screen_max = ((vg.window_y/fh)-2),
135 lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
136 lines_to_draw = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
137
138 int ptr = vg_log.buffer_line_current;
139
140 vg_uictx.cursor[0] = 0;
141 vg_uictx.cursor[1] = lines_to_draw*fh;
142 vg_uictx.cursor[3] = fh;
143 ui_fill_x();
144
145 for( int i=0; i<lines_to_draw; i ++ ){
146 ptr --;
147
148 if( ptr < 0 )
149 ptr = vg_list_size( vg_log.buffer )-1;
150
151 ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
152 vg_uictx.cursor[1] -= fh;
153 }
154
155 SDL_AtomicUnlock( &log_print_sl );
156
157 ui_resolve();
158 ui_draw( NULL );
159 #endif
160 }
161
162 VG_STATIC void _vg_loader_render_ring( float opacity )
163 {
164 glEnable(GL_BLEND);
165 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
166 glBlendEquation(GL_FUNC_ADD);
167
168 glUseProgram( _shader_loader.id );
169 glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
170 float ratio = (float)vg.window_x / (float)vg.window_y;
171 glUniform1f( glGetUniformLocation( _shader_loader.id, "uRatio"), ratio );
172 glUniform1f( glGetUniformLocation( _shader_loader.id, "uOpacity"), opacity );
173 glBindVertexArray( vg_loader.vao );
174 glDrawArrays( GL_TRIANGLES, 0, 6 );
175 }
176
177 VG_STATIC void _vg_loader_render(void)
178 {
179 glViewport( 0,0, vg.window_x, vg.window_y );
180 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
181 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
182 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
183
184 _vg_render_log();
185 vg.loader_ring = 0.8f;
186 }
187
188
189 VG_STATIC void vg_load_full(void);
190
191 VG_STATIC int _vg_loader_thread( void *pfn )
192 {
193 if( setjmp( vg.env_loader_exit ) )
194 return 0;
195
196 /* Run client loader */
197 vg_info( "Starting client loader thread @%p\n", pfn );
198 void (*call_func)(void *data) = pfn;
199 call_func( vg.thread_data );
200
201 SDL_SemPost( vg.sem_loader );
202 vg.thread_id_loader = 0;
203
204 return 0;
205 }
206
207 VG_STATIC void vg_loader_start( void(*pfn)(void *data), void *data )
208 {
209 SDL_SemWait( vg.sem_loader );
210
211 vg.thread_data = data;
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 */