2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Anim/AnimPlay.cpp $
15 * C module for playing back anim files
18 * Revision 1.7 2006/04/26 19:48:58 taylor
19 * various big-endian fixes, mainly networking support related
21 * Revision 1.6 2005/08/12 08:55:13 taylor
22 * sync up talking head fixes from FS2_Open code base (still not 100%)
24 * Revision 1.5 2004/09/20 01:31:44 theoddone33
27 * Revision 1.4 2004/06/11 00:27:06 tigital
28 * byte-swapping changes for bigendian systems
30 * Revision 1.3 2002/06/09 04:41:15 relnev
31 * added copyright header
33 * Revision 1.2 2002/05/07 03:16:43 theoddone33
34 * The Great Newline Fix
36 * Revision 1.1.1.1 2002/05/03 03:28:08 root
40 * 9 9/13/99 11:26p Andsager
41 * Add debug code to check for poorly sized anis
43 * 8 8/26/99 9:45a Dave
44 * First pass at easter eggs and cheats.
46 * 7 7/16/99 1:49p Dave
47 * 8 bit aabitmaps. yay.
49 * 6 7/13/99 1:15p Dave
50 * 32 bit support. Whee!
52 * 5 6/10/99 10:34a Dave
53 * Removed unnecessary assert.
55 * 4 11/30/98 1:07p Dave
56 * 16 bit conversion, first run.
58 * 3 10/22/98 6:14p Dave
59 * Optimized some #includes in Anim folder. Put in the beginnings of
60 * parse/localization support for externalized strings and tstrings.tbl
62 * 2 10/07/98 10:52a Dave
65 * 1 10/07/98 10:48a Dave
67 * 38 6/23/98 4:18p Hoffoss
68 * Fixed some bugs with AC release build.
70 * 37 5/18/98 5:59p Hoffoss
71 * Made command briefing advanced now once the speech stops and animation
72 * has fully played once, whichever is longer.
74 * 36 5/14/98 6:29p Hoffoss
75 * Fixed some warnings a release rebuild all turned up.
77 * 35 5/07/98 3:11a Lawrance
78 * Implement custom streaming code
80 * 34 4/27/98 3:36p Dave
82 * 33 3/25/98 8:43p Hoffoss
83 * Changed anim_play() to not be so damn complex when you try and call it.
85 * 32 2/05/98 9:21p John
86 * Some new Direct3D code. Added code to monitor a ton of stuff in the
89 * 31 1/19/98 11:37p Lawrance
90 * Fixing Optimization build warnings
92 * 30 1/19/98 3:09p Hoffoss
93 * Only free an anim instance if it is actually playing.
95 * 29 1/14/98 6:43p Lawrance
96 * Add ref_count to anim struct, so we don't free multiple times
98 * 28 12/30/97 6:44p John
99 * Made g3_Draw_bitmap functions account for aspect of bitmap.
101 * 27 12/27/97 2:35p John
102 * Restructed some code so that if the memory-mapped file open fails, it
103 * will still read it the old-fashioned, non-memory mapped way.
105 * 26 12/24/97 9:10p Lawrance
106 * take out some debugging statements
108 * 25 12/24/97 8:57p Lawrance
109 * Added anim_ignore_next_frametime()
111 * 24 12/07/97 2:06p Dave
112 * Removed some debug frame-checking code.
114 * 23 12/06/97 2:55p Dave
116 * 22 11/29/97 2:05p John
117 * made g3_draw_bitmap and g3_draw_rotated bitmap take w&h, not w/2 & h/2,
118 * like they used to incorrectly assume. Added code to model to read in
121 * 21 11/20/97 5:36p Dave
122 * Hooked in a bunch of main hall changes (including sound). Made it
123 * possible to reposition (rewind/ffwd)
124 * sound buffer pointers. Fixed animation direction change framerate
127 * 20 11/20/97 4:33p Sandeep
128 * ALAN: ensure instance->paused gets initialized
130 * 19 11/20/97 4:12p Lawrance
131 * when paused, don't increment time_elapsed
133 * 18 11/19/97 8:28p Dave
134 * Hooked in Main Hall screen. Put in Anim support for ping ponging
135 * animations as well as general reversal of anim direction.
137 * 17 9/11/97 4:17p Allender
138 * use _MAX_PATH instead of MAX_FILENAME_LENGTH for anim_play since it is
139 * called from MoviePlayer with full path
141 * 16 9/11/97 10:49a Lawrance
142 * improve anim_show_next_frame()
144 * 15 9/10/97 4:59p Lawrance
145 * improve comments to anim_play() function
147 * 14 9/03/97 4:19p John
148 * changed bmpman to only accept ani and pcx's. made passing .pcx or .ani
149 * to bm_load functions not needed. Made bmpman keep track of palettes
150 * for bitmaps not mapped into game palettes.
152 * 13 8/30/97 2:11p Lawrance
153 * allow animations to loop
155 * 12 8/25/97 11:13p Lawrance
156 * support framerate independent playback with the option of now advancing
157 * more than one frame at a time
159 * 11 8/22/97 8:20a Lawrance
160 * display scrolling text properly, with indicator that text exitsts to be
163 * 10 8/21/97 5:11p Lawrance
164 * frame numbering for ANI's now is from 0 -> total_frames-1.
166 * 9 8/19/97 9:30a Lawrance
167 * print out reason if file doesn't open right
169 * 8 7/28/97 10:42p Lawrance
170 * re-did interface to unpack_frame() to make more general
172 * 7 7/21/97 11:41a Lawrance
173 * make playback time of .ani files keyed of frametime
175 * 6 7/20/97 6:57p Lawrance
176 * supporting new RLE format
178 * 5 7/11/97 11:54a John
179 * added rotated 3d bitmaps.
181 * 4 6/27/97 4:36p Lawrance
182 * update pal translation table when gr_screen.signature changes
184 * 3 6/26/97 3:00p Lawrance
185 * fix bug with playing 3d anims
187 * 2 6/26/97 12:12a Lawrance
188 * supporting anti-aliased bitmap animations
190 * 1 6/23/97 5:09p Lawrance
192 * 24 6/03/97 5:53p Lawrance
193 * don't unload bitmap after bm_create
198 #include "animplay.h"
199 #include "linklist.h"
204 #include "grinternal.h"
205 #include "pcxutils.h"
206 #include "packunpack.h"
209 static color Color_xparent;
211 anim *first_anim = NULL;
212 anim_instance anim_free_list;
213 anim_instance anim_render_list;
215 #define MAX_ANIM_INSTANCES 25
216 anim_instance anim_render_instance[MAX_ANIM_INSTANCES];
218 int Anim_paused; // global variable to pause the playing back of anims
219 int Anim_inited = FALSE;
221 int Anim_ignore_frametime=0; // flag used to ignore frametime... useful when need to avoid saturated frametimes
223 // -------------------------------------------------------------------------------------------------
224 // anim_init() will queue all the anim_render_instance[] elements onto the anim_free_list
230 if ( Anim_inited == TRUE )
233 list_init( &anim_free_list );
234 list_init( &anim_render_list );
236 // Link all anim render slots into the free list
237 for (i=1; i < MAX_ANIM_INSTANCES; i++) {
238 list_append(&anim_free_list, &anim_render_instance[i]);
245 // -------------------------------------------------------------------------------------------------
246 // anim_render_all() will display the frames for the currently playing anims
248 void anim_render_all(int screen_id, float frametime)
253 A = GET_FIRST(&anim_render_list);
254 while( A !=END_OF_LIST(&anim_render_list) ) {
256 if ( A->screen_id == screen_id ) {
257 if ( Anim_ignore_frametime ) {
259 Anim_ignore_frametime=0;
261 if ( anim_show_next_frame(A, frametime) == -1 ) {
263 anim_release_render_instance(A);
270 // -------------------------------------------------------------------------------------------------
271 // anim_render_one() will display the frames for the passed animation, it will ignore animations which
272 // do not have the same id as the passed screen_id
274 void anim_render_one(int screen_id, anim_instance *ani, float frametime)
276 // make sure this guy's screen id matches the passed one
277 if(screen_id != ani->screen_id){
281 // otherwise render it
282 if ( Anim_ignore_frametime ) {
284 Anim_ignore_frametime=0;
286 if ( anim_show_next_frame(ani, frametime) == -1 ) {
288 anim_release_render_instance(ani);
292 MONITOR(NumANIPlayed);
294 // Setup an anim_play_struct for passing into anim_play(). Will fill in default values, which you
295 // can then change before calling anim_play().
297 void anim_play_init(anim_play_struct *aps, anim *a_info, int x, int y)
299 aps->anim_info = a_info;
303 aps->stop_at = a_info->total_frames - 1;
305 aps->world_pos = NULL;
307 aps->framerate_independent = 0;
309 aps->skip_frames = 1;
314 // -------------------------------------------------------------------------------------------------
315 // anim_play() will add an anim instance to the anim_render_list. This will cause the
316 // anim to be played at the x,y position specified in the parameter list.
320 // anim_info => the compressed animation that we should make an instance from
321 // x => x position of animation to play at (top left corner)
322 // y => y position of animation to play at ( top left corner)
323 // start_at => frame number to start at (note: numbering is from 0->num_frames-1)
324 // stop_at => frame number to stop at (note: numbering is from 0->num_frames-1)
325 // screen_id => OPTIONAL (default value 0): screen signature so animation only plays when
326 // anim_render_all() called with that same signature
327 // world_pos => OPTIONAL (default value NULL): only give a world pos when you want to
328 // play the animation at a 3D location. You must specify radius when
330 // radius => OPTIONAL (default value 0): only needed when the animation is playing
331 // as a 3D animation (this is only when world_pos in not NULL).
332 // fi => OPTIONAL (default value 0): framerate indepentdent flag, when set TRUE
333 // the animation will skip frames if necessary to maintain the fps value
334 // associated with the animation
335 // color => OPTIONAL (default value NULL) address of an alpha color struct. Only
336 // required when the animation should be drawn with an alpha color.
337 // skip_frames => OPTIONAL (default value 1) should anim skip frames when doing framerate
338 // independent playback
339 // looped => OPTIONAL (default value 0) should anim play looped (ie forever)
343 // pointer to instance => success
344 // NULL => if anim anim could not be played
346 anim_instance *anim_play(anim_play_struct *aps)
348 SDL_assert( aps->anim_info != NULL );
349 SDL_assert( aps->start_at >= 0 );
350 SDL_assert( aps->stop_at < aps->anim_info->total_frames );
351 // SDL_assert( aps->stop_at >= aps->start_at );
352 SDL_assert( !(aps->looped && aps->ping_pong) ); // shouldn't have these both set at once
354 MONITOR_INC(NumANIPlayed, 1);
356 // if (aps->ping_pong && !(aps->anim_info->flags & ANF_ALL_KEYFRAMES));
358 anim_instance *instance;
360 // Find next free anim instance slot on queue
361 instance = GET_FIRST(&anim_free_list);
362 SDL_assert( instance != &anim_free_list ); // shouldn't have the dummy element
364 // remove instance from the free list
365 list_remove( &anim_free_list, instance );
367 // insert instance onto the end of anim_render_list
368 list_append( &anim_render_list, instance );
370 aps->anim_info->instance_count++;
371 instance->frame_num = -1;
372 instance->last_frame_num = -99;
373 instance->parent = aps->anim_info;
374 instance->data = aps->anim_info->data;
375 if ( anim_instance_is_streamed(instance) ) {
376 instance->file_offset = instance->parent->file_offset;
378 instance->frame = (ubyte *) malloc(instance->parent->width * instance->parent->height * 2);
379 SDL_assert( instance->frame != NULL );
380 memset(instance->frame, 0, instance->parent->width * instance->parent->height * 2);
381 instance->time_elapsed = 0.0f;
382 instance->stop_at = aps->stop_at;
383 instance->x = aps->x;
384 instance->y = aps->y;
385 instance->world_pos = aps->world_pos;
386 instance->radius = aps->radius;
387 instance->framerate_independent = aps->framerate_independent;
388 instance->last_bitmap = -1;
389 instance->stop_now = FALSE;
390 instance->screen_id = aps->screen_id;
391 instance->aa_color = aps->color;
392 instance->skip_frames = aps->skip_frames;
393 instance->looped = aps->looped;
394 instance->ping_pong = aps->ping_pong;
395 instance->direction = ANIM_DIRECT_FORWARD;
396 instance->paused = 0;
397 instance->loop_count = 0;
398 if ( aps->color == NULL ){
399 instance->xlate_pal = 1;
401 instance->xlate_pal = 0;
404 // determining the start_at frame is more complicated, since it must be a key-frame.
405 // Futhermore, need to subtract 1 from key-frame number, since frame number is always
406 // incremented the first time anim_show_next_frame() is called
408 instance->start_at = aps->start_at;
410 if ( aps->start_at > 0 ) {
415 int frame_num = aps->start_at;
417 keyp = instance->parent->keys;
419 while (idx < instance->parent->num_keys) {
420 if (key == frame_num)
423 key = keyp[idx].frame_num - 1;
424 offset = keyp[idx].offset;
429 if (( (keyp->frame_num-1) <= frame_num) && ( (keyp->frame_num-1) > key)) { // find closest key
430 key = keyp->frame_num-1;
431 offset = keyp->offset;
432 if ( key == frame_num )
439 if (key > instance->frame_num) { // best key is closer than current position
440 instance->frame_num = key;
441 if ( anim_instance_is_streamed(instance) ) {
442 instance->file_offset = instance->parent->file_offset + offset;
444 instance->data = instance->parent->data + offset;
449 instance->frame_num--; // required
455 // -----------------------------------------------------------------------------
456 // anim_show_next_frame()
458 // This function is called to blit the next frame of an anim instance to the
459 // screen. This is normally called by the anim_render_all() function.
461 // input: instance => pointer to animation instance
462 // frametime => time elapsed since last call, in seconds
464 int anim_show_next_frame(anim_instance *instance, float frametime)
466 int bitmap_id, bitmap_flags=0, new_frame_num, frame_diff=0, i, n_frames=0,frame_save;
467 float percent_through, time;
472 SDL_assert( instance != NULL );
474 instance->time_elapsed += frametime;
476 // Advance to the next frame, if we determine enough time has elapsed.
477 if(instance->direction == ANIM_DIRECT_FORWARD)
478 n_frames = instance->stop_at - instance->start_at + 1;
479 else if(instance->direction == ANIM_DIRECT_REVERSE)
480 n_frames = instance->start_at - instance->stop_at + 1;
481 time = n_frames / i2fl(instance->parent->fps);
483 percent_through = instance->time_elapsed / time;
485 if(instance->direction == ANIM_DIRECT_FORWARD)
486 new_frame_num = instance->start_at - 1 + fl2i(percent_through * n_frames + 0.5f);
488 new_frame_num = instance->start_at - 1 - fl2i(percent_through * n_frames + 0.5f);
490 frame_save = instance->frame_num;
492 // If framerate independent, use the new_frame_num... unless instance->skip_frames is
493 // FALSE, then only advance a maximum of one frame (this is needed since some big animations
494 // should just play slower rather than taking the hit of decompressing multiple frames and
495 // creating an even greater slowdown
496 if (instance->framerate_independent) {
497 if(instance->direction == ANIM_DIRECT_FORWARD){
498 if ( new_frame_num > instance->last_frame_num) {
499 if ( instance->skip_frames )
500 instance->frame_num = new_frame_num;
502 instance->frame_num++;
504 } else if(instance->direction == ANIM_DIRECT_REVERSE){
505 if( new_frame_num < instance->last_frame_num) {
506 if ( instance->skip_frames )
507 instance->frame_num = new_frame_num;
509 instance->frame_num--;
514 if(instance->direction == ANIM_DIRECT_FORWARD){
515 if ( new_frame_num > instance->last_frame_num) {
516 instance->frame_num++;
518 } else if(instance->direction == ANIM_DIRECT_REVERSE){
519 if ( new_frame_num < instance->last_frame_num) {
520 instance->frame_num--;
525 if(instance->direction == ANIM_DIRECT_FORWARD){
526 if ( instance->frame_num < instance->start_at ) {
527 instance->frame_num = instance->start_at;
529 } else if(instance->direction == ANIM_DIRECT_REVERSE){
530 if ( instance->frame_num > instance->start_at ) {
531 instance->frame_num = instance->start_at;
535 if ( instance->stop_now == TRUE ) {
539 // If past the last frame, clamp to the last frame and then set the stop_now flag in the
540 // anim instance. The next iteration, the animation will stop.
541 if(instance->direction == ANIM_DIRECT_FORWARD){
542 if (instance->frame_num >= instance->stop_at ) {
543 if (instance->looped) { // looped animations
544 instance->frame_num = instance->stop_at;
545 instance->time_elapsed = 0.0f;
546 } else if(instance->ping_pong) { // pingponged animations
547 instance->frame_num = instance->stop_at;
548 // instance->time_elapsed = 0.0f;
549 anim_reverse_direction(instance);
550 } else { // one-shot animations
551 instance->frame_num = instance->stop_at;
552 instance->last_frame_num = instance->frame_num;
553 instance->stop_now = TRUE;
556 } else if(instance->direction == ANIM_DIRECT_REVERSE){
557 if (instance->frame_num <= instance->stop_at ) {
558 if (instance->looped) { // looped animations
559 instance->frame_num = instance->stop_at;
560 instance->time_elapsed = 0.0f;
561 } else if(instance->ping_pong) { // pingponged animations
562 instance->frame_num = instance->stop_at;
563 // instance->time_elapsed = 0.0f;
564 anim_reverse_direction(instance);
565 } else { // one-shot animations
566 instance->frame_num = instance->stop_at+1;
567 instance->last_frame_num = instance->frame_num;
568 instance->stop_now = TRUE;
573 if(instance->direction == ANIM_DIRECT_FORWARD){
574 if( instance->last_frame_num >= instance->start_at ) {
575 frame_diff = instance->frame_num - instance->last_frame_num;
579 } else if(instance->direction == ANIM_DIRECT_REVERSE){
580 if( instance->last_frame_num <= instance->start_at ) {
581 frame_diff = instance->last_frame_num - instance->frame_num;
586 SDL_assert(frame_diff >= 0);
587 // nprintf(("Alan","FRAME DIFF: %d\n",frame_diff));
588 SDL_assert( instance->frame_num >= 0 && instance->frame_num < instance->parent->total_frames );
590 // if the anim is paused, ignore all the above changes and still display this frame
591 if(instance->paused || Anim_paused){
592 instance->frame_num = frame_save;
593 instance->time_elapsed -= frametime;
597 if (instance->parent->flags & ANF_XPARENT){
598 // bitmap_flags = BMP_XPARENT;
602 if(instance->aa_color != NULL){
603 bitmap_flags |= BMP_AABITMAP;
608 if ( frame_diff > 0 ) {
609 instance->last_frame_num = instance->frame_num;
611 for ( i = 0; i < frame_diff; i++ ) {
612 anim_check_for_palette_change(instance);
614 // if we're playing backwards, every frame must be a keyframe and we set the data ptr here
615 if(instance->direction == ANIM_DIRECT_REVERSE){
616 if ( anim_instance_is_streamed(instance) ) {
617 instance->file_offset = instance->parent->file_offset + instance->parent->keys[instance->frame_num-1].offset;
619 instance->data = instance->parent->data + instance->parent->keys[instance->frame_num-1].offset;
624 int temp_file_offset = 0;
626 BM_SELECT_TEX_FORMAT();
628 if ( anim_instance_is_streamed(instance) ) {
629 if ( instance->xlate_pal ){
630 temp_file_offset = unpack_frame_from_file(instance, instance->frame, instance->parent->width*instance->parent->height, instance->parent->palette_translation, aabitmap, bpp);
632 temp_file_offset = unpack_frame_from_file(instance, instance->frame, instance->parent->width*instance->parent->height, NULL, aabitmap, bpp);
635 if ( instance->xlate_pal ){
636 temp = unpack_frame(instance, instance->data, instance->frame, instance->parent->width*instance->parent->height, instance->parent->palette_translation, aabitmap, bpp);
638 temp = unpack_frame(instance, instance->data, instance->frame, instance->parent->width*instance->parent->height, NULL, aabitmap, bpp);
642 // always go back to screen format
643 BM_SELECT_SCREEN_FORMAT();
645 if(instance->direction == ANIM_DIRECT_FORWARD){
646 if ( anim_instance_is_streamed(instance) ) {
647 instance->file_offset = temp_file_offset;
649 instance->data = temp;
655 // this only happens when the anim is being looped, we need to reset the last_frame_num
656 if ( (instance->time_elapsed == 0) && (instance->looped) ) {
657 instance->last_frame_num = -1;
658 instance->frame_num = -1;
659 instance->data = instance->parent->data;
660 instance->file_offset = instance->parent->file_offset;
661 instance->loop_count++;
665 if ( frame_diff == 0 && instance->last_bitmap != -1 ) {
666 bitmap_id = instance->last_bitmap;
669 if ( instance->last_bitmap != -1 ){
670 bm_release(instance->last_bitmap);
672 bitmap_id = bm_create(16, instance->parent->width, instance->parent->height, instance->frame, bitmap_flags);
675 if ( bitmap_id == -1 ) {
676 // anim has finsished playing, free the instance frame data
677 anim_release_render_instance(instance);
680 // NOTE: there is no need to free the instance, since it was pre-allocated as
681 // part of the anim_free_list
684 gr_set_bitmap(bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
686 // determine x,y to display the bitmap at
687 if ( instance->world_pos == NULL ) {
688 if ( instance->aa_color == NULL ) {
689 gr_bitmap(instance->x, instance->y);
692 gr_set_color_fast( (color*)instance->aa_color );
693 gr_aabitmap(instance->x, instance->y);
697 g3_rotate_vertex(&image_vertex,instance->world_pos);
698 SDL_assert(instance->radius != 0.0f);
699 g3_draw_bitmap(&image_vertex, 0, instance->radius*1.5f, TMAP_FLAG_TEXTURED );
702 //bm_release(bitmap_id);
703 instance->last_bitmap = bitmap_id;
709 // -----------------------------------------------------------------------------
710 // anim_stop_playing()
712 // Stop an anim instance that is on the anim_render_list from playing
714 int anim_stop_playing(anim_instance* instance)
716 SDL_assert(instance != NULL);
718 if ( anim_playing(instance) ) {
719 anim_release_render_instance(instance);
724 // -----------------------------------------------------------------------------
725 // anim_release_render_instance()
727 // Free a particular animation instance that is on the anim_render_list. Do
728 // not call this function to free an animation instance in general (use
729 // free_anim_instance() for that), only when you want to free an instance
730 // that is on the anim_render_list
732 void anim_release_render_instance(anim_instance* instance)
734 SDL_assert( instance != NULL );
735 SDL_assert(instance->frame);
736 free(instance->frame);
737 instance->frame = NULL;
738 instance->parent->instance_count--;
740 if ( instance->last_bitmap != -1 ) {
741 bm_release(instance->last_bitmap);
742 instance->last_bitmap = -1;
745 // remove instance from anim_render_list
746 list_remove( &anim_render_list, instance );
748 // insert instance into the anim_free_list
749 list_append( &anim_free_list, instance );
752 // -----------------------------------------------------------------------------
753 // anim_release_all_instances()
755 // Free all anim instances that are on the anim_render_list.
757 // input: screen_id => optional parameter that lets you only free a subset
758 // of the anim instances. A screen_id of 0 is the default
759 // value, and this is used for animations that always play
760 // when they are placed on the aim_render_list.
762 void anim_release_all_instances(int screen_id)
767 if ( Anim_inited == FALSE )
770 A = GET_FIRST(&anim_render_list);
771 while( A !=END_OF_LIST(&anim_render_list) ) {
773 if ( A->screen_id == screen_id || screen_id == 0 ) {
774 anim_release_render_instance(A);
780 // -----------------------------------------------------------------------------
781 // anim_read_header()
783 // Read the header of a .ani file. Below is the format of a .ani header
785 // #bytes | description
786 // 2 | obsolete, kept for compatibility with old versions
787 // 2 | version number
789 // 1 | transparent red value
790 // 1 | transparent green value
791 // 1 | transparent blue value
794 // 2 | number of frames
797 // 2 | number of key frames
798 // 2 | key frame number } repeats
799 // 4 | key frame offset } repeats
800 // 4 | compressed data length
802 void anim_read_header(anim *ptr, CFILE *fp)
804 ptr->width = cfread_short(fp);
805 // If first 2 bytes are zero, this means we are using a new format, which includes
806 // a version, and fps values. This is only done since a version number was not included
807 // in the original header.
810 Color_xparent.red = 0;
811 Color_xparent.green = 255;
812 Color_xparent.blue = 0;
814 if ( ptr->width == 0 ) {
815 ptr->version = cfread_short(fp);
816 ptr->fps = cfread_short(fp);
818 // version 2 added a custom transparency color
819 if ( ptr->version >= 2 ) {
820 cfread(&Color_xparent.red, 1, 1, fp);
821 cfread(&Color_xparent.green, 1, 1, fp);
822 cfread(&Color_xparent.blue, 1, 1, fp);
825 ptr->width = cfread_short(fp);
832 ptr->height = cfread_short(fp);
835 // get size of ani compared to power of 2
845 int floor_size = (int) pow(2.0, floor_pow);
846 int diff = ptr->height - floor_size;
847 float waste = 100.0f * float((floor_size - diff))/(2.0f *(float)floor_size);
850 if (ptr->height > 16) {
851 mprintf(("ANI %s with size %dx%d (%.1f%% wasted)\n", ptr->name, ptr->height, ptr->height, waste));
856 ptr->total_frames = cfread_short(fp);
857 ptr->packer_code = cfread_ubyte(fp);
858 cfread(&ptr->palette, 256, 3, fp);
859 ptr->num_keys = cfread_short(fp);
861 // store xparent colors
862 ptr->xparent_r = Color_xparent.red;
863 ptr->xparent_g = Color_xparent.green;
864 ptr->xparent_b = Color_xparent.blue;
866 if(ptr->total_frames == ptr->num_keys){
867 ptr->flags |= ANF_ALL_KEYFRAMES;
871 // -----------------------------------------------------------------------------
874 // Load an animation. This stores the compressed data, which instances
875 // of the animation can reference. Must be free'ed later with anim_free()
877 // input: name => filename of animation
879 // returns: pointer to anim that is loaded => sucess
882 anim *anim_load(const char *real_filename)
887 char name[MAX_PATH_LEN];
889 SDL_assert ( real_filename != NULL );
891 SDL_strlcpy(name, real_filename, SDL_arraysize(name));
892 char *p = SDL_strchr( name, '.' );
896 SDL_strlcat(name, ".ani", SDL_arraysize(name));
900 if (!SDL_strcasecmp(name, ptr->name))
907 fp = cfopen(name, "rb");
911 ptr = (anim *) malloc(sizeof(anim));
915 ptr->next = first_anim;
917 SDL_strlcpy(ptr->name, name, SDL_arraysize(ptr->name));
918 ptr->instance_count = 0;
921 ptr->total_frames = 0;
925 anim_read_header(ptr, fp);
927 if(ptr->num_keys > 0){
928 ptr->keys = (key_frame*)malloc(sizeof(key_frame) * ptr->num_keys);
929 SDL_assert(ptr->keys != NULL);
932 // store how long the anim should take on playback (in seconds)
933 ptr->time = i2fl(ptr->total_frames)/ptr->fps;
935 for(idx=0;idx<ptr->num_keys;idx++){
936 ptr->keys[idx].frame_num = 0;
937 cfread(&ptr->keys[idx].frame_num, 2, 1, fp);
938 cfread(&ptr->keys[idx].offset, 4, 1, fp);
939 ptr->keys[idx].frame_num = INTEL_INT( ptr->keys[idx].frame_num );
940 ptr->keys[idx].offset = INTEL_INT( ptr->keys[idx].offset );
943 /*prev_keyp = &ptr->keys;
944 count = ptr->num_keys;
946 keyp = (key_frame *) malloc(sizeof(key_frame));
947 keyp->next = *prev_keyp;
949 prev_keyp = &keyp->next;
952 cfread(&keyp->frame_num, 2, 1, fp);
953 cfread(&keyp->offset, 4, 1, fp);
955 cfread(&count, 4, 1, fp); // size of compressed data
956 count = INTEL_INT( count );
958 ptr->cfile_ptr = NULL;
960 // NOTE: mapped files no longer supported!!
961 ptr->flags |= ANF_STREAMED;
962 ptr->cfile_ptr = cfopen(name, "rb");
966 // If it opened properly as mem-mapped (or streamed)
967 if (ptr->cfile_ptr != NULL) {
968 // VERY IMPORTANT STEP
969 // Set the data pointer to the compressed data (which is not at the start of the
970 // file). Use ftell() to find out how far we've already parsed into the file
974 ptr->file_offset = offset;
975 if ( ptr->flags & ANF_STREAMED ) {
977 ptr->cache_file_offset = ptr->file_offset;
978 ptr->cache = (ubyte*)malloc(ANI_STREAM_CACHE_SIZE+2);
979 SDL_assert(ptr->cache);
980 cfseek(ptr->cfile_ptr, offset, CF_SEEK_SET);
981 cfread(ptr->cache, ANI_STREAM_CACHE_SIZE, 1, ptr->cfile_ptr);
986 // Not a memory mapped file (or streamed)
987 ptr->flags &= ~ANF_MEM_MAPPED;
988 ptr->flags &= ~ANF_STREAMED;
989 ptr->data = (ubyte *) malloc(count);
990 ptr->file_offset = -1;
991 cfread(ptr->data, count, 1, fp);
996 // store screen signature, so we can tell if palette changes
997 ptr->screen_sig = gr_screen.signature;
999 anim_set_palette(ptr);
1006 // ---------------------------------------------------
1009 // Free an animation that was loaded with anim_load(). All instances
1010 // referencing this animation must be free'ed or get an assert.
1012 int anim_free(anim *ptr)
1014 SDL_assert ( ptr != NULL );
1015 anim *list, **prev_anim;
1018 prev_anim = &first_anim;
1019 while (list && (list != ptr)) {
1020 prev_anim = &list->next;
1027 // only free when ref_count is 0
1029 if ( ptr->ref_count > 0 )
1032 // only free if there are no playing instances
1033 if ( ptr->instance_count > 0 )
1036 if(ptr->keys != NULL){
1041 if ( ptr->flags & (ANF_MEM_MAPPED|ANF_STREAMED) ) {
1042 cfclose(ptr->cfile_ptr);
1048 SDL_assert(ptr->data);
1052 *prev_anim = ptr->next;
1058 // ---------------------------------------------------------------------
1061 // Return if an anim is playing or not.
1063 int anim_playing(anim_instance *ai)
1065 SDL_assert(ai != NULL);
1066 if ( ai->frame == NULL )
1073 // ---------------------------------------------------------------------
1074 // anim_level_init()
1076 // Called at the beginning of a mission to initialize any mission dependent
1079 void anim_level_init()
1083 // ---------------------------------------------------------------------
1084 // anim_level_close()
1086 // Called after the end of a mission to clean up any mission dependent
1089 void anim_level_close()
1091 anim_release_all_instances();
1094 // ---------------------------------------------------
1095 // anim_write_frames_out()
1097 // Write the frames of a .ani file out to disk as .pcx files.
1098 // Use naming convention: filename0000.pcx, filename0001.pcx etc.
1100 // return: 0 => success
1103 int anim_write_frames_out(const char *filename)
1107 char root_name[256], pcxname[256];
1112 SDL_strlcpy(root_name, filename, SDL_arraysize(root_name));
1113 root_name[strlen(filename)-4] = 0;
1115 source_anim = anim_load(filename);
1116 if ( source_anim == NULL )
1119 ai = init_anim_instance(source_anim, 16);
1121 row_data = (ubyte**)malloc((source_anim->height+1) * 4);
1123 for ( i = 0; i < source_anim->total_frames; i++ ) {
1124 anim_get_next_raw_buffer(ai, 0, 0, 16);
1125 SDL_strlcpy(pcxname, root_name, SDL_arraysize(pcxname));
1126 SDL_snprintf(buf, SDL_arraysize(buf), "%04d", i);
1127 SDL_strlcat(pcxname, buf, SDL_arraysize(pcxname));
1129 for ( j = 0; j < source_anim->height; j++ ) {
1130 row_data[j] = &ai->frame[j*source_anim->width];
1134 pcx_write_bitmap( pcxname,
1136 source_anim->height,
1138 source_anim->palette);
1148 // ---------------------------------------------------
1149 // anim_display_info()
1151 // Display information and statistics about a .ani file.
1152 // This is called when -i switch is on when running ac.exe
1154 void anim_display_info(const char *real_filename)
1159 int i, uncompressed, compressed, *key_frame_nums=NULL, tmp;
1160 char filename[MAX_FILENAME_LEN];
1162 SDL_strlcpy( filename, real_filename, SDL_arraysize(filename) );
1163 char *p = SDL_strchr( filename, '.' );
1167 SDL_strlcat( filename, ".ani", SDL_arraysize(filename) );
1169 fp = cfopen(filename, "rb");
1171 printf("Fatal error opening %s", filename);
1175 anim_read_header(&A, fp);
1176 // read the keyframe frame nums and offsets
1177 key_frame_nums = (int*)malloc(sizeof(int)*A.num_keys);
1178 SDL_assert(key_frame_nums != NULL);
1179 for ( i = 0; i < A.num_keys; i++ ) {
1180 key_frame_nums[i] = 0;
1181 cfread(&key_frame_nums[i], 2, 1, fp);
1182 cfread(&tmp, 4, 1, fp);
1183 //printf("key frame num: %d,%d\n", key_frame_nums[i],tmp);
1186 cfread(&compressed, 4, 1, fp);
1188 uncompressed = A.width * A.height * A.total_frames; // 8 bits per pixel
1189 percent = i2fl(compressed) / uncompressed * 100.0f;
1191 printf("%% of uncompressed size: %.0f%%\n", percent);
1192 printf("Width: %d\n", A.width);
1193 printf("Height: %d\n", A.height);
1194 printf("Total Frames: %d\n", A.total_frames);
1197 printf("Key Frames: %d\n", A.num_keys);
1198 if ( A.num_keys > 1 && (A.total_frames != A.num_keys) ) {
1199 printf("key list: (");
1200 for ( i = 0; i < A.num_keys; i++ ) {
1201 if ( i < A.num_keys-1 )
1202 printf("%d, ", key_frame_nums[i]);
1204 printf("%d)\n", key_frame_nums[i]);
1209 printf("FPS: %d\n", A.fps);
1212 printf("Transparent RGB: (%d,%d,%d)\n", A.xparent_r, A.xparent_g, A.xparent_b);
1215 printf("ac version: %d\n", A.version);
1217 if ( key_frame_nums != NULL ) {
1218 free(key_frame_nums);
1224 void anim_reverse_direction(anim_instance *ai)
1228 if(!(ai->parent->flags & ANF_ALL_KEYFRAMES)){
1229 // you're not allowed to call anim_reverse_direction(...) unless every frame is a keyframe!!!!
1230 // The God of Delta-RLE demands it be thus.
1234 // flip the animation direction
1235 if(ai->direction == ANIM_DIRECT_FORWARD){
1236 ai->direction = ANIM_DIRECT_REVERSE;
1237 } else if(ai->direction == ANIM_DIRECT_REVERSE){
1238 ai->direction = ANIM_DIRECT_FORWARD;
1241 // flip frame_num and last_frame_num
1242 temp = ai->frame_num;
1243 ai->frame_num = ai->last_frame_num;
1244 ai->last_frame_num = temp;
1246 // flip the start and stop at frames
1248 ai->stop_at = ai->start_at;
1249 ai->start_at = temp;
1251 // make sure to sync up the time correctly
1252 if(ai->direction == ANIM_DIRECT_FORWARD){
1253 ai->time_elapsed = ((float)ai->frame_num - (float)ai->start_at - 1.0f) / (float)ai->parent->fps;
1254 } else if(ai->direction == ANIM_DIRECT_REVERSE) {
1255 ai->time_elapsed = ((float)ai->start_at - (float)ai->frame_num - 1.0f) / (float)ai->parent->fps;
1259 void anim_pause(anim_instance *ai)
1264 void anim_unpause(anim_instance *ai)
1269 void anim_ignore_next_frametime()
1271 Anim_ignore_frametime=1;
1274 int anim_instance_is_streamed(anim_instance *ai)
1277 return ( ai->parent->flags & ANF_STREAMED );
1280 unsigned char anim_instance_get_byte(anim_instance *ai, int offset)
1282 int absolute_offset;
1286 SDL_assert(ai->parent->cfile_ptr);
1287 SDL_assert(ai->parent->flags & ANF_STREAMED);
1289 parent = ai->parent;
1290 absolute_offset = ai->file_offset + offset;
1294 cache_offset = absolute_offset - parent->cache_file_offset;
1295 if ( (cache_offset >= 0) && (cache_offset < ANI_STREAM_CACHE_SIZE) ) {
1296 return parent->cache[cache_offset];
1299 cfseek(parent->cfile_ptr, absolute_offset, CF_SEEK_SET);
1300 cfread(parent->cache, ANI_STREAM_CACHE_SIZE, 1, parent->cfile_ptr);
1301 parent->cache_file_offset = absolute_offset;
1302 return parent->cache[0];