f28d7a82ac5b960fd4238cae9b0c72f442c5bd14
[carveJwlIkooP6JGAAIwe30JlM.git] / player_interface.h
1 #ifndef PLAYER_INTERFACE_H
2 #define PLAYER_INTERFACE_H
3
4 #include "model.h"
5 #include "camera.h"
6 #include "rigidbody.h"
7 #include "player_ragdoll.h"
8 #include "player_model.h"
9 #include "world.h"
10
11 typedef struct player_device player_device;
12 typedef struct player_interface player_interface;
13 typedef struct player_attachment player_attachment;
14 typedef mdl_keyframe player_pose[32];
15
16 struct player_interface
17 {
18 rigidbody rb;
19 camera cam;
20
21 struct player_attachment
22 {
23 player_device *device;
24 void *storage;
25 }
26 dev;
27
28 struct input_binding *input_js1h,
29 *input_js1v,
30 *input_js2h,
31 *input_js2v,
32 *input_jump,
33 *input_push,
34 *input_walk,
35 *input_walkh,
36 *input_walkv,
37 *input_use,
38 *input_reset,
39 *input_grab;
40
41 struct player_avatar *playeravatar;
42 glmesh *playermesh;
43 struct player_ragdoll ragdoll;
44 };
45
46 /* FIXME: yo */
47 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
48
49 struct player_device
50 {
51 void (* bind ) ( player_interface *player, player_attachment *at );
52 void (* pre_update) ( player_interface *player, player_attachment *at );
53 void (* update) ( player_interface *player, player_attachment *at );
54 void (* post_update)( player_interface *player, player_attachment *at );
55 void (* pose) ( player_interface *player, player_attachment *at,
56 player_pose pose, m4x3f transform );
57
58 void (* get_camera) ( player_interface *player, player_attachment *at,
59 camera *cam );
60
61 void (* attatch ) ( player_interface *player, player_attachment *at,
62 void *storage );
63
64 void (* reset ) ( player_interface *player, player_attachment *at,
65 struct respawn_point *spawn );
66
67 void (* store_state)( player_interface *player, player_attachment *at );
68 void (* load_state) ( player_interface *player, player_attachment *at );
69 void (* debug_ui) ( player_interface *player, player_attachment *at );
70 };
71
72 VG_STATIC void player_interface_create_player( player_interface *inst )
73 {
74 static int only_once = 0;
75 assert( only_once == 0 );
76 only_once ++;
77
78 inst->input_js1h = vg_create_named_input( "steer-h", k_input_type_axis );
79 inst->input_js1v = vg_create_named_input( "steer-v", k_input_type_axis );
80 inst->input_grab = vg_create_named_input( "grab", k_input_type_axis_norm);
81 inst->input_js2h = vg_create_named_input( "grab-h", k_input_type_axis );
82 inst->input_js2v = vg_create_named_input( "grab-v", k_input_type_axis );
83 inst->input_jump = vg_create_named_input( "jump", k_input_type_button );
84 inst->input_push = vg_create_named_input( "push", k_input_type_button );
85 inst->input_walk = vg_create_named_input( "walk", k_input_type_button );
86 inst->input_walkh= vg_create_named_input( "walk-h", k_input_type_axis );
87 inst->input_walkv= vg_create_named_input( "walk-v", k_input_type_axis );
88 inst->input_use = vg_create_named_input( "use", k_input_type_button );
89 inst->input_reset= vg_create_named_input( "reset", k_input_type_button );
90
91 const char *default_cfg[] =
92 {
93 "bind steer-h gp-ls-h",
94 "bind -steer-h a",
95 "bind +steer-h d",
96
97 "bind steer-v gp-ls-v",
98 "bind -steer-v w",
99 "bind +steer-v s",
100
101 "bind grab gp-rt",
102 "bind +grab shift",
103 "bind grab-h gp-rs-h",
104 "bind grab-v gp-rs-v",
105
106 "bind jump space",
107 "bind jump gp-a",
108
109 "bind push gp-b",
110 "bind push w",
111
112 "bind walk shift",
113 "bind walk gp-ls",
114
115 "bind walk-h gp-ls-h",
116 "bind walk-v -gp-ls-v",
117 "bind +walk-h d",
118 "bind -walk-h a",
119 "bind +walk-v w",
120 "bind -walk-v s",
121
122 "bind reset gp-lb",
123 "bind reset r",
124
125 "bind use gp-y",
126 "bind use e",
127 };
128
129 for( int i=0; i<vg_list_size(default_cfg); i++ )
130 vg_execute_console_input(default_cfg[i]);
131
132 v3_zero( inst->rb.co );
133 v3_zero( inst->rb.w );
134 v3_zero( inst->rb.v );
135 q_identity( inst->rb.q );
136 m4x3_identity( inst->rb.to_world );
137 m4x3_identity( inst->rb.to_local );
138 }
139
140 VG_STATIC void player_use_avatar( player_interface *player,
141 struct player_avatar *av )
142 {
143 player->playeravatar = av;
144 player_setup_ragdoll_from_avatar( &player->ragdoll, av );
145 }
146
147 VG_STATIC void player_use_mesh( player_interface *player, glmesh *mesh )
148 {
149 player->playermesh = mesh;
150 }
151
152 VG_STATIC void player_use_device( player_interface *player, player_device *dev,
153 void *storage )
154 {
155 player->dev.device = dev;
156 player->dev.storage = storage;
157
158 player->dev.device->bind( player, &player->dev );
159 }
160
161 VG_STATIC void player_pre_update( player_interface *player )
162 {
163 assert( player->dev.device );
164
165 if( player->dev.device->pre_update )
166 player->dev.device->pre_update( player, &player->dev );
167 }
168
169 VG_STATIC void player_update( player_interface *player )
170 {
171 assert( player->dev.device );
172
173 if( player->dev.device->update )
174 player->dev.device->update( player, &player->dev );
175 }
176
177 VG_STATIC void player_post_update( player_interface *player )
178 {
179 assert( player->dev.device );
180
181 if( player->dev.device->post_update )
182 player->dev.device->post_update( player, &player->dev );
183
184 #if 0
185 camera_update_transform( &player->cam );
186 camera_update_view( &player->cam );
187 camera_update_projection( &player->cam );
188 camera_finalize( &player->cam );
189 #endif
190 }
191
192 #if 0
193 VG_STATIC void player_pre_render( player_interface *player )
194 {
195 assert( player->dev.device );
196
197 if( player->dev.device->pre_render )
198 player->dev.device->pre_render( player );
199 }
200 #endif
201
202 VG_STATIC void player_pre_render( player_interface *player )
203 {
204 player_pose pose;
205 m4x3f transform;
206
207 player->dev.device->pose( player, &player->dev, pose, transform );
208
209 struct skeleton *sk = &player->playeravatar->sk;
210
211 skeleton_apply_pose( sk, pose, k_anim_apply_defer_ik );
212 skeleton_apply_ik_pass( sk );
213 skeleton_apply_pose( sk, pose, k_anim_apply_deffered_only );
214 skeleton_apply_inverses( sk );
215 skeleton_apply_transform( sk, transform );
216 skeleton_debug( sk );
217
218 player->dev.device->get_camera( player, &player->dev, &player->cam );
219 /* TODO: if dead copy ragdoll.. . */
220 }
221
222 VG_STATIC void player_render( camera *cam, player_interface *player )
223 {
224 shader_viewchar_use();
225 vg_tex2d_bind( &tex_characters, 0 );
226 shader_viewchar_uTexMain( 0 );
227 shader_viewchar_uCamera( cam->transform[3] );
228 shader_viewchar_uPv( cam->mtx.pv );
229 shader_link_standard_ub( _shader_viewchar.id, 2 );
230 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
231 player->playeravatar->sk.bone_count,
232 0,
233 (float *)player->playeravatar->sk.final_mtx );
234
235 mesh_bind( player->playermesh );
236 mesh_draw( player->playermesh );
237 }
238
239 VG_STATIC void player_debugtext( int size, const char *fmt, ... )
240 {
241 char buffer[ 1024 ];
242
243 va_list args;
244 va_start( args, fmt );
245 vsnprintf( buffer, 1024, fmt, args );
246 va_end( args );
247
248 ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
249 vg_uictx.cursor[1] += 14*size;
250 }
251
252 VG_STATIC void player_ui( player_interface *player )
253 {
254 /* TODO: if debugger enabled */
255
256 if( player->dev.device->debug_ui )
257 {
258 vg_uictx.cursor[0] = vg.window_x - 200;
259 vg_uictx.cursor[1] = 0;
260 vg_uictx.cursor[2] = 200;
261 vg_uictx.cursor[3] = 200;
262
263 struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 );
264
265 vg_uictx.cursor[0] = vg.window_x;
266 player->dev.device->debug_ui( player, &player->dev );
267
268 b[2].co[1] = vg_uictx.cursor[1];
269 b[3].co[1] = vg_uictx.cursor[1];
270 }
271 }
272
273 VG_STATIC void player_spawn( player_interface *player,
274 struct respawn_point *rp )
275 {
276 v3_copy( rp->co, player->rb.co );
277 v3_zero( player->rb.v );
278 v3_zero( player->rb.w );
279 q_identity( player->rb.q );
280 }
281
282 /*
283 * Apply per render-frame mouse look from player to angles
284 */
285 VG_STATIC void player_look( player_interface *player, v3f angles )
286 {
287 angles[2] = 0.0f;
288 v2_muladds( angles, vg.mouse_delta, 0.0025f, angles );
289
290 if( vg_input.controller_should_use_trackpad_look )
291 {
292 static v2f last_input;
293 static v2f vel;
294 static v2f vel_smooth;
295
296 v2f input = { player->input_js2h->axis.value,
297 player->input_js2v->axis.value };
298
299 if( (v2_length2(last_input) > 0.001f) && (v2_length2(input) > 0.001f) )
300 {
301 v2_sub( input, last_input, vel );
302 v2_muls( vel, 1.0f/vg.time_delta, vel );
303 }
304 else
305 {
306 v2_zero( vel );
307 }
308
309 v2_lerp( vel_smooth, vel, vg.time_delta*8.0f, vel_smooth );
310
311 v2_muladds( angles, vel_smooth, vg.time_delta, angles );
312 v2_copy( input, last_input );
313 }
314 else
315 {
316 angles[0] += player->input_js2h->axis.value * vg.time_delta * 4.0f;
317 angles[1] += player->input_js2v->axis.value * vg.time_delta * 4.0f;
318 }
319
320 angles[1] = vg_clampf( angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
321 }
322
323 #endif /* PLAYER_INTERFACE_H */