Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
lxs.hpp
1 //////////////////////////////
2 //
3 // File: lxs/system/lxs.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Feb 3 2013
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __LXS_HPP_
15 #define __LXS_HPP_
16 #include "lxs/coregraphics/camera.hpp"
17 #include "lxs/windowing/root.hpp"
18 
19 
20 #include "helper/opengl.h"
21 #include "helper/tr1.h"
22 #include <deque>
23 #include "lxs/system/servicethread.hpp"
24 #include <pthread.h>
25 //!@brief Engine namespace
26 //!LxS is main namespace for all stuff related to "Labyrinth x Space"
27 //! game engine including all stuff required both to implement Labyrinth
28 //! and to implement other games of similar genre
29 //!and gameplay mechanics
30 namespace LxS
31 {
32  //!@brief Windowing system namespace (buttons, menus etc)
33  namespace Windowing
34  {
35  class Root;
36  }
37  //!@brief core graphics related methods (rendering primitives, loading models, textures, shaders, cameras)
38  namespace CoreGraphics
39  {
40  class GLCamera;
41  }
42  //!@brief Low level OS-interaction, encapsulation for OS-specifics (FS-access, threading, user input). Contains Root of class ::LxS::Windowing::Root.
43  namespace System
44  {
45  class MyMutex;
46  //! smart pointer to a function destinied to be executed in OpenGL context
48  //!@brief basic class for what implements core of OS-game interaction. (platform-specialized versions. Is guaranteed singleton.
49  class LxS
50  {
51  private:
52  //!@brief handle for root thread for purposes of detecting whenever code is executed in root thread or side thread
53  pthread_t root_thread;
54  protected:
55  //!@brief Global camera object (represents global camera setup object used to render current scene)
57  //!@brief Is application active (i.e. in foregground)
58  int animating;
59  //!Root window
61  //!this us used to make sure that it is a singleton - allows extract single global instance and disallows to create more than one instance
62  static LxS * global_instance; // singleton
63  float invDPC; //!<inverted dots per centimeter (convenient
64  int width;//!<width of whole window/screen
65  int height;//!<height of whole window/screen
66  bool Dragging;//!<is user trying to move something on the screen?
67 
68  //! Given x coord in pixels convert it to centimeters from edge
69  float RecalculateX(int x){return invDPC*x;}
70  //! Given Y coord in pixels from top edge convert it to centimeters from bottom edge
71  float RecalculateY(int y){return invDPC*(height-y);}
72  //! Used for implementing "touch and rotate interface" user idiom
73  void PassDragEventAsAngularDelta(int x,int y);
74  //!@brief Queue of functions that MUST be executed in root thread
75  //! Queue of functions that MUST be executed in root thread
76  //! (generally OpenGL code). For details look at <a href="http://labyrinth.maciejkaminski.pl/articles/threading.pdf">threading documentation</a>
77  std::deque<GLFunction> func_queue;
78  //! sync object for func_queue
80  //! typically invoked before/after Render by LxS class implementation. Typically executes just one function from func_queue at a time
82  //! used during shutdown, to clear queue before exiting
84  //! for implementation of shutdown procedure
86  public:
87  //! it is not shutdown procedure; rather pause invoked from platform-main (used to implement for instance Android Run States)
88  void Halt() {animating=0;}
89  //! Opposite to Halt
90  void Resume() {animating=1;}
91  //! Initialiize Root
92  void InitRoot();
93  //! Deinitialize Root
94  void DeinitRoot();
95  //! Checks whenever OpenGL function queue is empty
96  bool GLQueueEmpty();
97  //! @brief Call OpenGL from man thread
98  //! Passes function pointer to execute.
99  //! It is than pushed to GL function queue (func_queue)
100  //! and is guaranteed to be executed in main GL thread
101  //! function is removed from queue upon execution;
103  //! Read pixel from current GL buffer
104  //! (position is indicated in centimeters from screen bottom-left edge)
105  GLuint ReadPixel(float x, float y);
106  //! default constructor
107  LxS();
108  //! Issue terminate request (shutdown)
109  void Terminate();
110  //! @brief Expose pointer to global instance
111  //! In some situations it is required to issue call to global LxS instance. I.e. when shutdown is requested from UI or when opengl function must be called in global main opengl thread. (see documentation for func_queue member)
112  static LxS* ExposeGlobalInstance();
113  //! global destructor
114  virtual ~LxS();
115  //! ways of drawing frame differs from platform to platform
116  virtual void DrawFrame()=0;
117  //! ways of initializing context differs from platform to platform
118  virtual void Init()=0;
119  //! ways of deinitializing context differs from platform to platform
120  virtual void Deinit()=0; // if needed, called by dtor
121  };
122  //#ifdef __APPLE__
123  // ios class
124  //#else
125 
126 #ifdef X11LXS
127  //! class defines variables (data members) critical to implementation of OpenGL ports for Linux and MacOS X (and possibly Windows NT)
128  class LxSGL2:public virtual LxS
129  {
130  protected:
131  //! glx context handler
132  GLXContext glc;
133  };
134  //#endif
135 #endif
136 
137 #if (!(defined __APPLE__)) && (!(defined HEADLESS))
138  //! Used as parent for all OpenGL|ES implementations except iOS one
139  class LxSGLES:public virtual LxS
140  {
141  protected:
142  //! display handle
143  EGLDisplay eglDisplay;
144  //! context handle
145  EGLContext eglContext;
146  //! surface handle
147  EGLSurface eglSurface;
148  };
149 #endif
150  }
151 #ifdef __APPLE__
152 #define ENQUEUE_OGL(...) ::LxS::System::LxS::ExposeGlobalInstance()->EnqueueOGLFunction(::std::bind(__VA_ARGS__))
153 #else
154 
155 #define ENQUEUE_OGL(...) ::LxS::System::LxS::ExposeGlobalInstance()->EnqueueOGLFunction(::std::tr1::bind(__VA_ARGS__))
156 #endif
157 }
158 #endif
159 
160 // MAIN ENTRY POINT OF LXS-based app
161 LxS::Windowing::Root *LxSAppSetup();