Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraComponent.cpp
More file actions
Latest commit
112 lines (97 loc) · 3.82 KB
/
Copy pathCameraComponent.cpp
File metadata and controls
112 lines (97 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include"CameraComponent.h"
#include"DekiObject.h"
#include"DekiEngine.h"
#include"ICamera.h"
#include"ComponentInterfaceAdapters.h"
// ============================================================================
// Component Registration
// ============================================================================
// NOTE: s_Properties[] and s_ComponentMeta are now auto-generated in
// CameraComponent.gen.h (included at end of CameraComponent.h)
// Register ICamera adapter so editor can use FindInterface<ICamera>()
staticstructCameraInterfaceRegistrar {
CameraInterfaceRegistrar() {
Deki::ComponentInterfaceAdapters::Register(
Deki::ICamera::InterfaceID, CameraComponent::StaticType,
[](Deki::Component* c) -> void* {
returnstatic_cast<Deki::ICamera*>(static_cast<CameraComponent*>(c));
});
}
} s_cameraInterfaceReg;
// ============================================================================
CameraComponent::CameraComponent()
{
}
floatCameraComponent::GetPixelsPerMeter() const
{
if (pixelsPerMeter > 0.0f)
return pixelsPerMeter;
constfloat global = Deki::EngineSettings::Global().pixelsPerMeter;
return global > 0.0f ? global : 1.0f;
}
voidCameraComponent::SetPixelsPerMeter(float ppm)
{
pixelsPerMeter = (ppm > 0.0f) ? ppm : 0.0f;
}
floatCameraComponent::GetPositionX() const
{
Deki::Object* owner = GetOwner();
return owner ? owner->GetWorldX() : 0.0f;
}
floatCameraComponent::GetPositionY() const
{
Deki::Object* owner = GetOwner();
return owner ? owner->GetWorldY() : 0.0f;
}
floatCameraComponent::GetVisibleWidth(int32_t screenWidth) const
{
constfloat ppm = GetPixelsPerMeter();
return (ppm > 0.0f) ? (static_cast<float>(screenWidth) / ppm) : 0.0f;
}
floatCameraComponent::GetVisibleHeight(int32_t screenHeight) const
{
constfloat ppm = GetPixelsPerMeter();
return (ppm > 0.0f) ? (static_cast<float>(screenHeight) / ppm) : 0.0f;
}
FrameCamera CameraComponent::CaptureFrameCamera(int screenWidth, int screenHeight) const
{
// World: meters, center origin, Y UP (positive Y = up)
// Screen: top-left origin, Y down
// Camera position is the world point that maps to screen center.
//
// When pixelSnap is on, the camera's own contribution is rounded to
// whole pixels (cam_x_px = round(cam_x * ppm) / ppm) so smooth camera
// tweens / shake quantize at the camera level. The per-renderer
// pixelSnap still applies on top of this.
FrameCamera fc;
fc.ppm = GetPixelsPerMeter();
fc.camX = GetPositionX();
fc.camY = GetPositionY();
if (pixelSnap && fc.ppm > 0.0f)
{
fc.camX = std::round(fc.camX * fc.ppm) / fc.ppm;
fc.camY = std::round(fc.camY * fc.ppm) / fc.ppm;
}
fc.halfW = static_cast<float>(screenWidth) * 0.5f;
fc.halfH = static_cast<float>(screenHeight) * 0.5f;
fc.valid = true;
return fc;
}
voidCameraComponent::WorldToScreen(float worldX, float worldY,
int screenWidth, int screenHeight,
float& screenX, float& screenY) const
{
CaptureFrameCamera(screenWidth, screenHeight).WorldToScreen(worldX, worldY, screenX, screenY);
}
voidCameraComponent::ScreenToWorld(float screenX, float screenY,
int screenWidth, int screenHeight,
float& worldX, float& worldY) const
{
// Inverse of WorldToScreen.
constfloat ppm = GetPixelsPerMeter();
constfloat inv = (ppm > 0.0f) ? (1.0f / ppm) : 0.0f;
constfloat rel_x = (screenX - static_cast<float>(screenWidth) * 0.5f) * inv;
constfloat rel_y = (screenY - static_cast<float>(screenHeight) * 0.5f) * inv;
worldX = rel_x + GetPositionX();
worldY = -rel_y + GetPositionY(); // Negate Y: screen Y down -> world Y up
}