| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /*
- * This source file is part of RmlUi, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://github.com/mikke89/RmlUi
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- * Copyright (c) 2019-2023 The RmlUi Team, and contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include "TextureResource.h"
- #include "../../Include/RmlUi/Core/Core.h"
- #include "../../Include/RmlUi/Core/Log.h"
- #include "../../Include/RmlUi/Core/Profiling.h"
- #include "../../Include/RmlUi/Core/RenderInterface.h"
- #include "TextureDatabase.h"
- namespace Rml {
- TextureResource::TextureResource() {}
- TextureResource::~TextureResource()
- {
- Reset();
- }
- void TextureResource::Set(const String& _source)
- {
- Reset();
- source = _source;
- }
- void TextureResource::Set(const String& name, const TextureCallback& callback)
- {
- Reset();
- source = name;
- texture_callback = MakeUnique<TextureCallback>(callback);
- TextureDatabase::AddCallbackTexture(this);
- }
- void TextureResource::Reset()
- {
- Release();
- if (texture_callback)
- {
- TextureDatabase::RemoveCallbackTexture(this);
- texture_callback.reset();
- }
- source.clear();
- }
- TextureHandle TextureResource::GetHandle()
- {
- if (!loaded)
- Load();
- return handle;
- }
- Vector2i TextureResource::GetDimensions()
- {
- if (!loaded)
- Load();
- return dimensions;
- }
- const String& TextureResource::GetSource() const
- {
- return source;
- }
- void TextureResource::Release()
- {
- if (loaded)
- {
- RenderInterface* render_interface = ::Rml::GetRenderInterface();
- RMLUI_ASSERT(render_interface);
- render_interface->ReleaseTexture(handle);
- handle = {};
- dimensions = {};
- loaded = false;
- }
- }
- bool TextureResource::IsLoaded() const
- {
- return loaded;
- }
- bool TextureResource::Load()
- {
- RMLUI_ZoneScoped;
- RenderInterface* render_interface = ::Rml::GetRenderInterface();
- loaded = true;
- // Generate the texture from the callback function if we have one.
- if (texture_callback)
- {
- TextureCallback& callback_fnc = *texture_callback;
- if (!callback_fnc(render_interface, source, handle, dimensions) || !handle)
- {
- Log::Message(Log::LT_WARNING, "Failed to generate texture from callback function %s.", source.c_str());
- handle = {};
- dimensions = {};
- return false;
- }
- return true;
- }
- // No callback function, load the texture through the render interface.
- if (!render_interface->LoadTexture(handle, dimensions, source))
- {
- Log::Message(Log::LT_WARNING, "Failed to load texture from %s.", source.c_str());
- handle = {};
- dimensions = {};
- return false;
- }
- return true;
- }
- } // namespace Rml
|