aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/video/video_subtitler.cpp
blob: 8c6d222cd38ec7903c1ccf7de51d6d65f4059e10 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This file is based on Wintermute Engine
 * http://dead-code.org/redir.php?target=wme
 * Copyright (c) 2011 Jan Nedoma
 */


//  #include "dcgf.h"
#include "engines/wintermute/video/video_subtitler.h"
#define S_OK 0
#define BYTE byte
#define LONG long
#define MAX_PATH 127
#define _MAX_DRIVE 127
#define _MAX_DIR 127
#define _MAX_FNAME 127
#define DWORD byte

namespace Wintermute {
//////////////////////////////////////////////////////////////////////////
CVidSubtitler::CVidSubtitler(BaseGame *inGame): BaseClass(inGame) {
	m_LastSample = -1;
	m_CurrentSubtitle = 0;
	m_ShowSubtitle = false;
}

//////////////////////////////////////////////////////////////////////////
CVidSubtitler::~CVidSubtitler(void) {
	for (int i = 0; i < m_Subtitles.size(); i++) delete m_Subtitles[i];
	m_Subtitles.clear();
}


//////////////////////////////////////////////////////////////////////////
bool CVidSubtitler::LoadSubtitles(char *Filename, char *SubtitleFile) {
	if (!Filename) return S_OK;

	for (int i = 0; i < m_Subtitles.size(); i++) delete m_Subtitles[i];
	m_Subtitles.clear();

	m_LastSample = -1;
	m_CurrentSubtitle = 0;
	m_ShowSubtitle = false;


	char NewFile[MAX_PATH];
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];

	if (SubtitleFile) {
		strcpy(NewFile, SubtitleFile);
	}
#if 0
	else {
		_splitpath(Filename, drive, dir, fname, NULL);
		_makepath(NewFile, drive, dir, fname, ".SUB");
	}
#endif
	DWORD Size;

#if 0
	BYTE *Buffer = _gameRef->m_FileManager->ReadWholeFile(NewFile, &Size, false);

	if (Buffer == NULL) return S_OK; // no subtitles


	LONG Start, End;
	bool InToken;
	char *TokenStart;
	int TokenLength;
	int TokenPos;
	int TextLength;

	int Pos = 0;
	int LineLength = 0;
	while (Pos < Size) {
		Start = End = -1;
		InToken = false;
		TokenPos = -1;
		TextLength = 0;

		LineLength = 0;
		while (Pos + LineLength < Size && Buffer[Pos + LineLength] != '\n' && Buffer[Pos + LineLength] != '\0') LineLength++;

		int RealLength = LineLength - (Pos + LineLength >= Size ? 0 : 1);
		char *Text = new char[RealLength + 1];
		char *line = (char *)&Buffer[Pos];

		for (int i = 0; i < RealLength; i++) {
			if (line[i] == '{') {
				if (!InToken) {
					InToken = true;
					TokenStart = line + i + 1;
					TokenLength = 0;
					TokenPos++;
				} else TokenLength++;
			} else if (line[i] == '}') {
				if (InToken) {
					InToken = false;
					char *Token = new char[TokenLength + 1];
					strncpy(Token, TokenStart, TokenLength);
					Token[TokenLength] = '\0';
					if (TokenPos == 0) Start = atoi(Token);
					else if (TokenPos == 1) End = atoi(Token);

					delete [] Token;
				} else {
					Text[TextLength] = line[i];
					TextLength++;
				}
			} else {
				if (InToken) {
					TokenLength++;
				} else {
					Text[TextLength] = line[i];
					if (Text[TextLength] == '|') Text[TextLength] = '\n';
					TextLength++;
				}
			}
		}
		Text[TextLength] = '\0';

		if (Start != -1 && TextLength > 0 && (Start != 1 || End != 1)) m_Subtitles.Add(new CVidSubtitle(Game, Text, Start, End));

		delete [] Text;

		Pos += LineLength + 1;
	}

	delete [] Buffer;
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
bool CVidSubtitler::Display() {
#if 0
	if (m_ShowSubtitle) {
		CBFont *font = Game->m_VideoFont ? Game->m_VideoFont : Game->m_SystemFont;
		int Height = font->GetTextHeight((BYTE *)m_Subtitles[m_CurrentSubtitle]->m_Text, Game->m_Renderer->m_Width);
		font->DrawText((BYTE *)m_Subtitles[m_CurrentSubtitle]->m_Text, 0, Game->m_Renderer->m_Height - Height - 5, Game->m_Renderer->m_Width, TAL_CENTER);
	}
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
bool CVidSubtitler::Update(LONG Frame) {
	if (Frame != m_LastSample) {
		m_LastSample = Frame;

		// process subtitles
		m_ShowSubtitle = false;
		while (m_CurrentSubtitle < m_Subtitles.size()) {
			int End = m_Subtitles[m_CurrentSubtitle]->m_EndFrame;

			bool NextFrameOK = (m_CurrentSubtitle < m_Subtitles.size() - 1 && m_Subtitles[m_CurrentSubtitle + 1]->m_StartFrame <= Frame);

			if (Frame > End) {
				if (NextFrameOK) {
					m_CurrentSubtitle++;
				} else {
					m_ShowSubtitle = (End == 0);
					break;
				}
			} else {
				m_ShowSubtitle = true;
				break;
			}
		}
	}
	return S_OK;
}
}