Mercator
Buffer.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU General Public License (See COPYING for details).
3 // Copyright (C) 2003 Alistair Riddoch
4 
5 #ifndef MERCATOR_BUFFER_H
6 #define MERCATOR_BUFFER_H
7 
8 namespace Mercator {
9 
10 class Segment;
11 
13 template<typename DataType>
14 class Buffer {
15  public:
17  const Segment & m_segment;
18  private:
20  const unsigned int m_channels;
22  const unsigned int m_size;
24  DataType * m_data;
25 
26  public:
31  explicit Buffer(const Segment & segment, unsigned int channels = 4);
32  virtual ~Buffer();
33 
39  DataType & operator()(unsigned int x,unsigned int y,unsigned int channel) {
40  return m_data[(y * m_size + x) * m_channels + channel];
41  }
42 
48  const DataType & operator()(unsigned int x,
49  unsigned int y,
50  unsigned int channel) const {
51  return m_data[(y * m_size + x) * m_channels + channel];
52  }
53 
55  const Segment & getSegment() const {
56  return m_segment;
57  }
58 
60  unsigned int getSize() const {
61  return m_size;
62  }
63 
65  unsigned int getChannels() const {
66  return m_channels;
67  }
68 
70  DataType * getData() {
71  return m_data;
72  }
73 
78  void allocate() {
79  m_data = new DataType[m_size * m_size * m_channels];
80  }
81 
85  bool isValid() const {
86  return (m_data != 0);
87  }
88 
92  void invalidate() {
93  delete [] m_data;
94  m_data = 0;
95  }
96 
97 };
98 
99 } // namespace Mercator
100 
101 #endif // MERCATOR_BUFFER_H
const Segment & getSegment() const
Accessor for the terrain height segment this buffer is associated with.
Definition: Buffer.h:55
DataType * m_data
Pointer to buffer containing data values.
Definition: Buffer.h:24
DataType * getData()
Accessor for a pointer to buffer containing data values.
Definition: Buffer.h:70
bool isValid() const
Determine if this buffer has valid allocated storage.
Definition: Buffer.h:85
unsigned int getSize() const
Accessor for the size of segment, m_res + 1.
Definition: Buffer.h:60
void invalidate()
De-allocate the storage for this buffer.
Definition: Buffer.h:92
Template for managing buffers of data for a segment.
Definition: Buffer.h:14
Definition: Area.cpp:20
const unsigned int m_size
The size of segment, m_res + 1.
Definition: Buffer.h:22
const Segment & m_segment
The terrain height segment this buffer is associated with.
Definition: Buffer.h:17
const unsigned int m_channels
The number of data values per height point.
Definition: Buffer.h:20
Buffer(const Segment &segment, unsigned int channels=4)
Constructor.
Definition: Buffer_impl.h:12
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:36
unsigned int getChannels() const
Accessor for the number of data values per height point.
Definition: Buffer.h:65
void allocate()
Allocate the storage required by the buffer.
Definition: Buffer.h:78
DataType & operator()(unsigned int x, unsigned int y, unsigned int channel)
Retrieve the data value at a given point.
Definition: Buffer.h:39
const DataType & operator()(unsigned int x, unsigned int y, unsigned int channel) const
Retrieve the data value at a given point.
Definition: Buffer.h:48