]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/ipatch.h
e9bee3d79bbc0bcf7d42bfefa84c07647f364bf2
[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 "generic/constant.h"
26 #include "generic/vector.h"
27
28 namespace scene
29 {
30 class Node;
31 }
32
33 template<typename Element>
34 class ArrayReference
35 {
36 std::size_t m_size;
37 Element* m_data;
38 public:
39 typedef Element value_type;
40 typedef value_type* iterator;
41 typedef const value_type* const_iterator;
42
43 ArrayReference()
44         : m_size( 0 ), m_data( 0 ){
45 }
46 ArrayReference( std::size_t size, Element* data )
47         : m_size( size ), m_data( data ){
48 }
49
50 iterator begin(){
51         return m_data;
52 }
53 const_iterator begin() const {
54         return m_data;
55 }
56 iterator end(){
57         return m_data + m_size;
58 }
59 const_iterator end() const {
60         return m_data + m_size;
61 }
62
63 value_type& operator[]( std::size_t index ){
64 #if defined( _DEBUG )
65         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
66 #endif
67         return m_data[index];
68 }
69 const value_type& operator[]( std::size_t index ) const {
70 #if defined( _DEBUG )
71         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
72 #endif
73         return m_data[index];
74 }
75 value_type* data(){
76         return m_data;
77 }
78 const value_type* data() const {
79         return m_data;
80 }
81 std::size_t size() const {
82         return m_size;
83 }
84 bool empty() const {
85         return m_size == 0;
86 }
87 };
88
89 #if 0
90 template<typename Element>
91 class MatrixIterator
92 {
93 Element* m_position;
94
95 void increment(){
96         ++m_position;
97 }
98
99 public:
100 typedef std::bidirectional_iterator_tag iterator_category;
101 typedef std::ptrdiff_t difference_type;
102 typedef difference_type distance_type;
103 typedef KeyValue<Key, Value> value_type;
104 typedef value_type* pointer;
105 typedef value_type& reference;
106
107 MatrixIterator( Element* position ) : m_position( position ){
108 }
109
110 Element* position(){
111         return m_position;
112 }
113
114 bool operator==( const MatrixIterator& other ) const {
115         return m_position == other.m_position;
116 }
117 bool operator!=( const MatrixIterator& other ) const {
118         return !operator==( other );
119 }
120 MatrixIterator& operator++(){
121         increment();
122         return *this;
123 }
124 MatrixIterator operator++( int ){
125         MatrixIterator tmp = *this;
126         increment();
127         return tmp;
128 }
129 value_type& operator*() const {
130         return m_position->m_value;
131 }
132 value_type* operator->() const {
133         return &( operator*() );
134 }
135 };
136 #endif
137
138 template<typename Element>
139 class Matrix
140 {
141 std::size_t m_x, m_y;
142 Element* m_data;
143 public:
144 typedef Element value_type;
145 typedef value_type* iterator;
146 typedef const value_type* const_iterator;
147
148 Matrix()
149         : m_x( 0 ), m_y( 0 ), m_data( 0 ){
150 }
151 Matrix( std::size_t x, std::size_t y, Element* data )
152         : m_x( x ), m_y( y ), m_data( data ){
153 }
154
155 iterator begin(){
156         return m_data;
157 }
158 const_iterator begin() const {
159         return m_data;
160 }
161 iterator end(){
162         return m_data + size();
163 }
164 const_iterator end() const {
165         return m_data + size();
166 }
167
168 value_type& operator[]( std::size_t index ){
169 #if defined( _DEBUG )
170         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
171 #endif
172         return m_data[index];
173 }
174 const value_type& operator[]( std::size_t index ) const {
175 #if defined( _DEBUG )
176         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
177 #endif
178         return m_data[index];
179 }
180 value_type& operator()( std::size_t x, std::size_t y ){
181 #if defined( _DEBUG )
182         ASSERT_MESSAGE( x < m_x && y < m_y, "array index out of bounds" );
183 #endif
184         return m_data[x * m_y + y];
185 }
186 const value_type& operator()( std::size_t x, std::size_t y ) const {
187 #if defined( _DEBUG )
188         ASSERT_MESSAGE( x < m_x && y < m_y, "array index out of bounds" );
189 #endif
190         return m_data[x * m_y + y];
191 }
192 value_type* data(){
193         return m_data;
194 }
195 const value_type* data() const {
196         return m_data;
197 }
198 std::size_t x() const {
199         return m_x;
200 }
201 std::size_t y() const {
202         return m_y;
203 }
204 std::size_t size() const {
205         return m_x * m_y;
206 }
207 bool empty() const {
208         return m_x == 0;
209 }
210 };
211
212 class PatchControl
213 {
214 public:
215 Vector3 m_vertex;
216 Vector2 m_texcoord;
217 };
218
219 typedef Matrix<PatchControl> PatchControlMatrix;
220
221
222 class PatchCreator
223 {
224 public:
225 INTEGER_CONSTANT( Version, 1 );
226 STRING_CONSTANT( Name, "patch" );
227 virtual scene::Node& createPatch() = 0;
228 virtual void Patch_undoSave( scene::Node& patch ) const = 0;
229 virtual void Patch_resize( scene::Node& patch, std::size_t width, std::size_t height ) const = 0;
230 virtual PatchControlMatrix Patch_getControlPoints( scene::Node& patch ) const = 0;
231 virtual void Patch_controlPointsChanged( scene::Node& patch ) const = 0;
232 virtual const char* Patch_getShader( scene::Node& patch ) const = 0;
233 virtual void Patch_setShader( scene::Node& patch, const char* shader ) const = 0;
234 };
235
236 #include "modulesystem.h"
237
238 template<typename Type>
239 class ModuleRef;
240 typedef ModuleRef<PatchCreator> PatchModuleRef;
241
242 template<typename Type>
243 class GlobalModule;
244 typedef GlobalModule<PatchCreator> GlobalPatchModule;
245
246 template<typename Type>
247 class GlobalModuleRef;
248 typedef GlobalModuleRef<PatchCreator> GlobalPatchModuleRef;
249
250 inline PatchCreator& GlobalPatchCreator(){
251         return GlobalPatchModule::getTable();
252 }
253
254 #endif