Fission FSN
Loading...
Searching...
No Matches
fsNAxmolRenderView.h
Go to the documentation of this file.
1/********************************************************************
2*
3* File: fsNAxmolRenderView.h
4*
5* Purpose: Shared Axmol RenderView helpers.
6*
7*********************************************************************/
8
9#ifndef fsNAxmolRenderViewhdr
10#define fsNAxmolRenderViewhdr
11
12#ifdef AXMOL_V3
13 #include <platform/RenderViewCore.h>
14#else
15 #include <platform/RenderView.h>
16#endif
17
18#include <fsCore/fsBaseTypes.h>
19
21{
22#ifdef AXMOL_V3
23using fsTRenderView = ax::RenderViewCore;
24#else
25using fsTRenderView = ax::RenderView;
26#endif
27
29{
30 fsF32 x = 0.0f;
31 fsF32 y = 0.0f;
32 fsF32 w = 0.0f;
33 fsF32 h = 0.0f;
34};
35
36// Fit a target aspect ratio inside a container, centred (letterbox/pillarbox).
37inline fsSAspectFitRect aspectFitRectGet(const fsF32 pTargetAspect, const fsF32 pContainerW, const fsF32 pContainerH)
38{
40 rect.w = pContainerW;
41 rect.h = pContainerH;
42 if (pContainerW <= 0.0f || pContainerH <= 0.0f || pTargetAspect <= 0.0f)
43 {
44 return rect;
45 }
46
47 const fsF32 containerAspect = pContainerW / pContainerH;
48 if (containerAspect < pTargetAspect)
49 {
50 rect.h = pContainerW / pTargetAspect;
51 rect.y = (pContainerH - rect.h) / 2.0f;
52 }
53 else if (containerAspect > pTargetAspect)
54 {
55 rect.w = pContainerH * pTargetAspect;
56 rect.x = (pContainerW - rect.w) / 2.0f;
57 }
58 return rect;
59}
60
61// Maps a raw view-space point (points, y-down, origin top-left, full window including any
62// letterbox bars -- e.g. UIKit's Touch::getLocationInView()) into design-canvas space (y-up,
63// origin bottom-left, 0..pDesignW/0..pDesignH). Needed on iOS under fixedAspect, where
64// Director::convertToGL() maps raw points straight against getDesignResolutionSize() and so
65// mismaps outside the letterboxed fit rect; reuses the same aspectFitRectGet() fit rect as
66// fsCDisplay::clipWindowSetDo's scissor-rect fix.
67inline ax::Vec2 fixedAspectViewPointToDesignPointGet(fsTRenderView* const pRenderView,
68 const ax::Vec2& pViewPoint, const fsF32 pDesignW,
69 const fsF32 pDesignH)
70{
71 if (!pRenderView || pDesignW <= 0.0f || pDesignH <= 0.0f)
72 {
73 return pViewPoint;
74 }
75
76 const auto visibleSize = pRenderView->getVisibleSize();
77 const auto fit = aspectFitRectGet(pDesignW / pDesignH, visibleSize.width, visibleSize.height);
78 if (fit.w <= 0.0f || fit.h <= 0.0f)
79 {
80 return pViewPoint;
81 }
82
83 return ax::Vec2((pViewPoint.x - fit.x) / fit.w * pDesignW,
84 pDesignH - (pViewPoint.y - fit.y) / fit.h * pDesignH);
85}
86
87inline const ax::Rect& viewportRectGet(fsTRenderView* const pRenderView)
88{
89#ifdef AXMOL_V3
90 return pRenderView->getViewportRect();
91#else
92 return pRenderView->getViewPortRect();
93#endif
94}
95
96inline void pixelScaleGet(fsTRenderView* const pRenderView, fsF32& pScaleX, fsF32& pScaleY)
97{
98 pScaleX = 1.0f;
99 pScaleY = 1.0f;
100 if (!pRenderView)
101 {
102 return;
103 }
104
105#ifndef AXMOL_V3
106 // Desktop Axmol v2 reports frame/visible sizes in points, so their ratio
107 // can remain 1 even when the backing framebuffer is Retina-scaled.
108 const fsF32 retinaScale = static_cast<fsF32>(pRenderView->getRetinaFactor());
109 if (retinaScale > 0.0f)
110 {
111 pScaleX = retinaScale;
112 pScaleY = retinaScale;
113 }
114#endif
115
116 const auto visibleSize = pRenderView->getVisibleSize();
117#ifdef AXMOL_V3
118 const auto frameSize = pRenderView->getWindowSize();
119#else
120 const auto frameSize = pRenderView->getFrameSize();
121#endif
122 if (visibleSize.width > 0.0f && frameSize.width > 0.0f)
123 {
124 const fsF32 frameScaleX = static_cast<fsF32>(frameSize.width / visibleSize.width);
125 if (frameScaleX > pScaleX)
126 {
127 pScaleX = frameScaleX;
128 }
129 }
130 if (visibleSize.height > 0.0f && frameSize.height > 0.0f)
131 {
132 const fsF32 frameScaleY = static_cast<fsF32>(frameSize.height / visibleSize.height);
133 if (frameScaleY > pScaleY)
134 {
135 pScaleY = frameScaleY;
136 }
137 }
138
139#ifdef AXMOL_V3
140 if (pRenderView->getRenderScale() > 0.0f)
141 {
142 pScaleX = pRenderView->getRenderScale();
143 pScaleY = pRenderView->getRenderScale();
144 }
145#endif
146}
147}
148
149#endif
float fsF32
Definition fsBaseTypes.h:28
Definition fsNAxmolRenderView.h:21
ax::Vec2 fixedAspectViewPointToDesignPointGet(fsTRenderView *const pRenderView, const ax::Vec2 &pViewPoint, const fsF32 pDesignW, const fsF32 pDesignH)
Definition fsNAxmolRenderView.h:67
void pixelScaleGet(fsTRenderView *const pRenderView, fsF32 &pScaleX, fsF32 &pScaleY)
Definition fsNAxmolRenderView.h:96
ax::RenderView fsTRenderView
Definition fsNAxmolRenderView.h:25
const ax::Rect & viewportRectGet(fsTRenderView *const pRenderView)
Definition fsNAxmolRenderView.h:87
fsSAspectFitRect aspectFitRectGet(const fsF32 pTargetAspect, const fsF32 pContainerW, const fsF32 pContainerH)
Definition fsNAxmolRenderView.h:37
Definition fsNAxmolRenderView.h:29
fsF32 w
Definition fsNAxmolRenderView.h:32
fsF32 y
Definition fsNAxmolRenderView.h:31
fsF32 h
Definition fsNAxmolRenderView.h:33
fsF32 x
Definition fsNAxmolRenderView.h:30