1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 //=============================================================================
24 //
25 // Graphics driver interface
26 //
27 //=============================================================================
28 
29 #ifndef AGS_ENGINE_GFX_GRAPHICS_DRIVER_H
30 #define AGS_ENGINE_GFX_GRAPHICS_DRIVER_H
31 
32 //#include "math/matrix.h"
33 #include "ags/lib/std/memory.h"
34 #include "ags/lib/allegro.h" // RGB, PALETTE
35 #include "ags/engine/gfx/gfx_defines.h"
36 #include "ags/engine/gfx/gfx_mode_list.h"
37 #include "ags/shared/util/geometry.h"
38 
39 namespace AGS3 {
40 namespace AGS {
41 
42 namespace Shared {
43 class Bitmap;
44 typedef std::shared_ptr<Shared::Bitmap> PBitmap;
45 } // namespace Shared
46 
47 namespace Engine {
48 
49 // Forward declaration
50 class IDriverDependantBitmap;
51 class IGfxFilter;
52 typedef std::shared_ptr<IGfxFilter> PGfxFilter;
53 using Shared::PBitmap;
54 
55 enum TintMethod {
56 	TintReColourise = 0,
57 	TintSpecifyMaximum = 1
58 };
59 
60 enum VideoSkipType {
61 	VideoSkipNone = 0,
62 	VideoSkipEscape = 1,
63 	VideoSkipAnyKey = 2,
64 	VideoSkipKeyOrMouse = 3
65 };
66 
67 // Sprite transformation
68 // TODO: combine with stretch parameters in the IDriverDependantBitmap?
69 struct SpriteTransform {
70 	// Translate
71 	int X, Y;
72 	float ScaleX, ScaleY;
73 	float Rotate; // angle, in radians
74 
SpriteTransformSpriteTransform75 	SpriteTransform()
76 		: X(0), Y(0), ScaleX(1.f), ScaleY(1.f), Rotate(0.f) {
77 	}
78 
79 	SpriteTransform(int x, int y, float scalex = 1.0f, float scaley = 1.0f, float rotate = 0.0f)
XSpriteTransform80 		: X(x), Y(y), ScaleX(scalex), ScaleY(scaley), Rotate(rotate) {
81 	}
82 };
83 
84 // Describes 3 render matrixes: world, view and projection
85 struct RenderMatrixes {
86 	/*
87 	glm::mat4 World;
88 	glm::mat4 View;
89 	glm::mat4 Projection;
90 	*/
91 };
92 
93 
94 typedef void (*GFXDRV_CLIENTCALLBACK)();
95 typedef bool (*GFXDRV_CLIENTCALLBACKXY)(int x, int y);
96 typedef void (*GFXDRV_CLIENTCALLBACKINITGFX)(void *data);
97 
98 class IGraphicsDriver {
99 public:
100 	virtual const char *GetDriverName() = 0;
101 	virtual const char *GetDriverID() = 0;
102 	virtual void SetTintMethod(TintMethod method) = 0;
103 	// Initialize given display mode
104 	virtual bool SetDisplayMode(const DisplayMode &mode) = 0;
105 	// Updates previously set display mode, accomodating to the new screen size
106 	virtual void UpdateDeviceScreen(const Size &screen_size) = 0;
107 	// Gets if a graphics mode was initialized
108 	virtual bool IsModeSet() const = 0;
109 	// Set the size of the native image size
110 	virtual bool SetNativeResolution(const GraphicResolution &native_res) = 0;
111 	virtual bool IsNativeSizeValid() const = 0;
112 	// Set game render frame and translation
113 	virtual bool SetRenderFrame(const Rect &dst_rect) = 0;
114 	virtual bool IsRenderFrameValid() const = 0;
115 	// Report which color depth options are best suited for the given native color depth
116 	virtual int  GetDisplayDepthForNativeDepth(int native_color_depth) const = 0;
117 	virtual IGfxModeList *GetSupportedModeList(int color_depth) = 0;
118 	virtual bool IsModeSupported(const DisplayMode &mode) = 0;
119 	virtual DisplayMode GetDisplayMode() const = 0;
120 	virtual PGfxFilter GetGraphicsFilter() const = 0;
121 	virtual Size GetNativeSize() const = 0;
122 	virtual Rect GetRenderDestination() const = 0;
123 	virtual void SetCallbackForPolling(GFXDRV_CLIENTCALLBACK callback) = 0;
124 	// TODO: get rid of draw screen callback at some point when all fade functions are more or less grouped in one
125 	virtual void SetCallbackToDrawScreen(GFXDRV_CLIENTCALLBACK callback, GFXDRV_CLIENTCALLBACK post_callback) = 0;
126 	virtual void SetCallbackOnInit(GFXDRV_CLIENTCALLBACKINITGFX callback) = 0;
127 	// The NullSprite callback is called in the main render loop when a
128 	// null sprite is encountered. You can use this to hook into the rendering
129 	// process.
130 	virtual void SetCallbackForNullSprite(GFXDRV_CLIENTCALLBACKXY callback) = 0;
131 	// Clears the screen rectangle. The coordinates are expected in the **native game resolution**.
132 	virtual void ClearRectangle(int x1, int y1, int x2, int y2, RGB *colorToUse) = 0;
133 	// Gets closest recommended bitmap format (currently - only color depth) for the given original format.
134 	// Engine needs to have game bitmaps brought to the certain range of formats, easing conversion into the video bitmaps.
135 	virtual int  GetCompatibleBitmapFormat(int color_depth) = 0;
136 	virtual IDriverDependantBitmap *CreateDDBFromBitmap(Shared::Bitmap *bitmap, bool hasAlpha, bool opaque = false) = 0;
137 	virtual void UpdateDDBFromBitmap(IDriverDependantBitmap *bitmapToUpdate, Shared::Bitmap *bitmap, bool hasAlpha) = 0;
138 	virtual void DestroyDDB(IDriverDependantBitmap *bitmap) = 0;
139 
140 	// Prepares next sprite batch, a list of sprites with defined viewport and optional
141 	// global model transformation; all subsequent calls to DrawSprite will be adding
142 	// sprites to this batch's list.
143 	virtual void BeginSpriteBatch(const Rect &viewport, const SpriteTransform &transform,
144 	                              const Point offset = Point(), GlobalFlipType flip = kFlip_None, PBitmap surface = nullptr) = 0;
145 	// Adds sprite to the active batch
146 	virtual void DrawSprite(int x, int y, IDriverDependantBitmap *bitmap) = 0;
147 	// Adds fade overlay fx to the active batch
148 	virtual void SetScreenFade(int red, int green, int blue) = 0;
149 	// Adds tint overlay fx to the active batch
150 	// TODO: redesign this to allow various post-fx per sprite batch?
151 	virtual void SetScreenTint(int red, int green, int blue) = 0;
152 	// Clears all sprite batches, resets batch counter
153 	virtual void ClearDrawLists() = 0;
154 	virtual void RenderToBackBuffer() = 0;
155 	virtual void Render() = 0;
156 	// Renders with additional final offset and flip
157 	// TODO: leftover from old code, solely for software renderer; remove when
158 	// software mode either discarded or scene node graph properly implemented.
159 	virtual void Render(int xoff, int yoff, GlobalFlipType flip) = 0;
160 	// Copies contents of the game screen into bitmap using simple blit or pixel copy.
161 	// Bitmap must be of supported size and pixel format. If it's not the method will
162 	// fail and optionally write wanted destination format into 'want_fmt' pointer.
163 	virtual bool GetCopyOfScreenIntoBitmap(Shared::Bitmap *destination, bool at_native_res, GraphicResolution *want_fmt = nullptr) = 0;
164 	virtual void EnableVsyncBeforeRender(bool enabled) = 0;
165 	virtual void Vsync() = 0;
166 	// Enables or disables rendering mode that draws sprite list directly into
167 	// the final resolution, as opposed to drawing to native-resolution buffer
168 	// and scaling to final frame. The effect may be that sprites that are
169 	// drawn with additional fractional scaling will appear more detailed than
170 	// the rest of the game. The effect is stronger for the low-res games being
171 	// rendered in the high-res mode.
172 	virtual void RenderSpritesAtScreenResolution(bool enabled, int supersampling = 1) = 0;
173 	// TODO: move fade-in/out/boxout functions out of the graphics driver!! make everything render through
174 	// main drawing procedure. Since currently it does not - we need to init our own sprite batch
175 	// internally to let it set up correct viewport settings instead of relying on a chance.
176 	// Runs fade-out animation in a blocking manner.
177 	virtual void FadeOut(int speed, int targetColourRed, int targetColourGreen, int targetColourBlue) = 0;
178 	// Runs fade-in animation in a blocking manner.
179 	virtual void FadeIn(int speed, PALETTE p, int targetColourRed, int targetColourGreen, int targetColourBlue) = 0;
180 	// Runs box-out animation in a blocking manner.
181 	virtual void BoxOutEffect(bool blackingOut, int speed, int delay) = 0;
182 	virtual void UseSmoothScaling(bool enabled) = 0;
183 	virtual bool SupportsGammaControl() = 0;
184 	virtual void SetGamma(int newGamma) = 0;
185 	// Returns the virtual screen. Will return NULL if renderer does not support memory backbuffer.
186 	// In normal case you should use GetStageBackBuffer() instead.
187 	virtual Shared::Bitmap *GetMemoryBackBuffer() = 0;
188 	// Sets custom backbuffer bitmap to render to.
189 	// Passing NULL pointer will tell renderer to switch back to its original virtual screen.
190 	// Note that only software renderer supports this.
191 	virtual void SetMemoryBackBuffer(Shared::Bitmap *backBuffer) = 0;
192 	// Returns memory backbuffer for the current rendering stage (or base virtual screen if called outside of render pass).
193 	// All renderers should support this.
194 	virtual Shared::Bitmap *GetStageBackBuffer(bool mark_dirty) = 0;
195 	// Retrieves 3 transform matrixes for the current rendering stage: world (model), view and projection.
196 	// These matrixes will be filled in accordance to the renderer's compatible format;
197 	// returns false if renderer does not use matrixes (not a 3D renderer).
198 	virtual bool GetStageMatrixes(RenderMatrixes &rm) = 0;
199 	virtual bool RequiresFullRedrawEachFrame() = 0;
200 	virtual bool HasAcceleratedTransform() = 0;
201 	virtual bool UsesMemoryBackBuffer() = 0;
~IGraphicsDriver()202 	virtual ~IGraphicsDriver() {}
203 };
204 
205 } // namespace Engine
206 } // namespace AGS
207 } // namespace AGS3
208 
209 #endif
210