PageRenderTime 48ms CodeModel.GetById 18ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

/xbmc/screensavers/rsxs-0.9/src/common.hh

http://github.com/xbmc/xbmc
C++ Header | 374 lines | 301 code | 51 blank | 22 comment | 35 complexity | 47482d6bc420d4ce21eba22e2795172b MD5 | raw file
  1/*
  2 * Really Slick XScreenSavers
  3 * Copyright (C) 2002-2006  Michael Chapman
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * it under the terms of the GNU General Public License version 2 as
  7 * published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program; if not, write to the Free Software
 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 17 *
 18 *****************************************************************************
 19 *
 20 * This is a Linux port of the Really Slick Screensavers,
 21 * Copyright (C) 2002 Terence M. Welsh, available from www.reallyslick.com
 22 */
 23#ifndef _COMMON_HH
 24#define _COMMON_HH
 25
 26#if HAVE_CONFIG_H
 27	#include <config.h>
 28#endif
 29
 30#if ! HAVE_CXX_BOOL
 31	enum _boolType { false, true };
 32	typedef enum _boolType bool;
 33#endif
 34
 35#if ! HAVE_CXX_NEW_FOR_SCOPING
 36#	define for if (0) {} else for
 37#endif
 38
 39#define GLX_GLXEXT_PROTOTYPES 1
 40
 41#include <algorithm>
 42#include <argp.h>
 43#include <cmath>
 44#include <functional>
 45#include <iostream>
 46#include <list>
 47#if HAVE_CXX_STRINGSTREAM
 48	#include <sstream>
 49#endif
 50#if HAVE_CXX_STRSTREAM
 51	#include <strstream>
 52#endif
 53#include <string>
 54#include <utility>
 55#include <vector>
 56
 57#if HAVE_GL_GL_H
 58	#include <GL/gl.h>
 59#endif
 60#if HAVE_GL_GLEXT_H
 61	#include <GL/glext.h>
 62#endif
 63#if HAVE_GL_GLU_H
 64	#include <GL/glu.h>
 65#endif
 66#if HAVE_GL_GLX_H
 67	#include <GL/glx.h>
 68#endif
 69#if HAVE_X11_XLIB_H
 70	#include <X11/Xlib.h>
 71#endif
 72
 73namespace std {
 74#if HAVE_CXX_STRSTREAM && !HAVE_CXX_STRINGSTREAM
 75	typedef istrstream istringstream;
 76	typedef ostrstream ostringstream;
 77	typedef strstream stringstream;
 78#endif
 79};
 80
 81class ResourceManager;
 82
 83namespace Common {
 84	typedef std::string Exception;
 85
 86	extern std::string program;
 87	extern Display* display;
 88	extern XVisualInfo* visualInfo;
 89	extern unsigned int screen;
 90	extern Window window;
 91	extern GLXContext context;
 92	extern std::string resourceDir;
 93
 94	extern unsigned int width, height, depth;
 95	extern unsigned int centerX, centerY;
 96	extern float aspectRatio;
 97	extern Colormap colormap;
 98	extern bool doubleBuffered;
 99
100	extern bool running;
101	extern unsigned int elapsedMicros;
102	extern float elapsedSecs;
103	extern float speed;
104	extern float elapsedTime;
105
106	extern ResourceManager* resources;
107
108#ifndef NOXBMC
109         void init(int argc, char** argv);
110         void run();
111#endif
112
113	static inline int randomInt(int x) {
114		return std::rand() % x;
115	}
116
117	static inline float randomFloat(float x) {
118		return float(std::rand()) * x / float(RAND_MAX);
119	}
120
121	static inline void flush(bool swap = doubleBuffered) {
122		if (swap)
123			glXSwapBuffers(display, window);
124		else
125			glFlush();
126	}
127
128	template <typename T>
129	static inline const T& clamp(
130		const T& v, const T& min, const T& max
131	) {
132		return std::max(std::min(v, max), min);
133	}
134
135	template <typename T>
136	static inline bool parseArg(
137		const char* arg, T& item, const T& min, const T& max
138	) {
139		return
140			!(std::istringstream(arg) >> item) ||
141			item < min || item > max;
142	}
143
144	template <typename T>
145	static inline bool parseArg(
146		const char* arg, T& item, const T& min, const T& max, const T& extra
147	) {
148		return
149			!(std::istringstream(arg) >> item) ||
150			(item != extra && (item < min || item > max));
151	}
152};
153
154#ifdef NDEBUG
155	#define TRACE(x) do {} while (0)
156#else
157	#define TRACE(x) do { \
158		std::cerr << "[" << __PRETTY_FUNCTION__ << " @ " << __FILE__ << \
159			":" << __LINE__ << "]" << std::endl << "  " << x << std::endl; \
160	} while (0)
161#endif
162
163#define WARN(x) do { \
164	std::cerr << Common::program << ": " << x << std::endl; \
165} while (0)
166
167namespace stdx {
168	class oss {
169	private:
170		std::ostringstream _oss;
171	public:
172		operator std::string() const { return _oss.str(); }
173		template <typename T>
174		oss& operator<<(const T& t) {
175			_oss << t;
176			return *this;
177		}
178	};
179
180	template <typename In, typename MFunc>
181	static inline void call_each(In p, In q, MFunc f) {
182		for ( ; p != q; ++p)
183			((*p).*f)();
184	}
185
186	template <typename In, typename MFunc, typename Arg>
187	static inline void call_each(In p, In q, MFunc f, const Arg& a) {
188		for ( ; p != q; ++p)
189			((*p).*f)(a);
190	}
191
192	template <typename Con, typename MFunc>
193	static inline void call_all(Con& c, MFunc f) {
194		call_each(c.begin(), c.end(), f);
195	}
196
197	template <typename Con, typename MFunc, typename Arg>
198	static inline void call_all(Con& c, MFunc f, const Arg& a) {
199		call_each(c.begin(), c.end(), f, a);
200	}
201
202	template <typename In, typename Out, typename UFunc>
203	static inline void map_each(In p, In q, Out o, UFunc f) {
204		for ( ; p != q; ++p)
205			*o++ = ((*p).*f)();
206	}
207
208	template <typename Con1, typename Con2, typename UFunc>
209	static inline void map_all(const Con1& in, Con2& out, UFunc f) {
210		map_each(in.begin(), in.end(), out.begin(), f);
211	}
212
213	template <typename In, typename MFuncPtr>
214	static inline void call_each_ptr(In p, In q, MFuncPtr f) {
215		for ( ; p != q; ++p)
216			((*p)->*f)();
217	}
218
219	template <typename In, typename MFuncPtr, typename Arg>
220	static inline void call_each_ptr(
221		In p, In q, MFuncPtr f, const Arg& a
222	) {
223		for ( ; p != q; ++p)
224			((*p)->*f)(a);
225	}
226
227	template <typename Con, typename MFuncPtr>
228	static inline void call_all_ptr(Con& c, MFuncPtr f) {
229		call_each_ptr(c.begin(), c.end(), f);
230	}
231
232	template <typename Con, typename MFuncPtr, typename Arg>
233	static inline void call_all_ptr(Con& c, MFuncPtr f, const Arg& a) {
234		call_each_ptr(c.begin(), c.end(), f, a);
235	}
236
237	template <typename In, typename Out, typename UFunc>
238	static inline void map_each_ptr(In p, In q, Out o, UFunc f) {
239		for ( ; p != q; ++p)
240			*o++ = ((*p)->*f)();
241	}
242
243	template <typename Con1, typename Con2, typename UFunc>
244	static inline void map_all_ptr(const Con1& in, Con2& out, UFunc f) {
245		map_each_ptr(in.begin(), in.end(), out.begin(), f);
246	}
247
248	template <typename In>
249	static inline void destroy_each_ptr(In p, const In& q) {
250		for ( ; p != q; ++p)
251			delete *p;
252	}
253
254	template <typename Con>
255	static inline void destroy_all_ptr(Con& c) {
256		destroy_each_ptr(c.begin(), c.end());
257	}
258
259	template <typename T>
260	class constructor_t {
261	public:
262		inline T operator()() { return T(); }
263	};
264
265	template <typename T, typename Arg>
266	class constructor1_t {
267	private:
268		const Arg& _x;
269	public:
270		constructor1_t(const Arg& x) : _x(x) {}
271		inline T operator()() { return T(_x); }
272	};
273
274	template <typename T>
275	static inline constructor_t<T> construct() {
276		return constructor_t<T>();
277	}
278
279	template <typename T, typename Arg>
280	static inline constructor1_t<T, Arg> construct(const Arg& a) {
281		return constructor1_t<T, Arg>(a);
282	}
283
284	template <typename Con>
285	static inline void construct_n(Con& c, typename Con::size_type n) {
286		c.reserve(c.size() + n);
287		while (n--) c.push_back(typename Con::value_type());
288	}
289
290	template <typename Con, typename Arg>
291	static inline void construct_n(
292		Con& c, typename Con::size_type n, const Arg& a
293	) {
294		c.reserve(c.size() + n);
295		while (n--) c.push_back(typename Con::value_type(a));
296	}
297
298	template <typename T, typename std::vector<int>::size_type I = 0>
299	class dim2 : public std::vector<T> {
300	public:
301		typedef typename std::vector<T>::reference reference;
302		typedef typename std::vector<T>::const_reference const_reference;
303		typedef typename std::vector<T>::size_type size_type;
304	private:
305		size_type _i;
306	public:
307		dim2(size_type i = 0, size_type j = 0) : std::vector<T>() {
308			resize(i, j);
309		}
310
311		void resize(size_type i = 0, size_type j = 0) {
312			if (I)
313				std::vector<T>::resize(I * i);
314			else {
315				_i = i;
316				std::vector<T>::resize(i * j);
317			}
318		}
319
320		reference
321		operator()(size_type i, size_type j) {
322			return (*this)[i * (I ? I : _i) + j];
323		}
324		const_reference
325		operator()(size_type i, size_type j) const {
326			return (*this)[i * (I ? I : _i) + j];
327		}
328	};
329
330	template <
331		typename T,
332		typename std::vector<int>::size_type J = 0,
333		typename std::vector<int>::size_type I = 0
334	> class dim3 : public std::vector<T> {
335	public:
336		typedef typename std::vector<T>::reference reference;
337		typedef typename std::vector<T>::const_reference const_reference;
338		typedef typename std::vector<T>::size_type size_type;
339	private:
340		size_type _j, _i;
341	public:
342		dim3(size_type i = 0, size_type j = 0, size_type k = 0)
343			: std::vector<T>()
344		{
345			resize(i, j, k);
346		}
347
348		void resize(size_type i = 0, size_type j = 0, size_type k = 0) {
349			if (I && J)
350				std::vector<T>::resize(I * J * i);
351			else if (J) {
352				_i = i;
353				std::vector<T>::resize(i * J * j);
354			} else {
355				_i = i;
356				_j = j;
357				std::vector<T>::resize(i * j * k);
358			}
359		}
360
361		reference
362		operator()(size_type i, size_type j, size_type k) {
363			return (*this)[(i * (I ? I : _i) + j) * (J ? J : _j) + k];
364		}
365		const_reference
366		operator()(size_type i, size_type j, size_type k) const {
367			return (*this)[(i * (I ? I : _i) + j) * (J ? J : _j) + k];
368		}
369	};
370};
371
372#include <resource.hh>
373
374#endif // _COMMON_HH