Nilorea Library

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros
n_anim.c
Go to the documentation of this file.
1
15 #ifndef NOISOENGINE
16
17
18 #include "n_common.h"
19 #include "n_str.h"
20 #include "n_gfx.h"
21 #include "n_anim.h"
22
23 /*******************************************************************************
24 SPRITE ROUTINES
25 *******************************************************************************/
26
27
28
46 int init_sprite( SPRITE **spr , char *name ,
47 int w , int h ,
48 int x , int y ,
49 int type ,
50 int delay ,
51 int max )
52 {
53
54 /* allocating */
55
56 Malloc( *spr , SPRITE , 1 );
57
58 if ( !spr )
59 return FALSE;
60
61 if ( name ) {
62
63 ( *spr ) -> namelenght = ustrsizez( name );
64
65 Malloc( ( *spr ) -> name , char , ( *spr ) -> namelenght );
66 fill_str( ( *spr ) -> name , 0 , ( *spr ) -> namelenght );
67 Strcpy( name , ( *spr ) -> name , ( *spr ) -> namelenght );
68 }
69
70
71 ( *spr ) -> w = w ;
72 ( *spr ) -> h = h ;
73 ( *spr ) -> x = x ;
74 ( *spr ) -> y = y ;
75 ( *spr ) -> type = type ;
76 ( *spr ) -> delay = delay ;
77 ( *spr ) -> current = 0 ;
78
79
80 if ( max != -1 ) {
81
82 ( *spr ) -> max = max;
83
84 /* allocating image data array */
85
86 Malloc( ( *spr ) -> spr , BITMAP * , max );
87
88 if ( !( *spr ) -> spr )
89 return FALSE;
90
91 Malloc( ( *spr ) -> handles , GLuint , max );
92
93 if ( !( ( *spr ) -> handles ) )
94 return FALSE;
95
96 }
97
98 (*spr) -> alive = FALSE ;
99
100 return TRUE;
101
102 } /* init_sprite(...) */
103
104
105
116 int add_bmp_to_spr( BITMAP *bmp , SPRITE *spr )
117 {
118
119 /* checking space */
120
121 if ( spr -> current >= spr -> max )
122 return FALSE;
123
124 spr -> spr[ spr -> current ] = bmp ;
125
126 if ( gfx_status( GET , 0 ) == MODE_OGL )
127 spr -> handles[ spr -> current ] = allegro_gl_make_texture( spr -> spr[ spr -> current ] );
128
129 spr -> current += 1;
130 spr -> alive = TRUE ;
131
132
133 return TRUE;
134
135 } /* add_bmp_to_sprite(...) */
136
137
138
149 int read_sprite_in_file( PACKFILE *file , SPRITE **spr )
150 {
151
152 int it=0,it1=0,it2=0,r,g,b,a;
153
154 /* error checking*/
155
156 if ( !file )
157 return FALSE;
158
159 if ( init_sprite( &( *spr ) , NULL , -1, -1, -1, -1, -1, -1, -1 ) == FALSE )
160 return FALSE;
161
162 ( *spr ) -> w = pack_igetl( file );
163
164 ( *spr ) -> h = pack_igetl( file );
165
166 ( *spr ) -> x = pack_igetl( file );
167
168 ( *spr ) -> y = pack_igetl( file );
169
170 ( *spr ) -> type = pack_igetl( file );
171
172 ( *spr ) -> delay = pack_igetl( file );
173
174 ( *spr ) -> current = pack_igetl( file );
175
176 ( *spr ) -> max = pack_igetl( file );
177
178 Malloc( (*spr) -> spr , BITMAP * , (*spr) -> max ) ;
179 Malloc( (*spr) -> handles , GLuint , (*spr) -> max ) ;
180
181 ( *spr ) -> alive = pack_igetl( file );
182
183 if ( ( *spr ) -> alive == TRUE ) {
184
185 ( *spr ) -> namelenght = pack_igetl( file );
186 pack_fread( &( *spr ) -> name , ( *spr ) -> namelenght , file );
187
188 for ( it = 0 ; it < (*spr) -> current ; it ++ ) {
189 (*spr) -> spr[ it ] = create_bitmap( (*spr) -> w , (*spr) -> h );
190 for( it1 = 0 ; it1 < (*spr) -> w; it1 ++ ){
191 for( it2 = 0 ; it2 < (*spr) -> h; it2 ++ ){
192 r = pack_igetl( file );
193 g = pack_igetl( file );
194 b = pack_igetl( file );
195 a = pack_igetl( file );
196 putpixel( (*spr) -> spr[ it ] , it1 , it2 ,makeacol(r,g,b,a) );
197
198 } }
199 if( gfx_status( GET , 0 ) == MODE_OGL )
200 (*spr) -> handles[ it ] = allegro_gl_make_texture( (*spr) -> spr[ it ] );
201
202
203 }
204 }
205
206
207 return TRUE;
208
209
210
211 } /* read_sprite_in_file(...) */
212
213
214
225 int write_sprite_in_file( PACKFILE *file , SPRITE *spr )
226 {
227
228 int it=0,it1=0,it2=0,color,r,g,b,a;
229
230 /* error checking*/
231
232 if ( !file )
233 return FALSE;
234
235 if ( !spr )
236 return FALSE;
237
238 pack_iputl( spr -> w , file );
239
240 pack_iputl( spr -> h , file );
241
242 pack_iputl( spr -> x , file );
243
244 pack_iputl( spr -> y , file );
245
246 pack_iputl( spr -> type , file );
247
248 pack_iputl( spr -> delay , file );
249
250 pack_iputl( spr -> current , file );
251
252 pack_iputl( spr -> max , file );
253
254 pack_iputl( spr -> alive , file );
255
256 if ( spr -> alive == TRUE ) {
257
258 pack_iputl( spr -> namelenght , file );
259 pack_fwrite( spr -> name , spr -> namelenght , file );
260
261 for ( it = 0 ; it < spr -> current ; it ++ ) {
262 for( it1 = 0 ; it1 < spr -> w; it1 ++ ){
263 for( it2 = 0 ; it2 < spr -> h; it2 ++ ){
264 color = getpixel( spr -> spr[ it ] , it1 , it2 );
265 r = getr( color );
266 g = getg( color );
267 b = getb( color );
268 a = geta( color );
269 pack_iputl( r , file );
270 pack_iputl( g , file );
271 pack_iputl( b , file );
272 pack_iputl( a , file );
273 }}
274 }
275 }
276
277 return TRUE;
278
279
280 } /* write_sprite_in_file( ... ) */
281
282
283
284 /****************************get an old sprite to a new***********************/
296 SPRITE *GetOldSprite( char *bmpfilename , char *txtfilename , char *name )
297 {
298
299 int x, y, c;
300
301 /* temporary sprite pointer */
302 SPRITE *newSP;
303
304 /* input text file */
305 FILE *file;
306
307 /* input bitmap */
308 BITMAP *bmp;
309 BITMAP *lodbmp;
310
311 /* temp string&other */
312 /*char *str;*/
313 int it, it2;
314
315 /* loading old files */
316 file = fopen( txtfilename , "r" );
317
318 if ( !file )
319 return NULL;
320
321 lodbmp = load_bitmap( bmpfilename , NULL );
322
323
324 if ( !lodbmp )
325 return NULL;
326
327 bmp = create_bitmap_ex( 32 , SCREEN_W,SCREEN_H );
328
329 clear_to_color( bmp , makeacol( 0 , 0 , 0 , 0 ));
330
331 for ( x = 0;x < bmp->w;x++ ) {
332 for ( y = 0;y < bmp->h;y++ ) {
333
334 c = getpixel( lodbmp, x, y );
335
336 if ( c == makecol( 0, 255, 0 ) )
337 putpixel( bmp, x, y, makeacol( 255, 0, 255, 0 ) );
338 else
339 putpixel( bmp, x, y, makeacol( getr( c ) , getg( c ) , getb( c ) , 255 ) );
340
341 }
342 }
343
344 destroy_bitmap( lodbmp );
345
346 init_sprite( &newSP , name ,
347 -1 , -1 ,
348 -1 , -1 ,
349 -1 ,
350 -1 ,
351 -1 );
352
353
354 /*reading from txt file*/
355 fscanf( file , "%d" , &it );
356
357 if ( it != 211282 )
358 return NULL;
359
360 newSP -> alive = TRUE;
361
362
363 fscanf( file , "%d %d %d" , &newSP -> w , &newSP -> h , &newSP -> max );
364
365 fscanf( file, "%d %d %d %d %d", &it , &it2 , &newSP -> x , &newSP -> y , &newSP -> delay );
366
367 fclose( file );
368
369 newSP -> current = newSP -> max;
370
371 Malloc( newSP -> handles , GLuint , newSP -> max );
372
373 Malloc( newSP -> spr , BITMAP * , newSP-> max );
374
375 /* loading image data */
376 for ( it = 0 ; it < newSP -> max ; it ++ ) {
377
378 newSP -> spr[ it ] = create_bitmap( newSP -> w , newSP -> h );
379
380 clear( newSP -> spr[ it] );
381
382 blit( bmp , newSP -> spr[ it ] , it * newSP -> w , 0 , 0 , 0 , newSP -> w , newSP -> h );
383
384 if( gfx_status( GET , 0 ) == MODE_OGL )newSP -> handles[ it ] = allegro_gl_make_texture( newSP -> spr[ it ] );
385
386 }
387
388 destroy_bitmap( bmp );
389
390 newSP -> type = MASKED_SPRITE;
391
392 return newSP;
393
394 } /* GetOldSprite */
395
396
397
398
412 int blit_sprite( SPRITE *sprite , int x , int y , int frame , BITMAP *bmp )
413 {
414
415
416 if ( !sprite || sprite -> alive == FALSE || frame < 0 || frame >= sprite -> current )
417 return FALSE;
418
419 switch ( sprite -> type ) {
420
421 /* normal blit */
422
423 case NORMAL_SPRITE :
424
425 if ( bmp )
426 blit( sprite -> spr[ frame ] , bmp ,
427 0 , 0 ,
428 x - sprite -> x ,
429 y - sprite -> y ,
430 sprite -> w,
431 sprite -> h
432 );
433 else
434 ogl_blit( sprite -> handles[ frame ] , x - sprite -> x , y - sprite -> y , 1 , 1 , 1 , 1 , sprite -> w , sprite -> h , sprite -> type );
435
436
437
438 return TRUE;
439
440 break;
441
442
443 /* masked blit */
444
445 case MASKED_SPRITE:
446
447 if ( bmp ) {
448 draw_sprite( bmp , sprite -> spr[ frame ] , x - sprite -> x , y - sprite -> y );
449 }
450
451 else
452
453
454 ogl_blit( sprite -> handles[ frame ] , x - sprite -> x , y - sprite -> y , 1 , 1 , 1 , 1 , sprite -> w , sprite -> h , sprite -> type );
455
456
457 return TRUE;
458
459 break;
460
461
462 /* translucent blit */
463
464 case TRANS_SPRITE:
465
466 if ( bmp ) {
467 set_alpha_blender();
468 draw_trans_sprite( bmp , sprite -> spr[ frame ] , x - sprite -> x , y - sprite -> y );
469 }
470
471 else
472
473 ogl_blit( sprite -> handles[ frame ] , x - sprite -> x , y - sprite -> y , 1 , 1 , 1 , 1 , sprite -> w , sprite -> h , sprite -> type );
474
475
476 return TRUE;
477
478 break;
479
480
481 /* Sprite has no type ! */
482
483 default:
484
485 return FALSE;
486
487 break;
488
489 } /*switch(...)*/
490
491 return TRUE;
492
493 } /* blit_sprite(...) */
494
495
496
506 int free_sprite( SPRITE **spr )
507 {
508
509 int it;
510
511 for ( it = 0 ; it < ( *spr ) -> max ; it ++ )
512 Free( ( *spr ) -> spr[ it ] );
513
514 Free( ( *spr ) -> spr );
515
516 Free( ( *spr ) -> name );
517
518 Free( ( *spr ) );
519
520 return TRUE;
521
522 } /* free_sprite(..) */
523
524
525
526 /*******************************************************************************
527 ANIMATION ROUTINES
528 *******************************************************************************/
529
530
531
543 int create_animlib( ANIM **lib , char *name , int nbspr )
544 {
545
546 /* allocating and testing anim struct */
547 Malloc( lib , ANIM *, 1 );
548
549 if ( !( *lib ) )
550 return FALSE;
551
552
553 /* allocating and testing sprite array of anim */
554 Malloc( ( *lib ) -> data , SPRITE * , nbspr );
555
556 if ( !( *lib ) ->data )
557 return FALSE;
558
559
560 /* saving nb max spr */
561 ( *lib ) -> max = nbspr;
562
563
564 /* allocating, testing and saving name */
565 if ( name ) {
566
567 Malloc( ( *lib ) -> name , char , strlen( name ) + 1 );
568
569 if ( !( *lib ) -> name )
570 return FALSE;
571
572 strcpy( name , ( *lib ) -> name );
573
574 }
575
576 /* initialising counter */
577 ( *lib ) -> hightest = 0;
578
579 return TRUE;
580
581 } /* create_animlib(...) */
582
583
584
596 int create_iso_animlib( ISO_ANIM **lib , char *name , int nbspr )
597 {
598
599 int it = 0 , it1 = 0 , it2 = 0 ;
600
601 /* allocating and testing anim struct */
602 Malloc( lib , ISO_ANIM *, 1 );
603
604 if ( !( *lib ) )
605 return FALSE;
606
607
608 /* allocating and testing sprite array of anim */
609 Malloc( ( *lib ) -> data , SPRITE *** , nbspr );
610
611
612 for( it = 0 ; it < nbspr ; it ++ )
613 {
614 for( it1 = 0 ; it1 < 4 ; it1 ++ )
615 {
616 for ( it2 = 0 ; it2 < 8 ; it2 ++ )
617 {
618 ( *lib ) -> data[ it ][ it1 ][ it2 ] -> alive = FALSE;
619 }
620 }
621 }
622
623
624 if ( !( *lib ) ->data )
625 return FALSE;
626
627
628 /* saving nb max spr */
629 ( *lib ) -> max = nbspr;
630
631
632 /* allocating, testing and saving name */
633 if ( name )
634 {
635
636 Malloc( ( *lib ) -> name , char , strlen( name ) + 1 );
637
638 if ( !( *lib ) -> name )
639 return FALSE;
640
641 strcpy( name , ( *lib ) -> name );
642
643 }
644 else
645 {
646 (*lib) -> name = NULL ;
647 }
648
649
650 /* initialising counter */
651 ( *lib ) -> hightest = 0;
652
653 return TRUE;
654
655 } /* create_iso_animlib(...) */
656
657
658
659
670 int load_animlib( PACKFILE *file , ANIM **lib )
671 {
672
673
674 int it = 0;
675
676 /* error checking */
677 Malloc( ( *lib ) , ANIM , 1 );
678
679 if ( !( *lib ) )
680 return FALSE;
681
682 if ( !file )
683 return FALSE;
684
685 /*reading data*/
686 ( *lib ) -> hightest = pack_igetl( file );
687
688 ( *lib ) -> max = pack_igetl( file );
689
690 ( *lib ) -> namelenght = pack_igetl( file );
691
692 Malloc( ( *lib ) -> name , char , ( *lib ) -> namelenght + 1 );
693
694 fill_str( ( *lib ) -> name , 0 , ( *lib ) -> namelenght + 1 );
695
696 pack_fread( &( *lib ) -> name[ it ], ( *lib ) -> namelenght , file );
697
698 Malloc( ( *lib ) -> data , SPRITE * , ( *lib ) -> hightest );
699
700 for ( it = 0 ; it < ( *lib ) -> hightest ; it ++ )
701
702 read_sprite_in_file( file, &( *lib ) -> data[ it ] );
703
704 return TRUE;
705
706 } /* load_animlib(...) */
707
708
709
720 int save_animlib( PACKFILE *file , ANIM *lib )
721 {
722
723 int it;
724
725 /* error checking */
726
727 if ( !lib )
728 return FALSE;
729
730
731 if ( !file )
732 return FALSE;
733
734 /*writting data*/
735
736 pack_iputl( lib -> hightest , file );
737
738 pack_iputl( lib -> max , file );
739
740 pack_iputl( lib -> namelenght , file );
741
742 pack_fwrite( lib -> name , lib -> namelenght , file );
743
744 for ( it = 0 ; it < lib -> max ; it ++ )
745 write_sprite_in_file( file , lib -> data[ it ] );
746
747 return TRUE;
748
749 } /* save_animlib(...) */
750
751
752
763 int add_sprite_to_lib( SPRITE *spr , ANIM *lib )
764 {
765
766 int current = 0 ;
767
768 /* error checking */
769
770 if ( lib -> hightest + 1 > lib -> max )
771 return FALSE;
772
773 /* searching first free place */
774 while ( lib -> data[ current ] -> spr )
775 current ++;
776
777 /* pointing to anim */
778 lib -> data[ current ] = spr;
779
780 /* updating new position */
781 lib -> hightest += 1;
782
783 return TRUE;
784
785 } /* add_sprite_to_lib(...) */
786
787
788
799 int remove_sprite_from_lib( ANIM *lib , int id )
800 {
801
802
803 if ( !lib -> data[ id ] )
804 return FALSE;
805
806
807 free_sprite( &lib -> data[ id ] );
808
809 if ( id == lib -> data[ id ] -> current ) {
810 lib -> data[ id ] -> current -= 1;
811 }
812
813
814 return TRUE;
815
816 } /* remove_sprite(...)*/
817
818
819
829 int free_anim( ANIM *lib )
830 {
831
832 int it = 0;
833
834 if ( !lib )
835 return FALSE;
836
837 for ( it = 0 ; it < lib -> max ; it ++ )
838 if ( lib -> data[ it ] )
839 free_sprite( &lib -> data[ it ] );
840
841 Free( lib -> name );
842
843 Free( lib -> data );
844
845 Free( lib );
846
847 return TRUE;
848
849 } /* free_anim(...) */
850
851
852
863 int resize_anim_lib( ANIM **lib , int nb )
864 {
865
866 ANIM * tmp;
867
868 int it = 0 , it1 = 0;
869
870 if ( nb < ( *lib ) -> hightest )
871 return 2;
872
873 if ( nb == ( *lib ) -> hightest || nb == ( *lib ) -> max )
874 return 3;
875
876 if ( create_animlib( &tmp , ( *lib ) -> name , nb ) == FALSE ) {
877 free_anim( tmp );
878 return 2;
879 }
880
881 tmp -> nbsprite = ( *lib ) -> nbsprite;
882 tmp -> hightest = ( *lib ) -> hightest;
883 tmp -> max = ( *lib ) -> max;
884
885 for ( it = 0 ; it < tmp -> hightest ; it ++ ) {
886 if ( ( *lib ) -> data[ it ] ) {
887
888 tmp -> data[ it ] -> w = ( *lib ) -> data[ it ] -> w;
889 tmp -> data[ it ] -> h = ( *lib ) -> data[ it ] -> h;
890 tmp -> data[ it ] -> x = ( *lib ) -> data[ it ] -> x;
891 tmp -> data[ it ] -> y = ( *lib ) -> data[ it ] -> y;
892 tmp -> data[ it ] -> type = ( *lib ) -> data[ it ] -> type;
893 tmp -> data[ it ] -> delay = ( *lib ) -> data[ it ] -> delay;
894 tmp -> data[ it ] -> current = ( *lib ) -> data[ it ] -> current;
895 tmp -> data[ it ] -> max = ( *lib ) -> data[ it ] -> max;
896
897 for ( it1 = 0 ; it1 < tmp -> max ; it1++ ) {
898
899
900 if ( ( *lib ) -> data[ it ] -> handles ) {
901 tmp -> data[ it1 ] -> handles = ( *lib ) -> data[ it1 ] -> handles ;
902 }
903
904 if ( ( *lib ) -> data[ it ] -> spr [ it1 ] ) {
905 tmp -> data[ it ] -> spr[ it1 ] = create_bitmap( tmp -> data[ it ] -> w , tmp -> data[ it ] -> h );
906
907 if ( !tmp -> data[ it ] -> spr[ it1 ] ) {
908 free_anim( tmp );
909 return 2;
910 }
911
912 blit( (*lib) -> data[ it ] -> spr[ it1 ] , tmp -> data[ it ] -> spr[ it1 ] , 0 , 0 , 0 , 0 , tmp -> data[ it ] ->w , tmp -> data[ it ] -> h );
913
914 }
915
916 }
917
918 }
919
920 }
921
922 free_anim( ( *lib ) );
923
924 ( *lib ) = tmp;
925
926 return 3;
927
928 }
929
930
931
946 int blit_anim( ANIM *lib , int id , int x , int y , int frame , BITMAP *bmp )
947 {
948
949 if ( !lib -> data[ id ] )
950 return FALSE;
951
952 return blit_sprite( lib -> data[ id ] , x , y, frame , bmp );
953
954 } /* blit_anim(...) */
955
956
957
965 int exist_sprite( ANIM *lib , char *name )
966 {
967
968 int it = 0;
969 int found = -2;
970
971 if ( !lib )
972 return -2;
973
974 if ( lib -> hightest == 0 )
975 return -2;
976
977
978 while ( found == -2 && it < lib -> hightest ) {
979
980 if ( lib -> data[ it ] && lib -> data[ it ] -> name ) {
981 if ( strcmp( lib -> data[ it ] -> name , name ) != 0 )
982 it ++;
983 else
984 found = it;
985 } /* if( ... ) */
986
987 } /* while( ... ) */
988
989 return found;
990
991
992 } /* exist_sprite( ... ) */
993
994
995
996 #endif /* #ifndef NOISOENGINE */
997
998
999
1000
1001
1002
1003
1004