// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) // This file is part of the "Nazara Engine - OpenGL renderer" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include #include #include namespace Nz::GL { template DeviceObject::DeviceObject(OpenGLDevice& device, CreateArgs... args) { ErrorFlags errFlags(ErrorMode::ThrowException); Create(device, args...); } template DeviceObject::~DeviceObject() { Destroy(); } template bool DeviceObject::Create(OpenGLDevice& device, CreateArgs... args) { Destroy(); m_device = &device; const Context& context = EnsureDeviceContext(); m_objectId = C::CreateHelper(*m_device, context, args...); if (m_objectId == InvalidObject) { NazaraError("Failed to create OpenGL object"); //< TODO: Handle error message return false; } return true; } template void DeviceObject::Destroy() { if (IsValid()) { const Context& context = EnsureDeviceContext(); C::DestroyHelper(*m_device, context, m_objectId); m_objectId = InvalidObject; } } template const Context& DeviceObject::EnsureDeviceContext() const { assert(m_device); const Context* activeContext = Context::GetCurrentContext(); if (!activeContext || activeContext->GetDevice() != m_device) { const Context& referenceContext = m_device->GetReferenceContext(); if (!Context::SetCurrentContext(&referenceContext)) throw std::runtime_error("failed to activate context"); return referenceContext; } return *activeContext; } template bool DeviceObject::IsValid() const { return m_objectId != InvalidObject; } template OpenGLDevice* DeviceObject::GetDevice() const { return m_device; } template GLuint DeviceObject::GetObjectId() const { return m_objectId; } template void DeviceObject::SetDebugName(const std::string_view& name) { const Context& context = EnsureDeviceContext(); if (context.glObjectLabel) context.glObjectLabel(ObjectType, m_objectId, SafeCast(name.size()), name.data()); } } #include