#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_GRAPHICS_RASTERIZATION_RENDERTARGET2_H #define NUCLEX_GRAPHICS_RASTERIZATION_RENDERTARGET2_H #include "Texture.h" #include "../Rectangle.h" #include "../Color.h" #include namespace Nuclex { namespace Graphics { namespace Rasterization { // ------------------------------------------------------------------------------------------- // ///2D texture onto which the rasterizer can draw class RenderTarget2 : public Texture { #pragma region class BackingObserver /// Allows the render target to be read from public: class BackingObserver : public Observer { /// Frees the resources used by the backer public: virtual ~BackingObserver() {} /// Reads the specified region of the render target /// Region of the render target that will be read /// /// Address at which the render target's pixels will be deposited /// /// Bytes required to get from one line to the next public: virtual void Read( const Rectangle ®ion, void *target, std::size_t targetStride ) = 0; /// Resizes the render target to the specified width and height /// New width of the render target /// New height of the render target public: virtual void Resized(std::size_t width, std::size_t height) = 0; /// Clears the render target to the specified values /// Color the render target will be cleared to /// Depth value the depth buffer will be cleared to /// Stencil bits the stencil buffer will be cleared to public: virtual void Cleared(Color color, float depth, std::uint8_t stencil) = 0; }; #pragma endregion // class BackingObserver /// Initializes a new texture with the specified size /// Width of the texture in pixels /// Height of the texture in pixels /// Memory layout the texture's pixels will use /// How the texture is going to be used public: NUCLEX_GRAPHICS_API RenderTarget2( std::size_t width, std::size_t height, PixelFormat::Enum pixelFormat, Usage::Enum usage = Usage::Immutable ); /// Initializes a render target by copying an existing render target /// Existing render target that will be copied public: NUCLEX_GRAPHICS_API RenderTarget2(const RenderTarget2 &other); /// Destroys the render target public: NUCLEX_GRAPHICS_API virtual ~RenderTarget2(); /// Resizes the render target to the specified width and height /// New width of the render target /// New height of the render target public: NUCLEX_GRAPHICS_API void Resize(std::size_t width, std::size_t height); /// Clears the contents of the render target /// /// This will clear the render target to black, with a depth of 1.0 and /// erase all bits in its stencil buffer. /// public: NUCLEX_GRAPHICS_API void Clear() { Clear(Color::Black); } /// Clears the contents of the render target /// Color the render target will be cleared to /// Depth value the depth buffer will be filled with /// Stencil bits the stencil buffer will be filled with public: NUCLEX_GRAPHICS_API void Clear( Color color, float depth = 1.0f, std::uint8_t stencil = 0 ); /// Counts the number of dimensions the texture has /// The texture's number of dimensions public: NUCLEX_GRAPHICS_API virtual std::size_t CountDimensions() const { return 2; } /// Retrieves the length of one of the texture's dimensions /// /// Index of the dimension whose length will be returned /// /// The length of the specified dimension public: NUCLEX_GRAPHICS_API virtual std::size_t GetLength( std::size_t dimensionIndex ) const; /// Retrieves the width of the texture in pixels /// The texture's width in pixels public: NUCLEX_GRAPHICS_API std::size_t GetWidth() const { return this->width; } /// Retrieves the height of the texture in pixels /// The texture's height in pixels public: NUCLEX_GRAPHICS_API std::size_t GetHeight() const { return this->height; } /// Suppresses the notifications and begins queueing them /// /// Use this method if you plan to apply many small changes to the buffer in /// order to avoid the overhead that would result in any observers having /// to process hundreds of micro-changes. The buffer will collect all changes /// and merge them. Once notifications are enabled again, a single notification /// even will be fired, containing the changed area. /// public: NUCLEX_GRAPHICS_API void SuppressChangeNotifications(); /// Enables change notifications again /// /// If any changes happened while notifications were suppressed, this will /// trigger a change notification. /// public: NUCLEX_GRAPHICS_API void PermitChangeNotifications(); /// Retrieves the observer attached by the specified owner /// Owner whose observer will be looked up /// The observer registered by the specified owner or a nullptr public: NUCLEX_GRAPHICS_API BackingObserver *GetObserver(void *owner) const; /// Attaches an observer to the render target /// The observer's owner, used as a unique key /// Observer that will be attached to the texture public: NUCLEX_GRAPHICS_API void AttachObserver(void *owner, BackingObserver *observer); /// Detaches the specified owner's observer from the render target /// Owner whose observer will be detached public: NUCLEX_GRAPHICS_API void DetachObserver(void *owner); /// Copies the contents of another buffer into this buffer /// Other buffer whose contents will be copied /// A reference to this buffer protected: NUCLEX_GRAPHICS_API RenderTarget2 &operator =(const RenderTarget2 &other); /// /// Notifies all observers that the render target is about to be destroyed /// protected: NUCLEX_GRAPHICS_API void OnDestroying(); /// Notifies all observers that the render target has been resized /// New width of the render target /// New height of the render target protected: NUCLEX_GRAPHICS_API void OnResized(std::size_t width, std::size_t height); /// Notifies all observers that the render target has been cleared protected: NUCLEX_GRAPHICS_API void OnCleared(); /// Notifies all observers that the render target has been cleared /// Color the render target was cleared to /// Depth value the depth buffer was cleared to /// Stencil bits the stencil buffer was cleared to protected: NUCLEX_GRAPHICS_API void OnCleared( Color color, float depth, std::uint8_t stencil ); /// Notifies all observers that the render target has been changed /// Region that has been changed protected: NUCLEX_GRAPHICS_API void OnChanged(const Rectangle &changedRegion); /// Stores the owner and observer attached to the texture private: typedef std::pair AttachedObserver; /// Width of the texture in pixels protected: std::size_t width; /// Height of the texture in pixels protected: std::size_t height; /// Observers to changes happening to the buffer private: std::vector observers; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Graphics::Rasterization #endif // NUCLEX_GRAPHICS_RASTERIZATION_RENDERTARGET2_H