added files to .cvsignore
[btb/d2x.git] / libmve / mve_main.c
1 /* $Id: mve_main.c,v 1.2 2003-02-19 00:42:40 btb Exp $ */
2
3 #ifdef HAVE_CONFIG_H
4 #include <conf.h>
5 #endif
6
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12
13 #include <SDL.h>
14
15 #include "mvelib.h"
16
17 #define SWAPINT(x)   (((x)<<24) | (((unsigned int)(x)) >> 24) | (((x) &0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8))
18
19 #define MAX_FILES 256
20
21 extern int g_spdFactorNum;
22 extern int g_sdlVidFlags;
23 extern int g_loop;
24
25 void initializeMovie(MVESTREAM *mve);
26 void playMovie(MVESTREAM *mve);
27 void shutdownMovie(MVESTREAM *mve);
28
29 static void usage(void)
30 {
31     fprintf(stderr, "usage: mveplay [-f] [-l] [-s <n>] [<mvlfile>] <filename>\n"
32                         "-f\tFullscreen mode\n"
33                         "-s\tSpeed Factor <n>\n"
34                         );
35     exit(1);
36 }
37
38 static int doPlay(int filehandle)
39 {
40     MVESTREAM *mve = mve_open_filehandle(filehandle);
41     if (mve == NULL) {
42         fprintf(stderr, "can't open MVE file\n");
43                 exit(1);
44         }
45
46     initializeMovie(mve);
47     playMovie(mve);
48     shutdownMovie(mve);
49
50     mve_close_filehandle(mve);
51
52     return 1;
53 }
54
55 int main(int argc, char *argv[])
56 {
57         int i, filehandle;
58         char *mvlfile = NULL, *mvefile = NULL;
59
60         for (i = 1; i < argc; i++) {
61                 if (!strcmp(argv[i], "-h"))
62                         usage();
63
64                 if (!strcmp(argv[i], "-f"))
65                         g_sdlVidFlags |= SDL_FULLSCREEN;
66
67                 if (!strcmp(argv[i], "-l"))
68                         g_loop = 1;
69
70                 if (!strcmp(argv[i], "-s")) {
71                         if (argc < i + 2)
72                                 usage();
73                         g_spdFactorNum = atoi(argv[i + 1]);
74                         i++;
75                 }
76
77                 if (strchr(argv[i], '.') && !strcasecmp(strchr(argv[i], '.'), ".mvl"))
78                         mvlfile = argv[i];
79
80                 if (strchr(argv[i], '.') && !strcasecmp(strchr(argv[i], '.'), ".mve"))
81                         mvefile = argv[i];
82         }
83
84         if (mvlfile) {
85                 int nfiles;
86                 char filename[MAX_FILES][13];
87                 int filesize[MAX_FILES];
88                 char sig[4];
89
90 #ifdef O_BINARY
91                 filehandle = open(mvlfile, O_RDONLY | O_BINARY);
92 #else
93                 filehandle = open(mvlfile, O_RDONLY);
94 #endif
95                 if (filehandle == -1) {
96                         fprintf(stderr, "Error opening %s\n", mvlfile);
97                         exit(1);
98                 }
99                 if ((read(filehandle, sig, 4) < 4) ||
100                         (strncmp(sig, "DMVL", 4)) ||
101                         (read(filehandle, &nfiles, 4) < 4)) {
102                         fprintf(stderr, "Error reading %s\n", mvlfile);
103                         exit(1);
104                 }
105 #ifdef WORDS_BIGENDIAN
106                 nfiles = SWAPINT(nfiles);
107 #endif
108                 if (nfiles > MAX_FILES) {
109                         fprintf(stderr, "Error reading %s: nfiles = %d, MAX_FILES = %d\n",
110                                         mvlfile, nfiles, MAX_FILES);
111                 }
112                 for (i = 0; i < nfiles; i++) {
113                         if ((read(filehandle, filename[i], 13) < 13) ||
114                                 (read(filehandle, &filesize[i], 4) < 4) ||
115                                 (strlen(filename[i]) > 12))     {
116                                 fprintf(stderr, "Error reading %s\n", mvlfile);
117                                 exit(1);
118                         }
119 #ifdef WORDS_BIGENDIAN
120                         filesize[i] = SWAPINT(filesize[i]);
121 #endif
122                 }
123
124                 for (i = 0; i < nfiles; i++) {
125                         if (mvefile) {
126                                 if (!strcasecmp(filename[i], mvefile))
127                                         break;
128                                 else
129                                         lseek(filehandle, filesize[i], SEEK_CUR);
130                         } else
131                                 printf("%13s\t%d\n", filename[i], filesize[i]);
132                 }
133                 if (!mvefile)
134                         exit(0);
135
136         } else if (mvefile) {
137 #ifdef O_BINARY
138                 filehandle = open(mvefile, O_RDONLY | O_BINARY);
139 #else
140                 filehandle = open(mvefile, O_RDONLY);
141 #endif
142         } else
143                 usage();
144
145     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
146     {
147         fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
148         exit(1);
149     }
150     atexit(SDL_Quit);
151
152         doPlay(filehandle);
153
154         return 0;
155 }