Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
device.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_HW_DEVICE_H_)
21 #define __MITSUBA_HW_DEVICE_H_
22 
23 #include <mitsuba/hw/session.h>
24 #include <list>
25 
27 
28 class Renderer;
29 
30 /** \brief The device event structure encapsulates event
31  * information such as mouse movement or key presses
32  * \ingroup libhw
33  */
35 public:
36  /// Default constructor
37  inline DeviceEvent() { }
38 
39  /// Type constructor
40  inline DeviceEvent(unsigned short type) { m_type = type; }
41 
42  /// Return the event type
43  inline unsigned short getType() const { return m_type; }
44 
45  /// Set the event type
46  inline void setType(int type) { m_type = type; }
47 
48  /// Get the mouse position vector
49  inline void setMousePosition(const Point2i &position) { m_mouse.x = position.x; m_mouse.y = position.y; }
50 
51  /// Set the mouse position vector
52  inline Point2i getMousePosition() const { return Point2i(m_mouse.x, m_mouse.y); }
53 
54  /// Get the relative mouse movement vector
55  inline void setMouseRelative(const Vector2i &relative) { m_mouse.xrel = relative.x; m_mouse.yrel = relative.y; }
56 
57  /// Set the relative mouse movement vector
58  inline Vector2i getMouseRelative() const { return Vector2i(m_mouse.xrel, m_mouse.yrel); }
59 
60  /// Set the enum of the pressed mouse button
61  inline void setMouseButton(unsigned short button) { m_mouse.button = button; }
62 
63  /// Get the enum of the pressed mouse button
64  inline unsigned short getMouseButton() const { return m_mouse.button; }
65 
66  /// Set the pressed keyboard key (latin1)
67  inline void setKeyboardKey(char key) { m_keyboard.key = key; }
68 
69  /// Get the pressed keyboard key (latin1)
70  inline char getKeyboardKey() const { return m_keyboard.key; }
71 
72  /// Set the pressed keyboard key special identifier enum (see Device::ESpecialKeys)
73  inline void setKeyboardSpecial(unsigned short special) { m_keyboard.special = special; }
74 
75  /// Get the pressed keyboard key special identifier enum (see Device::ESpecialKeys)
76  inline unsigned short getKeyboardSpecial() const { return m_keyboard.special; }
77 
78  /// Set the keyboard modifiers (see Device::EKeyboardModifiers)
79  inline void setKeyboardModifiers(unsigned short modifiers) { m_keyboard.modifiers = modifiers; }
80 
81  /// Get the keyboard modifiers (see Device::EKeyboardModifiers)
82  inline unsigned short getKeyboardModifiers() const { return m_keyboard.modifiers; }
83 
84  /// Get the interpreted keypress data
85  inline const char* getKeyboardInterpreted() const { return m_keyboard.interpreted; }
86 
87  /// Get the interpreted keypress data
88  inline char* getKeyboardInterpreted() { return m_keyboard.interpreted; }
89 
90  /// Set the event source
91  inline void setActionSource(Object *source) { m_action.source = source; }
92 
93  /// Get the event source
94  inline Object *getActionSource() { return m_action.source; }
95 
96  /// Return a string representation
97  std::string toString() const;
98 private:
99  unsigned short m_type;
100 
101  union {
102  struct {
103  unsigned short special, modifiers;
104  char key, interpreted[16];
105  } m_keyboard;
106 
107  struct {
108  int x, y, xrel, yrel;
109  unsigned short button;
110  } m_mouse;
111 
112  struct {
114  } m_action;
115  };
116 };
117 
118 /** \brief Abstract device event callback
119  * \ingroup libhw
120  */
122 public:
123  /** \brief Called when a device event occurs
124  * \param event The event data structure
125  * \return True if the result has been handled, false otherwise
126  */
127  virtual bool deviceEventOccurred(const DeviceEvent &event) = 0;
128 protected:
129  /// Virtual destructor
130  virtual ~DeviceEventListener() { }
131 };
132 
133 /** \brief An abstract drawing device
134  * \ingroup libhw
135  */
136 class MTS_EXPORT_HW Device : public Object {
137 public:
138  /// Device event types
139  enum EEventType {
140  ENoEvent = 0x0000,
141  EQuitEvent = 0x0001,
142  EKeyDownEvent = 0x0002,
143  EKeyUpEvent = 0x0004,
144  EMouseMotionEvent = 0x0008,
145  EMouseDragEvent = 0x0010,
146  EMouseButtonDownEvent = 0x0020,
147  EMouseButtonUpEvent = 0x0040,
148  EMouseEnterEvent = 0x0080,
149  EMouseLeaveEvent = 0x0100,
150  EMouseBeginDragEvent = 0x0200,
151  EMouseEndDragEvent = 0x0400,
152  EMouseDoubleClickEvent = 0x0800,
153  EGainFocusEvent = 0x1000,
154  ELoseFocusEvent = 0x2000,
155  EResizeEvent = 0x4000
156  };
157 
158  /// Device keyboard event modifiers
160  EShiftModifier = 0x01,
161  EControlModifier = 0x02,
162  EAltModifier = 0x04,
163  EMetaModifier = 0x08
164  };
165 
166  /// Device keyboard event modifiers
168  ENoButton = 0x0,
169  ELeftButton = 0x01,
170  EMiddleButton = 0x02,
171  ERightButton = 0x04,
172  EWheelUpButton = 0x08,
173  EWheelDownButton = 0x10
174  };
175 
176  /// Device special keys
178  ENoSpecial = 0,
238  EKeyLastSpecialKey
239  };
240 
241  /// Construct a new device using the appropriate implementation
242  static Device *create(Session *session);
243 
244  /// Return the dimension of the device
245  inline Vector2i getSize() const { return m_size; }
246 
247  /// Set the dimension of the device
248  void setSize(const Vector2i &dimension);
249 
250  /// Return the aspect ratio of the device
251  inline Float getAspect() const { return (Float) m_size.x / (Float) m_size.y; }
252 
253  /// Return the position of the device
254  inline Point2i getPosition() const { return m_position; }
255 
256  /// Set the position of the device
257  virtual void setPosition(const Point2i &position);
258 
259  /// Set the FSAA sample count, do this before Init()
260  void setFSAA(int fsaa);
261 
262  /// Return the FSAA sample count
263  inline int getFSAA() const { return m_fsaa; }
264 
265  /// Only applies to devices, which are UI windows
266  virtual void setVisible(bool visible) = 0;
267 
268  /** \brief A convenience method.
269  *
270  * Sets the amount of bits for the red, green and
271  * blue components
272  */
273  void setColorBits(int colorBits);
274 
275  /// Set the amount of bits for the red component
276  void setRedBits(int redBits);
277 
278  /// Return the amount of bits for the red component
279  inline int getRedBits() const { return m_redBits; }
280 
281  /// Set the amount of bits for the green component
282  void setGreenBits(int greenBits);
283 
284  /// Return the amount of bits for the green component
285  inline int getGreenBits() const { return m_greenBits; }
286 
287  /// Set the amount of bits for the blue component
288  void setBlueBits(int blueBits);
289 
290  /// Return the amount of bits for the blue component
291  inline int getBlueBits() const { return m_blueBits; }
292 
293  /// Set the amount of bits for the alpha component
294  void setAlphaBits(int alphaBits);
295 
296  /// Return the amount of bits for the alpha component
297  inline int getAlphaBits() const { return m_alphaBits; }
298 
299  /// Set the amount of bits for the depth component
300  void setDepthBits(int depthBits);
301 
302  /// Return the amount of bits for the depth component
303  inline int getDepthBits() const { return m_depthBits; }
304 
305  /// Set the amount of bits for the stencil component
306  void setStencilBits(int stencilBits);
307 
308  /// Return the amount of bits for the stencil component
309  inline int getStencilBits() const { return m_stencilBits; }
310 
311  /// Define whether to enable double buffering
312  void setDoubleBuffer(bool doubleBuffer);
313 
314  // Return whether double buffering is enabled
315  inline bool getDoubleBuffer() const { return m_doubleBuffer; }
316 
317  // Define whether to enable full screen drawing
318  void setFullscreen(bool fullscreen);
319 
320  /// Return whether full screen drawing is enabled
321  inline bool getFullscreen() const { return m_fullscreen; }
322 
323  // Specify whether resizing the window is allowed
324  void setResizeAllowed(bool resizeAllowed);
325 
326  /// Return whether it is possible to resize the window
327  inline bool isResizeAllowed() const { return m_resizeAllowed; }
328 
329  /// Define whether to enable window centering
330  void setCenter(bool center);
331 
332  /// Return whether window centering is enabled
333  inline bool getCenter() const { return m_center; }
334 
335  /// Define whether to show the frames per second
336  inline void setShowFPS(bool showFPS) { m_showFPS = showFPS; }
337 
338  /// Return whether to show the frames per second
339  inline bool getShowFPS() const { return m_showFPS; }
340 
341  /// Return the frames per second (0 if no data is available)
342  inline int getFPS() const { return m_fps; }
343 
344  /// Set the x window position
345  void setXPos(int xpos);
346 
347  /// Set the window title
348  virtual void setTitle(const std::string &title);
349 
350  /// Return the window title
351  inline const std::string &getTitle() const { return m_title; }
352 
353  /// Get the session
354  inline const Session *getSession() const { return m_session.get(); }
355 
356  /// Get the session
357  inline Session *getSession() { return m_session; }
358 
359  /**
360  * Initialize the renderer. Optionally, an existing device instance
361  * can be provided as a second argument -- this is primarily meant
362  * to create a device that will be able to support a shared context
363  * with another device.
364  */
365  virtual void init(Device *other = NULL);
366 
367  /// Shut the device down
368  virtual void shutdown();
369 
370  /// Add an event callback to the device
371  void addCallback(DeviceEventListener *callback);
372 
373  /// Remove an event callback from the device
374  void removeCallback(DeviceEventListener *callback);
375 
376  /// Associate the renderer with this device
377  virtual void makeCurrent(Renderer *renderer) = 0;
378 
379  /// Flip the buffers (when using double buffering)
380  virtual void flip();
381 
383 protected:
384  /// Virtual destructor
385  virtual ~Device();
386 
387  /// Create a new device
388  Device(Session *session);
389 
390  /** \brief Send a device event using
391  * the registered callbacks
392  */
393  void fireDeviceEvent(const DeviceEvent &event);
394 protected:
395  ref<Session> m_session;
396  ref<Timer> m_timer;
397  Vector2i m_size;
398  Point2i m_position;
399  int m_fsaa;
400  int m_redBits, m_greenBits, m_blueBits;
401  int m_alphaBits, m_depthBits, m_stencilBits;
402  bool m_doubleBuffer, m_initialized, m_fullscreen;
403  bool m_center, m_showFPS, m_resizeAllowed;
404  int m_fpsCounter, m_fps, m_lastTime;
405  std::string m_title;
406  std::list<DeviceEventListener *> m_callbacks;
407 };
408 
410 
411 #endif /* __MITSUBA_HW_DEVICE_H_ */
Definition: device.h:236
Definition: device.h:193
int getAlphaBits() const
Return the amount of bits for the alpha component.
Definition: device.h:297
TVector2< int > Vector2i
Definition: fwd.h:108
Definition: device.h:210
Definition: device.h:237
void setKeyboardSpecial(unsigned short special)
Set the pressed keyboard key special identifier enum (see Device::ESpecialKeys)
Definition: device.h:73
Definition: device.h:234
Definition: device.h:226
The device event structure encapsulates event information such as mouse movement or key presses...
Definition: device.h:34
Definition: device.h:199
Definition: device.h:209
EMouseButton
Device keyboard event modifiers.
Definition: device.h:167
Definition: device.h:187
Definition: device.h:218
virtual ~DeviceEventListener()
Virtual destructor.
Definition: device.h:130
Definition: device.h:182
const char * getKeyboardInterpreted() const
Get the interpreted keypress data.
Definition: device.h:85
bool getDoubleBuffer() const
Definition: device.h:315
void setMouseRelative(const Vector2i &relative)
Get the relative mouse movement vector.
Definition: device.h:55
Definition: device.h:195
Definition: device.h:223
char key
Definition: device.h:104
Definition: device.h:190
unsigned short getKeyboardModifiers() const
Get the keyboard modifiers (see Device::EKeyboardModifiers)
Definition: device.h:82
DeviceEvent(unsigned short type)
Type constructor.
Definition: device.h:40
Point2i getPosition() const
Return the position of the device.
Definition: device.h:254
const std::string & getTitle() const
Return the window title.
Definition: device.h:351
#define MTS_EXPORT_HW
Definition: platform.h:114
bool getCenter() const
Return whether window centering is enabled.
Definition: device.h:333
Definition: device.h:222
Definition: device.h:230
DeviceEvent()
Default constructor.
Definition: device.h:37
EKeyboardModifiers
Device keyboard event modifiers.
Definition: device.h:159
unsigned short getKeyboardSpecial() const
Get the pressed keyboard key special identifier enum (see Device::ESpecialKeys)
Definition: device.h:76
Definition: device.h:227
Definition: device.h:181
Definition: device.h:232
Vector2i getMouseRelative() const
Set the relative mouse movement vector.
Definition: device.h:58
char getKeyboardKey() const
Get the pressed keyboard key (latin1)
Definition: device.h:70
void setType(int type)
Set the event type.
Definition: device.h:46
Definition: device.h:183
unsigned short getMouseButton() const
Get the enum of the pressed mouse button.
Definition: device.h:64
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
void setKeyboardModifiers(unsigned short modifiers)
Set the keyboard modifiers (see Device::EKeyboardModifiers)
Definition: device.h:79
bool getShowFPS() const
Return whether to show the frames per second.
Definition: device.h:339
Definition: device.h:219
Definition: device.h:211
EEventType
Device event types.
Definition: device.h:139
int getRedBits() const
Return the amount of bits for the red component.
Definition: device.h:279
Vector2i getSize() const
Return the dimension of the device.
Definition: device.h:245
Definition: device.h:220
Definition: device.h:191
Definition: device.h:185
Definition: device.h:215
int yrel
Definition: device.h:108
unsigned short button
Definition: device.h:109
Definition: device.h:196
ESpecialKeys
Device special keys.
Definition: device.h:177
An abstract drawing device.
Definition: device.h:136
unsigned short special
Definition: device.h:103
Definition: device.h:179
void setMousePosition(const Point2i &position)
Get the mouse position vector.
Definition: device.h:49
Abstract device event callback.
Definition: device.h:121
Float getAspect() const
Return the aspect ratio of the device.
Definition: device.h:251
Definition: device.h:221
Session * getSession()
Get the session.
Definition: device.h:357
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Definition: device.h:214
Definition: fwd.h:99
Reference counting helper.
Definition: ref.h:40
Definition: device.h:188
Definition: device.h:217
Definition: device.h:229
Abstract renderer implementation.
Definition: renderer.h:79
int getFSAA() const
Return the FSAA sample count.
Definition: device.h:263
Platform independent milli/micro/nanosecond timerThis class implements a simple cross-platform timer ...
Definition: timer.h:37
int getStencilBits() const
Return the amount of bits for the stencil component.
Definition: device.h:309
Abstract windowing environment session.
Definition: session.h:32
Point2i getMousePosition() const
Set the mouse position vector.
Definition: device.h:52
Definition: device.h:208
Definition: device.h:194
int getDepthBits() const
Return the amount of bits for the depth component.
Definition: device.h:303
Definition: device.h:184
const Session * getSession() const
Get the session.
Definition: device.h:354
unsigned short getType() const
Return the event type.
Definition: device.h:43
void setMouseButton(unsigned short button)
Set the enum of the pressed mouse button.
Definition: device.h:61
Definition: device.h:224
void setKeyboardKey(char key)
Set the pressed keyboard key (latin1)
Definition: device.h:67
bool isResizeAllowed() const
Return whether it is possible to resize the window.
Definition: device.h:327
void setShowFPS(bool showFPS)
Define whether to show the frames per second.
Definition: device.h:336
Definition: device.h:235
Definition: device.h:203
Definition: device.h:197
Definition: device.h:205
Definition: device.h:180
Object * getActionSource()
Get the event source.
Definition: device.h:94
Definition: device.h:233
Definition: device.h:228
Definition: device.h:206
Definition: device.h:202
Definition: device.h:198
Parent of all Mitsuba classes.
Definition: object.h:38
Object * source
Definition: device.h:113
Definition: device.h:231
TPoint2< int > Point2i
Definition: fwd.h:131
Definition: device.h:200
Definition: device.h:204
void setActionSource(Object *source)
Set the event source.
Definition: device.h:91
int getFPS() const
Return the frames per second (0 if no data is available)
Definition: device.h:342
Definition: device.h:201
Definition: device.h:192
Definition: device.h:212
char * getKeyboardInterpreted()
Get the interpreted keypress data.
Definition: device.h:88
Definition: device.h:207
Definition: device.h:213
Definition: fwd.h:95
#define MTS_NAMESPACE_END
Definition: platform.h:138
bool getFullscreen() const
Return whether full screen drawing is enabled.
Definition: device.h:321
int getGreenBits() const
Return the amount of bits for the green component.
Definition: device.h:285
Definition: device.h:216
Definition: device.h:189
Definition: device.h:225
Definition: device.h:186
int getBlueBits() const
Return the amount of bits for the blue component.
Definition: device.h:291