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
|
/* 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
*
*/
/*
* BS_GraphicEngine
* ----------------
* This the graphics engine interface.
*
* Autor: Malte Thiesen
*/
#ifndef SWORD25_GRAPHICENGINE_H
#define SWORD25_GRAPHICENGINE_H
// Includes
#include "common/array.h"
#include "common/rect.h"
#include "common/str.h"
#include "graphics/surface.h"
#include "sword25/kernel/common.h"
#include "sword25/kernel/bs_stdint.h"
#include "sword25/kernel/resservice.h"
#include "sword25/kernel/persistable.h"
#include "sword25/gfx/framecounter.h"
#include "sword25/gfx/renderobjectptr.h"
#include "sword25/math/vertex.h"
namespace Sword25 {
class Kernel;
class Image;
class Panel;
class Screenshot;
class RenderObjectManager;
// Typen
typedef uint BS_COLOR;
// Makros
#define BS_RGB(R,G,B) (0xFF000000 | ((R) << 16) | ((G) << 8) | (B))
#define BS_ARGB(A,R,G,B) (((A) << 24) | ((R) << 16) | ((G) << 8) | (B))
/**
@brief Dies ist das Graphik-Engine Interface, dass alle Methoden und Klassen enth�lt, die eine Graphik-Engine implementieren muss.
Hier sind nur wenige Rumpffunktionen realisiert, wie z.B. das Abfragen der Parameter des Ausgabepuffers.
Die Hauptfunktionen muss eine Implementation dieses Inferfaces stellen.<br>
Die bisher einzige Implementation ist BS_DDrawGfx.
*/
class GraphicEngine : public ResourceService, public Persistable {
public:
// Enums
// -----
// Colour formats
//
/**
* The colour format used by the engine
*/
enum COLOR_FORMATS {
/// Undefined/unknown colour format
CF_UNKNOWN = 0,
/**
* 24-bit colour format (R8G8B8)
*/
CF_RGB24,
/**
* 32-bit colour format (A8R8G8B8) (little endian)
*/
CF_ARGB32,
/**
32-bit colour format (A8B8G8R8) (little endian)
*/
CF_ABGR32
};
// Constructor
// -----------
GraphicEngine(Kernel *pKernel);
~GraphicEngine();
// Interface
// ---------
/**
* Initialises the graphics engine and sets the screen mode. Returns true if initialisation failed.
* Notes: This method should be called immediately after the initialisation of all services.
*
* @param Height The height of the output buffer in pixels. The default value is 600
* @param BitDepth The bit depth of the desired output buffer in bits. The default value is 16
* @param BackbufferCount The number of back buffers to be created. The default value is 2
* @param Windowed Indicates whether the engine is to run in windowed mode.
*/
bool Init(int Width = 800, int Height = 600, int BitDepth = 16, int BackbufferCount = 2, bool Windowed = false);
/**
* Begins rendering a new frame.
* Notes: This method must be called at the beginning of the main loop, before any rendering methods are used.
* Notes: Implementations of this method must call _UpdateLastFrameDuration()
* @param UpdateAll Specifies whether the renderer should redraw everything on the next frame.
* This feature can be useful if the renderer with Dirty Rectangles works, but sometimes the client may
*/
bool StartFrame(bool UpdateAll = false);
/**
* Ends the rendering of a frame and draws it on the screen.
*
* This method must be at the end of the main loop. After this call, no further Render method may be called.
* This should only be called once for a given previous call to #StartFrame.
*/
bool EndFrame();
// Debug methods
/**
* Draws a line in the frame buffer
*
* This method must be called between calls to StartFrame() and EndFrame(), and is intended only for debugging
* purposes. The line will only appear for a single frame. If the line is to be shown permanently, it must be
* called for every frame.
* @param Start The starting point of the line
* @param End The ending point of the line
* @param Color The colour of the line. The default is BS_RGB (255,255,255) (White)
*/
void DrawDebugLine(const Vertex &Start, const Vertex &End, uint Color = BS_RGB(255, 255, 255));
/**
* Creates a screenshot of the current frame buffer and writes it to a graphic file in PNG format.
* Returns true if the screenshot was saved successfully.
* Notes: This method should only be called after a call to EndFrame(), and before the next call to StartFrame().
* @param Filename The filename for the screenshot
*/
bool SaveScreenshot(const Common::String &Filename);
/**
* Creates a thumbnail with the dimensions of 200x125. This will not include the top and bottom of the screen..
* the interface boards the the image as a 16th of it's original size.
* Notes: This method should only be called after a call to EndFrame(), and before the next call to StartFrame().
* The frame buffer must have a resolution of 800x600.
* @param Filename The filename for the screenshot
*/
bool SaveThumbnailScreenshot(const Common::String &Filename);
/**
* Reads the current contents of the frame buffer
* Notes: This method is for creating screenshots. It is not very optimised. It should only be called
* after a call to EndFrame(), and before the next call to StartFrame().
* @param Width Returns the width of the frame buffer
* @param Height Returns the height of the frame buffer
* @param Data Returns the raw data of the frame buffer as an array of 32-bit colour values.
*/
Graphics::Surface *GetScreenshot();
RenderObjectPtr<Panel> GetMainPanel();
/**
* Specifies the time (in microseconds) since the last frame has passed
*/
int GetLastFrameDurationMicro() const {
if (!m_TimerActive)
return 0;
return m_LastFrameDuration;
}
/**
* Specifies the time (in microseconds) the previous frame took
*/
float GetLastFrameDuration() const {
if (!m_TimerActive)
return 0;
return static_cast<float>(m_LastFrameDuration) / 1000000.0f;
}
void StopMainTimer() {
m_TimerActive = false;
}
void ResumeMainTimer() {
m_TimerActive = true;
}
float GetSecondaryFrameDuration() const {
return static_cast<float>(m_LastFrameDuration) / 1000000.0f;
}
// Accessor methods
/**
* Returns the width of the output buffer in pixels
*/
int GetDisplayWidth() const {
return m_Width;
}
/**
* Returns the height of the output buffer in pixels
*/
int GetDisplayHeight() const {
return m_Height;
}
/**
* Returns the bounding box of the output buffer: (0, 0, Width, Height)
*/
Common::Rect &GetDisplayRect() {
return m_ScreenRect;
}
/**
* Returns the bit depth of the output buffer
*/
int GetBitDepth() {
return m_BitDepth;
}
/**
* Determines whether the frame buffer change is to be synchronised with Vsync. This is turned on by default.
* Notes: In windowed mode, this setting has no effect.
* @param Vsync Indicates whether the frame buffer changes are to be synchronised with Vsync.
*/
void SetVsync(bool Vsync);
/**
* Returns true if V-Sync is on.
* Notes: In windowed mode, this setting has no effect.
*/
bool GetVsync() const;
/**
* Returns true if the engine is running in Windowed mode.
*/
bool IsWindowed() {
return m_Windowed;
}
/**
* Fills a rectangular area of the frame buffer with a colour.
* Notes: It is possible to create transparent rectangles by passing a colour with an Alpha value of 255.
* @param FillRectPtr Pointer to a Common::Rect, which specifies the section of the frame buffer to be filled.
* If the rectangle falls partly off-screen, then it is automatically trimmed.
* If a NULL value is passed, then the entire image is to be filled.
* @param Color The 32-bit colour with which the area is to be filled. The default is BS_RGB(0, 0, 0) (black)
@remark Falls das Rechteck nicht v�llig innerhalb des Bildschirms ist, wird es automatisch zurechtgestutzt.
*/
bool fill(const Common::Rect *FillRectPtr = 0, uint Color = BS_RGB(0, 0, 0));
// Debugging Methods
int GetFPSCount() const {
return m_FPSCounter.GetFPS();
}
int GetRepaintedPixels() const {
return m_RepaintedPixels;
}
Graphics::Surface _backSurface;
Graphics::Surface *getSurface() { return &_backSurface; }
Graphics::Surface _frameBuffer;
Graphics::Surface *getFrameBuffer() { return &_frameBuffer; }
Common::MemoryReadStream *_thumbnail;
Common::MemoryReadStream *getThumbnail() { return _thumbnail; }
// Access methods
/**
* Returns the size of a pixel entry in bytes for a particular colour format
* @param ColorFormat The desired colour format. The parameter must be of type COLOR_FORMATS
* @return Returns the size of a pixel in bytes. If the colour format is unknown, -1 is returned.
*/
static int GetPixelSize(GraphicEngine::COLOR_FORMATS ColorFormat) {
switch (ColorFormat) {
case GraphicEngine::CF_ARGB32:
return 4;
default:
return -1;
}
}
/**
* Calculates the length of an image line in bytes, depending on a given colour format.
* @param ColorFormat The colour format
* @param Width The width of the line in pixels
* @return Reflects the length of the line in bytes. If the colour format is
* unknown, -1 is returned
*/
static int CalcPitch(GraphicEngine::COLOR_FORMATS ColorFormat, int Width) {
switch (ColorFormat) {
case GraphicEngine::CF_ARGB32:
return Width * 4;
default:
BS_ASSERT(false);
}
return -1;
}
// Resource-Managing Methods
// --------------------------
virtual Resource *loadResource(const Common::String &fileName);
virtual bool canLoadResource(const Common::String &fileName);
// Persistence Methods
// -------------------
virtual bool persist(OutputPersistenceBlock &Writer);
virtual bool unpersist(InputPersistenceBlock &Reader);
static void ARGBColorToLuaColor(lua_State *L, uint Color);
static uint LuaColorToARGBColor(lua_State *L, int StackIndex);
protected:
// Display Variables
// -----------------
int m_Width;
int m_Height;
Common::Rect m_ScreenRect;
int m_BitDepth;
bool m_Windowed;
// Debugging Variables
// -------------------
Framecounter m_FPSCounter;
uint m_RepaintedPixels;
/**
* Calculates the time since the last frame beginning has passed.
*/
void UpdateLastFrameDuration();
private:
bool RegisterScriptBindings();
// LastFrameDuration Variables
// ---------------------------
uint m_LastTimeStamp;
uint m_LastFrameDuration;
bool m_TimerActive;
Common::Array<uint> m_FrameTimeSamples;
uint m_FrameTimeSampleSlot;
private:
byte *_backBuffer;
RenderObjectPtr<Panel> m_MainPanelPtr;
Common::ScopedPtr<RenderObjectManager> _renderObjectManagerPtr;
struct DebugLine {
DebugLine(const Vertex &_Start, const Vertex &_End, uint _Color) :
Start(_Start),
End(_End),
Color(_Color) {}
DebugLine() {}
Vertex Start;
Vertex End;
uint Color;
};
Common::Array<DebugLine> m_DebugLines;
};
} // End of namespace Sword25
#endif
|