6e3882af4c2bd5696f7c09e4b451e172136e483e
[vg.git] / src / vg / 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_loader
16 {
17 /* Shutdown steps */
18 struct loader_free_step
19 {
20 void (*fn_free)(void *);
21 void *data;
22 }
23 *step_buffer;
24 u32 step_count, step_cap, step_action;
25 }
26 vg_loader;
27
28 static int vg_loader_init(void)
29 {
30 return 1;
31 }
32
33 static void vg_loader_free(void)
34 {
35 vg_info( "vg_loader_free\n" );
36
37 for( int i=0; i<vg_loader.step_count; i++ )
38 {
39 struct loader_free_step *step =
40 &vg_loader.step_buffer[vg_loader.step_count -1 -i];
41
42 vg_info( " -> %p\n", step->fn_free );
43 step->fn_free( step->data );
44 }
45
46 vg_free( vg_loader.step_buffer );
47 vg_info( "done\n" );
48 }
49
50 static float hue_to_rgb( float p, float q, float t )
51 {
52 if(t < 0.0f) t += 1.0f;
53 if(t > 1.0f) t -= 1.0f;
54 if(t < 1.0f/6.0f) return p + (q - p) * 6.0f * t;
55 if(t < 1.0f/2.0f) return q;
56 if(t < 2.0f/3.0f) return p + (q - p) * (2.0f/3.0f - t) * 6.0f;
57 return p;
58 }
59
60 static void vg_loader_render(void)
61 {
62 float h = vg_randf(),
63 s = 0.7f,
64 l = 0.1f, //* (0.5f+vg_fractf(vg_time*40.0)*0.5f),
65 q = l < 0.5f ? l * (1.0f + s) : l + s - l * s,
66 p = 2.0f * l - q,
67 r = hue_to_rgb( p, q, h + 1.0f/3.0f ),
68 g = hue_to_rgb( p, q, h ),
69 b = hue_to_rgb( p, q, h - 1.0f/3.0f );
70
71 glClearColor( r, g, b, 1.0f );
72 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
73
74
75
76 ui_begin( &ui_global_ctx, vg_window_x, vg_window_y );
77
78
79 int const fh = 14;
80 int lines_screen_max = ((vg_window_y/fh)-2),
81 lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
82 lines_to_draw = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
83
84 int ptr = vg_log.buffer_line_current;
85
86 ui_global_ctx.cursor[0] = 0;
87 ui_global_ctx.cursor[1] = lines_to_draw*fh;
88 ui_global_ctx.cursor[3] = fh;
89 ui_fill_x( &ui_global_ctx );
90
91 for( int i=0; i<lines_to_draw; i ++ )
92 {
93 ptr --;
94
95 if( ptr < 0 )
96 ptr = vg_list_size( vg_log.buffer )-1;
97
98 ui_text( &ui_global_ctx, ui_global_ctx.cursor,
99 vg_log.buffer[ptr], vg_console.scale, 0 );
100
101 ui_global_ctx.cursor[1] -= fh*vg_console.scale;
102 }
103
104 ui_resolve( &ui_global_ctx );
105 ui_draw( &ui_global_ctx, NULL );
106 }
107
108
109 static void vg_load_full(void);
110
111 static void vg_loader_thread(void * nothing)
112 {
113 vg_thread_info.gl_context_level = 0;
114 vg_thread_info.purpose = k_thread_purpose_loader;
115 vg_set_thread_name( "[vg] Loader" );
116
117 /* Run client loader */
118 vg_load_full();
119 vg_semaphore_post( &vg.sem_loader );
120 }
121
122 static void vg_loader_start(void)
123 {
124 vg_semaphore_wait( &vg.sem_loader );
125 vg_thread_run( vg_loader_thread, NULL );
126 }
127
128 static void vg_free_libc_malloced( void *data )
129 {
130 vg_free( data );
131 }
132
133 static void vg_loader_push_free_step( struct loader_free_step step )
134 {
135 vg_loader.step_buffer =
136 buffer_reserve( vg_loader.step_buffer, vg_loader.step_count,
137 &vg_loader.step_cap, 1,
138 sizeof( struct loader_free_step ) );
139
140 vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
141 }
142
143 /*
144 * Schedule something to be ran now, freed later
145 */
146 static void vg_loader_highwater( void( *fn_load )(void),
147 void( *fn_free )(void *), void *data )
148 {
149 if( fn_load )
150 fn_load();
151
152 if( fn_free )
153 {
154 struct loader_free_step step;
155 step.data = data;
156 step.fn_free = fn_free;
157
158 vg_loader_push_free_step( step );
159 }
160 else
161 {
162 if( data )
163 {
164 struct loader_free_step step;
165 step.data = data;
166 step.fn_free = vg_free_libc_malloced;
167
168 vg_loader_push_free_step( step );
169 }
170 }
171
172 vg_ensure_engine_running();
173 }
174
175 #endif /* VG_LOADER_H */