c890166d93cbe30ca5b03146852e953af96c023a
[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 "world.h"
8
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];
13
14 struct player_interface
15 {
16 rigidbody rb;
17 camera cam;
18
19 struct player_attachment
20 {
21 player_device *device;
22 void *storage;
23 }
24 dev;
25
26 struct input_binding *input_js1h,
27 *input_js1v,
28 *input_js2h,
29 *input_js2v,
30 *input_jump,
31 *input_push,
32 *input_walk,
33 *input_walkh,
34 *input_walkv,
35 *input_use,
36 *input_reset,
37 *input_grab;
38 };
39
40 struct player_device
41 {
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,
46 player_pose pose );
47
48 void (* get_camera) ( player_interface *player, player_attachment *at,
49 camera *cam );
50
51 void (* attatch ) ( player_interface *player, player_attachment *at,
52 void *storage );
53
54 void (* reset ) ( player_interface *player, player_attachment *at,
55 struct respawn_point *spawn );
56
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 );
60 };
61
62 VG_STATIC void player_interface_create_player( player_interface *inst )
63 {
64 static int only_once = 0;
65 assert( only_once == 0 );
66 only_once ++;
67
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 );
80
81 const char *default_cfg[] =
82 {
83 "bind steer-h gp-ls-h",
84 "bind -steer-h a",
85 "bind +steer-h d",
86
87 "bind steer-v gp-ls-v",
88 "bind -steer-v w",
89 "bind +steer-v s",
90
91 "bind grab gp-rt",
92 "bind +grab shift",
93 "bind grab-h gp-rs-h",
94 "bind grab-v gp-rs-v",
95
96 "bind jump space",
97 "bind jump gp-a",
98
99 "bind push gp-b",
100 "bind push w",
101
102 "bind walk shift",
103 "bind walk gp-ls",
104
105 "bind walk-h gp-ls-h",
106 "bind walk-v -gp-ls-v",
107 "bind +walk-h d",
108 "bind -walk-h a",
109 "bind +walk-v w",
110 "bind -walk-v s",
111
112 "bind reset gp-lb",
113 "bind reset r",
114
115 "bind use gp-y",
116 "bind use e",
117 };
118
119 for( int i=0; i<vg_list_size(default_cfg); i++ )
120 vg_execute_console_input(default_cfg[i]);
121
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 );
128 }
129
130 VG_STATIC void player_use_device( player_interface *player, player_device *dev,
131 void *storage )
132 {
133 player->dev.device = dev;
134 player->dev.storage = storage;
135 }
136
137 VG_STATIC void player_pre_update( player_interface *player )
138 {
139 assert( player->dev.device );
140
141 if( player->dev.device->pre_update )
142 player->dev.device->pre_update( player, &player->dev );
143 }
144
145 VG_STATIC void player_update( player_interface *player )
146 {
147 assert( player->dev.device );
148
149 if( player->dev.device->update )
150 player->dev.device->update( player, &player->dev );
151 }
152
153 VG_STATIC void player_post_update( player_interface *player )
154 {
155 assert( player->dev.device );
156
157 if( player->dev.device->post_update )
158 player->dev.device->post_update( player, &player->dev );
159
160 if( player->dev.device->get_camera )
161 player->dev.device->get_camera( player, &player->dev, &player->cam );
162 #if 0
163 camera_update_transform( &player->cam );
164 camera_update_view( &player->cam );
165 camera_update_projection( &player->cam );
166 camera_finalize( &player->cam );
167 #endif
168 }
169
170 #if 0
171 VG_STATIC void player_pre_render( player_interface *player )
172 {
173 assert( player->dev.device );
174
175 if( player->dev.device->pre_render )
176 player->dev.device->pre_render( player );
177 }
178 #endif
179
180 VG_STATIC void player_debugtext( int size, const char *fmt, ... )
181 {
182 char buffer[ 1024 ];
183
184 va_list args;
185 va_start( args, fmt );
186 vsnprintf( buffer, 1024, fmt, args );
187 va_end( args );
188
189 ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
190 vg_uictx.cursor[1] += 14*size;
191 }
192
193 VG_STATIC void player_ui( player_interface *player )
194 {
195 /* TODO: if debugger enabled */
196
197 if( player->dev.device->debug_ui )
198 {
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;
203
204 struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 );
205
206 player->dev.device->debug_ui( player, &player->dev );
207
208 b[2].co[1] = vg_uictx.cursor[1];
209 b[3].co[1] = vg_uictx.cursor[1];
210 }
211 }
212
213 VG_STATIC void player_spawn( player_interface *player,
214 struct respawn_point *rp )
215 {
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 );
220 }
221
222 /*
223 * Apply per render-frame mouse look from player to angles
224 */
225 VG_STATIC void player_look( player_interface *player, v3f angles )
226 {
227 angles[2] = 0.0f;
228 v2_muladds( angles, vg.mouse_delta, 0.0025f, angles );
229
230 if( vg_input.controller_should_use_trackpad_look )
231 {
232 static v2f last_input;
233 static v2f vel;
234 static v2f vel_smooth;
235
236 v2f input = { player->input_js2h->axis.value,
237 player->input_js2v->axis.value };
238
239 if( (v2_length2(last_input) > 0.001f) && (v2_length2(input) > 0.001f) )
240 {
241 v2_sub( input, last_input, vel );
242 v2_muls( vel, 1.0f/vg.time_delta, vel );
243 }
244 else
245 {
246 v2_zero( vel );
247 }
248
249 v2_lerp( vel_smooth, vel, vg.time_delta*8.0f, vel_smooth );
250
251 v2_muladds( angles, vel_smooth, vg.time_delta, angles );
252 v2_copy( input, last_input );
253 }
254 else
255 {
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;
258 }
259
260 angles[1] = vg_clampf( angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
261 }
262
263 #endif /* PLAYER_INTERFACE_H */