1 /***************************************************************************
2                       sdlhandler.h  -  Interface to SDL
3                              -------------------
4     begin                : Sat May 3 2003
5     copyright            : (C) 2003 by Gabor Torok
6     email                : cctorok@yahoo.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef SDLHANDLER_H
19 #define SDLHANDLER_H
20 #pragma once
21 
22 #include <iostream>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include "gui/gui.h"
26 
27 class Widget;
28 class GameAdapter;
29 class SDLEventHandler;
30 class SDLScreenView;
31 class Preferences;
32 class FontMgr;
33 class EventHandler;
34 
35 /**
36   *@author Gabor Torok
37   */
38 
39 /**
40   How many measurements to take to average the fps.
41 */
42 #define MAX_TICK_COUNT 20
43 
44 /* screen width, height, and bit depth */
45 #define SCREEN_WIDTH  640
46 #define SCREEN_HEIGHT 480
47 #define SCREEN_BPP     16
48 
49 /* Define our booleans */
50 #define TRUE  1
51 #define FALSE 0
52 
53 /// Interface to SDL.
54 class SDLHandler : public ScourgeGui {
55 private:
56 	GameAdapter *gameAdapter;
57 
58 	/* These are to calculate our fps */
59 	GLint T0, Frames;
60 	double fps;
61 	SDL_Surface *screen;
62 	SDLEventHandler *eventHandler;
63 	SDLScreenView *screenView;
64 	/* Flags to pass to SDL_SetVideoMode */
65 	int videoFlags;
66 	bool invertMouse;
67 	int cursorMode;
68 	float orthoDepthMin, orthoDepthMax;
69 
70 	// rotation for test draw view
71 	GLfloat rtri, rquad;
72 	int lastWidth, lastHeight;
73 
74 	SDLEventHandler *eventHandlers[10];
75 	SDLScreenView *screenViews[10];
76 	int handlerCount;
77 
78 	// the last event fired by a widget
79 	Widget *storedWidget;
80 	SDL_Event *storedEvent;
81 
82 	// truetype font
83 	bool font_initialized;
84 	int fontType;
85 
86 	char *debugStr;
87 
88 	bool attackCursor;
89 
90 	// only do stencil buffer ops if this is true
91 	static bool stencilBufferUsed;
92 
93 	// mouse locking
94 	Widget *mouseLock;
95 	bool willUnlockMouse;
96 
97 	bool willBlockEvent;
98 
99 	Uint32 forbiddenTimer;
100 
101 	float fadeoutStartAlpha, fadeoutEndAlpha;
102 	Uint32 fadeoutTimer;
103 	int fadeoutSteps, fadeoutCurrentStep;
104 
105 	bool running;
106 	bool cursorVisible;
107 
108 	std::string continueFunc;
109 	Uint32 continueTimeout, continueStart;
110 
111 	std::map<Widget*, EventHandler*> widgetEventHandlers;
112 
113 	void getVideoModes();
114 
115 public:
116 
117 	struct FontInfo {
118 		std::string path;
119 		int size;
120 		int style;
121 		int yoffset;
122 		int shadowX, shadowY;
123 
124 		TTF_Font *font;
125 		FontMgr *fontMgr;
126 	};
127 	static std::vector<FontInfo*> fontInfos;
128 
129 	static bool showDebugInfo;
130 
131 	bool dontUpdateScreen;
132 
133 	SDLHandler( GameAdapter *gameAdapter );
134 	virtual ~SDLHandler();
135 
136 	virtual void playSound( const std::string& file, int panning );
137 
setCursorVisible(bool b)138 	inline void setCursorVisible( bool b ) {
139 		cursorVisible = b;
140 	}
endMainLoop()141 	inline void endMainLoop() {
142 		running = false;
143 	}
144 
145 	void fade( float startAlpha, float endAlpha, int steps = 50 );
146 
blockEvent()147 	inline void blockEvent() {
148 		willBlockEvent = true;
149 	}
150 
lockMouse(Widget * widget)151 	inline void lockMouse( Widget *widget ) {
152 		mouseLock = widget;
153 	}
unlockMouse()154 	inline void unlockMouse() {
155 		willUnlockMouse = true;
156 	}
157 
158 	void drawTooltip( float xpos2, float ypos2, float zpos2,
159 	                  float zrot, float yrot,
160 	                  char *message,
161 	                  float r = 0, float g = 0.15f, float b = 0.05f,
162 	                  float zoom = 1.0f );
163 
setDebugStr(char * s)164 	inline void setDebugStr( char *s ) {
165 		debugStr = s;
166 	}
167 
168 	Uint16 mouseX, mouseY, lastMouseX, lastMouseY;
169 	Uint16 mouseFocusX, mouseFocusY;
170 	Uint8 mouseButton, mouseEvent;
171 	bool mouseDragging;
172 	bool mouseIsMovingOverMap;
173 	Uint32 lastMouseMoveTime;
174 	Uint32 lastLeftClick;
175 	bool isDoubleClick;
176 
177 	// for ScourgeGui
getMouseX()178 	inline Uint16 getMouseX() {
179 		return mouseX;
180 	}
getMouseY()181 	inline Uint16 getMouseY() {
182 		return mouseY;
183 	}
getScreenWidth()184 	inline int getScreenWidth() {
185 		return getScreen()->w;
186 	}
getScreenHeight()187 	inline int getScreenHeight() {
188 		return getScreen()->h;
189 	}
190 	virtual Texture const& getHighlightTexture();
191 	virtual Texture const& getGuiTexture();
192 	virtual Texture const& getGuiTexture2();
193 	virtual Texture const& loadSystemTexture( char *line );
194 	void allWindowsClosed();
195 
196 	void setCursorMode( int n, bool useTimer = false );
getCursorMode()197 	int getCursorMode() {
198 		return cursorMode;
199 	}
200 	void applyMouseOffset( int x, int y, int *newX, int *newY );
201 
202 	/**
203 	 * Add a new set of handlers and push the old ones on the stack.
204 	 * When the eventHandler returns true, the stack will be popped.
205 	 * If the stack is empty the game will quit.
206 	 */
207 	void pushHandlers( SDLEventHandler *eventHandler, SDLScreenView *screenView );
208 
209 	/**
210 	 * Replace the current handlers with the new ones
211 	 */
212 	void setHandlers( SDLEventHandler *eventHandler, SDLScreenView *screenView );
213 
214 	/**
215 	Get the current event handler.
216 	*/
getEventHandler()217 	inline SDLEventHandler *getEventHandler() {
218 		return eventHandler;
219 	}
220 
221 	//
222 	int getVideoModeCount();
223 	std::string getVideoMode( int mode );
224 	void setVideoMode( Preferences *uc );
225 	void mainLoop();
226 	void drawScreen();
227 	bool processEvents( bool *isActive = NULL );
228 	void processEventsAndRepaint();
229 	void fireEvent( Widget *widget, SDL_Event *event );
230 	bool firedEventWaiting();
231 
setFontType(int fontType)232 	inline void setFontType( int fontType ) {
233 		this->fontType = fontType;
234 	}
getFontType()235 	inline int getFontType() {
236 		return fontType;
237 	}
238 	void texPrint( GLfloat x, GLfloat y, const char *fmt, ... );
239 	int textWidth( const char *fmt, ... );
240 	TTF_Font *getCurrentTTFFont();
241 	FontMgr *getCurrentFontManager();
242 	void initFonts();
243 
244 	void glPrint( const char *fmt, ... );
245 
246 	static bool intersects( int x, int y, int w, int h,
247 	                        int x2, int y2, int w2, int h2 );
248 	static bool intersects( SDL_Rect *r1, SDL_Rect *r2 );
249 	static bool sectionIntersects( int a1, int a2, int b1, int b2 );
250 
251 
getFps()252 	inline double getFps() {
253 		return fps;
254 	}
255 
getScreen()256 	inline SDL_Surface *getScreen() {
257 		return screen;
258 	}
259 
260 	void testDrawView();
261 
262 	void quit( int returnCode );
263 
getFPS()264 	inline double getFPS() {
265 		return fps;
266 	}
267 
268 	void saveScreen( std::string& path, bool thumbnail = false );
269 
270 	void setUpdate( char *message, int n = -1, int total = -1 );
271 
272 	void setContinueAt( char *func, int timeout );
273 
274 	void setDepthLimits( float min, float max );
275 	void resetDepthLimits();
276 
277 	void setOrthoView();
278 
registerEventHandler(Widget * w,EventHandler * eh)279 	inline void registerEventHandler( Widget *w, EventHandler *eh ) {
280 		widgetEventHandlers[w] = eh;
281 	}
unregisterEventHandler(Widget * w)282 	inline void unregisterEventHandler( Widget *w ) {
283 		if( widgetEventHandlers.find( w ) != widgetEventHandlers.end() ) {
284 			widgetEventHandlers.erase( w );
285 		}
286 	}
getEventHandler(Widget * w)287 	inline EventHandler *getEventHandler( Widget *w ) {
288 		if( widgetEventHandlers.find( w ) != widgetEventHandlers.end() ) {
289 			return widgetEventHandlers[w];
290 		} else {
291 			return NULL;
292 		}
293 	}
294 	bool popHandlers();
295 
296 protected:
297 	int resizeWindow( int width, int height );
298 	int initGL();
299 	void drawCursor();
300 
301 	void drawScreenInternal();
302 	void saveScreenInternal( std::string& path, bool thumbnail = false );
303 	void calculateFps();
304 	void drawDebugInfo();
305 	void drawFadeout();
306 
307 	DECLARE_NOISY_OPENGL_SUPPORT();
308 };
309 
310 #endif
311 
312