dark-hammer / src / engine / components / cmp-anim.c

The branch '0.4.7' does not exist.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/***********************************************************************************
 * Copyright (c) 2013, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/

#include "core/core.h"

#include "anim.h"
#include "cmp-mgr.h"
#include "res-mgr.h"
#include "gfx-model.h"
#include "anim.h"
#include "mem-ids.h"
#include "engine.h"

#include "components/cmp-anim.h"
#include "components/cmp-model.h"


/*************************************************************************************************
 * fwd declarations
 */
result_t cmp_anim_create(struct cmp_obj* obj, void* data, cmphandle_t hdl);
void cmp_anim_destroy(struct cmp_obj* obj, void* data, cmphandle_t hdl);
void cmp_anim_update(cmp_t c, fl32 dt, void* param);
void cmp_anim_destroydata(struct cmp_obj* obj, struct cmp_anim* data, cmphandle_t hdl,
    bool_t release_mesh);
result_t cmp_anim_bind(struct cmp_obj* obj, void* data, struct allocator* alloc, 
    struct allocator* tmp_alloc, reshandle_t hdl);
result_t cmp_anim_createhierarchy(struct cmp_anim* a, struct cmp_model* m, struct gfx_model* gmodel);
void cmp_anim_destroyhierarchy(struct cmp_anim* a);

void cmp_anim_clearinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
    uint32 cnt);
void cmp_anim_rebuildhinstances(struct allocator* alloc, struct allocator* tmp_alloc, 
    reshandle_t clip_hdl, const struct cmp_instance_desc** insts, uint32 cnt);
void cmp_anim_destroyinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
    uint32 cnt);

/*************************************************************************************************/
result_t cmp_anim_register(struct allocator* alloc)
{
    struct cmp_createparams params;
    memset(&params, 0x00, sizeof(params));

    params.name = "anim";
    params.stride = sizeof(struct cmp_anim);
    params.create_func = cmp_anim_create;
    params.destroy_func = cmp_anim_destroy;
    //params.debug_func = cmp_anim_debug;
    params.grow_cnt = 300;
    params.initial_cnt = 300;
    params.values = cmp_anim_values;
    params.value_cnt = cmp_anim_valuecnt;
    params.type = cmp_anim_type;
    params.update_funcs[CMP_UPDATE_STAGE1] = cmp_anim_update;
    params.flags = CMP_FLAG_ALWAYSUPDATE;
    return cmp_register_component(alloc, &params);
}

result_t cmp_anim_create(struct cmp_obj* obj, void* data, cmphandle_t hdl)
{
    struct cmp_anim* anim = data;
    anim->play_rate = 1.0f;
    anim->clip_hdl = INVALID_HANDLE;
    anim->clip_id = 0;
    anim->frame_idx = INVALID_INDEX;

    obj->anim_cmp = hdl;
    return RET_OK;
}

void cmp_anim_destroy(struct cmp_obj* obj, void* data, cmphandle_t hdl)
{
    cmp_anim_destroydata(obj, data, hdl, TRUE);

    obj->anim_cmp = INVALID_HANDLE;
}

void cmp_anim_update(cmp_t c, fl32 dt, void* param)
{
    uint32 cnt;
    const struct cmp_instance_desc** updates = cmp_get_updateinstances(c, &cnt);
    for (uint32 i = 0; i < cnt; i++)	{
        const struct cmp_instance_desc* inst = updates[i];
        struct cmp_anim* a = (struct cmp_anim*)inst->data;
#if !defined(_RETAIL_)
        if (a->clip_hdl != INVALID_HANDLE)   {
#endif
            struct anim_clip* clip = rs_get_anim(a->clip_hdl);
            fl32 t = a->t;
            struct anim_subclip* subclip = &clip->subclips[a->clip_id];
#if !defined(_RETAIL_)            
            if (a->frame_idx == INVALID_INDEX)  {
#endif
                if (subclip->looped)    {
                    t = fmodf(t, subclip->duration);
                }   else if (t > subclip->duration)    {
                    a->t = 0.0f;
                    cmp_anim_stop(inst->hdl);
                    continue;
                }
#if !defined(_RETAIL_)
            }   else    {
                t = ((fl32)a->frame_idx)*clip->ft;
            }            
#endif

            anim_update_clip_hierarchal(clip, a->clip_id, t, a->bindmap, a->xform_hdls, 
                a->frame_idx);
#if !defined(_RETAIL_)
        }
#endif
        a->t += dt * a->play_rate;
    }
}

