Make movies use new plugin system.
[gemrb.git] / gemrb / plugins / MVEPlayer / MVEPlay.cpp
blob5de99a2e8b68dfadd9d0818c19829685fa5468a8
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * $Id$
22 #include <assert.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <cstdio>
26 #include "../Core/Interface.h"
27 #include "../Core/Video.h"
28 #include "../Core/Audio.h"
29 #include "../Core/Variables.h"
30 #include "MVEPlay.h"
31 #include "mve_player.h"
32 #include "../../includes/ie_types.h"
34 static const char MVESignature[] = "Interplay MVE File\x1A";
35 static const int MVE_SIGNATURE_LEN = 19;
37 static Video *video = NULL;
38 static unsigned char g_palette[768];
39 static int g_truecolor;
40 static ieDword maxRow = 0;
41 static ieDword rowCount = 0;
42 static ieDword frameCount = 0;
43 static ieDword *cbAtFrame = NULL;
44 static ieDword *strRef = NULL;
46 MVEPlay::MVEPlay(void)
48 video = core->GetVideoDriver();
51 MVEPlay::~MVEPlay(void)
55 bool MVEPlay::Open(DataStream* stream, bool autoFree)
57 if (!Resource::Open(stream,autoFree))
58 return false;
59 validVideo = false;
61 char Signature[MVE_SIGNATURE_LEN];
62 str->Read( Signature, MVE_SIGNATURE_LEN );
63 if (memcmp( Signature, MVESignature, MVE_SIGNATURE_LEN ) != 0) {
64 return false;
67 str->Seek( 0, GEM_STREAM_START );
68 validVideo = true;
69 return true;
72 void MVEPlay::CallBackAtFrames(ieDword cnt, ieDword *arg, ieDword *arg2 )
74 maxRow = cnt;
75 frameCount = 0;
76 rowCount = 0;
77 cbAtFrame = arg;
78 strRef = arg2;
81 int MVEPlay::Play()
83 if (!validVideo) {
84 return 0;
86 //Start Movie Playback
87 frameCount = 0;
88 return doPlay( );
91 int MVEPlay::doPlay()
93 int done = 0;
94 MVEPlayer player(this);
96 memset( g_palette, 0, 768 );
98 //ieDword volume;
99 //core->GetDictionary()->Lookup( "Volume Movie", volume );
100 player.sound_init( core->GetAudioDrv()->CanPlay() );
102 int w,h;
104 video->InitMovieScreen(w,h);
105 player.video_init(w, h);
107 if (!player.start_playback()) {
108 printf("Failed to decode movie!\n");
109 return 1;
112 g_truecolor = player.is_truecolour();
114 while (!done && player.next_frame()) {
115 done = video->PollMovieEvents();
118 return 0;
121 unsigned int MVEPlay::fileRead(void* buf, unsigned int count)
123 unsigned numread;
125 numread = str->Read( buf, count );
126 return ( numread == count );
129 void MVEPlay::showFrame(unsigned char* buf, unsigned int bufw,
130 unsigned int bufh, unsigned int sx, unsigned int sy, unsigned int w,
131 unsigned int h, unsigned int dstx, unsigned int dsty)
133 ieDword titleref = 0;
135 if (cbAtFrame && strRef) {
136 frameCount ++;
137 if ((rowCount<maxRow) && (frameCount >= cbAtFrame[rowCount]) ) {
138 rowCount++;
140 //draw subtitle here
141 if (rowCount) {
142 titleref = strRef[rowCount-1];
145 video->showFrame(buf,bufw,bufh,sx,sy,w,h,dstx,dsty, g_truecolor, g_palette, titleref);
148 void MVEPlay::setPalette(unsigned char* p, unsigned start, unsigned count)
150 //Set color 0 to be black
151 g_palette[0] = g_palette[1] = g_palette[2] = 0;
153 //Set color 255 to be our subtitle color
154 g_palette[765] = g_palette[766] = g_palette[767] = 50;
156 //movie libs palette into our array
157 memcpy( g_palette + start * 3, p + start * 3, count * 3 );
160 int MVEPlay::setAudioStream()
162 ieDword volume ;
163 core->GetDictionary()->Lookup( "Volume Movie", volume) ;
164 int source = core->GetAudioDrv()->SetupNewStream(0, 0, 0, volume, false, false) ;
165 return source;
168 void MVEPlay::freeAudioStream(int stream)
170 if (stream > -1)
171 core->GetAudioDrv()->ReleaseStream(stream, true);
174 void MVEPlay::queueBuffer(int stream, unsigned short bits,
175 int channels, short* memory,
176 int size, int samplerate)
178 if (stream > -1)
179 core->GetAudioDrv()->QueueBuffer(stream, bits, channels, memory, size, samplerate) ;
183 #include "../../includes/plugindef.h"
185 GEMRB_PLUGIN(0x218963DC, "MVE Video Player")
186 PLUGIN_IE_RESOURCE(&MoviePlayer::ID, MVEPlay, ".mve", (ieWord)IE_MVE_CLASS_ID)
187 END_PLUGIN()