83b9b835544671c094d3172f0e9fb4feb4784272
1 #ifndef PLAYER_REPLAY_C
2 #define PLAYER_REPLAY_C
4 #include "player_replay.h"
6 VG_STATIC
void local_replay_init( u32 bytes
){
7 localplayer
.replay
.data
= vg_linear_alloc( vg_mem
.rtmemory
, bytes
);
8 localplayer
.replay
.size
= bytes
;
9 localplayer
.replay
.cursor
= -9999.9;
10 vg_console_reg_var( "k_replay_test", &k_replay_test
, k_var_dtype_i32
, 0 );
13 u32
replay_frame_size( replay_frame
*frame
){
14 /* eventually it will be a dynamic size */
15 return vg_align8( sizeof( replay_frame
) );
18 VG_STATIC replay_frame
*replay_newframe( replay_buffer
*replay
){
19 replay_frame
*frame
= NULL
;
21 assert( replay
->head
);
23 u32 headsize
= replay_frame_size( replay
->head
),
24 nextpos
= ((void *)replay
->head
- replay
->data
) + headsize
,
25 nextsize
= vg_align8( sizeof(replay_frame
) );
27 if( nextsize
> replay
->size
){
28 vg_error( "Keyframe too big\n" );
32 if( nextpos
+ nextsize
> replay
->size
)
37 u32 tailpos
= (void *)replay
->tail
- replay
->data
;
39 if( tailpos
>= nextpos
){
40 if( nextpos
+ nextsize
> tailpos
){
41 if( replay
->cursor_frame
== replay
->tail
){
42 replay
->cursor_frame
= NULL
;
45 replay
->tail
= replay
->tail
->r
;
48 replay
->tail
->l
= NULL
;
57 frame
= replay
->data
+ nextpos
;
60 replay
->head
->r
= frame
;
67 frame
->l
= replay
->head
;
70 if( !replay
->tail
) replay
->tail
= frame
;
75 VG_STATIC
void replay_seek( replay_buffer
*replay
, f64 t
){
76 if( !replay
->head
) return;
77 assert( replay
->tail
);
79 if( t
< replay
->tail
->time
) t
= replay
->tail
->time
;
80 if( t
> replay
->head
->time
) t
= replay
->head
->time
;
82 if( !replay
->cursor_frame
) {
83 replay
->cursor
= replay
->head
->time
;
84 replay
->cursor_frame
= replay
->head
;
86 if( fabs(replay
->head
->time
-t
) > fabs(replay
->tail
->time
-t
) ){
87 replay
->cursor
= replay
->tail
->time
;
88 replay
->cursor_frame
= replay
->tail
;
92 f64 dir
= t
- replay
->cursor
;
93 if( dir
== 0.0 ) return;
94 dir
= vg_signf( dir
);
99 if( t
> replay
->cursor_frame
->time
) break;
102 if( dir
> 0.0 ) next
= replay
->cursor_frame
->r
;
103 else next
= replay
->cursor_frame
->l
;
108 if( t
< next
->time
) break;
110 replay
->cursor_frame
= next
;
116 VG_STATIC
void replay_debug_info( player_instance
*player
){
117 player__debugtext( 2, "replay info" );
119 replay_buffer
*replay
= &player
->replay
;
123 if( replay
->tail
) tail
= (void *)replay
->tail
- replay
->data
;
124 if( replay
->head
) head
= (void *)replay
->head
- replay
->data
;
126 player__debugtext( 1, "head @%u | tail @%u\n", head
, tail
);
128 f64 start
= replay
->cursor
,
129 end
= replay
->cursor
;
130 if( replay
->tail
) start
= replay
->tail
->time
;
131 if( replay
->head
) end
= replay
->head
->time
;
133 f64 cur
= replay
->cursor
- start
,
136 player__debugtext( 1, "cursor: %.2fs / %.2fs\n", cur
, len
);
139 VG_STATIC
void replay_imgui( player_instance
*player
){
140 if( !k_replay_test
) return;
142 replay_buffer
*replay
= &player
->replay
;
143 f64 start
= replay
->cursor
,
144 end
= replay
->cursor
;
145 if( replay
->tail
) start
= replay
->tail
->time
;
146 if( replay
->head
) end
= replay
->head
->time
;
147 f64 len
= end
- start
,
148 cur
= (replay
->cursor
- start
) / len
;
154 ui_rect bar
= { 0, vg
.window_y
- height
, vg
.window_x
, height
};
155 ui_fill( bar
, ui_colour( k_ui_bg
) );
157 ui_rect cusor
= { cur
* (f64
)vg
.window_x
- (cwidth
/2), bar
[1],
159 ui_fill( cusor
, ui_colour( k_ui_bg
+4 ) );
163 snprintf( buffer
, 128, "-%.2fs\n", (end
-replay
->cursor
) );
164 ui_text( cusor
, buffer
, 1, k_ui_align_middle_left
, 0 );
166 snprintf( buffer
, 128, "-%.2fs\n", len
);
167 ui_text( bar
, buffer
, 1, k_ui_align_middle_left
, 0 );
168 ui_text( bar
, "0s", 1, k_ui_align_middle_right
, 0 );
171 #endif /* PLAYER_REPLAY_C */