result_t cmp_anim_modify(struct cmp_obj* obj, struct allocator* alloc, struct allocator* tmp_alloc, 
    void* data, cmphandle_t cur_hdl)
{
    result_t r;
    struct cmp_anim* a = data;

    uint32 filehash = hashtable_str(a->filepath);
    bool_t reload = (a->filepathhash == filehash);
    cmp_anim_destroydata(obj, a, cur_hdl, !reload);

    a->alloc = alloc;
    if (str_isempty(a->filepath))
        return RET_OK;

    if (!reload)
        a->clip_hdl = rs_load_anim(a->filepath, 0);
    
    if (a->clip_hdl == INVALID_HANDLE) {
        a->filepath[0] = 0;
        return RET_FAIL;
    }

    /* bind */
    r = cmp_anim_bind(obj, a, alloc, tmp_alloc, cur_hdl);
    if (IS_FAIL(r)) {
        err_sendtolog(TRUE);
        log_print(LOG_WARNING, "binding anim-set failed");
        return RET_FAIL;
    }

    a->filepathhash = filehash;
    return RET_OK;
}

result_t cmp_anim_bind(struct cmp_obj* obj, void* data, struct allocator* alloc, 
    struct allocator* tmp_alloc, reshandle_t hdl)
{
    struct cmp_anim* a = data;

    /* bind animation to object, by evaluating object's model component
     * - obj->model_cmp is invalid: wait to model to update (will be called from cmp_model)
     * - obj->model_cmp is skinned: bind skeleton, find first geometry that is skinned, bind to that
     * - obj->model_cmp is not skinned: bind hierarchal animation
     */
     if (obj->model_cmp == INVALID_HANDLE)
        return RET_OK;

    struct cmp_model* m = cmp_getinstancedata(obj->model_cmp);
    if (m->model_hdl == INVALID_HANDLE)
        return RET_OK;

    /* choose between skinned geometry and hierarchal */
    struct gfx_model* gmodel = rs_get_model(m->model_hdl);
    uint32 geo_idx = INVALID_INDEX;
    for (uint32 i = 0; i < gmodel->geo_cnt; i++)    {
        if (gmodel->geos[i].skeleton != NULL)   {
            geo_idx = i;
            break;
        }
    }

    result_t r = RET_FAIL;
    if (geo_idx != INVALID_INDEX)   {
        ASSERT(0);
    }    else   {
        r = cmp_anim_createhierarchy(a, m, gmodel);
    }

    if (IS_OK(r))   {
        cmp_anim_play(hdl);
    }

    return r;
}

result_t cmp_anim_createhierarchy(struct cmp_anim* a, struct cmp_model* m, struct gfx_model* gmodel)
{
    a->xform_hdls = m->xforms;
    a->xform_cnt = m->xform_cnt;

    /* bind maps */
    struct anim_clip* clip = rs_get_anim(a->clip_hdl);
    uint32 pose_cnt = clip->pose_cnt;
    a->bindmap = A_ALLOC(a->alloc, sizeof(uint32)*clip->pose_cnt, MID_ANIM);
    if (a->bindmap == NULL)    {
        cmp_anim_destroyhierarchy(a);
        return RET_OUTOFMEMORY;
    }

    for (uint32 i = 0; i < pose_cnt; i++)   {
        a->bindmap[i] = gfx_model_findnode(gmodel, clip->binds + i*32);
        if (a->bindmap[i] == INVALID_INDEX) {
            cmp_anim_destroyhierarchy(a);
            return RET_FAIL;
        }
    } /* foreach: pose */

    return RET_OK;
}

void cmp_anim_destroyhierarchy(struct cmp_anim* a)
{
    if (a->alloc != NULL && a->bindmap != NULL)    {
        A_FREE(a->alloc, a->bindmap);
    }

    a->xform_cnt = 0;
    a->xform_hdls = NULL;
    a->bindmap = NULL;
}

void cmp_anim_play(cmphandle_t hdl)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    a->frame_idx = INVALID_INDEX;
    a->playing = TRUE;

    cmp_updateinstance(hdl);
}

void cmp_anim_stop(cmphandle_t hdl)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    a->playing = FALSE;

    cmp_updateinstance_reset(hdl);
}

void cmp_anim_destroydata(struct cmp_obj* obj, struct cmp_anim* data, cmphandle_t hdl,
    bool_t release_clip)
{
    cmp_anim_stop(hdl);

    cmp_anim_destroyhierarchy(data);

    if (release_clip && data->clip_hdl != INVALID_HANDLE)   {
        rs_unload(data->clip_hdl);
        data->filepathhash = 0;
        data->filepath[0] = 0;
        data->clip_hdl = INVALID_HANDLE;
    }

    data->alloc = NULL;
}

void cmp_anim_reload(const char* filepath, reshandle_t hdl, uptr_t param1, uptr_t param2)
{
    uint32 cnt;
    struct allocator* tmp_alloc = eng_get_framealloc();
    cmp_t c = cmp_findtype(cmp_anim_type);
    const struct cmp_instance_desc** insts = cmp_get_allinstances(c, &cnt, tmp_alloc);

    /* reload model and refresh all model component data */
    cmp_anim_destroyinstances(hdl, insts, cnt);
    reshandle_t nhdl = rs_load_anim(filepath, RS_LOAD_REFRESH);
    if (nhdl != INVALID_HANDLE)
        cmp_anim_rebuildhinstances(eng_get_dataalloc(), tmp_alloc, hdl, insts, cnt);
    else
        cmp_anim_clearinstances(hdl, insts, cnt);  /* this happens when model-reload fails,
                                                       we have to invalidate handles */

    A_FREE(tmp_alloc, insts);
}

