1 /****************************************************************************
2  *            hardwarebuffer.hpp
3  *
4  * Author: 2011  Daniel Jungmann <el.3d.source@googlemail.com>
5  * Copyright: See COPYING file that comes with this distribution
6  ****************************************************************************/
7 
8 #ifndef	UUID_ebe8ffbc_ae1a_4db5_bdd0_e78938bd6dfc
9 #define	UUID_ebe8ffbc_ae1a_4db5_bdd0_e78938bd6dfc
10 
11 #ifndef	__cplusplus
12 #error	"Including C++ header in C translation unit!"
13 #endif	/* __cplusplus */
14 
15 #include "../platform.h"
16 
17 /**
18  * @file
19  * @brief The @c class HardwareBuffer.
20  * This file contains the @c class HardwareBuffer.
21  */
22 namespace eternal_lands
23 {
24 
25 	/**
26 	 * Defines how to access the hardware buffer.
27 	 * @{
28 	 */
29 	enum HardwareBufferAccessType
30 	{
31 		hbat_read_only = GL_READ_ONLY,
32 		hbat_read_write = GL_READ_WRITE,
33 		hbat_write_only = GL_WRITE_ONLY
34 	};
35 	/**
36 	 * @}
37 	 */
38 
39 	/**
40 	 * Defines for what we use the hardware buffer. This is not static,
41 	 * so you can use render-to-vertex-buffer etc.
42 	 * @{
43 	 */
44 	enum HardwareBufferType
45 	{
46 		hbt_vertex = GL_ARRAY_BUFFER,
47 		hbt_index = GL_ELEMENT_ARRAY_BUFFER,
48 		hbt_pixel_pack = GL_PIXEL_PACK_BUFFER,
49 		hbt_pixel_unpack = GL_PIXEL_UNPACK_BUFFER
50 	};
51 	/**
52 	 * @}
53 	 */
54 
55 	/**
56 	 * Defines how to use the hardware buffer.
57 	 * @{
58 	 */
59 	enum HardwareBufferUsageType
60 	{
61 		hbut_dynamic_copy = GL_DYNAMIC_COPY,
62 		hbut_dynamic_draw = GL_DYNAMIC_DRAW,
63 		hbut_dynamic_read = GL_DYNAMIC_READ,
64 		hbut_static_copy = GL_STATIC_COPY,
65 		hbut_static_draw = GL_STATIC_DRAW,
66 		hbut_static_read = GL_STATIC_READ,
67 		hbut_stream_copy = GL_STREAM_COPY,
68 		hbut_stream_draw = GL_STREAM_DRAW,
69 		hbut_stream_read = GL_STREAM_READ
70 	};
71 	/**
72 	 * @}
73 	 */
74 
75 	/**
76 	 * @brief @c class for opengl hardware buffers.
77 	 *
78 	 * @c class for opengl hardware buffers.
79 	 */
80 	class HardwareBuffer
81 	{
82 		private:
83 			/**
84 			 * This is the size of the buffer in bytes.
85 			 */
86 			Uint64 m_size;
87 
88 			/**
89 			 * This is the OpenGL buffer id.
90 			 */
91 			GLuint m_id;
92 
93 		public:
94 			/**
95 			 * Default constructor, creates the OpenGL id.
96 			 */
97 			HardwareBuffer();
98 
99 			/**
100 			 * Default destructor, frees the memory and the
101 			 * OpenGL id.
102 			 */
103 			~HardwareBuffer() throw();
104 
105 			/**
106 			 * Returns the size of the buffer in bytes.
107 			 */
get_size() const108 			inline Uint64 get_size() const
109 			{
110 				return m_size;
111 			}
112 
113 			/**
114 			 * Sets the size of the buffer in bytes.
115 			 * @param type The type of the buffer.
116 			 * @param size The new size to use.
117 			 * @param usage The usage of the buffer.
118 			 */
119 			void set_size(const HardwareBufferType type,
120 				const Uint64 size,
121 				const HardwareBufferUsageType usage);
122 
123 			/**
124 			 * Binds the buffer.
125 			 * @param type The type of the buffer to bind.
126 			 */
127 			void bind(const HardwareBufferType type);
128 
129 			/**
130 			 * Unbinds the currently bound buffer.
131 			 * @param type The type of the buffer to unbind.
132 			 */
133 			static void unbind(const HardwareBufferType type);
134 
135 			/**
136 			 * Map the buffer the currently bound buffer.
137 			 * @param type The hardware buffer type to unmap.
138 			 * @param access The access type that is performed.
139 			 * @return Pointer of the buffer data.
140 			 */
141 			static void* map(const HardwareBufferType type,
142 				const HardwareBufferAccessType access);
143 
144 			/**
145 			 * Unmap the buffer the currently bound buffer.
146 			 * @param type The hardware buffer type to unmap.
147 			 * @return bool Returns true on success, else false.
148 			 */
149 			static bool unmap(const HardwareBufferType type);
150 
151 	};
152 
153 }
154 
155 #endif	/* UUID_ebe8ffbc_ae1a_4db5_bdd0_e78938bd6dfc */
156 
157