Refactor, GLFW->SDL
[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 static struct vg_shader _shader_loader =
16 {
17 .name = "[vg] loader",
18 .link = NULL,
19 .vs =
20 {
21 .orig_file = NULL,
22 .static_src = ""
23 "layout (location=0) in vec2 a_co;"
24 "out vec2 aUv;"
25 "void main()"
26 "{"
27 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
28 "aUv = a_co;"
29 "}"
30 },
31 .fs =
32 {
33 .orig_file = NULL,
34 .static_src =
35
36 "out vec4 FragColor;"
37 "uniform float uTime;"
38 "in vec2 aUv;"
39
40 "void main()"
41 "{"
42 "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
43 "float grad = 1.0-(aUv.y*0.5+0.5);"
44 "float fmt1 = step( 0.5, grad+dither );"
45
46 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
47
48 "FragColor = vec4(col*grad*fmt1,1.0);"
49 "}"
50 }
51 };
52
53 static struct vg_loader
54 {
55 /* Shutdown steps */
56 struct loader_free_step
57 {
58 void (*fn_free)(void *);
59 void *data;
60 }
61 step_buffer[16];
62 u32 step_count, step_action;
63
64 GLuint vao, vbo;
65 }
66 vg_loader;
67
68 VG_STATIC void vg_loader_init(void)
69 {
70 float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
71 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };
72
73 glGenVertexArrays( 1, &vg_loader.vao );
74 glGenBuffers( 1, &vg_loader.vbo );
75 glBindVertexArray( vg_loader.vao );
76 glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo );
77 glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
78 glBindVertexArray( vg_loader.vao );
79 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 );
80 glEnableVertexAttribArray( 0 );
81
82 VG_CHECK_GL_ERR();
83
84 if( !vg_shader_compile( &_shader_loader ) )
85 vg_fatal_exit_loop( "failed to compile shader" );
86 }
87
88 VG_STATIC void vg_loader_free(void)
89 {
90 vg_info( "vg_loader_free\n" );
91 glDeleteVertexArrays( 1, &vg_loader.vao );
92 glDeleteBuffers( 1, &vg_loader.vbo );
93
94 for( int i=0; i<vg_loader.step_count; i++ )
95 {
96 struct loader_free_step *step =
97 &vg_loader.step_buffer[vg_loader.step_count -1 -i];
98
99 vg_info( " -> %p\n", step->fn_free );
100 step->fn_free( step->data );
101 }
102
103 vg_info( "done\n" );
104 }
105
106 VG_STATIC float hue_to_rgb( float p, float q, float t )
107 {
108 if(t < 0.0f) t += 1.0f;
109 if(t > 1.0f) t -= 1.0f;
110 if(t < 1.0f/6.0f) return p + (q - p) * 6.0f * t;
111 if(t < 1.0f/2.0f) return q;
112 if(t < 2.0f/3.0f) return p + (q - p) * (2.0f/3.0f - t) * 6.0f;
113 return p;
114 }
115
116 VG_STATIC void vg_render_log(void)
117 {
118 ui_begin( vg.window_x, vg.window_y );
119 SDL_AtomicLock( &log_print_sl );
120
121 int const fh = 14;
122 int lines_screen_max = ((vg.window_y/fh)-2),
123 lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
124 lines_to_draw = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
125
126 int ptr = vg_log.buffer_line_current;
127
128 vg_uictx.cursor[0] = 0;
129 vg_uictx.cursor[1] = lines_to_draw*fh;
130 vg_uictx.cursor[3] = fh;
131 ui_fill_x();
132
133 for( int i=0; i<lines_to_draw; i ++ )
134 {
135 ptr --;
136
137 if( ptr < 0 )
138 ptr = vg_list_size( vg_log.buffer )-1;
139
140 ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
141 vg_uictx.cursor[1] -= fh;
142 }
143
144 SDL_AtomicUnlock( &log_print_sl );
145
146 ui_resolve();
147 ui_draw( NULL );
148 }
149
150 VG_STATIC void vg_loader_render(void)
151 {
152 glViewport( 0,0, vg.window_x, vg.window_y );
153 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
154 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
155 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
156
157 glUseProgram( _shader_loader.id );
158 glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
159
160 glBindVertexArray( vg_loader.vao );
161 glDrawArrays( GL_TRIANGLES, 0, 6 );
162
163 vg_render_log();
164 }
165
166
167 VG_STATIC void vg_load_full(void);
168
169 VG_STATIC int vg_loader_thread(void * nothing)
170 {
171 SDL_AtomicLock( &vg.sl_context );
172 vg.thread_id_loader = SDL_GetThreadID(NULL);
173 SDL_AtomicUnlock( &vg.sl_context );
174
175 /* Run client loader */
176 vg_load_full();
177
178 SDL_SemPost( vg.sem_loader );
179 vg.thread_id_loader = 0;
180
181 return 0;
182 }
183
184 VG_STATIC void vg_loader_start(void)
185 {
186 SDL_SemWait( vg.sem_loader );
187 SDL_CreateThread( vg_loader_thread, "Loader thread", NULL );
188 }
189
190 /* this is maybe probably unused now */
191 VG_STATIC void vg_free_libc_malloced( void *data )
192 {
193 free( data );
194 }
195
196 VG_STATIC void vg_loader_push_free_step( struct loader_free_step step )
197 {
198 if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
199 vg_fatal_exit_loop( "Too many free steps" );
200
201 vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
202 }
203
204 /*
205 * Schedule something to be ran now, freed later. Checks in with engine status
206 */
207 VG_STATIC void vg_loader_highwater( void( *fn_load )(void),
208 void( *fn_free )(void *), void *data )
209 {
210 if( fn_load )
211 fn_load();
212
213 if( fn_free )
214 {
215 struct loader_free_step step;
216 step.data = data;
217 step.fn_free = fn_free;
218
219 vg_loader_push_free_step( step );
220 }
221 else
222 {
223 if( data )
224 {
225 struct loader_free_step step;
226 step.data = data;
227 step.fn_free = vg_free_libc_malloced;
228
229 vg_loader_push_free_step( step );
230 }
231 }
232
233 vg_ensure_engine_running();
234 }
235
236 #endif /* VG_LOADER_H */