Esempio n. 1
0
Texture* Rectangle::texture() const
{
  Texture* texture = new Texture();

  NOM_ASSERT(texture != nullptr);
  if( texture == nullptr ) {
    NOM_LOG_ERR(  NOM_LOG_CATEGORY_RENDER, "Could not update cache:",
                  "failed to allocate texture memory." );
    NOM_DELETE_PTR(texture);
    return nullptr;
  }

  RenderWindow* context = nom::render_interface();
  NOM_ASSERT(context != nullptr);

  if( context == nullptr ) {
    NOM_LOG_ERR(  NOM_LOG_CATEGORY_RENDER, "Could not update cache",
                  "invalid renderer." );
    NOM_DELETE_PTR(texture);
    return nullptr;
  }

  // Obtain the optimal pixel format for the platform
  RendererInfo caps = context->caps();

  if( texture->initialize( caps.optimal_texture_format(),
      SDL_TEXTUREACCESS_TARGET, this->size() ) == false )
  {
    NOM_LOG_ERR(  NOM_LOG_CATEGORY_APPLICATION, "Could not update cache:",
                  "failed texture creation." );
    NOM_DELETE_PTR(texture);
    return nullptr;
  }

  texture->set_position( this->position() );

  if( context->set_render_target(texture) == false ) {
    NOM_LOG_ERR(  NOM_LOG_CATEGORY_APPLICATION, "Could not update cache:",
                  "could not set rendering target." );
    NOM_DELETE_PTR(texture);
    return nullptr;
  }

  if( context->fill( this->fill_color() ) == false ) {
    NOM_LOG_ERR(  NOM_LOG_CATEGORY_APPLICATION, "Could not update cache:",
                  "failed to set texture color." );
    NOM_DELETE_PTR(texture);
    return nullptr;
  }

  this->draw(*context);

  if( context->reset_render_target() == false ) {
    NOM_LOG_ERR(  NOM_LOG_CATEGORY_APPLICATION, "Could not update cache:",
                  "failed to reset the rendering target." );
    NOM_DELETE_PTR(texture);
    return nullptr;
  }

  return texture;
}