#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 // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_GRAPHICS_SOURCE 1 #include "Nuclex/Graphics/Rasterization/RenderTarget2.h" #include #include // Who keeps redefining min and max here? Grr... #undef min #undef max namespace Nuclex { namespace Graphics { namespace Rasterization { // ------------------------------------------------------------------------------------------- // RenderTarget2::RenderTarget2( std::size_t width, std::size_t height, PixelFormat::Enum pixelFormat, Usage::Enum usage /* = Usage::Immutable */ ) : Texture(CountRequiredBytes(width * height, pixelFormat), pixelFormat, usage), width(width), height(height) {} // ------------------------------------------------------------------------------------------- // RenderTarget2::RenderTarget2(const RenderTarget2 &other) : Texture(other), width(other.width), height(other.height) {} // ------------------------------------------------------------------------------------------- // RenderTarget2::~RenderTarget2() { OnDestroying(); } // ------------------------------------------------------------------------------------------- // void RenderTarget2::Resize(std::size_t width, std::size_t height) { this->width = width; this->height = height; // TODO: Add resize method to Buffer and Texture classes OnResized(width, height); } // ------------------------------------------------------------------------------------------- // void RenderTarget2::Clear( Color color, float depth /* = 1.0f */, std::uint8_t stencil /* = 0 */ ) { //std::uint8_t fillValue = 0; //std::fill(this->Pixels, this->Pixels + GetSizeInBytes(), fillValue); OnCleared(color, depth, stencil); } // ------------------------------------------------------------------------------------------- // std::size_t RenderTarget2::GetLength(std::size_t dimensionIndex) const { switch(dimensionIndex) { case 0: { return this->width; } case 1: { return this->height; } default: { throw std::out_of_range("Invalid dimension specified"); } } } // ------------------------------------------------------------------------------------------- // void RenderTarget2::SuppressChangeNotifications() { // Not sure this is such a good idea for render targets... // // Maybe rename the method to SuppressChangeNotifications() since clear and // destroy notifications must not ever be suppressed! } // ------------------------------------------------------------------------------------------- // void RenderTarget2::PermitChangeNotifications() { // Not sure this is such a good idea for render targets... // // Maybe rename the method to SuppressChangeNotifications() since clear and // destroy notifications must not ever be suppressed! } // ------------------------------------------------------------------------------------------- // RenderTarget2::BackingObserver *RenderTarget2::GetObserver(void *owner) const { for(std::size_t index = 0; index < this->observers.size(); ++index) { if(this->observers[index].first == owner) { return this->observers[index].second; } } // This is not a sign of sloppiness because owners need to check whether they // already have an attached observer. The other option would be for owners to // keep a big map associating each texture with an observer, but why use an // O(log(n)) construct when you can have O(1) in nearly all use cases. return nullptr; } // ------------------------------------------------------------------------------------------- // void RenderTarget2::AttachObserver(void *owner, BackingObserver *observer) { using namespace std; // Observer management should be accurate and error-free! assert( (GetObserver(owner) == nullptr) && "Only one observer can be attached per owner" ); this->observers.push_back(AttachedObserver(owner, observer)); } // ------------------------------------------------------------------------------------------- // void RenderTarget2::DetachObserver(void *owner) { using namespace std; for(std::size_t index = 0; index < this->observers.size(); ++index) { if(this->observers[index].first == owner) { this->observers.erase(this->observers.begin() + index); return; } } // Observer management should be accurate and error-free! assert(!"Attempted to detach an observer that was not attached"); } // ------------------------------------------------------------------------------------------- // void RenderTarget2::OnDestroying() { for(std::size_t index = 0; index < this->observers.size(); ++index) { this->observers[index].second->Destroying(); } } // ------------------------------------------------------------------------------------------- // void RenderTarget2::OnResized(std::size_t width, std::size_t height) { for(std::size_t index = 0; index < this->observers.size(); ++index) { this->observers[index].second->Resized(width, height); } } // ------------------------------------------------------------------------------------------- // void RenderTarget2::OnCleared(Color color, float depth, std::uint8_t stencil) { for(std::size_t index = 0; index < this->observers.size(); ++index) { this->observers[index].second->Cleared(color, depth, stencil); } } // ------------------------------------------------------------------------------------------- // void RenderTarget2::OnChanged(const Rectangle &) { // Do we really want to support writing to render targets? } // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Graphics::Rasterization