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 static_cast<Cogs::Core::ViewContext*>(bv)->setSize(x, y);
38}
39
40void setDPI(BridgeContext* ctx, int dpi, float scaleFactor) {
41 Cogs::Core::ViewContext* view = static_cast<Cogs::Core::Context*>(ctx)->getDefaultView();
42
43 if (view) {
44 setViewDPI(view, dpi, scaleFactor);
45 }
46}
47
48void setViewDPI(BridgeView* bv, int dpi, float scaleFactor) {
49 static_cast<Cogs::Core::ViewContext*>(bv)->setDPI(dpi, scaleFactor);
50}
51
52void setRenderMode(BridgeContext * ctx, int mode)
53{
54 auto context = static_cast<Context *>(ctx);
55
56 context->renderer->setMode(static_cast<RenderMode>(mode));
57 context->engine->setDirty();
58}
59
60void setBackgroundColor(BridgeContext * ctx, float * color)
61{
62 auto context = static_cast<Context *>(ctx);
63
64 context->renderer->setBackgroundColor(glm::vec4(color[0], color[1], color[2], color[3]));
65 context->engine->setDirty();
66}
67
68void * renderScreenshot(BridgeContext * ctx, const void * settingsPtr, uint32_t * stride)
69{
70 auto context = static_cast<Context *>(ctx);
71 auto settings = static_cast<const ScreenshotSettings *>(settingsPtr);
72
73 auto oldSize = context->renderer->getSize();
74
75 resize(ctx, settings->width, settings->height);
76
77 context->renderer->beginFrame();
78
79 context->time->update();
80 context->engine->updateSystems();
81 context->engine->preRender();
82
83 static std::vector<uint8_t> bytes;
84 context->renderer->renderScreenshot(*settings, bytes, stride);
85
87
88 resize(ctx, static_cast<int>(oldSize.x), static_cast<int>(oldSize.y));
89
90 return bytes.data();
91}
92
93int getEnginePermutationId(BridgeContext * ctx, const char * name)
94{
95 auto context = static_cast<Context *>(ctx);
96 return (int)context->renderer->getEnginePermutations().getIndex(name);
97}
98
99void setEnginePermutationProperty(BridgeContext * ctx, int id, const char * key, const void * value, int valueSize)
100{
101 auto context = static_cast<Context *>(ctx);
102 context->renderer->getEnginePermutations().get(id)->setProperty(key, value, valueSize);
103}
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, uint32_t 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
Screenshot render settings.
Definition: IRenderer.h:28
@ NoSwap
Disables frame buffer swapping. This will leave the currently presented buffer as is.
Definition: Common.h:181