BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
renderTarget.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_VIEW_RENDERING_RENDERTARGET_H
6 #define BALL_VIEW_RENDERING_RENDERTARGET_H
7 
8 #include <BALL/COMMON/global.h>
10 
11 #include <assert.h>
12 #include <vector>
13 #include <boost/smart_ptr.hpp>
14 #include <boost/shared_array.hpp>
15 
17 
18 namespace BALL
19 {
20  namespace VIEW
21  {
22 
24  struct Resolution
25  {
26  unsigned int width;
27  unsigned int height;
28 
29  Resolution() : width(0), height(0) { }
30 
31  Resolution(const unsigned width, const unsigned int height) : width(width),height(height) { }
32  };
33 
34 
39  {
40  public:
41 
44  width(0), height(0), pitch(0), pixelFormat(PixelFormat::RGB_32)
45  { }
46 
47  FrameBufferFormat(const unsigned int width,
48  const unsigned int height, const PixelFormat &pixelFormat)
49  : width(width), height(height),
50  pitch(width * pixelFormat.computeByteSize()),
51  pixelFormat(pixelFormat)
52  {
53  assert(pitch >= width * pixelFormat.computeByteSize());
54  }
55 
56  FrameBufferFormat(const unsigned int width, const unsigned int height,
57  const unsigned int pitch, const PixelFormat &pixelFormat )
58  : width(width), height(height), pitch(pitch), pixelFormat(pixelFormat)
59  {
60  assert(pitch >= width * pixelFormat.computeByteSize());
61  }
62 
64  : width(format.width), height(format.height),
65  pitch(format.pitch), pixelFormat(format.pixelFormat)
66  {
67  }
68 
70  {
71  width = format.width;
72  height = format.height;
73  pitch = format.pitch;
74  pixelFormat = format.pixelFormat;
75  return *this;
76  }
77 
78  bool operator==(const FrameBufferFormat& format) const
79  {
80  return width==format.width && height==format.height &&
81  pitch==format.pitch && pixelFormat==format.pixelFormat;
82  }
83 
84  bool operator!=(const FrameBufferFormat& format) const
85  {
86  return width!=format.width || height!=format.height ||
87  pitch!=format.pitch || pixelFormat!=format.pixelFormat;
88  }
89 
90  bool isValid() const { return width > 0 && height > 0 && pitch > 0; }
91 
95  unsigned int getWidth() const { return width; }
96 
100  void setWidth(unsigned int width) { this->width = width; }
101 
103  unsigned int getHeight() const { return height; }
104 
106  void setHeight(unsigned int height) { this->height = height; }
107 
109  unsigned int getPitch() const { return pitch; }
110 
112  void setPitch(unsigned int pitch) { this->pitch = pitch; }
113 
115  const PixelFormat &getPixelFormat() const { return pixelFormat; }
116 
119  { this->pixelFormat = pixelFormat; }
120 
124  size_t computeSize() const
125  {
126  // pitch is in bytes
127  return isValid() ? (getPitch() * getHeight()) : 0;
128  }
129 
131  void resize(const unsigned int newWidth, const unsigned int newHeight)
132  {
133  this->width = newWidth;
134  this->height = newHeight;
135  }
136 
140  const FrameBufferFormat resized(const unsigned int newWidth,
141  const unsigned int newHeight) const
142  {
143  return FrameBufferFormat(newWidth, newHeight, pitch, pixelFormat);
144  }
145 
146  private:
148  unsigned int width;
149 
151  unsigned int height;
152 
159  unsigned int pitch;
160 
162  };
163 
164  inline std::ostream &operator<<(std::ostream &o, const FrameBufferFormat &f)
165  {
166  o << f.getWidth() << "x" << f.getHeight() << ", pitch " << f.getPitch()
167  << ", pixel format: " << f.getPixelFormat();
168  return o;
169  }
170 
171  typedef std::vector<FrameBufferFormat> BufferFormatList;
172 
173  //-----------------------------------------------------------------------------
174 
176  {
177  public:
178 
183  data(data), format(format)
184  {
185  }
186 
190  virtual ~FrameBuffer() {}
191 
193  void *getData() { return data; }
194 
196  const void *getData() const { return data; }
197 
199  const FrameBufferFormat &getFormat() const { return format; }
200 
201  protected:
202 
204  void setData(void *data) { this->data = data; }
205 
207  void setFormat(const FrameBufferFormat &format) { this->format = format; }
208 
209  private:
214  void *data;
215 
220  };
221 
222  typedef boost::shared_ptr<FrameBuffer> FrameBufferPtr;
223 
224  //-----------------------------------------------------------------------------
225 
227  {
228  public:
229 
230  virtual ~RenderTarget() { }
231 
240  virtual FrameBufferPtr getBuffer() throw(BALL::Exception::NoBufferAvailable) = 0;
241 
242  virtual FrameBufferFormat getFormat() const = 0;
243 
250  virtual void releaseBuffer(FrameBufferPtr buffer) = 0;
251 
254  virtual bool init() = 0;
255 
258  virtual bool resize(const unsigned int width, const unsigned int height) = 0;
259 
262  virtual void refresh() = 0;
263 
264  /* Prepare the window for rendering, e.g., make it current if necessary.
265  */
266  virtual void prepareRendering() = 0;
267  };
268 
269  } //namespace VIEW
270 
271 } // namespace BALL
272 
273 #endif // BALL_VIEW_RENDERING_RENDERTARGET_H