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

82 lines
2.3 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 {
GraphicsContext *GraphicsContext::s_Context = nullptr;
GraphicsContext::~GraphicsContext()
{
}
Scope<GraphicsContext> GraphicsContext::create(GraphicsAPI api, GLFWwindow *windowHandle)
2025-07-05 13:28:41 +03:30
{
// terminate 'GraphicsContext' dependent classes
if (s_Context)
{
s_Context->m_renderer.reset();
s_Context->m_user_interface.reset();
2025-07-05 13:28:41 +03:30
delete s_Context;
}
// 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
Scope<GraphicsContext> scopeGfx;
switch (api)
{
// opengl
case GraphicsAPI::OpenGL:
scopeGfx = create_scope<glGraphicsContext>(windowHandle);
2025-07-05 13:28:41 +03:30
s_Context = scopeGfx.get();
break;
// directx
case GraphicsAPI::DirectX:
lt_win(scopeGfx = create_scope<dxGraphicsContext>(windowHandle); s_Context = scopeGfx.get();
2025-07-05 13:28:41 +03:30
break;)
default:
lt_assert(
2025-07-05 13:28:41 +03:30
false,
"Invalid/unsupported 'GraphicsAPI' {}",
Stringifier::graphics_api_to_string(api)
2025-07-05 13:28:41 +03:30
);
return nullptr;
}
// create 'GraphicsContext' dependent classes
s_Context->m_user_interface = UserInterface::create(windowHandle, s_Context->m_shared_context);
s_Context->m_renderer = renderer::create(windowHandle, s_Context->m_shared_context);
2025-07-05 13:28:41 +03:30
// check
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
return std::move(scopeGfx);
}
} // namespace Light