1#include "ViewContext.h"
3#include "Components/Behavior/OrbitingCameraController.h"
4#include "Input/InputManager.h"
5#include "Platform/Gamepad.h"
6#include "Renderer/IRenderer.h"
7#include "Renderer/RenderResources.h"
8#include "Renderer/RenderTexture.h"
9#include "Services/DPIService.h"
10#include "Resources/TextureManager.h"
11#include "Systems/Core/CameraSystem.h"
13#include "Rendering/IGraphicsDevice.h"
14#include "Rendering/ISwapChain.h"
16#include "Foundation/Logging/Logger.h"
28 , gamepadHandler(
std::make_unique<Platform::GamepadHandler>())
31 setWindowData(windowData);
34Cogs::Core::ViewContext::~ViewContext() {
44 if (OrbitingCameraController* occ = cam->getComponent<OrbitingCameraController>(); occ) {
45 occ->attachToView(
nullptr);
50 RenderResource* renderTexture = texture->getAttachedResource();
52 static_cast<RenderResources*
>(context->renderer->getResources())->destroyResource(renderTexture);
53 texture->attachResource(
nullptr);
56 if (swapChain && (swapChain != context->device->getDefaultSwapChain())) {
57 context->device->deleteSwapChain(swapChain);
65 swapChain = context->device->getDefaultSwapChain();
68 swapChain = context->device->createSwapChain(refWindowData());
70 LOG_WARNING(logger,
"Swapchain creation failed, falling back to the default one.");
71 swapChain = context->device->getDefaultSwapChain();
75 texture = context->textureManager->create();
77 texture->description.format = context->device->getSettings().colorFormat;
80 renderTexture->setPersistent();
81 renderTexture->setOwned();
82 renderTexture->setResource(texture.resolve());
83 renderTexture->swapChain = swapChain;
85 texture->attachResource(renderTexture);
88 setCamera(renderCamera);
92 inputManager->initialize();
94 refGestures().setDisplayScale(dpiService->getScaleFactor());
95 gamepadHandler->initialize(
this);
102 if (refWindowData()) {
103 inputManager->update();
108 if (swapChain && texture) {
109 texture->description.width = swapChain->getWidth();
110 texture->description.height = swapChain->getHeight();
115 camera = renderCamera;
116 texture->getAttachedResource()->setName(
"ViewTexture (" + renderCamera->getName() +
")");
123 occ->attachToView(
this);
129 if (context->engine->isReady()) {
131 swapChain->setSize(newWidth, newHeight);
133 else if ((context->getDefaultView() ==
this) && context->device) {
134 context->device->setSize(newWidth, newHeight);
137 glm::vec2 vpSize(newWidth, newHeight);
148 attachment->incrementGeneration();
152 for (ResizeCallback* cb : resizeCallbacks) {
153 cb(
this, newWidth, newHeight);
156 context->engine->triggerUpdate();
159void Cogs::Core::ViewContext::setDPI(
int newDPI,
float scaleFactor) {
160 dpiService->set(newDPI, scaleFactor);
162 context->engine->triggerUpdate();
165glm::vec2 Cogs::Core::ViewContext::getSize()
const {
167 return glm::vec2(swapChain->getWidth(), swapChain->getHeight());
172 if (context->device->getSize(width, height)) {
173 return glm::vec2(width, height);
175 return glm::vec2(0.0f);
180 return camera.lock();
188void Cogs::Core::ViewContext::addResizeCallback(ResizeCallback* callback) {
189 auto e = resizeCallbacks.end();
191 if (std::find(resizeCallbacks.begin(), e, callback) == e) {
192 resizeCallbacks.push_back(callback);
196void Cogs::Core::ViewContext::removeResizeCallback(ResizeCallback* callback) {
197 auto e = resizeCallbacks.end();
198 auto i = std::find(resizeCallbacks.begin(), e, callback);
201 resizeCallbacks.erase(i);
void setChanged()
Sets the component to the ComponentFlags::Changed state with carry.
ComponentType * getComponent() const
glm::vec2 viewportSize
Size of the viewport covered by this instance, given in pixels.
TextureHandle renderTexture
The render texture to output the rendered scene from the camera to.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Component that calculates position and orientation of the entity TransformComponent.
Contains render resources used by the renderer.
CameraComponent * getCameraComponent() const
Utility to get the CameraComponent from.
void update()
Updates the components in this view. Called once per frame from the owning Context's update call.
Cogs::Core::EntityPtr getCamera() const
Gets view camera. The camera must be valid when rendering a frame in the view.
void preRender()
Called by the owning Context during its preRender call.
void setSize(int newWidth, int newHeight)
Resizes this view.
ViewContext(Context *ctx, WindowData *windowData)
Constructs a new ViewContext instance.
void setCamera(const Cogs::Core::EntityPtr &renderCamera)
Sets or updates the camera of this ViewContext instance.
bool initialize(const Cogs::Core::EntityPtr &renderCamera)
Initialises the standard components in this ViewContext instance.
Log implementation class.
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.
@ RenderTarget
The texture can be used as a render target and drawn into.
@ Texture
Texture usage, see Default.