add jumping
[carveJwlIkooP6JGAAIwe30JlM.git] / player_model.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef CHARACTER_H
6 #define CHARACTER_H
7
8 #include "player.h"
9 #include "player_ragdoll.h"
10 #include "shaders/viewchar.h"
11
12 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
13
14 static void player_model_init(void)
15 {
16 shader_viewchar_register();
17 vg_acquire_thread_sync();
18 {
19 vg_tex2d_init( (vg_tex2d *[]){ &tex_characters }, 1 );
20 }
21 vg_release_thread_sync();
22 }
23
24 static void player_model_free(void *_)
25 {
26 mesh_free( &player.mdl.mesh );
27 vg_tex2d_free( (vg_tex2d *[]){ &tex_characters }, 1 );
28 }
29
30 /*
31 * Load model from file (.mdl)
32 */
33 static void player_load_model( const char *name )
34 {
35 char buf[64];
36
37 snprintf( buf, sizeof(buf)-1, "models/%s.mdl", name );
38 mdl_header *src = mdl_load( buf );
39
40 if( !src )
41 {
42 vg_error( "Could not load model\n" );
43 return;
44 }
45
46 struct player_model temp;
47
48 mdl_unpack_glmesh( src, &temp.mesh );
49 skeleton_setup( &temp.sk, src );
50
51 /*
52 * Link animations
53 */
54 struct _load_anim
55 {
56 const char *name;
57 struct skeleton_anim **anim;
58 }
59 anims[] = {
60 { "pose_stand", &temp.anim_stand },
61 { "pose_highg", &temp.anim_highg },
62 { "pose_slide", &temp.anim_slide },
63 { "pose_air", &temp.anim_air },
64 { "push", &temp.anim_push },
65 { "push_reverse", &temp.anim_push_reverse },
66 { "ollie", &temp.anim_ollie },
67 { "ollie_reverse",&temp.anim_ollie_reverse },
68 { "grabs", &temp.anim_grabs },
69 { "walk", &temp.anim_walk },
70 { "run", &temp.anim_run },
71 { "idle_cycle", &temp.anim_idle },
72 { "jump", &temp.anim_jump }
73 };
74
75 for( int i=0; i<vg_list_size(anims); i++ )
76 {
77 *anims[i].anim = skeleton_get_anim( &temp.sk, anims[i].name );
78
79 if( !(*anims[i].anim) )
80 {
81 vg_error( "Animation '%s' is missing from character '%s'\n",
82 anims[i].name, name );
83 vg_free( src );
84 return;
85 }
86 }
87
88 /*
89 * Link bones
90 */
91 struct _load_bone
92 {
93 const char *name;
94 u32 *bone_id;
95 }
96 bones[] = {
97 { "hips", &temp.id_hip },
98 { "hand.IK.L", &temp.id_ik_hand_l },
99 { "hand.IK.R", &temp.id_ik_hand_r },
100 { "elbow.L", &temp.id_ik_elbow_l },
101 { "elbow.R", &temp.id_ik_elbow_r },
102 { "head", &temp.id_head }
103 };
104
105 for( int i=0; i<vg_list_size(bones); i++ )
106 {
107 *bones[i].bone_id = skeleton_bone_id( &temp.sk, bones[i].name );
108
109 if( !(*bones[i].bone_id) )
110 {
111 vg_error( "Required bone '%s' is missing from character '%s'\n",
112 bones[i].name, name );
113 vg_free( src );
114 return;
115 }
116 }
117
118 /* swap temp into actual model */
119 mesh_free( &player.mdl.mesh );
120 player.mdl = temp;
121 player_init_ragdoll( src );
122 vg_free( src );
123 }
124
125 #endif