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