aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/gfx/opengl/openglgfx.cpp
blob: c30fc8ed0dcda1bc78c3ff167c1573602902a122 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

#include "common/system.h"

#include "sword25/gfx/bitmapresource.h"
#include "sword25/gfx/animationresource.h"
#include "sword25/gfx/fontresource.h"
#include "sword25/gfx/panel.h"
#include "sword25/gfx/renderobjectmanager.h"
#include "sword25/gfx/image/vectorimage.h"
#include "sword25/package/packagemanager.h"
#include "sword25/kernel/inputpersistenceblock.h"
#include "sword25/kernel/outputpersistenceblock.h"

#include "sword25/gfx/opengl/openglgfx.h"
#include "sword25/gfx/opengl/glimage.h"
#include "sword25/gfx/opengl/swimage.h"

namespace Sword25 {

#define BS_LOG_PREFIX "OPENGLGFX"


// -----------------------------------------------------------------------------
// CONSTANTS
// -----------------------------------------------------------------------------

namespace {
const int BIT_DEPTH = 32;
const int BACKBUFFER_COUNT = 1;
const Common::String PNG_EXTENSION(".png");
const Common::String PNG_S_EXTENSION("_s.png");
const Common::String ANI_EXTENSION("_ani.xml");
const Common::String FNT_EXTENSION("_fnt.xml");
const Common::String SWF_EXTENSION(".swf");
const Common::String B25S_EXTENSION(".b25s");
}


// -----------------------------------------------------------------------------
// CONSTRUCTION / DESTRUCTION
// -----------------------------------------------------------------------------

OpenGLGfx::OpenGLGfx(BS_Kernel *pKernel) :
	GraphicEngine(pKernel),
	m_GLspritesInitialized(false) {
}

// -----------------------------------------------------------------------------

OpenGLGfx::~OpenGLGfx() {
	_backSurface.free();
}

// -----------------------------------------------------------------------------

BS_Service *OpenGLGfx_CreateObject(BS_Kernel *pKernel) {
	return new OpenGLGfx(pKernel);
}


// -----------------------------------------------------------------------------
// INTERFACE
// -----------------------------------------------------------------------------

bool OpenGLGfx::Init(int Width, int Height, int BitDepth, int BackbufferCount, bool Windowed) {
	// Warnung ausgeben, wenn eine nicht unterst�tzte Bittiefe gew�hlt wurde.
	if (BitDepth != BIT_DEPTH) {
		BS_LOG_WARNINGLN("Can't use a bit depth of %d (not supported). Falling back to %d.", BitDepth, BIT_DEPTH);
		m_BitDepth = BIT_DEPTH;
	}

	// Warnung ausgeben, wenn nicht genau ein Backbuffer gew�hlt wurde.
	if (BackbufferCount != BACKBUFFER_COUNT) {
		BS_LOG_WARNINGLN("Can't use %d backbuffers (not supported). Falling back to %d.", BackbufferCount, BACKBUFFER_COUNT);
		BackbufferCount = BACKBUFFER_COUNT;
	}

	// Parameter in lokale Variablen kopieren
	m_Width = Width;
	m_Height = Height;
	m_BitDepth = BitDepth;
	m_Windowed = Windowed;
	m_ScreenRect.left = 0;
	m_ScreenRect.top = 0;
	m_ScreenRect.right = m_Width;
	m_ScreenRect.bottom = m_Height;

	_backSurface.create(Width, Height, 4);

	// We already iniitalized gfx after the engine creation
	m_GLspritesInitialized = true;

	// Standardm��ig ist Vsync an.
	SetVsync(true);

	// Layer-Manager initialisieren.
	m_RenderObjectManagerPtr.reset(new RenderObjectManager(Width, Height, BackbufferCount + 1));

	// Hauptpanel erstellen
	m_MainPanelPtr = m_RenderObjectManagerPtr->GetTreeRoot()->AddPanel(Width, Height, BS_ARGB(0, 0, 0, 0));
	if (!m_MainPanelPtr.IsValid()) return false;
	m_MainPanelPtr->SetVisible(true);

	return true;
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::StartFrame(bool UpdateAll) {
	// Berechnen, wie viel Zeit seit dem letzten Frame vergangen ist.
	// Dieser Wert kann �ber GetLastFrameDuration() von Modulen abgefragt werden, die zeitabh�ngig arbeiten.
	UpdateLastFrameDuration();

	// Den Layer-Manager auf den n�chsten Frame vorbereiten
	m_RenderObjectManagerPtr->StartFrame();

	return true;
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::EndFrame() {
	// Scene zeichnen
	m_RenderObjectManagerPtr->Render();

	g_system->updateScreen();

	// Debug-Lines zeichnen
	if (!m_DebugLines.empty()) {
#if 0
		glEnable(GL_LINE_SMOOTH);
		glBegin(GL_LINES);

		Common::Array<DebugLine>::const_iterator iter = m_DebugLines.begin();
		for (; iter != m_DebugLines.end(); ++iter) {
			const unsigned int &Color = (*iter).Color;
			const BS_Vertex &Start = (*iter).Start;
			const BS_Vertex &End = (*iter).End;

			glColor4ub((Color >> 16) & 0xff, (Color >> 8) & 0xff, Color & 0xff, Color >> 24);
			glVertex2d(Start.X, Start.Y);
			glVertex2d(End.X, End.Y);
		}

		glEnd();
		glDisable(GL_LINE_SMOOTH);
#endif

		warning("STUB: Drawing debug lines");

		m_DebugLines.clear();
	}

	// Framecounter aktualisieren
	m_FPSCounter.Update();

	return true;
}

// -----------------------------------------------------------------------------

RenderObjectPtr<Panel> OpenGLGfx::GetMainPanel() {
	return m_MainPanelPtr;
}

// -----------------------------------------------------------------------------

void OpenGLGfx::SetVsync(bool Vsync) {
	warning("STUB: SetVsync(%d)", Vsync);
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::GetVsync() const {
	warning("STUB: GetVsync()");

	return true;
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::Fill(const BS_Rect *FillRectPtr, unsigned int Color) {
	BS_Rect Rect;

	if (!FillRectPtr) {
		Rect.left = 0;
		Rect.top = 0;
		Rect.right = m_Width;
		Rect.bottom = m_Height;
		FillRectPtr = &Rect;
	}

	warning("STUB: Fill()");

	return true;
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::GetScreenshot(unsigned int &Width, unsigned int &Height, byte **Data) {
	if (!ReadFramebufferContents(m_Width, m_Height, Data))
		return false;

	// Die Gr��e des Framebuffers zur�ckgeben.
	Width = m_Width;
	Height = m_Height;

	// Bilddaten vom OpenGL-Format in unser eigenes Format umwandeln.
	ReverseRGBAComponentOrder(*Data, Width * Height);
	FlipImagedataVertical(Width, Height, *Data);

	return true;
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::ReadFramebufferContents(unsigned int Width, unsigned int Height, byte **Data) {
    *Data = (byte *)malloc(Width * Height * 4);
	
	return true;
}

// -----------------------------------------------------------------------------

void OpenGLGfx::ReverseRGBAComponentOrder(byte *Data, uint size) {
	uint32 *ptr = (uint32 *)Data;

	for (uint i = 0; i < size; i++) {
		unsigned int Pixel = *ptr;
		*ptr = (Pixel & 0xff00ff00) | ((Pixel >> 16) & 0xff) | ((Pixel & 0xff) << 16);
		++ptr;
	}
}

// -----------------------------------------------------------------------------

void OpenGLGfx::FlipImagedataVertical(unsigned int Width, unsigned int Height, byte *Data) {
#if 0 // TODO
	vector<unsigned int> LineBuffer(Width);

	for (unsigned int Y = 0; Y < Height / 2; ++Y) {
		vector<unsigned int>::iterator Line1It = Data.begin() + Y * Width;
		vector<unsigned int>::iterator Line2It = Data.begin() + (Height - 1 - Y) * Width;
		copy(Line1It, Line1It + Width, LineBuffer.begin());
		copy(Line2It, Line2It + Width, Line1It);
		copy(LineBuffer.begin(), LineBuffer.end(), Line2It);
	}
#endif
}

// -----------------------------------------------------------------------------
// RESOURCE MANAGING
// -----------------------------------------------------------------------------

BS_Resource *OpenGLGfx::LoadResource(const Common::String &FileName) {
	BS_ASSERT(CanLoadResource(FileName));

	// Bild f�r den Softwarebuffer laden
	if (FileName.hasSuffix(PNG_S_EXTENSION)) {
		bool Result;
		SWImage *pImage = new SWImage(FileName, Result);
		if (!Result) {
			delete pImage;
			return 0;
		}

		BitmapResource *pResource = new BitmapResource(FileName, pImage);
		if (!pResource->IsValid()) {
			delete pResource;
			return 0;
		}

		return pResource;
	}

	// Sprite-Bild laden
	if (FileName.hasSuffix(PNG_EXTENSION) || FileName.hasSuffix(B25S_EXTENSION)) {
		bool Result;
		GLImage *pImage = new GLImage(FileName, Result);
		if (!Result) {
			delete pImage;
			return 0;
		}

		BitmapResource *pResource = new BitmapResource(FileName, pImage);
		if (!pResource->IsValid()) {
			delete pResource;
			return 0;
		}

		return pResource;
	}


	// Vectorgraphik laden
	if (FileName.hasSuffix(SWF_EXTENSION)) {
		// Pointer auf Package-Manager holen
		PackageManager *pPackage = BS_Kernel::GetInstance()->GetPackage();
		BS_ASSERT(pPackage);

		// Datei laden
		unsigned char *pFileData;
		unsigned int FileSize;
		if (!(pFileData = static_cast<unsigned char *>(pPackage->GetFile(FileName, &FileSize)))) {
			BS_LOG_ERRORLN("File \"%s\" could not be loaded.", FileName.c_str());
			return 0;
		}

		bool Result;
		VectorImage *pImage = new VectorImage(pFileData, FileSize, Result);
		if (!Result) {
			delete pImage;
			delete [] pFileData;
			return 0;
		}

		BitmapResource *pResource = new BitmapResource(FileName, pImage);
		if (!pResource->IsValid()) {
			delete pResource;
			delete [] pFileData;
			return 0;
		}

		delete [] pFileData;
		return pResource;
	}

	// Animation laden
	if (FileName.hasSuffix(ANI_EXTENSION)) {
		AnimationResource *pResource = new AnimationResource(FileName);
		if (pResource->IsValid())
			return pResource;
		else {
			delete pResource;
			return 0;
		}
	}

	// Font laden
	if (FileName.hasSuffix(FNT_EXTENSION)) {
		FontResource *pResource = new FontResource(BS_Kernel::GetInstance(), FileName);
		if (pResource->IsValid())
			return pResource;
		else {
			delete pResource;
			return 0;
		}
	}

	BS_LOG_ERRORLN("Service cannot load \"%s\".", FileName.c_str());
	return 0;
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::CanLoadResource(const Common::String &FileName) {
	return FileName.hasSuffix(PNG_EXTENSION) ||
		FileName.hasSuffix(ANI_EXTENSION) ||
		FileName.hasSuffix(FNT_EXTENSION) ||
		FileName.hasSuffix(SWF_EXTENSION) ||
		FileName.hasSuffix(B25S_EXTENSION);
}


// -----------------------------------------------------------------------------
// DEBUGGING
// -----------------------------------------------------------------------------

void OpenGLGfx::DrawDebugLine(const Vertex &Start, const Vertex &End, unsigned int Color) {
	m_DebugLines.push_back(DebugLine(Start, End, Color));
}

// -----------------------------------------------------------------------------
// PERSISTENZ
// -----------------------------------------------------------------------------

bool OpenGLGfx::Persist(BS_OutputPersistenceBlock &Writer) {
	bool result = true;

	result &= GraphicEngine::Persist(Writer);
	result &= m_RenderObjectManagerPtr->Persist(Writer);

	return result;
}

// -----------------------------------------------------------------------------

bool OpenGLGfx::Unpersist(BS_InputPersistenceBlock &Reader) {
	bool result = true;

	result &= GraphicEngine::Unpersist(Reader);
	result &= m_RenderObjectManagerPtr->Unpersist(Reader);

	return result && Reader.IsGood();
}

} // End of namespace Sword25