diff options
Diffstat (limited to 'Movie.c')
-rwxr-xr-x | Movie.c | 81 |
1 files changed, 68 insertions, 13 deletions
@@ -1,10 +1,15 @@ | |||
1 | #include <time.h> | 1 | #include <time.h> |
2 | #include <stdlib.h> | ||
3 | #include <stdio.h> | ||
4 | #include "Movie.h" | ||
5 | |||
6 | #ifndef NULL | ||
7 | #define NULL ((void*)0) | ||
8 | #endif | ||
2 | 9 | ||
3 | struct Frame; | 10 | struct Frame; |
4 | struct Movie; | ||
5 | 11 | ||
6 | typedef struct Frame Frame; | 12 | typedef struct Frame Frame; |
7 | typedef struct Movie Movie; | ||
8 | 13 | ||
9 | struct Frame { | 14 | struct Frame { |
10 | unsigned long *framedata; | 15 | unsigned long *framedata; |
@@ -13,23 +18,25 @@ struct Frame { | |||
13 | 18 | ||
14 | struct Movie { | 19 | struct Movie { |
15 | Frame *Movie; | 20 | Frame *Movie; |
21 | Frame *current; | ||
16 | Movie *next; | 22 | Movie *next; |
17 | int rate; | 23 | int rate; |
18 | time_t starttime; | 24 | time_t starttime; |
19 | }; | 25 | }; |
20 | 26 | ||
21 | static Movie *movie_list; | 27 | static Movie movie_list = { NULL, NULL, NULL, 0, 0}; |
22 | static Movie *current; | 28 | static Movie *current; |
29 | static int MoviePoints; | ||
23 | 30 | ||
24 | void Movie_init( int points ) { | 31 | void Movie_init( int points ) { |
25 | movie_list = (Movie*)0; | 32 | MoviePoints = points; |
26 | current = (Movie*)0; | 33 | current = NULL; |
27 | } | 34 | } |
28 | 35 | ||
29 | void Movie_destroy( void ) { | 36 | void Movie_destroy( void ) { |
30 | Movie *tmp = movie_list; | 37 | Movie *tmp = movie_list.next; |
31 | 38 | ||
32 | current = (Movie*)0; | 39 | current = NULL; |
33 | while( tmp ) { | 40 | while( tmp ) { |
34 | Movie *next = tmp->next; | 41 | Movie *next = tmp->next; |
35 | Movie_unloadmovie( tmp ); | 42 | Movie_unloadmovie( tmp ); |
@@ -38,20 +45,68 @@ void Movie_destroy( void ) { | |||
38 | } | 45 | } |
39 | 46 | ||
40 | Movie *Movie_loadmovie( char *filename ) { | 47 | Movie *Movie_loadmovie( char *filename ) { |
48 | FILE *infile = fopen( filename, "r" ); | ||
49 | Movie *movie = NULL; | ||
50 | Frame **loaded = NULL; | ||
51 | int points, frames, i; | ||
52 | |||
53 | if( infile == NULL ) return NULL; | ||
54 | |||
55 | if( ( fscanf( infile, "%d\n", &points ) != 1 ) || | ||
56 | ( MoviePoints != points ) || | ||
57 | ( fscanf( infile, "%d\n", &frames ) != 1 ) || | ||
58 | ( ( movie = malloc( sizeof( Movie ) ) ) == NULL ) || | ||
59 | ( fscanf( infile, "%d\n", &movie->rate ) != 1 ) ) | ||
60 | goto error; | ||
61 | |||
62 | loaded = &movie->Movie; | ||
63 | |||
64 | while( frames-- > 0 ) { | ||
65 | if( | ||
66 | ( ( *loaded = malloc( sizeof( Frame ) ) ) == NULL ) || | ||
67 | ( ( (*loaded)->framedata = malloc( points * points * points * sizeof( unsigned long ) ) ) == NULL ) | ||
68 | ) | ||
69 | goto error; | ||
70 | |||
71 | for( i = 0; i < points * points * points; ++i ) { | ||
72 | if( fscanf( infile, "%d%d%d", ((char *)((*loaded)->framedata)), | ||
73 | ((char *)((*loaded)->framedata))+1, | ||
74 | ((char *)((*loaded)->framedata))+2 ) | ||
75 | != 3 ) | ||
76 | goto error; | ||
77 | } | ||
78 | loaded = &((*loaded)->next); | ||
79 | } | ||
41 | 80 | ||
81 | error: | ||
82 | if( loaded ) | ||
83 | (*loaded)->next = NULL; | ||
84 | if( movie ) | ||
85 | Movie_unloadmovie( movie ); | ||
86 | movie = NULL; | ||
87 | success: | ||
88 | fclose( infile ); | ||
89 | return movie; | ||
42 | } | 90 | } |
43 | 91 | ||
44 | void Movie_unloadmovie( Movie *movie ) { | 92 | void Movie_unloadmovie( Movie *movie ) { |
45 | Movie *tmp = movie_list; | 93 | Movie *tmp = &movie_list; |
46 | if( current == movie ) | 94 | if( current == movie ) |
47 | current = (Movie*)0; | 95 | current = NULL; |
48 | 96 | ||
49 | while( tmp && ( tmp != movie )) | 97 | while( tmp && ( tmp->next != movie )) |
50 | tmp = tmp->next; | 98 | tmp = tmp->next; |
51 | 99 | ||
52 | if( tmp) { | 100 | if( tmp->next) { |
53 | 101 | Frame *frame = tmp->next->Movie; | |
54 | free( tmp ); | 102 | while( frame != NULL ) { |
103 | Frame *next = frame->next; | ||
104 | free( frame ); | ||
105 | frame = next; | ||
106 | } | ||
107 | |||
108 | tmp->next = tmp->next->next; | ||
109 | free( tmp->next ); | ||
55 | } | 110 | } |
56 | } | 111 | } |
57 | 112 | ||