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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import "Common.h"
#import "EAGLView.h"
#import "../Categories/UIKitCategories.h"
#include "Framework.h"
#ifndef USE_DRAPE
#include "RenderBuffer.hpp"
#include "RenderContext.hpp"
#include "../../graphics/resource_manager.hpp"
#include "../../graphics/opengl/opengl.hpp"
#include "../../graphics/data_formats.hpp"
#include "../../indexer/classificator_loader.hpp"
#else
#import "../Platform/opengl/iosOGLContextFactory.h"
#endif
#include "../../render/render_policy.hpp"
#include "../../platform/platform.hpp"
#include "../../platform/video_timer.hpp"
#include "../../std/bind.hpp"
@implementation EAGLView
// You must implement this method
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
// The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder *)coder
{
NSLog(@"EAGLView initWithCoder Started");
if ((self = [super initWithCoder:coder]))
{
// Setup Layer Properties
CAEAGLLayer * eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = YES;
// ColorFormat : RGB565
// Backbuffer : YES, (to prevent from loosing content when mixing with ordinary layers).
eaglLayer.drawableProperties = @{kEAGLDrawablePropertyRetainedBacking : @NO, kEAGLDrawablePropertyColorFormat : kEAGLColorFormatRGB565};
// Correct retina display support in opengl renderbuffer
self.contentScaleFactor = [self correctContentScale];
#ifndef USE_DRAPE
renderContext = shared_ptr<iphone::RenderContext>(new iphone::RenderContext());
if (!renderContext.get())
{
NSLog(@"EAGLView initWithCoder Error");
return nil;
}
renderContext->makeCurrent();
typedef void (*drawFrameFn)(id, SEL);
SEL drawFrameSel = @selector(drawFrame);
drawFrameFn drawFrameImpl = (drawFrameFn)[self methodForSelector:drawFrameSel];
videoTimer = CreateIOSVideoTimer(bind(drawFrameImpl, self, drawFrameSel));
#else
dp::ThreadSafeFactory * factory = new dp::ThreadSafeFactory(new iosOGLContextFactory(eaglLayer));
m_factory.Reset(factory);
#endif
}
NSLog(@"EAGLView initWithCoder Ended");
return self;
}
- (void)initRenderPolicy
{
NSLog(@"EAGLView initRenderPolicy Started");
#ifndef USE_DRAPE
graphics::ResourceManager::Params rmParams;
rmParams.m_videoMemoryLimit = GetPlatform().VideoMemoryLimit();
rmParams.m_texFormat = graphics::Data4Bpp;
RenderPolicy::Params rpParams;
UIScreen * screen = [UIScreen mainScreen];
CGRect screenRect = screen.bounds;
double vs = self.contentScaleFactor;
rpParams.m_screenWidth = screenRect.size.width * vs;
rpParams.m_screenHeight = screenRect.size.height * vs;
rpParams.m_skinName = "basic.skn";
if (vs == 1.0)
rpParams.m_density = graphics::EDensityMDPI;
else if (vs > 2.0)
rpParams.m_density = graphics::EDensityIPhone6Plus;
else
rpParams.m_density = graphics::EDensityXHDPI;
rpParams.m_videoTimer = videoTimer;
rpParams.m_useDefaultFB = false;
rpParams.m_rmParams = rmParams;
rpParams.m_primaryRC = renderContext;
try
{
renderPolicy = CreateRenderPolicy(rpParams);
}
catch (graphics::gl::platform_unsupported const & )
{
/// terminate program (though this situation is unreal :) )
}
frameBuffer = renderPolicy->GetDrawer()->Screen()->frameBuffer();
Framework & f = GetFramework();
f.SetRenderPolicy(renderPolicy);
f.InitGuiSubsystem();
#else
CGRect frameRect = [UIScreen mainScreen].applicationFrame;
GetFramework().CreateDrapeEngine(m_factory.GetRefPointer(), self.contentScaleFactor, frameRect.size.width, frameRect.size.height);
#endif
NSLog(@"EAGLView initRenderPolicy Ended");
}
- (void)setMapStyle:(MapStyle)mapStyle
{
Framework & f = GetFramework();
if (f.GetMapStyle() == mapStyle)
return;
NSLog(@"EAGLView setMapStyle Started");
renderContext->makeCurrent();
/// drop old render policy
f.SetRenderPolicy(nullptr);
frameBuffer.reset();
f.SetMapStyle(mapStyle);
/// init new render policy
[self initRenderPolicy];
/// restore render policy screen
CGFloat const scale = self.contentScaleFactor;
CGSize const s = self.bounds.size;
[self onSize:s.width * scale withHeight:s.height * scale];
/// update framework
f.SetUpdatesEnabled(true);
NSLog(@"EAGLView setMapStyle Ended");
}
- (void)onSize:(int)width withHeight:(int)height
{
#ifndef USE_DRAPE
frameBuffer->onSize(width, height);
graphics::Screen * screen = renderPolicy->GetDrawer()->Screen();
/// free old render buffer, as we would not create a new one.
screen->resetRenderTarget();
screen->resetDepthBuffer();
renderBuffer.reset();
/// detaching of old render target will occur inside beginFrame
screen->beginFrame();
screen->endFrame();
/// allocate the new one
renderBuffer.reset();
renderBuffer.reset(new iphone::RenderBuffer(renderContext, (CAEAGLLayer*)self.layer));
screen->setRenderTarget(renderBuffer);
screen->setDepthBuffer(make_shared<graphics::gl::RenderBuffer>(width, height, true));
#endif
GetFramework().OnSize(width, height);
#ifndef USE_DRAPE
screen->beginFrame();
screen->clear(graphics::Screen::s_bgColor);
screen->endFrame();
#endif
}
- (double)correctContentScale
{
UIScreen * uiScreen = [UIScreen mainScreen];
if (isIOSVersionLessThan(8))
return uiScreen.scale;
else
return uiScreen.nativeScale;
}
#ifndef USE_DRAPE
- (void)drawFrame
{
shared_ptr<PaintEvent> pe(new PaintEvent(renderPolicy->GetDrawer().get()));
Framework & f = GetFramework();
if (f.NeedRedraw())
{
f.SetNeedRedraw(false);
f.BeginPaint(pe);
f.DoPaint(pe);
renderBuffer->present();
f.EndPaint(pe);
}
}
#endif
- (void)layoutSubviews
{
if (!CGRectEqualToRect(lastViewSize, self.frame))
{
lastViewSize = self.frame;
#ifndef USE_DRAPE
CGFloat const scale = self.contentScaleFactor;
CGSize const s = self.bounds.size;
[self onSize:s.width * scale withHeight:s.height * scale];
#else
CGSize const s = self.bounds.size;
[self onSize:s.width withHeight:s.height];
#endif
}
}
- (void)dealloc
{
#ifndef USE_DRAPE
delete videoTimer;
[EAGLContext setCurrentContext:nil];
#else
GetFramework().PrepareToShutdown();
m_factory.Destroy();
#endif
}
- (CGPoint)viewPoint2GlobalPoint:(CGPoint)pt
{
CGFloat const scaleFactor = self.contentScaleFactor;
m2::PointD const ptG = GetFramework().PtoG(m2::PointD(pt.x * scaleFactor, pt.y * scaleFactor));
return CGPointMake(ptG.x, ptG.y);
}
- (CGPoint)globalPoint2ViewPoint:(CGPoint)pt
{
CGFloat const scaleFactor = self.contentScaleFactor;
m2::PointD const ptP = GetFramework().GtoP(m2::PointD(pt.x, pt.y));
return CGPointMake(ptP.x / scaleFactor, ptP.y / scaleFactor);
}
@end
|