light/modules/engine/src/graphics/graphics_context.cpp

82 lines
2.4 KiB
C++
Raw Normal View History

2025-07-05 13:28:41 +03:30
#include <engine/graphics/graphics_context.hpp>
#include <engine/platform/graphics/opengl/graphics_context.hpp>
#ifdef LIGHT_PLATFORM_WINDOWS
#include <engine/platform/graphics/directx/graphics_context.hpp>
#include <engine/platform/graphics/directx/shared_context.hpp>
#endif
#include <engine/graphics/blender.hpp> // required for forward declaratio>
#include <engine/graphics/buffers.hpp> // required for forward declaratio>
#include <engine/graphics/render_command.hpp> // required for forward declaratio>
#include <engine/graphics/renderer.hpp> // required for forward declaratio>
#include <engine/user_interface/user_interface.hpp> // required for forward declaratio>
#include <engine/utils/resource_manager.hpp>
namespace Light {
2025-07-05 16:07:51 +03:30
GraphicsContext *GraphicsContext::s_context = nullptr;
2025-07-05 13:28:41 +03:30
GraphicsContext::~GraphicsContext()
2025-07-06 16:52:50 +03:30
= default;
2025-07-05 13:28:41 +03:30
2025-07-06 14:02:50 +03:30
auto GraphicsContext::create(GraphicsAPI api, GLFWwindow *windowHandle) -> Scope<GraphicsContext>
2025-07-05 13:28:41 +03:30
{
// terminate 'GraphicsContext' dependent classes
2025-07-05 16:07:51 +03:30
if (s_context)
2025-07-05 13:28:41 +03:30
{
2025-07-05 16:07:51 +03:30
s_context->m_renderer.reset();
s_context->m_user_interface.reset();
2025-07-05 13:28:41 +03:30
2025-07-05 16:07:51 +03:30
delete s_context;
2025-07-05 13:28:41 +03:30
}
// determine the default api
if (api == GraphicsAPI::Default)
{
#if defined(LIGHT_PLATFORM_WINDOWS)
api = GraphicsAPI::DirectX;
#elif defined(LIGHT_PLATFORM_LINUX)
api = GraphicsAPI::OpenGL;
#elif defined(LIGHT_PLATFORM_MAC)
api = GraphicsAPI::OpenGL;
#endif
}
// create gfx context
2025-07-06 14:02:50 +03:30
auto scope_gfx = Scope<GraphicsContext> {};
2025-07-05 13:28:41 +03:30
switch (api)
{
// opengl
case GraphicsAPI::OpenGL:
2025-07-06 14:02:50 +03:30
scope_gfx = create_scope<glGraphicsContext>(windowHandle);
s_context = scope_gfx.get();
2025-07-05 13:28:41 +03:30
break;
// directx
case GraphicsAPI::DirectX:
2025-07-06 14:02:50 +03:30
lt_win(scope_gfx = create_scope<dxGraphicsContext>(windowHandle);
s_context = scope_gfx.get();
2025-07-05 13:28:41 +03:30
break;)
2025-07-05 16:07:51 +03:30
default
: lt_assert(
false,
"Invalid/unsupported 'GraphicsAPI' {}",
Stringifier::graphics_api_to_string(api)
);
2025-07-05 13:28:41 +03:30
return nullptr;
}
// create 'GraphicsContext' dependent classes
2025-07-05 16:07:51 +03:30
s_context->m_user_interface = UserInterface::create(windowHandle, s_context->m_shared_context);
2025-07-06 14:02:50 +03:30
s_context->m_renderer = Renderer::create(windowHandle, s_context->m_shared_context);
2025-07-05 13:28:41 +03:30
// check
2025-07-05 16:07:51 +03:30
lt_assert(s_context->m_user_interface, "Failed to create UserInterface");
lt_assert(s_context->m_renderer, "Failed to create renderer");
2025-07-05 13:28:41 +03:30
2025-07-06 14:02:50 +03:30
return std::move(scope_gfx);
2025-07-05 13:28:41 +03:30
}
} // namespace Light