summaryrefslogtreecommitdiff
path: root/Movie.c
blob: 44a15e9f629f237fa5b0447944166a569c0d29ff (plain)
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
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "Movie.h"

#ifndef NULL
  #define NULL ((void*)0)
#endif

struct Frame;

typedef struct Frame Frame;

struct Frame {
    unsigned long *framedata;
    Frame *next;
};

struct Movie {
    Frame *Movie;
    Frame *current;
    Movie *next;
    int    rate;
    time_t starttime;
};

static Movie movie_list = { NULL, NULL, NULL, 0, 0};
static Movie *current;
static int   MoviePoints;

void Movie_init( int points ) {
    MoviePoints = points;
    current    = NULL;
}

void Movie_destroy( void ) {
    Movie *tmp = movie_list.next;

    current = NULL;
    while( tmp ) {
        Movie *next = tmp->next;
        Movie_unloadmovie( tmp );
        tmp = next;
    }
}

Movie *Movie_loadmovie( char *filename ) {
    FILE  *infile = fopen( filename, "r" );
    Movie *movie  = NULL;
    Frame **loaded = NULL;
    int    points, frames, i;

    if( infile == NULL ) return NULL;

    if( ( fscanf( infile, "%d\n", &points ) != 1 )        ||
        ( MoviePoints != points )                         ||
        ( fscanf( infile, "%d\n", &frames ) != 1 )        ||
        ( ( movie = malloc( sizeof( Movie ) ) ) == NULL ) ||
        ( fscanf( infile, "%d\n", &movie->rate ) != 1 )   )
        goto error;

    loaded = &movie->Movie;

    while( frames-- > 0 ) {
        if(
            ( (  *loaded             = malloc( sizeof( Frame ) ) ) == NULL ) ||
            ( ( (*loaded)->framedata = malloc( points * points * points * sizeof( unsigned long ) ) ) == NULL )
          )
            goto error;

        for( i = 0; i < points * points * points; ++i ) {
            if( fscanf( infile, "%d%d%d", ((char *)((*loaded)->framedata)),
                                          ((char *)((*loaded)->framedata))+1,
                                          ((char *)((*loaded)->framedata))+2 )
                       != 3 )
                goto error;
        }
        loaded = &((*loaded)->next);
    }

    error:
    if( loaded )
        (*loaded)->next = NULL;
    if( movie )
        Movie_unloadmovie( movie );
    movie = NULL;
    success:
    fclose( infile );
    return movie;
}

void Movie_unloadmovie( Movie *movie ) {
    Movie *tmp = &movie_list;
    if( current == movie )
        current = NULL;

    while( tmp && ( tmp->next != movie ))
        tmp = tmp->next;

    if( tmp->next) {
        Frame *frame = tmp->next->Movie;
        while( frame != NULL ) {
            Frame *next = frame->next;
            free( frame );
            frame = next;
        }

        tmp->next = tmp->next->next;
        free( tmp->next );
    }
}

void Movie_playmovie( Movie *movie, int rate ) {

}

void Movie_stamptime( void ) {

}

int Movie_checkframe( ) {
    return 1;
}

unsigned long Movie_getpixelcolor( int x, int y, int z) {

}