Cogs.Core
RenderFunctions.cpp
1#include "RenderFunctions.h"
2
3#include "Context.h"
4#include "DebugFunctions.h"
5#include "Engine.h"
6#include "ViewContext.h"
7
8#include "Services/DPIService.h"
9#include "Services/Time.h"
10
11#include "Renderer/IRenderer.h"
12
13#include "Systems/Core/CameraSystem.h"
14
15#include "Components/Core/CameraComponent.h"
16
17#include "Foundation/Platform/Mouse.h"
18
19using namespace Cogs::Core;
20
21CogsBool render(BridgeContext* ctx) {
22 Cogs::Core::Context* context = static_cast<Cogs::Core::Context *>(ctx);
23
24 CHECKED(context, context->engine->update());
25 return context->engine->needsUpdate();
26}
27
28void resize(BridgeContext* ctx, int x, int y) {
29 Cogs::Core::ViewContext* view = static_cast<Cogs::Core::Context*>(ctx)->getDefaultView();
30
31 if (view) {
32 resizeView(view, x, y);
33 }
34}
35
36void resizeView(BridgeView* bv, int x, int y) {
37 // rendering will fail to resume unless we now enforce minimum render target size which is checked in Engine.cpp
38 static_cast<Cogs::Core::ViewContext*>(bv)->setSize(std::max(x, 2), std::max(y, 2));
39}
40
41void setDPI(BridgeContext* ctx, int dpi, float scaleFactor) {
42 Cogs::Core::ViewContext* view = static_cast<Cogs::Core::Context*>(ctx)->getDefaultView();
43
44 if (view) {
45 setViewDPI(view, dpi, scaleFactor);
46 }
47}
48
49void setViewDPI(BridgeView* bv, int dpi, float scaleFactor) {
50 static_cast<Cogs::Core::ViewContext*>(bv)->setDPI(dpi, scaleFactor);
51}
52
53void setRenderMode(BridgeContext * ctx, int mode)
54{
55 auto context = static_cast<Context *>(ctx);
56
57 context->renderer->setMode(static_cast<RenderMode>(mode));
58 context->engine->setDirty();
59}
60
61void setBackgroundColor(BridgeContext * ctx, float * color)
62{
63 auto context = static_cast<Context *>(ctx);
64
65 context->renderer->setBackgroundColor(glm::vec4(color[0], color[1], color[2], color[3]));
66 context->engine->setDirty();
67}
68
69void * renderScreenshot(BridgeContext * ctx, const void * settingsPtr, uint32_t * stride)
70{
71 auto context = static_cast<Context *>(ctx);
72 auto settings = static_cast<const ScreenshotSettings *>(settingsPtr);
73
74 auto oldSize = context->renderer->getSize();
75
76 resize(ctx, settings->width, settings->height);
77
78 context->renderer->beginFrame();
79
80 context->time->update();
81 context->engine->updateSystems();
82 context->engine->preRender();
83
84 static std::vector<uint8_t> bytes;
85 context->renderer->renderScreenshot(*settings, bytes, stride);
86
88
89 resize(ctx, static_cast<int>(oldSize.x), static_cast<int>(oldSize.y));
90
91 return bytes.data();
92}
93
94int getEnginePermutationId(BridgeContext * ctx, const char * name)
95{
96 auto context = static_cast<Context *>(ctx);
97 return (int)context->renderer->getEnginePermutations().getIndex(name);
98}
99
100void setEnginePermutationProperty(BridgeContext * ctx, int id, const char * key, const void * value, int valueSize)
101{
102 auto context = static_cast<Context *>(ctx);
103 context->renderer->getEnginePermutations().get(id)->setProperty(key, value, valueSize);
104}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
class IRenderer * renderer
Renderer.
Definition: Context.h:228
std::unique_ptr< class Time > time
Time service instance.
Definition: Context.h:198
std::unique_ptr< class Engine > engine
Engine instance.
Definition: Context.h:222
virtual void endFrame(uint32_t syncInterval=0, Cogs::PresentFlags presentFlags=Cogs::PresentFlags::None)=0
Signals the end of the current frame.
virtual void renderScreenshot(const ScreenshotSettings &settings, std::vector< uint8_t > &bytes, uint32_t *stride)=0
Render screenshot.
virtual void setMode(RenderMode mode)=0
Set the rendering mode the renderer should use.
virtual void beginFrame()=0
Signals the beginning of a new frame.
virtual EnginePermutations & getEnginePermutations()=0
Get the reference to the EnginePermutations structure.
virtual glm::vec2 getSize() const =0
Get the output surface size of the renderer.
virtual void setBackgroundColor(const glm::vec4 &color)=0
Set the background color applied to the output surface before rendering.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
RenderMode
Defines global rendering modes that can be supported by the renderer.
Definition: IRenderer.h:37
@ NoSwap
Disables frame buffer swapping. This will leave the currently presented buffer as is.
Screenshot render settings.
Definition: IRenderer.h:28