]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/ipatch.h
Merge branch 'fixcflags' into 'master'
[xonotic/netradiant.git] / include / ipatch.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_IPATCH_H )
23 #define INCLUDED_IPATCH_H
24
25 #include "debugging/debugging.h"
26 #include "generic/constant.h"
27 #include "generic/vector.h"
28
29 namespace scene
30 {
31 class Node;
32 }
33
34 template<typename Element>
35 class ArrayReference
36 {
37 std::size_t m_size;
38 Element* m_data;
39 public:
40 typedef Element value_type;
41 typedef value_type* iterator;
42 typedef const value_type* const_iterator;
43
44 ArrayReference()
45         : m_size( 0 ), m_data( 0 ){
46 }
47 ArrayReference( std::size_t size, Element* data )
48         : m_size( size ), m_data( data ){
49 }
50
51 iterator begin(){
52         return m_data;
53 }
54 const_iterator begin() const {
55         return m_data;
56 }
57 iterator end(){
58         return m_data + m_size;
59 }
60 const_iterator end() const {
61         return m_data + m_size;
62 }
63
64 value_type& operator[]( std::size_t index ){
65 #if defined( _DEBUG )
66         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
67 #endif
68         return m_data[index];
69 }
70 const value_type& operator[]( std::size_t index ) const {
71 #if defined( _DEBUG )
72         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
73 #endif
74         return m_data[index];
75 }
76 value_type* data(){
77         return m_data;
78 }
79 const value_type* data() const {
80         return m_data;
81 }
82 std::size_t size() const {
83         return m_size;
84 }
85 bool empty() const {
86         return m_size == 0;
87 }
88 };
89
90 #if 0
91 template<typename Element>
92 class MatrixIterator
93 {
94 Element* m_position;
95
96 void increment(){
97         ++m_position;
98 }
99
100 public:
101 typedef std::bidirectional_iterator_tag iterator_category;
102 typedef std::ptrdiff_t difference_type;
103 typedef difference_type distance_type;
104 typedef KeyValue<Key, Value> value_type;
105 typedef value_type* pointer;
106 typedef value_type& reference;
107
108 MatrixIterator( Element* position ) : m_position( position ){
109 }
110
111 Element* position(){
112         return m_position;
113 }
114
115 bool operator==( const MatrixIterator& other ) const {
116         return m_position == other.m_position;
117 }
118 bool operator!=( const MatrixIterator& other ) const {
119         return !operator==( other );
120 }
121 MatrixIterator& operator++(){
122         increment();
123         return *this;
124 }
125 MatrixIterator operator++( int ){
126         MatrixIterator tmp = *this;
127         increment();
128         return tmp;
129 }
130 value_type& operator*() const {
131         return m_position->m_value;
132 }
133 value_type* operator->() const {
134         return &( operator*() );
135 }
136 };
137 #endif
138
139 template<typename Element>
140 class Matrix
141 {
142 std::size_t m_x, m_y;
143 Element* m_data;
144 public:
145 typedef Element value_type;
146 typedef value_type* iterator;
147 typedef const value_type* const_iterator;
148
149 Matrix()
150         : m_x( 0 ), m_y( 0 ), m_data( 0 ){
151 }
152 Matrix( std::size_t x, std::size_t y, Element* data )
153         : m_x( x ), m_y( y ), m_data( data ){
154 }
155
156 iterator begin(){
157         return m_data;
158 }
159 const_iterator begin() const {
160         return m_data;
161 }
162 iterator end(){
163         return m_data + size();
164 }
165 const_iterator end() const {
166         return m_data + size();
167 }
168
169 value_type& operator[]( std::size_t index ){
170 #if defined( _DEBUG )
171         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
172 #endif
173         return m_data[index];
174 }
175 const value_type& operator[]( std::size_t index ) const {
176 #if defined( _DEBUG )
177         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
178 #endif
179         return m_data[index];
180 }
181 value_type& operator()( std::size_t x, std::size_t y ){
182 #if defined( _DEBUG )
183         ASSERT_MESSAGE( x < m_x && y < m_y, "array index out of bounds" );
184 #endif
185         return m_data[x * m_y + y];
186 }
187 const value_type& operator()( std::size_t x, std::size_t y ) const {
188 #if defined( _DEBUG )
189         ASSERT_MESSAGE( x < m_x && y < m_y, "array index out of bounds" );
190 #endif
191         return m_data[x * m_y + y];
192 }
193 value_type* data(){
194         return m_data;
195 }
196 const value_type* data() const {
197         return m_data;
198 }
199 std::size_t x() const {
200         return m_x;
201 }
202 std::size_t y() const {
203         return m_y;
204 }
205 std::size_t size() const {
206         return m_x * m_y;
207 }
208 bool empty() const {
209         return m_x == 0;
210 }
211 };
212
213 class PatchControl
214 {
215 public:
216 Vector3 m_vertex;
217 Vector2 m_texcoord;
218 };
219
220 typedef Matrix<PatchControl> PatchControlMatrix;
221
222
223 class PatchCreator
224 {
225 public:
226 INTEGER_CONSTANT( Version, 1 );
227 STRING_CONSTANT( Name, "patch" );
228 virtual scene::Node& createPatch() = 0;
229 virtual void Patch_undoSave( scene::Node& patch ) const = 0;
230 virtual void Patch_resize( scene::Node& patch, std::size_t width, std::size_t height ) const = 0;
231 virtual PatchControlMatrix Patch_getControlPoints( scene::Node& patch ) const = 0;
232 virtual void Patch_controlPointsChanged( scene::Node& patch ) const = 0;
233 virtual const char* Patch_getShader( scene::Node& patch ) const = 0;
234 virtual void Patch_setShader( scene::Node& patch, const char* shader ) const = 0;
235 };
236
237 #include "modulesystem.h"
238
239 template<typename Type>
240 class ModuleRef;
241 typedef ModuleRef<PatchCreator> PatchModuleRef;
242
243 template<typename Type>
244 class GlobalModule;
245 typedef GlobalModule<PatchCreator> GlobalPatchModule;
246
247 template<typename Type>
248 class GlobalModuleRef;
249 typedef GlobalModuleRef<PatchCreator> GlobalPatchModuleRef;
250
251 inline PatchCreator& GlobalPatchCreator(){
252         return GlobalPatchModule::getTable();
253 }
254
255 #endif