void cmp_anim_destroyinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
    uint32 cnt)
{
    for (uint32 i = 0; i < cnt; i++) {
        const struct cmp_instance_desc* inst = insts[i];
        const struct cmp_anim* a = (const struct cmp_anim*)inst->data;
        if (clip_hdl == a->clip_hdl)
            cmp_anim_destroydata(inst->host, (struct cmp_anim*)inst->data, inst->hdl, FALSE);
    }
}

void cmp_anim_rebuildhinstances(struct allocator* alloc, struct allocator* tmp_alloc, 
    reshandle_t clip_hdl, const struct cmp_instance_desc** insts, uint32 cnt)
{
    for (uint32 i = 0; i < cnt; i++) {
        const struct cmp_instance_desc* inst = insts[i];
        const struct cmp_anim* a = (const struct cmp_anim*)inst->data;
        if (clip_hdl == a->clip_hdl)  {
            cmp_anim_modify(inst->host, alloc, tmp_alloc, inst->data, inst->hdl);
        }
    }
}

void cmp_anim_clearinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
    uint32 cnt)
{
    for (uint32 i = 0; i < cnt; i++) {
        const struct cmp_instance_desc* inst = insts[i];
        struct cmp_anim* a = (struct cmp_anim*)inst->data;
        if (clip_hdl == a->clip_hdl)
            a->clip_hdl = INVALID_HANDLE;
    }
}

result_t cmp_anim_modify_clip(struct cmp_obj* obj, struct allocator* alloc,
    struct allocator* tmp_alloc, void* data, cmphandle_t cur_hdl)
{
    struct cmp_anim* a = data;
    if (a->clip_hdl != INVALID_HANDLE)   {
        struct anim_clip* clip = rs_get_anim(a->clip_hdl);
        uint32 clip_idx = anim_findclip(clip, a->clip_name);
        if (clip_idx != INVALID_INDEX)   {
            a->clip_id = clip_idx;
        } else  {
            a->clip_id = 0;
            log_printf(LOG_WARNING, "Animation clip '%s' does not exist in '%s'", a->clip_name,
                a->filepath);
        }
    }
    a->t = 0.0f;
    return RET_OK;
}

uint32 cmp_anim_getframecnt(cmphandle_t hdl)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    if (a->clip_hdl != INVALID_HANDLE)  {
        struct anim_clip* clip = rs_get_anim(a->clip_hdl);
        return clip->frame_cnt;
    }   else    {
        return 0;
    }        
}

result_t cmp_anim_modify_frame(struct cmp_obj* obj, struct allocator* alloc,
    struct allocator* tmp_alloc, void* data, cmphandle_t cur_hdl)
{
    struct cmp_anim* a = data;
    uint32 frame_cnt = 0;
    if (a->clip_hdl != INVALID_HANDLE)  {
        struct anim_clip* clip = rs_get_anim(a->clip_hdl);
        frame_cnt = clip->frame_cnt;
        a->frame_idx = clampun(a->frame_idx, 0, frame_cnt-1);
        a->playing = FALSE;
        cmp_updateinstance(cur_hdl);
    }  else {
        a->frame_idx = INVALID_INDEX;
    }
    return RET_OK;
}

bool_t cmp_anim_isplaying(cmphandle_t hdl)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    return a->playing;
}

uint32 cmp_anim_getcurframe(cmphandle_t hdl)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    if (a->frame_idx != INVALID_INDEX)
        return a->frame_idx;

    if (a->clip_hdl != INVALID_HANDLE)  {
        struct anim_clip* clip = rs_get_anim(a->clip_hdl);
        return clampun((uint32)(a->t/clip->ft), 0, clip->frame_cnt-1);
    }  else {
        return 0;
    }
}

uint32 cmp_anim_getfps(cmphandle_t hdl)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    if (a->clip_hdl != INVALID_HANDLE)  {
        struct anim_clip* clip = rs_get_anim(a->clip_hdl);
        return clip->fps;
    }   else    {
        return 0;
    } 
}

uint32 cmp_anim_getclipcnt(cmphandle_t hdl)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    if (a->clip_hdl != INVALID_HANDLE)  {
        struct anim_clip* clip = rs_get_anim(a->clip_hdl);
        return clip->subclip_cnt;
    }    else   {
        return 0;
    }
}

const char* cmp_anim_getclipname(cmphandle_t hdl, uint32 clip_idx)
{
    struct cmp_anim* a = cmp_getinstancedata(hdl);
    if (a->clip_hdl != INVALID_HANDLE)  {
        struct anim_clip* clip = rs_get_anim(a->clip_hdl);
        ASSERT(clip_idx , clip->subclip_cnt);
        return clip->subclips[clip_idx].name;
    }    else   {
        return "";
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.