1 /*
2  * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3  * It is copyright by its individual contributors, as recorded in the
4  * project's Git history.  See COPYING.txt at the top level for license
5  * terms and a link to the Git history.
6  */
7 
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <string.h>
12 
13 #ifdef _WIN32
14 # include <windows.h>
15 #endif
16 
17 #include <SDL.h>
18 
19 #include "libmve.h"
20 
21 static SDL_Surface *g_screen;
22 static palette_array_t g_palette;
23 static int g_truecolor;
24 
25 static int doPlay(const char *filename);
26 
usage(void)27 static void usage(void)
28 {
29 	fprintf(stderr, "usage: mveplay filename\n");
30 	exit(1);
31 }
32 
main(int c,char ** v)33 int main(int c, char **v)
34 {
35 	if (c != 2)
36 		usage();
37 
38 	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
39 	{
40 		fprintf(stderr, "Could not initialize SDL: %s\n",SDL_GetError());
41 		exit(1);
42 	}
43 	atexit(SDL_Quit);
44 
45 	return doPlay(v[1]);
46 }
47 
MovieFileRead(void * handle,void * buf,unsigned int count)48 unsigned int MovieFileRead(void *handle, void *buf, unsigned int count)
49 {
50 	unsigned numread;
51 	numread = fread(buf, 1, count, (FILE *)handle);
52 	return (numread == count);
53 }
54 
MovieShowFrame(unsigned char * buf,int dstx,int dsty,int bufw,int bufh,int sw,int sh)55 void MovieShowFrame(unsigned char *buf, int dstx, int dsty, int bufw, int bufh, int sw, int sh)
56 {
57 	int i;
58 	unsigned char *pal;
59 	SDL_Surface *sprite;
60 	SDL_Rect srcRect, destRect;
61 
62 	if (dstx == -1) // center it
63 		dstx = (sw - bufw) / 2;
64 	if (dsty == -1) // center it
65 		dsty = (sh - bufh) / 2;
66 
67 	if (g_truecolor)
68 		sprite = SDL_CreateRGBSurfaceFrom(buf, bufw, bufh, 16, 2 * bufw, 0x7C00, 0x03E0, 0x001F, 0);
69 	else
70 	{
71 		sprite = SDL_CreateRGBSurfaceFrom(buf, bufw, bufh, 8, bufw, 0x7C00, 0x03E0, 0x001F, 0);
72 
73 		pal = g_palette;
74 		for(i = 0; i < 256; i++)
75 		{
76 			sprite->format->palette->colors[i].r = (*pal++) << 2;
77 			sprite->format->palette->colors[i].g = (*pal++) << 2;
78 			sprite->format->palette->colors[i].b = (*pal++) << 2;
79 			sprite->format->palette->colors[i].unused = 0;
80 		}
81 	}
82 
83 	srcRect.x = 0;
84 	srcRect.y = 0;
85 	srcRect.w = bufw;
86 	srcRect.h = bufh;
87 	destRect.x = dstx;
88 	destRect.y = dsty;
89 	destRect.w = bufw;
90 	destRect.h = bufh;
91 
92 	SDL_BlitSurface(sprite, &srcRect, g_screen, &destRect);
93 
94 	if ( (g_screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF )
95 		SDL_Flip(g_screen);
96 	else
97 		SDL_UpdateRects(g_screen, 1, &destRect);
98 
99 	SDL_FreeSurface(sprite);
100 }
101 
MovieSetPalette(const unsigned char * p,unsigned start,unsigned count)102 void MovieSetPalette(const unsigned char *p, unsigned start, unsigned count)
103 {
104 	//Set color 0 to be black
105 	g_palette[0] = g_palette[1] = g_palette[2] = 0;
106 
107 	//Set color 255 to be our subtitle color
108 	g_palette[765] = g_palette[766] = g_palette[767] = 50;
109 
110 	//movie libs palette into our array
111 	memcpy(g_palette + start*3, p+start*3, count*3);
112 }
113 
pollEvents()114 static int pollEvents()
115 {
116 	SDL_Event event;
117 
118 	while (SDL_PollEvent(&event))
119 	{
120 		switch(event.type)
121 		{
122 		case SDL_QUIT:
123 		case SDL_MOUSEBUTTONDOWN:
124 		case SDL_MOUSEBUTTONUP:
125 			return 1;
126 		case SDL_KEYDOWN:
127 			switch (event.key.keysym.sym)
128 			{
129 			case SDLK_ESCAPE:
130 			case SDLK_q:
131 				return 1;
132 			case SDLK_f:
133 				SDL_WM_ToggleFullScreen(g_screen);
134 				break;
135 			default:
136 				break;
137 			}
138 			break;
139 		default:
140 			break;
141 		}
142 	}
143 
144 	return 0;
145 }
146 
MovieMemoryAllocate(std::size_t size)147 void *MovieMemoryAllocate(std::size_t size)
148 {
149 	return malloc(size);
150 }
151 
MovieMemoryFree(void * p)152 void MovieMemoryFree(void *p)
153 {
154 	free(p);
155 }
156 
doPlay(const char * filename)157 static int doPlay(const char *filename)
158 {
159 	int result;
160 	int done = 0;
161 	int bpp = 0;
162 	FILE *mve;
163 	MVE_videoSpec vSpec;
164 
165 	mve = fopen(filename, "rb");
166 	if (mve == NULL) {
167 		fprintf(stderr, "can't open MVE file\n");
168 		return 1;
169 	}
170 
171 	memset(g_palette, 0, 768);
172 
173 	MVE_sndInit(1);
174 
175 	MVE_rmPrepMovie(mve, -1, -1, 1);
176 
177 	MVE_getVideoSpec(&vSpec);
178 
179 	bpp = vSpec.truecolor?16:8;
180 
181 	g_screen = SDL_SetVideoMode(vSpec.screenWidth, vSpec.screenHeight, bpp, SDL_ANYFORMAT);
182 
183 	g_truecolor = vSpec.truecolor;
184 
185 	while (!done && (result = MVE_rmStepMovie()) == 0)
186 	{
187 		done = pollEvents();
188 	}
189 
190 	MVE_rmEndMovie();
191 
192 	fclose(mve);
193 
194 	return 0;
195 }
196