aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/gfx/opengl/glimage.cpp
blob: 54d660aa7f1d555bb8237b39106c10eef5408266 (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
/* 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
 *
 */

// -----------------------------------------------------------------------------
// INCLUDES
// -----------------------------------------------------------------------------

#include "sword25/util/glsprites/glsprites.h"
#include "sword25/package/packagemanager.h"
#include "sword25/gfx/image/imageloader.h"
#include "sword25/gfx/opengl/openglgfx.h"
#include "sword25/gfx/opengl/glimage.h"

namespace Sword25 {

#define BS_LOG_PREFIX "GLIMAGE"

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

BS_GLImage::BS_GLImage(const Common::String & Filename, bool & Result) :
	m_Sprite(0),
	m_Width(0),
	m_Height(0)
{
	Result = false;

	BS_PackageManager * pPackage = static_cast<BS_PackageManager*>(BS_Kernel::GetInstance()->GetService("package"));
	BS_ASSERT(pPackage);

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

	// Bildeigenschaften bestimmen
	BS_GraphicEngine::COLOR_FORMATS ColorFormat;
	int Pitch;
	if (!BS_ImageLoader::ExtractImageProperties(pFileData, FileSize, ColorFormat, m_Width, m_Height))
	{
		BS_LOG_ERRORLN("Could not read image properties.");
		return;
	}

	// Das Bild dekomprimieren
	char * pUncompressedData;
	if (!BS_ImageLoader::LoadImage(pFileData, FileSize, BS_GraphicEngine::CF_ABGR32, pUncompressedData, m_Width, m_Height, Pitch))
	{
		BS_LOG_ERRORLN("Could not decode image.");
		return;
	}

	// Dateidaten freigeben
	delete[] pFileData;

	// GLS-Sprite mit den Bilddaten erstellen
	GLS_Result GLSResult = GLS_NewSprite(m_Width, m_Height,
										 (ColorFormat == BS_GraphicEngine::CF_ARGB32) ? GLS_True : GLS_False,
										 pUncompressedData,
										 &m_Sprite);
	if (Result != GLS_OK)
	{
		BS_LOG_ERRORLN("Could not create GLS_Sprite. Reason: %s", GLS_ResultString(GLSResult));
		return;
	}

	// Bilddaten freigeben
	delete[] pUncompressedData;

	Result = true;
	return;
}

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

BS_GLImage::BS_GLImage(unsigned int Width, unsigned int Height, bool & Result) :
	m_Sprite(0),
	m_Width(Width),
	m_Height(Height)
{
	Result = false;

	// GLS-Sprite mit den Bilddaten erstellen
	GLS_Result GLSResult = GLS_NewSprite(m_Width, m_Height,
										 GLS_True,
										 0,
										 &m_Sprite);
	if (GLSResult != GLS_OK)
	{
		BS_LOG_ERRORLN("Could not create GLS_Sprite. Reason: %s", GLS_ResultString(GLSResult));
		return;
	}

	Result = true;
	return;
}

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

BS_GLImage::~BS_GLImage()
{
	if (m_Sprite) GLS_DeleteSprite(m_Sprite);
}

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

bool BS_GLImage::Fill(const BS_Rect* pFillRect, unsigned int Color)
{
	BS_LOG_ERRORLN("Fill() is not supported.");
	return false;
}

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

bool BS_GLImage::SetContent(const byte *Pixeldata, unsigned int Offset, unsigned int Stride)
{
	// �berpr�fen, ob PixelData ausreichend viele Pixel enth�lt um ein Bild der Gr��e Width * Height zu erzeugen
	if (Pixeldata.size() < static_cast<unsigned int>(m_Width * m_Height * 4))
	{
		BS_LOG_ERRORLN("PixelData vector is too small to define a 32 bit %dx%d image.", m_Width, m_Height);
		return false;
	}

	// GLS-Sprite mit den Bilddaten f�llen
	GLS_Result GLSResult = GLS_SetSpriteData(m_Sprite, m_Width, m_Height, &Pixeldata[Offset], Stride / 4);
	if (GLSResult != GLS_OK)
	{
		BS_LOG_ERRORLN("CGLS_SetSpriteData() failed. Reason: %s", GLS_ResultString(GLSResult));
		return false;
	}

	return true;
}

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

unsigned int BS_GLImage::GetPixel(int X, int Y)
{
	BS_LOG_ERRORLN("GetPixel() is not supported. Returning black.");
	return 0;
}

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

bool BS_GLImage::Blit(int PosX, int PosY,
					  int Flipping,
					  BS_Rect* pPartRect,
					  unsigned int Color,
					  int Width, int Height)
{
	// BS_Rect nach GLS_Rect konvertieren
	GLS_Rect SubImage;
	if (pPartRect)
	{
		SubImage.x1 = pPartRect->left;
		SubImage.y1 = pPartRect->top;
		SubImage.x2 = pPartRect->right;
		SubImage.y2 = pPartRect->bottom;
	}

	// Farbe nach GLS_Color konvertieren
	GLS_Color GLSColor;
	GLSColor.a = Color >> 24;
	GLSColor.r = (Color >> 16) & 0xff;
	GLSColor.g = (Color >> 8) & 0xff;
	GLSColor.b = Color & 0xff;

	// Skalierungen berechnen
	GLS_Float ScaleX, ScaleY;
	if (Width == -1) Width = m_Width;
	ScaleX = (GLS_Float) Width / (GLS_Float) m_Width;

	if (Height == -1) Height = m_Height;
	ScaleY = (GLS_Float) Height / (GLS_Float) m_Height;

	// Rendern
	// TODO:
	// Die Bedeutung von FLIP_V und FLIP_H ist vertauscht. Allerdings glaubt der Rest der Engine auch daran, daher war es einfacher diesen Fehler
	// weiterzuf�hren. Bei Gelegenheit ist dieses aber zu �ndern.
	GLS_Result Result = GLS_Blit(m_Sprite,
								 PosX, PosY,
								 pPartRect ? &SubImage : 0, &GLSColor,
								 (Flipping & BS_Image::FLIP_V) ? GLS_True : GLS_False,
								 (Flipping & BS_Image::FLIP_H) ? GLS_True : GLS_False,
								 ScaleX, ScaleY);
	if (Result != GLS_OK) BS_LOG_ERRORLN("GLS_Blit() failed. Reason: %s", GLS_ResultString(Result));

	return Result == GLS_OK;
}

} // End of namespace Sword25