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 #ifndef PLATFORM_3DS_H
24 #define PLATFORM_3DS_H
25 
26 #define FORBIDDEN_SYMBOL_EXCEPTION_time_h
27 
28 #include "backends/mutex/mutex.h"
29 #include "backends/base-backend.h"
30 #include "graphics/palette.h"
31 #include "base/main.h"
32 #include "audio/mixer_intern.h"
33 #include "backends/graphics/graphics.h"
34 #include "backends/log/log.h"
35 #include "backends/platform/3ds/sprite.h"
36 #include "common/rect.h"
37 #include "common/queue.h"
38 #include "common/ustr.h"
39 #include "engines/engine.h"
40 
41 #define TICKS_PER_MSEC 268123
42 
43 namespace N3DS {
44 
45 enum MagnifyMode {
46 	MODE_MAGON,
47 	MODE_MAGOFF,
48 };
49 
50 enum InputMode {
51 	MODE_HOVER,
52 	MODE_DRAG,
53 };
54 
55 enum GraphicsModeID {
56 	RGBA8,
57 	RGB565,
58 	RGB555,
59 	RGB5A1,
60 	CLUT8
61 };
62 
63 enum TransactionState {
64 	kTransactionNone = 0,
65 	kTransactionActive = 1,
66 	kTransactionRollback = 2
67 };
68 
69 
70 struct TransactionDetails {
71 	bool formatChanged, modeChanged;
72 
TransactionDetailsTransactionDetails73 	TransactionDetails() {
74 		formatChanged = false;
75 		modeChanged = false;
76 	}
77 };
78 
79 typedef struct GfxMode3DS {
80 	Graphics::PixelFormat surfaceFormat;
81 	GPU_TEXCOLOR textureFormat;
82 	uint32 textureTransferFlags;
83 } GfxMode3DS;
84 
85 struct GfxState {
86 	bool setup;
87 	GraphicsModeID gfxModeID;
88 	const GfxMode3DS *gfxMode;
89 
GfxStateGfxState90 	GfxState() {
91 		setup = false;
92 		gfxModeID = CLUT8;
93 	}
94 };
95 
96 
97 class OSystem_3DS : public EventsBaseBackend, public PaletteManager, public Common::EventObserver {
98 public:
99 	OSystem_3DS();
100 	virtual ~OSystem_3DS();
101 
102 	volatile bool exiting;
103 	volatile bool sleeping;
104 
105 	virtual void initBackend();
106 
107 	virtual bool hasFeature(OSystem::Feature f);
108 	virtual void setFeatureState(OSystem::Feature f, bool enable);
109 	virtual bool getFeatureState(OSystem::Feature f);
110 
111 	bool pollEvent(Common::Event &event) override;
112 	bool notifyEvent(const Common::Event &event) override;
113 	Common::HardwareInputSet *getHardwareInputSet() override;
114 	Common::KeymapArray getGlobalKeymaps() override;
115 	Common::KeymapperDefaultBindings *getKeymapperDefaultBindings() override;
116 
117 	virtual uint32 getMillis(bool skipRecord = false);
118 	virtual void delayMillis(uint msecs);
119 	virtual void getTimeAndDate(TimeDate &td, bool skipRecord = false) const;
120 
121 	virtual MutexRef createMutex();
122 	virtual void lockMutex(MutexRef mutex);
123 	virtual void unlockMutex(MutexRef mutex);
124 	virtual void deleteMutex(MutexRef mutex);
125 
126 	virtual void logMessage(LogMessageType::Type type, const char *message);
127 
128 	virtual Audio::Mixer *getMixer();
getPaletteManager()129 	virtual PaletteManager *getPaletteManager() { return this; }
130 	virtual Common::String getSystemLanguage() const;
131 	virtual void fatalError();
132 	virtual void quit();
133 
134 	virtual Common::String getDefaultConfigFileName();
135 	void addSysArchivesToSearchSet(Common::SearchSet &s, int priority) override;
136 
137 	// Graphics
getScreenFormat()138 	inline Graphics::PixelFormat getScreenFormat() const { return _pfGame; }
139 	virtual Common::List<Graphics::PixelFormat> getSupportedFormats() const;
140 	void initSize(uint width, uint height,
141 	              const Graphics::PixelFormat *format = NULL);
getScreenChangeID()142 	virtual int getScreenChangeID() const { return _screenChangeId; };
143 	GraphicsModeID chooseMode(Graphics::PixelFormat *format);
144 	bool setGraphicsMode(GraphicsModeID modeID);
145 
146 	void beginGFXTransaction();
147 	OSystem::TransactionError endGFXTransaction();
getHeight()148 	int16 getHeight(){ return _gameHeight; }
getWidth()149 	int16 getWidth(){ return _gameWidth; }
150 	float getScaleRatio() const;
151 	void setPalette(const byte *colors, uint start, uint num);
152 	void grabPalette(byte *colors, uint start, uint num) const;
153 	void copyRectToScreen(const void *buf, int pitch, int x, int y, int w,
154 	                      int h);
155 	Graphics::Surface *lockScreen();
156 	void unlockScreen();
157 	void updateScreen();
158 	void setShakePos(int shakeXOffset, int shakeYOffset);
159 	void setFocusRectangle(const Common::Rect &rect);
160 	void clearFocusRectangle();
161 	void showOverlay();
162 	void hideOverlay();
isOverlayVisible()163 	bool isOverlayVisible() const { return _overlayVisible; }
164 	Graphics::PixelFormat getOverlayFormat() const;
165 	void clearOverlay();
166 	void grabOverlay(Graphics::Surface &surface);
167 	void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w,
168 	                       int h);
169 	virtual int16 getOverlayHeight();
170 	virtual int16 getOverlayWidth();
171 	void displayMessageOnOSD(const Common::U32String &msg) override;
172 	void displayActivityIconOnOSD(const Graphics::Surface *icon) override;
173 
174 	bool showMouse(bool visible);
175 	void warpMouse(int x, int y);
176 	void setMouseCursor(const void *buf, uint w, uint h, int hotspotX,
177 	                    int hotspotY, uint32 keycolor, bool dontScale = false,
178 	                    const Graphics::PixelFormat *format = NULL);
179 	void setCursorPalette(const byte *colors, uint start, uint num);
180 
181 	// Transform point from touchscreen coords into gamescreen coords
182 	void transformPoint(touchPosition &point);
183 	// Clip point to gamescreen coords
184 	void clipPoint(touchPosition &point);
185 
186 	void setCursorDelta(float deltaX, float deltaY);
187 
188 	void updateFocus();
189 	void updateMagnify();
190 	void updateConfig();
191 	void updateSize();
192 
193 private:
194 	void init3DSGraphics();
195 	void destroy3DSGraphics();
196 	void initAudio();
197 	void destroyAudio();
198 	void initEvents();
199 	void destroyEvents();
200 	void runOptionsDialog();
201 
202 	void flushGameScreen();
203 	void flushCursor();
204 
205 	virtual Common::String getDefaultLogFileName();
206 	virtual Common::WriteStream *createLogFile();
207 
208 protected:
209 	Audio::MixerImpl *_mixer;
210 	Backends::Log::Log *_logger;
211 
212 private:
213 	u16 _gameWidth, _gameHeight;
214 	u16 _gameTopX, _gameTopY;
215 	u16 _gameBottomX, _gameBottomY;
216 
217 	// Audio
218 	Thread audioThread;
219 
220 	// Graphics
221 	GraphicsModeID _graphicsModeID;
222 	TransactionState _transactionState;
223 	TransactionDetails _transactionDetails;
224 
225 	GfxState _gfxState, _oldGfxState;
226 	Graphics::PixelFormat _pfDefaultTexture;
227 	Graphics::PixelFormat _pfGame, _oldPfGame;
228 	Graphics::PixelFormat _pfCursor;
229 	byte _palette[3 * 256];
230 	byte _cursorPalette[3 * 256];
231 
232 	Graphics::Surface _gameScreen;
233 	bool _gameTextureDirty;
234 	Sprite _gameTopTexture;
235 	Sprite _gameBottomTexture;
236 	Sprite _overlay;
237 	Sprite _activityIcon;
238 	Sprite _osdMessage;
239 	bool _filteringEnabled;
240 
241 	enum {
242 		kOSDMessageDuration = 800
243 	};
244 	uint32 _osdMessageEndTime;
245 
246 	int _screenShakeXOffset;
247 	int _screenShakeYOffset;
248 	bool _overlayVisible;
249 	int _screenChangeId;
250 
251 	DVLB_s *_dvlb;
252 	shaderProgram_s _program;
253 	int _projectionLocation;
254 	int _modelviewLocation;
255 	C3D_Mtx _projectionTop;
256 	C3D_Mtx _projectionBottom;
257 	C3D_RenderTarget* _renderTargetTop;
258 	C3D_RenderTarget* _renderTargetBottom;
259 
260 	// Focus
261 	Common::Rect _focusRect;
262 	bool _focusDirty;
263 	C3D_Mtx _focusMatrix;
264 	int _focusPosX, _focusPosY;
265 	int _focusTargetPosX, _focusTargetPosY;
266 	float _focusStepPosX, _focusStepPosY;
267 	float _focusScaleX, _focusScaleY;
268 	float _focusTargetScaleX, _focusTargetScaleY;
269 	float _focusStepScaleX, _focusStepScaleY;
270 	uint32 _focusClearTime;
271 
272 	// Events
273 	Thread _eventThread;
274 	Thread _timerThread;
275 	Common::Queue<Common::Event> _eventQueue;
276 
277 	// Cursor
278 	Graphics::Surface _cursor;
279 	Sprite _cursorTexture;
280 	bool _cursorPaletteEnabled;
281 	bool _cursorVisible;
282 	bool _cursorScalable;
283 	float _cursorScreenX, _cursorScreenY;
284 	float _cursorOverlayX, _cursorOverlayY;
285 	float _cursorDeltaX, _cursorDeltaY;
286 	int _cursorHotspotX, _cursorHotspotY;
287 	uint32 _cursorKeyColor;
288 
289 	// Magnify
290 	MagnifyMode _magnifyMode;
291 	u16 _magX, _magY;
292 	u16 _magWidth, _magHeight;
293 	u16 _magCenterX, _magCenterY;
294 
295 	Common::String _logFilePath;
296 
297 public:
298 	// Pause
299 	PauseToken _sleepPauseToken;
300 };
301 
302 } // namespace N3DS
303 
304 #endif
305