1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM 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 #ifndef BACKENDS_GRAPHICS3D_OPENGLSDL_GRAPHICS3D_H
24 #define BACKENDS_GRAPHICS3D_OPENGLSDL_GRAPHICS3D_H
25 
26 #include "backends/graphics/sdl/sdl-graphics.h"
27 
28 #include "math/rect2d.h"
29 
30 #if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
31 
32 namespace OpenGL {
33 	class FrameBuffer;
34 	class SurfaceRenderer;
35 	class TextureGL;
36 	class TiledSurface;
37 }
38 
39 /**
40  * SDL OpenGL based graphics manager
41  *
42  * Used when rendering games with OpenGL
43  */
44 class OpenGLSdlGraphics3dManager : public SdlGraphicsManager {
45 public:
46 	OpenGLSdlGraphics3dManager(SdlEventSource *eventSource, SdlWindow *window, bool supportsFrameBuffer);
47 	virtual ~OpenGLSdlGraphics3dManager();
48 
49 	// GraphicsManager API - Features
50 	virtual bool hasFeature(OSystem::Feature f) const override;
51 	virtual bool getFeatureState(OSystem::Feature f) const override;
52 	virtual void setFeatureState(OSystem::Feature f, bool enable) override;
53 
54 	virtual const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
55 	virtual int getDefaultGraphicsMode() const override;
56 	virtual bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override;
57 	virtual int getGraphicsMode() const override;
58 
59 	virtual void beginGFXTransaction() override;
60 	virtual OSystem::TransactionError endGFXTransaction() override;
61 
62 	// GraphicsManager API - Graphics mode
63 #ifdef USE_RGB_COLOR
getScreenFormat()64 	virtual Graphics::PixelFormat getScreenFormat() const override { return _overlayFormat; }
getSupportedFormats()65 	virtual Common::List<Graphics::PixelFormat> getSupportedFormats() const override {
66 		Common::List<Graphics::PixelFormat> supportedFormats;
67 		return supportedFormats;
68 	}
69 #endif
getScreenChangeID()70 	virtual int getScreenChangeID() const override { return _screenChangeCount; }
71 	virtual void initSize(uint w, uint h, const Graphics::PixelFormat *format) override;
72 	virtual int16 getHeight() const override;
73 	virtual int16 getWidth() const override;
74 
75 	// GraphicsManager API - Draw methods
76 	virtual void updateScreen() override;
77 	// Following methods are not used by 3D graphics managers
setPalette(const byte * colors,uint start,uint num)78 	virtual void setPalette(const byte *colors, uint start, uint num) override {}
grabPalette(byte * colors,uint start,uint num)79 	virtual void grabPalette(byte *colors, uint start, uint num) const override {}
copyRectToScreen(const void * buf,int pitch,int x,int y,int w,int h)80 	virtual void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h) override {}
lockScreen()81 	virtual Graphics::Surface *lockScreen() override { return nullptr; }
unlockScreen()82 	virtual void unlockScreen() override {}
fillScreen(uint32 col)83 	virtual void fillScreen(uint32 col) override {}
setShakePos(int shakeXOffset,int shakeYOffset)84 	virtual void setShakePos(int shakeXOffset, int shakeYOffset) override {};
setFocusRectangle(const Common::Rect & rect)85 	virtual void setFocusRectangle(const Common::Rect& rect) override {}
clearFocusRectangle()86 	virtual void clearFocusRectangle() override {}
87 
88 	// GraphicsManager API - Overlay
89 	virtual void showOverlay() override;
90 	virtual void hideOverlay() override;
getOverlayFormat()91 	virtual Graphics::PixelFormat getOverlayFormat() const override { return _overlayFormat; }
92 	virtual void clearOverlay() override;
93 	virtual void grabOverlay(Graphics::Surface &surface) const override;
94 	virtual void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h) override;
95 	int16 getOverlayWidth() const override;
96 	int16 getOverlayHeight() const override;
isOverlayVisible()97 	virtual bool isOverlayVisible() const override { return _overlayVisible; }
98 
99 	// GraphicsManager API - Mouse
100 	virtual bool showMouse(bool visible) override;
101 	virtual void warpMouse(int x, int y) override;
102 	virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale = false, const Graphics::PixelFormat *format = NULL) override {}
setCursorPalette(const byte * colors,uint start,uint num)103 	virtual void setCursorPalette(const byte *colors, uint start, uint num) override {}
104 
105 	// SdlGraphicsManager API
notifyVideoExpose()106 	virtual void notifyVideoExpose() override {};
107 	virtual void notifyResize(const int width, const int height) override;
108 
gameNeedsAspectRatioCorrection()109 	virtual bool gameNeedsAspectRatioCorrection() const override { return false; }
getGraphicsModeScale(int mode)110 	virtual int getGraphicsModeScale(int mode) const override { return 1; }
111 
112 	void transformMouseCoordinates(Common::Point &point);
notifyMousePosition(Common::Point & mouse)113 	virtual bool notifyMousePosition(Common::Point &mouse) override {
114 		transformMouseCoordinates(mouse);
115 
116 		return true;
117 	}
118 
119 protected:
120 #if SDL_VERSION_ATLEAST(2, 0, 0)
121 	SDL_GLContext _glContext;
122 	void deinitializeRenderer();
123 #endif
124 
125 	bool _supportsFrameBuffer;
126 
127 	Math::Rect2d _gameRect;
128 
129 	struct OpenGLPixelFormat {
130 		uint bytesPerPixel;
131 		uint redSize;
132 		uint blueSize;
133 		uint greenSize;
134 		uint alphaSize;
135 		int multisampleSamples;
136 
137 		OpenGLPixelFormat(uint screenBytesPerPixel, uint red, uint blue, uint green, uint alpha, int samples);
138 	};
139 
140 	/**
141 	 * Initialize an OpenGL window matching as closely as possible the required properties
142 	 *
143 	 * When unable to create a context with anti-aliasing this tries without.
144 	 * When unable to create a context with the desired pixel depth this tries lower values.
145 	 */
146 	bool createOrUpdateGLContext(uint gameWidth, uint gameHeight, uint effectiveWidth, uint effectiveHeight,
147 	                             bool renderToFramebuffer, bool engineSupportsArbitraryResolutions);
148 
149 	void createOrUpdateScreen();
150 	void setupScreen();
151 
152 	/** Compute the size and position of the game rectangle in the screen */
153 	Math::Rect2d computeGameRect(bool renderToFrameBuffer, uint gameWidth, uint gameHeight,
154 	                             uint screenWidth, uint screenHeight);
155 
156 	virtual bool saveScreenshot(const Common::String &filename) const override;
157 
158 	uint _engineRequestedWidth, _engineRequestedHeight;
159 
160 	int _screenChangeCount;
161 	int _antialiasing;
162 	bool _vsync;
163 	bool _fullscreen;
164 	bool _lockAspectRatio;
165 	bool _overlayVisible;
166 
167 	OpenGL::TiledSurface *_overlayScreen;
168 	OpenGL::TiledSurface *_overlayBackground;
169 	OpenGL::SurfaceRenderer *_surfaceRenderer;
170 
171 	Graphics::PixelFormat _overlayFormat;
172 
173 	void initializeOpenGLContext() const;
174 	void drawOverlay();
175 	void closeOverlay();
176 
177 	OpenGL::FrameBuffer *_frameBuffer;
178 	OpenGL::FrameBuffer *createFramebuffer(uint width, uint height);
179 	bool shouldRenderToFramebuffer() const;
180 
181 	bool isVSyncEnabled() const;
182 
183 protected:
184 
185 	enum TransactionMode {
186 		kTransactionNone = 0,
187 		kTransactionActive = 1,
188 		kTransactionRollback = 2
189 	};
190 
191 	/**
192 	 * The current transaction mode.
193 	 */
194 	TransactionMode _transactionMode;
195 };
196 
197 #endif // defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
198 
199 #endif
200