MyGUI 3.2.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_Canvas.cpp
Go to the documentation of this file.
1 /*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7 #include "MyGUI_Precompiled.h"
8 #include "MyGUI_Canvas.h"
9 #include "MyGUI_ResourceManager.h"
10 #include "MyGUI_Gui.h"
11 #include "MyGUI_RenderManager.h"
12 #include "MyGUI_Bitwise.h"
13
14 namespace MyGUI
15 {
16
17 Canvas::Canvas() :
18 mTexture( nullptr ),
19 mTexResizeMode( TRM_PT_CONST_SIZE ),
20 mTexData( 0 ),
21 mTexManaged( true ),
22 mFrameAdvise( false ),
23 mInvalidateData(false)
24 {
25 mGenTexName = utility::toString((size_t)this, "_Canvas");
26 }
27
28 void Canvas::createTexture( TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
29 {
30 int width = std::max(1, getWidth());
31 int height = std::max(1, getHeight());
32
33 createTexture( width, height, _resizeMode, _usage, _format );
34 }
35
36 void Canvas::createTexture( const IntSize& _size, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
37 {
38 int width = std::max(1, _size.width);
39 int height = std::max(1, _size.height);
40
41 createTexture( width, height, _resizeMode, _usage, _format );
42 }
43
44 void Canvas::createExactTexture( int _width, int _height, TextureUsage _usage, PixelFormat _format )
45 {
46 int width = std::max(1, _width);
47 int height = std::max(1, _height);
48
49 destroyTexture();
50
51 mTexture = RenderManager::getInstance().createTexture(mGenTexName);
52 mTexture->setInvalidateListener(this);
53 mTexture->createManual( width, height, _usage, _format );
54
55 mTexManaged = true;
56
57 _setTextureName( mGenTexName );
58 correctUV();
59 requestUpdateCanvas( this, Event( true, true, mInvalidateData ) );
60 }
61
62 void Canvas::resize( const IntSize& _size )
63 {
64 if ( _size.width <= 0 || _size.height <= 0 || ! mTexManaged )
65 return;
66
67 mReqTexSize = _size;
68
69 frameAdvise( true );
70 }
71
72 void Canvas::createTexture( int _width, int _height, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
73 {
74 mTexResizeMode = _resizeMode;
75
76 int width = std::max(1, _width);
77 int height = std::max(1, _height);
78
79 if (_resizeMode == TRM_PT_CONST_SIZE)
80 {
81 mReqTexSize = IntSize(width, height);
82 }
83 else
84 {
85 mReqTexSize = IntSize(std::max(1, getWidth()), std::max(1, getHeight()));
86 }
87
88 bool create = checkCreate( width, height );
89
90 width = Bitwise::firstPO2From(width);
91 height = Bitwise::firstPO2From(height);
92
93 if ( create )
94 createExactTexture( width, height, _usage, _format );
95 }
96
97 void Canvas::setSize( const IntSize& _size )
98 {
99 resize( _size );
100
101 Base::setSize( _size );
102 }
103
104 void Canvas::setCoord( const IntCoord& _coord )
105 {
106 resize( _coord.size() );
107
108 Base::setCoord( _coord );
109 }
110
111 void Canvas::updateTexture()
112 {
113 mInvalidateData = true;
114 frameAdvise( true );
115 }
116
117 bool Canvas::checkCreate( int _width, int _height ) const
118 {
119 if ( mTexture == nullptr )
120 return true;
121
122 if ( mTexture->getWidth() >= _width && mTexture->getHeight() >= _height )
123 return false;
124
125 return true;
126 }
127
128 void Canvas::validate( int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format ) const
129 {
130 _width = std::max(1, _width);
131 _height = std::max(1, _height);
132
133 _width = Bitwise::firstPO2From(_width);
134 _height = Bitwise::firstPO2From(_height);
135
136 // restore usage and format
137 if ( mTexture != nullptr )
138 {
139 if ( _usage == getDefaultTextureUsage() )
140 _usage = mTexture->getUsage();
141
142 if ( _format == getDefaultTextureFormat() )
143 _format = mTexture->getFormat();
144 }
145 }
146
147 void Canvas::destroyTexture()
148 {
149 _destroyTexture( true );
150 }
151
152 void Canvas::shutdownOverride()
153 {
154 _destroyTexture(false);
155 frameAdvise(false);
156 }
157
158 void Canvas::initialiseOverride()
159 {
160 }
161
162 void Canvas::_destroyTexture( bool _sendEvent )
163 {
164 if ( mTexture != nullptr )
165 {
166 if ( _sendEvent )
167 {
168 eventPreTextureChanges( this );
169 }
170
171 RenderManager::getInstance().destroyTexture( mTexture );
172 mTexture = nullptr;
173 }
174
175 }
176
177 void Canvas::correctUV()
178 {
179 if ( mTexResizeMode == TRM_PT_VIEW_REQUESTED )
180 {
181 _setUVSet(
182 FloatRect(
183 0,
184 0,
185 (float) mReqTexSize.width / (float) getTextureRealWidth(),
186 (float) mReqTexSize.height / (float) getTextureRealHeight()));
187 }
188
189 if ( mTexResizeMode == TRM_PT_CONST_SIZE || mTexResizeMode == TRM_PT_VIEW_ALL )
190 {
191 _setUVSet( FloatRect( 0, 0, 1, 1 ) );
192 }
193 }
194
195 void* Canvas::lock(TextureUsage _usage)
196 {
197 void* data = mTexture->lock(_usage);
198
199 mTexData = reinterpret_cast< uint8* >( data );
200
201 return data;
202 }
203
204 void Canvas::unlock()
205 {
206 mTexture->unlock();
207 }
208
209 bool Canvas::isTextureSrcSize() const
210 {
211 return getTextureSrcSize() == getTextureRealSize();
212 }
213
214 void Canvas::frameAdvise( bool _advise )
215 {
216 if ( _advise )
217 {
218 if ( ! mFrameAdvise )
219 {
220 MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate( this, &Canvas::frameEntered );
221 mFrameAdvise = true;
222 }
223 }
224 else
225 {
226 if ( mFrameAdvise )
227 {
228 MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate( this, &Canvas::frameEntered );
229 mFrameAdvise = false;
230 }
231 }
232 }
233
234 void Canvas::frameEntered( float _time )
235 {
236 int width = mReqTexSize.width;
237 int height = mReqTexSize.height;
238 TextureUsage usage = getDefaultTextureUsage();
239 PixelFormat format = getDefaultTextureFormat();
240
241 validate( width, height, usage, format );
242
243 bool create = checkCreate( width, height );
244
245 if ( mTexResizeMode == TRM_PT_CONST_SIZE )
246 create = false;
247
248 if ( create )
249 {
250 createExactTexture( width, height, usage, format );
251 correctUV();
252 }
253 else // I thought order is important
254 {
255 correctUV();
256 requestUpdateCanvas( this, Event( false, true, mInvalidateData ) );
257 }
258
259 mInvalidateData = false;
260 frameAdvise( false );
261 }
262
263 void Canvas::textureInvalidate(ITexture* _texture)
264 {
265 updateTexture();
266 }
267
268 void Canvas::_setUVSet(const FloatRect& _rect)
269 {
270 if (nullptr != getSubWidgetMain())
271 getSubWidgetMain()->_setUVSet(_rect);
272 }
273
274 bool Canvas::isLocked() const
275 {
276 return mTexture->isLocked();
277 }
278
279 int Canvas::getTextureRealWidth() const
280 {
281 return (int) mTexture->getWidth();
282 }
283
284 int Canvas::getTextureRealHeight() const
285 {
286 return (int) mTexture->getHeight();
287 }
288
289 IntSize Canvas::getTextureRealSize() const
290 {
291 return IntSize( getTextureRealWidth(), getTextureRealHeight() );
292 }
293
294 int Canvas::getTextureSrcWidth() const
295 {
296 return mReqTexSize.width;
297 }
298
299 int Canvas::getTextureSrcHeight() const
300 {
301 return mReqTexSize.height;
302 }
303
304 IntSize Canvas::getTextureSrcSize() const
305 {
306 return mReqTexSize;
307 }
308
309 PixelFormat Canvas::getTextureFormat() const
310 {
311 return mTexture->getFormat();
312 }
313
314 const std::string& Canvas::getTextureName() const
315 {
316 return mTexture->getName();
317 }
318
319 void Canvas::setSize(int _width, int _height)
320 {
321 setSize(IntSize(_width, _height));
322 }
323
324 void Canvas::setCoord(int _left, int _top, int _width, int _height)
325 {
326 setCoord(IntCoord(_left, _top, _width, _height));
327 }
328
329 Canvas::TextureResizeMode Canvas::getResizeMode() const
330 {
331 return mTexResizeMode;
332 }
333
334 void Canvas::setResizeMode(TextureResizeMode _value)
335 {
336 mTexResizeMode = _value;
337 }
338
339 bool Canvas::isTextureCreated() const
340 {
341 return mTexture != nullptr;
342 }
343
344 bool Canvas::isTextureManaged() const
345 {
346 return mTexManaged;
347 }
348
349 ITexture* Canvas::getTexture() const
350 {
351 return mTexture;
352 }
353
354 void Canvas::setTextureManaged(bool _value)
355 {
356 mTexManaged = _value;
357 }
358
359 TextureUsage Canvas::getDefaultTextureUsage()
360 {
361 return TextureUsage::Stream | TextureUsage::Write;
362 }
363
364 PixelFormat Canvas::getDefaultTextureFormat()
365 {
366 return PixelFormat::R8G8B8A8;
367 }
368
369 } // namespace MyGUI
MyGUI::types::TCoord< int >
MyGUI::Canvas::getTextureSrcWidth
int getTextureSrcWidth() const
Returns needed width while creating texture.
Definition: MyGUI_Canvas.cpp:294
MyGUI::Canvas::_setUVSet
void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_Canvas.cpp:268
MyGUI::Canvas::requestUpdateCanvas
EventHandle_CanvasPtrEvent requestUpdateCanvas
Definition: MyGUI_Canvas.h:189
MyGUI_Canvas.h
MyGUI::Canvas::isLocked
bool isLocked() const
Checks lockness of hardware _pixel buffer.
Definition: MyGUI_Canvas.cpp:274
MyGUI::Canvas::checkCreate
bool checkCreate(int _width, int _height) const
Checks if we need to create a texture with such sizes.
Definition: MyGUI_Canvas.cpp:117
MyGUI::ITexture::createManual
virtual void createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format)=0
MyGUI::Canvas::frameEntered
void frameEntered(float _time)
For updating once per frame.
Definition: MyGUI_Canvas.cpp:234
MyGUI::Canvas::lock
void * lock(TextureUsage _usage=TextureUsage::Write)
Locks hardware pixel buffer.
Definition: MyGUI_Canvas.cpp:195
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:150
MyGUI::Canvas::getTextureRealHeight
int getTextureRealHeight() const
Returns real height of texture.
Definition: MyGUI_Canvas.cpp:284
MyGUI::Canvas::unlock
void unlock()
Unlocks hardware pixel buffer.
Definition: MyGUI_Canvas.cpp:204
MyGUI::Canvas::frameAdvise
void frameAdvise(bool _advise)
For updating once per frame.
Definition: MyGUI_Canvas.cpp:214
MyGUI::IntSize
types::TSize< int > IntSize
Definition: MyGUI_Types.h:29
MyGUI_Precompiled.h
MyGUI::ICroppedRectangle::getHeight
int getHeight() const
Definition: MyGUI_ICroppedRectangle.h:119
MyGUI::Canvas::getDefaultTextureFormat
static PixelFormat getDefaultTextureFormat()
Returns default GUI texture format.
Definition: MyGUI_Canvas.cpp:364
MyGUI::types::TSize< int >
MyGUI::newDelegate
delegates::IDelegate0 * newDelegate(void(*_func)())
Definition: MyGUI_Delegate.h:118
MyGUI::Singleton< RenderManager >::getInstance
static RenderManager & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI::types::TCoord::size
TSize< T > size() const
Definition: MyGUI_TCoord.h:190
MyGUI::Canvas::mFrameAdvise
bool mFrameAdvise
For updating once per frame. True state means updating before next frame starts.
Definition: MyGUI_Canvas.h:243
MyGUI::TextureUsage::Write
Definition: MyGUI_RenderFormat.h:93
MyGUI::Canvas::eventPreTextureChanges
EventHandle_CanvasPtr eventPreTextureChanges
Definition: MyGUI_Canvas.h:182
MyGUI::Canvas::Canvas
Canvas()
Definition: MyGUI_Canvas.cpp:17
MyGUI::Canvas::setSize
virtual void setSize(const IntSize &_value)
Definition: MyGUI_Canvas.cpp:97
MyGUI::Canvas::mTexture
ITexture * mTexture
Current texture.
Definition: MyGUI_Canvas.h:225
MyGUI::Canvas::correctUV
void correctUV()
Correct texture uv-coordinates.
Definition: MyGUI_Canvas.cpp:177
MyGUI::Canvas::createExactTexture
void createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
Creates the texture itself.
Definition: MyGUI_Canvas.cpp:44
MyGUI::ITexture::lock
virtual void * lock(TextureUsage _access)=0
MyGUI::ITexture::isLocked
virtual bool isLocked()=0
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI::ISubWidgetRect::_setUVSet
virtual void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_ISubWidgetRect.h:25
MyGUI::ITexture::unlock
virtual void unlock()=0
MyGUI::SkinItem::_setTextureName
void _setTextureName(const std::string &_texture)
Definition: MyGUI_SkinItem.cpp:133
MyGUI::ITexture::getName
virtual const std::string & getName() const =0
MyGUI::Canvas::getResizeMode
TextureResizeMode getResizeMode() const
Returns resize mode.
Definition: MyGUI_Canvas.cpp:329
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:35
MyGUI::Canvas::getTextureSrcHeight
int getTextureSrcHeight() const
Returns needed height while creating texture.
Definition: MyGUI_Canvas.cpp:299
MyGUI::Canvas::updateTexture
void updateTexture()
Call user delegate update and removes old texture if it isn't original.
Definition: MyGUI_Canvas.cpp:111
MyGUI::SkinItem::getSubWidgetMain
ISubWidgetRect * getSubWidgetMain()
Definition: MyGUI_SkinItem.cpp:160
MyGUI::Canvas::getTextureFormat
PixelFormat getTextureFormat() const
Returns needed sizes while creating texture.
Definition: MyGUI_Canvas.cpp:309
MyGUI::Canvas::initialiseOverride
virtual void initialiseOverride()
Definition: MyGUI_Canvas.cpp:158
MyGUI::Canvas::textureInvalidate
virtual void textureInvalidate(ITexture *_texture)
Definition: MyGUI_Canvas.cpp:263
MyGUI_RenderManager.h
MyGUI::Canvas::setCoord
virtual void setCoord(const IntCoord &_value)
Definition: MyGUI_Canvas.cpp:104
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::TextureUsage
Definition: MyGUI_RenderFormat.h:84
MyGUI::ICroppedRectangle::getWidth
int getWidth() const
Definition: MyGUI_ICroppedRectangle.h:114
MyGUI::types::TRect< float >
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:20
MyGUI::Canvas::createTexture
void createTexture(TextureResizeMode _resizeMode, TextureUsage _usage=getDefaultTextureUsage(), PixelFormat _format=getDefaultTextureFormat())
Creates texture.
Definition: MyGUI_Canvas.cpp:28
MyGUI::Bitwise::firstPO2From
static __inline Type firstPO2From(Type _value)
Definition: MyGUI_Bitwise.h:21
MyGUI::Canvas::mReqTexSize
IntSize mReqTexSize
Requested bu user sizes.
Definition: MyGUI_Canvas.h:228
MyGUI::ITexture::getHeight
virtual int getHeight()=0
MyGUI::Canvas::getTextureSrcSize
IntSize getTextureSrcSize() const
Returns needed sizes while creating texture.
Definition: MyGUI_Canvas.cpp:304
MyGUI::Canvas::isTextureCreated
bool isTextureCreated() const
Returns true if the texture was created (and exists), otherwise false.
Definition: MyGUI_Canvas.cpp:339
MyGUI::utility::toString
std::string toString(T p)
Definition: MyGUI_StringUtility.h:27
MyGUI::ITexture
Definition: MyGUI_ITexture.h:26
MyGUI::Canvas::getDefaultTextureUsage
static TextureUsage getDefaultTextureUsage()
Returns default GUI texture usage.
Definition: MyGUI_Canvas.cpp:359
MyGUI::Canvas::resize
void resize(const IntSize &_size)
Calls when resize widget.
Definition: MyGUI_Canvas.cpp:62
MyGUI::Canvas::mTexResizeMode
TextureResizeMode mTexResizeMode
Texture resize mode.
Definition: MyGUI_Canvas.h:234
MyGUI::Canvas::setTextureManaged
void setTextureManaged(bool _value)
Sets the texture managed.
Definition: MyGUI_Canvas.cpp:354
MyGUI::Canvas::isTextureSrcSize
bool isTextureSrcSize() const
Checks if the texture has the source (required by user) size, otherwise real texture size are bigger...
Definition: MyGUI_Canvas.cpp:209
MyGUI_ResourceManager.h
MyGUI::Canvas::getTextureRealSize
IntSize getTextureRealSize() const
Returns real _size of texture.
Definition: MyGUI_Canvas.cpp:289
MyGUI::FloatRect
types::TRect< float > FloatRect
Definition: MyGUI_Types.h:33
MyGUI::Canvas::getTextureRealWidth
int getTextureRealWidth() const
Returns real width of texture.
Definition: MyGUI_Canvas.cpp:279
MyGUI::ITexture::getFormat
virtual PixelFormat getFormat()=0
MyGUI::Canvas::Event
Definition: MyGUI_Canvas.h:30
MyGUI::Canvas::validate
void validate(int &_width, int &_height, TextureUsage &_usage, PixelFormat &_format) const
Update entered parameters according to current texture resize mode(size) and restore (if can) paramet...
Definition: MyGUI_Canvas.cpp:128
MyGUI::ITexture::getUsage
virtual TextureUsage getUsage()=0
MyGUI::Canvas::TRM_PT_VIEW_ALL
Definition: MyGUI_Canvas.h:88
MyGUI::Canvas::TextureResizeMode
TextureResizeMode
Definition: MyGUI_Canvas.h:53
MyGUI::TextureUsage::Stream
Definition: MyGUI_RenderFormat.h:91
MyGUI::Canvas::_destroyTexture
void _destroyTexture(bool _sendEvent)
Destroys texture.
Definition: MyGUI_Canvas.cpp:162
MyGUI::PixelFormat
Definition: MyGUI_RenderFormat.h:49
MyGUI::Canvas::TRM_PT_CONST_SIZE
Definition: MyGUI_Canvas.h:61
MyGUI::Canvas::isTextureManaged
bool isTextureManaged() const
Returns true if we own the texture, otherwise false.
Definition: MyGUI_Canvas.cpp:344
MyGUI_Gui.h
MyGUI::Canvas::setResizeMode
void setResizeMode(TextureResizeMode _value)
Sets resize mode of texture.
Definition: MyGUI_Canvas.cpp:334
MyGUI_Bitwise.h
MyGUI::Canvas::getTextureName
const std::string & getTextureName() const
Returns name of the current texture.
Definition: MyGUI_Canvas.cpp:314
MyGUI::Canvas::shutdownOverride
virtual void shutdownOverride()
Definition: MyGUI_Canvas.cpp:152
MyGUI::Canvas::mTexManaged
bool mTexManaged
true if we own the texture (can delete it or replace by another instance), otherwise false ...
Definition: MyGUI_Canvas.h:240
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:21
MyGUI::PixelFormat::R8G8B8A8
Definition: MyGUI_RenderFormat.h:57
MyGUI::ITexture::setInvalidateListener
virtual void setInvalidateListener(ITextureInvalidateListener *_listener)
Definition: MyGUI_ITexture.h:37
MyGUI::uint8
unsigned char uint8
Definition: MyGUI_Types.h:46
MyGUI::RenderManager::createTexture
virtual ITexture * createTexture(const std::string &_name)=0
MyGUI::Canvas::getTexture
ITexture * getTexture() const
Reurns interface texture.
Definition: MyGUI_Canvas.cpp:349
MyGUI::Canvas::mGenTexName
std::string mGenTexName
Generated texture name.
Definition: MyGUI_Canvas.h:231
MyGUI::Canvas::mInvalidateData
bool mInvalidateData
Definition: MyGUI_Canvas.h:245
MyGUI::Canvas::destroyTexture
void destroyTexture()
Destroys texture.
Definition: MyGUI_Canvas.cpp:147
MyGUI::ITexture::getWidth
virtual int getWidth()=0
MyGUI::Canvas::mTexData
uint8 * mTexData
Saved pointer from last calling lock.
Definition: MyGUI_Canvas.h:237
MyGUI::Canvas::TRM_PT_VIEW_REQUESTED
Definition: MyGUI_Canvas.h:75
MyGUI::RenderManager::destroyTexture
virtual void destroyTexture(ITexture *_texture)=0