/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. #if HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #if ! HAVE_CXX_BOOL
  29. enum _boolType { false, true };
  30. typedef enum _boolType bool;
  31. #endif
  32. #if ! HAVE_CXX_NEW_FOR_SCOPING
  33. # define for if (0) {} else for
  34. #endif
  35. #define GLX_GLXEXT_PROTOTYPES 1
  36. #include <algorithm>
  37. #include <argp.h>
  38. #include <cmath>
  39. #include <functional>
  40. #include <iostream>
  41. #include <list>
  42. #if HAVE_CXX_STRINGSTREAM
  43. #include <sstream>
  44. #endif
  45. #if HAVE_CXX_STRSTREAM
  46. #include <strstream>
  47. #endif
  48. #include <string>
  49. #include <utility>
  50. #include <vector>
  51. #if HAVE_GL_GL_H
  52. #include <GL/gl.h>
  53. #endif
  54. #if HAVE_GL_GLEXT_H
  55. #include <GL/glext.h>
  56. #endif
  57. #if HAVE_GL_GLU_H
  58. #include <GL/glu.h>
  59. #endif
  60. #if HAVE_GL_GLX_H
  61. #include <GL/glx.h>
  62. #endif
  63. #if HAVE_X11_XLIB_H
  64. #include <X11/Xlib.h>
  65. #endif
  66. namespace std {
  67. #if HAVE_CXX_STRSTREAM && !HAVE_CXX_STRINGSTREAM
  68. typedef istrstream istringstream;
  69. typedef ostrstream ostringstream;
  70. typedef strstream stringstream;
  71. #endif
  72. };
  73. class ResourceManager;
  74. namespace Common {
  75. typedef std::string Exception;
  76. extern std::string program;
  77. extern Display* display;
  78. extern XVisualInfo* visualInfo;
  79. extern unsigned int screen;
  80. extern Window window;
  81. extern GLXContext context;
  82. extern std::string resourceDir;
  83. extern unsigned int width, height, depth;
  84. extern unsigned int centerX, centerY;
  85. extern float aspectRatio;
  86. extern Colormap colormap;
  87. extern bool doubleBuffered;
  88. extern bool running;
  89. extern unsigned int elapsedMicros;
  90. extern float elapsedSecs;
  91. extern float speed;
  92. extern float elapsedTime;
  93. extern ResourceManager* resources;
  94. #ifndef NOXBMC
  95. void init(int argc, char** argv);
  96. void run();
  97. #endif
  98. static inline int randomInt(int x) {
  99. return std::rand() % x;
  100. }
  101. static inline float randomFloat(float x) {
  102. return float(std::rand()) * x / float(RAND_MAX);
  103. }
  104. static inline void flush(bool swap = doubleBuffered) {
  105. if (swap)
  106. glXSwapBuffers(display, window);
  107. else
  108. glFlush();
  109. }
  110. template <typename T>
  111. static inline const T& clamp(
  112. const T& v, const T& min, const T& max
  113. ) {
  114. return std::max(std::min(v, max), min);
  115. }
  116. template <typename T>
  117. static inline bool parseArg(
  118. const char* arg, T& item, const T& min, const T& max
  119. ) {
  120. return
  121. !(std::istringstream(arg) >> item) ||
  122. item < min || item > max;
  123. }
  124. template <typename T>
  125. static inline bool parseArg(
  126. const char* arg, T& item, const T& min, const T& max, const T& extra
  127. ) {
  128. return
  129. !(std::istringstream(arg) >> item) ||
  130. (item != extra && (item < min || item > max));
  131. }
  132. };
  133. #ifdef NDEBUG
  134. #define TRACE(x) do {} while (0)
  135. #else
  136. #define TRACE(x) do { \
  137. std::cerr << "[" << __PRETTY_FUNCTION__ << " @ " << __FILE__ << \
  138. ":" << __LINE__ << "]" << std::endl << " " << x << std::endl; \
  139. } while (0)
  140. #endif
  141. #define WARN(x) do { \
  142. std::cerr << Common::program << ": " << x << std::endl; \
  143. } while (0)
  144. namespace stdx {
  145. class oss {
  146. private:
  147. std::ostringstream _oss;
  148. public:
  149. operator std::string() const { return _oss.str(); }
  150. template <typename T>
  151. oss& operator<<(const T& t) {
  152. _oss << t;
  153. return *this;
  154. }
  155. };
  156. template <typename In, typename MFunc>
  157. static inline void call_each(In p, In q, MFunc f) {
  158. for ( ; p != q; ++p)
  159. ((*p).*f)();
  160. }
  161. template <typename In, typename MFunc, typename Arg>
  162. static inline void call_each(In p, In q, MFunc f, const Arg& a) {
  163. for ( ; p != q; ++p)
  164. ((*p).*f)(a);
  165. }
  166. template <typename Con, typename MFunc>
  167. static inline void call_all(Con& c, MFunc f) {
  168. call_each(c.begin(), c.end(), f);
  169. }
  170. template <typename Con, typename MFunc, typename Arg>
  171. static inline void call_all(Con& c, MFunc f, const Arg& a) {
  172. call_each(c.begin(), c.end(), f, a);
  173. }
  174. template <typename In, typename Out, typename UFunc>
  175. static inline void map_each(In p, In q, Out o, UFunc f) {
  176. for ( ; p != q; ++p)
  177. *o++ = ((*p).*f)();
  178. }
  179. template <typename Con1, typename Con2, typename UFunc>
  180. static inline void map_all(const Con1& in, Con2& out, UFunc f) {
  181. map_each(in.begin(), in.end(), out.begin(), f);
  182. }
  183. template <typename In, typename MFuncPtr>
  184. static inline void call_each_ptr(In p, In q, MFuncPtr f) {
  185. for ( ; p != q; ++p)
  186. ((*p)->*f)();
  187. }
  188. template <typename In, typename MFuncPtr, typename Arg>
  189. static inline void call_each_ptr(
  190. In p, In q, MFuncPtr f, const Arg& a
  191. ) {
  192. for ( ; p != q; ++p)
  193. ((*p)->*f)(a);
  194. }
  195. template <typename Con, typename MFuncPtr>
  196. static inline void call_all_ptr(Con& c, MFuncPtr f) {
  197. call_each_ptr(c.begin(), c.end(), f);
  198. }
  199. template <typename Con, typename MFuncPtr, typename Arg>
  200. static inline void call_all_ptr(Con& c, MFuncPtr f, const Arg& a) {
  201. call_each_ptr(c.begin(), c.end(), f, a);
  202. }
  203. template <typename In, typename Out, typename UFunc>
  204. static inline void map_each_ptr(In p, In q, Out o, UFunc f) {
  205. for ( ; p != q; ++p)
  206. *o++ = ((*p)->*f)();
  207. }
  208. template <typename Con1, typename Con2, typename UFunc>
  209. static inline void map_all_ptr(const Con1& in, Con2& out, UFunc f) {
  210. map_each_ptr(in.begin(), in.end(), out.begin(), f);
  211. }
  212. template <typename In>
  213. static inline void destroy_each_ptr(In p, const In& q) {
  214. for ( ; p != q; ++p)
  215. delete *p;
  216. }
  217. template <typename Con>
  218. static inline void destroy_all_ptr(Con& c) {
  219. destroy_each_ptr(c.begin(), c.end());
  220. }
  221. template <typename T>
  222. class constructor_t {
  223. public:
  224. inline T operator()() { return T(); }
  225. };
  226. template <typename T, typename Arg>
  227. class constructor1_t {
  228. private:
  229. const Arg& _x;
  230. public:
  231. constructor1_t(const Arg& x) : _x(x) {}
  232. inline T operator()() { return T(_x); }
  233. };
  234. template <typename T>
  235. static inline constructor_t<T> construct() {
  236. return constructor_t<T>();
  237. }
  238. template <typename T, typename Arg>
  239. static inline constructor1_t<T, Arg> construct(const Arg& a) {
  240. return constructor1_t<T, Arg>(a);
  241. }
  242. template <typename Con>
  243. static inline void construct_n(Con& c, typename Con::size_type n) {
  244. c.reserve(c.size() + n);
  245. while (n--) c.push_back(typename Con::value_type());
  246. }
  247. template <typename Con, typename Arg>
  248. static inline void construct_n(
  249. Con& c, typename Con::size_type n, const Arg& a
  250. ) {
  251. c.reserve(c.size() + n);
  252. while (n--) c.push_back(typename Con::value_type(a));
  253. }
  254. template <typename T, typename std::vector<int>::size_type I = 0>
  255. class dim2 : public std::vector<T> {
  256. public:
  257. typedef typename std::vector<T>::reference reference;
  258. typedef typename std::vector<T>::const_reference const_reference;
  259. typedef typename std::vector<T>::size_type size_type;
  260. private:
  261. size_type _i;
  262. public:
  263. dim2(size_type i = 0, size_type j = 0) : std::vector<T>() {
  264. resize(i, j);
  265. }
  266. void resize(size_type i = 0, size_type j = 0) {
  267. if (I)
  268. std::vector<T>::resize(I * i);
  269. else {
  270. _i = i;
  271. std::vector<T>::resize(i * j);
  272. }
  273. }
  274. reference
  275. operator()(size_type i, size_type j) {
  276. return (*this)[i * (I ? I : _i) + j];
  277. }
  278. const_reference
  279. operator()(size_type i, size_type j) const {
  280. return (*this)[i * (I ? I : _i) + j];
  281. }
  282. };
  283. template <
  284. typename T,
  285. typename std::vector<int>::size_type J = 0,
  286. typename std::vector<int>::size_type I = 0
  287. > class dim3 : public std::vector<T> {
  288. public:
  289. typedef typename std::vector<T>::reference reference;
  290. typedef typename std::vector<T>::const_reference const_reference;
  291. typedef typename std::vector<T>::size_type size_type;
  292. private:
  293. size_type _j, _i;
  294. public:
  295. dim3(size_type i = 0, size_type j = 0, size_type k = 0)
  296. : std::vector<T>()
  297. {
  298. resize(i, j, k);
  299. }
  300. void resize(size_type i = 0, size_type j = 0, size_type k = 0) {
  301. if (I && J)
  302. std::vector<T>::resize(I * J * i);
  303. else if (J) {
  304. _i = i;
  305. std::vector<T>::resize(i * J * j);
  306. } else {
  307. _i = i;
  308. _j = j;
  309. std::vector<T>::resize(i * j * k);
  310. }
  311. }
  312. reference
  313. operator()(size_type i, size_type j, size_type k) {
  314. return (*this)[(i * (I ? I : _i) + j) * (J ? J : _j) + k];
  315. }
  316. const_reference
  317. operator()(size_type i, size_type j, size_type k) const {
  318. return (*this)[(i * (I ? I : _i) + j) * (J ? J : _j) + k];
  319. }
  320. };
  321. };
  322. #include <resource.hh>
  323. #endif // _COMMON_HH