1 #ifndef PLAYER_INTERFACE_H
2 #define PLAYER_INTERFACE_H
9 typedef struct player_device player_device
;
10 typedef struct player_interface player_interface
;
11 typedef struct player_attachment player_attachment
;
12 typedef mdl_keyframe player_pose
[32];
14 struct player_interface
19 struct player_attachment
21 player_device
*device
;
26 struct input_binding
*input_js1h
,
42 void (* pre_update
) ( player_interface
*player
, player_attachment
*at
);
43 void (* update
) ( player_interface
*player
, player_attachment
*at
);
44 void (* post_update
)( player_interface
*player
, player_attachment
*at
);
45 void (* pose
) ( player_interface
*player
, player_attachment
*at
,
48 void (* get_camera
) ( player_interface
*player
, player_attachment
*at
,
51 void (* attatch
) ( player_interface
*player
, player_attachment
*at
,
54 void (* reset
) ( player_interface
*player
, player_attachment
*at
,
55 struct respawn_point
*spawn
);
57 void (* store_state
)( player_interface
*player
, player_attachment
*at
);
58 void (* load_state
) ( player_interface
*player
, player_attachment
*at
);
59 void (* debug_ui
) ( player_interface
*player
, player_attachment
*at
);
62 VG_STATIC
void player_interface_create_player( player_interface
*inst
)
64 static int only_once
= 0;
65 assert( only_once
== 0 );
68 inst
->input_js1h
= vg_create_named_input( "steer-h", k_input_type_axis
);
69 inst
->input_js1v
= vg_create_named_input( "steer-v", k_input_type_axis
);
70 inst
->input_grab
= vg_create_named_input( "grab", k_input_type_axis_norm
);
71 inst
->input_js2h
= vg_create_named_input( "grab-h", k_input_type_axis
);
72 inst
->input_js2v
= vg_create_named_input( "grab-v", k_input_type_axis
);
73 inst
->input_jump
= vg_create_named_input( "jump", k_input_type_button
);
74 inst
->input_push
= vg_create_named_input( "push", k_input_type_button
);
75 inst
->input_walk
= vg_create_named_input( "walk", k_input_type_button
);
76 inst
->input_walkh
= vg_create_named_input( "walk-h", k_input_type_axis
);
77 inst
->input_walkv
= vg_create_named_input( "walk-v", k_input_type_axis
);
78 inst
->input_use
= vg_create_named_input( "use", k_input_type_button
);
79 inst
->input_reset
= vg_create_named_input( "reset", k_input_type_button
);
81 const char *default_cfg
[] =
83 "bind steer-h gp-ls-h",
87 "bind steer-v gp-ls-v",
93 "bind grab-h gp-rs-h",
94 "bind grab-v gp-rs-v",
105 "bind walk-h gp-ls-h",
106 "bind walk-v -gp-ls-v",
119 for( int i
=0; i
<vg_list_size(default_cfg
); i
++ )
120 vg_execute_console_input(default_cfg
[i
]);
122 v3_zero( inst
->rb
.co
);
123 v3_zero( inst
->rb
.w
);
124 v3_zero( inst
->rb
.v
);
125 q_identity( inst
->rb
.q
);
126 m4x3_identity( inst
->rb
.to_world
);
127 m4x3_identity( inst
->rb
.to_local
);
130 VG_STATIC
void player_use_device( player_interface
*player
, player_device
*dev
,
133 player
->dev
.device
= dev
;
134 player
->dev
.storage
= storage
;
137 VG_STATIC
void player_pre_update( player_interface
*player
)
139 assert( player
->dev
.device
);
141 if( player
->dev
.device
->pre_update
)
142 player
->dev
.device
->pre_update( player
, &player
->dev
);
145 VG_STATIC
void player_update( player_interface
*player
)
147 assert( player
->dev
.device
);
149 if( player
->dev
.device
->update
)
150 player
->dev
.device
->update( player
, &player
->dev
);
153 VG_STATIC
void player_post_update( player_interface
*player
)
155 assert( player
->dev
.device
);
157 if( player
->dev
.device
->post_update
)
158 player
->dev
.device
->post_update( player
, &player
->dev
);
160 if( player
->dev
.device
->get_camera
)
161 player
->dev
.device
->get_camera( player
, &player
->dev
, &player
->cam
);
163 camera_update_transform( &player
->cam
);
164 camera_update_view( &player
->cam
);
165 camera_update_projection( &player
->cam
);
166 camera_finalize( &player
->cam
);
171 VG_STATIC
void player_pre_render( player_interface
*player
)
173 assert( player
->dev
.device
);
175 if( player
->dev
.device
->pre_render
)
176 player
->dev
.device
->pre_render( player
);
180 VG_STATIC
void player_debugtext( int size
, const char *fmt
, ... )
185 va_start( args
, fmt
);
186 vsnprintf( buffer
, 1024, fmt
, args
);
189 ui_text( vg_uictx
.cursor
, buffer
, size
, k_text_align_right
);
190 vg_uictx
.cursor
[1] += 14*size
;
193 VG_STATIC
void player_ui( player_interface
*player
)
195 /* TODO: if debugger enabled */
197 if( player
->dev
.device
->debug_ui
)
199 vg_uictx
.cursor
[0] = vg
.window_x
- 200;
200 vg_uictx
.cursor
[1] = 0;
201 vg_uictx
.cursor
[2] = 0;
202 vg_uictx
.cursor
[3] = 200;
204 struct ui_vert
*b
= ui_fill_rect( vg_uictx
.cursor
, 0x70000000 );
206 player
->dev
.device
->debug_ui( player
, &player
->dev
);
208 b
[2].co
[1] = vg_uictx
.cursor
[1];
209 b
[3].co
[1] = vg_uictx
.cursor
[1];
213 VG_STATIC
void player_spawn( player_interface
*player
,
214 struct respawn_point
*rp
)
216 v3_copy( rp
->co
, player
->rb
.co
);
217 v3_zero( player
->rb
.v
);
218 v3_zero( player
->rb
.w
);
219 q_identity( player
->rb
.q
);
223 * Apply per render-frame mouse look from player to angles
225 VG_STATIC
void player_look( player_interface
*player
, v3f angles
)
228 v2_muladds( angles
, vg
.mouse_delta
, 0.0025f
, angles
);
230 if( vg_input
.controller_should_use_trackpad_look
)
232 static v2f last_input
;
234 static v2f vel_smooth
;
236 v2f input
= { player
->input_js2h
->axis
.value
,
237 player
->input_js2v
->axis
.value
};
239 if( (v2_length2(last_input
) > 0.001f
) && (v2_length2(input
) > 0.001f
) )
241 v2_sub( input
, last_input
, vel
);
242 v2_muls( vel
, 1.0f
/vg
.time_delta
, vel
);
249 v2_lerp( vel_smooth
, vel
, vg
.time_delta
*8.0f
, vel_smooth
);
251 v2_muladds( angles
, vel_smooth
, vg
.time_delta
, angles
);
252 v2_copy( input
, last_input
);
256 angles
[0] += player
->input_js2h
->axis
.value
* vg
.time_delta
* 4.0f
;
257 angles
[1] += player
->input_js2v
->axis
.value
* vg
.time_delta
* 4.0f
;
260 angles
[1] = vg_clampf( angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
263 #endif /* PLAYER_INTERFACE_H */