summaryrefslogtreecommitdiffstats
path: root/NativeApp/Apple/Source/Classes/iOS/AppViewBase.mm
blob: 3e75910c05e1440a06ebbf48ac97cd80f7b2ca36 (plain)
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
#import "AppViewBase.h"

#include "NativeAppBase.hpp"
#include <memory>
#include <string>

@interface AppViewBase ()
{
    std::unique_ptr<Diligent::NativeAppBase> _theApp;
    NSInteger _preferredFramesPerSecond;
    CADisplayLink* _displayLink;
    std::string _error;
}
@end

@implementation AppViewBase

- (void) initApp:(int)deviceType;
{
    try
    {
        _theApp.reset(Diligent::CreateApplication());
        // Init our renderer.
        _theApp->Initialize(deviceType, (__bridge void*)self.layer);
    }
    catch(std::runtime_error &err)
    {
        _error = err.what();
        _theApp.reset();
    }

    [super stopAnimation];
    _preferredFramesPerSecond = 60;
    _displayLink = nil;
}

- (void) render
{
    if(_theApp)
    {
        _theApp->Update();
        _theApp->Render();
        _theApp->Present();
    }
}

- (void) layoutSubviews
{
    auto bounds = [self.layer bounds];
    auto scale = [self.layer contentsScale];

    if(_theApp)
    {
        _theApp->WindowResize(bounds.size.width * scale, bounds.size.height * scale);
    }
}

- (NSInteger) preferredFramesPerSecond
{
	return _preferredFramesPerSecond;
}

- (void) setPreferredFramesPerSecond:(NSInteger)preferredFPS
{
	if (preferredFPS >= 1)
	{
		_preferredFramesPerSecond = preferredFPS;

		if (self.animating)
		{
			[self stopAnimation];
			[self startAnimation];
		}
	}
}

- (void) startAnimation
{
	if (!self.animating)
	{
        // Create the display link and set the callback to our drawView method
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView:)];

        // Set it to our _animationFrameInterval
        [_displayLink setPreferredFramesPerSecond:_preferredFramesPerSecond];

        // Have the display link run on the default run loop (and the main thread)
        [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

        [super startAnimation];
	}
}

- (void)stopAnimation
{
	if (self.animating)
	{
        [_displayLink invalidate];
        _displayLink = nil;
        [super stopAnimation];
	}
}

- (void)terminate
{
    _theApp.reset();
    [super terminate];
}

- (NSString*)getError
{
    return _error.empty() ? nil : [NSString stringWithFormat:@"%s", _error.c_str()];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches
           withEvent:(UIEvent *)event;
{
    UITouch *firstTouch = touches.allObjects[0];
    CGPoint location = [firstTouch locationInView:self];
    if(_theApp)
    {
        _theApp->OnTouchBegan(location.x, location.y);
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches
           withEvent:(UIEvent *)event;
{
    UITouch *firstTouch = touches.allObjects[0];
    CGPoint location = [firstTouch locationInView:self];
    if(_theApp)
    {
        _theApp->OnTouchMoved(location.x, location.y);
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches
           withEvent:(UIEvent *)event;
{
    UITouch *firstTouch = touches.allObjects[0];
    CGPoint location = [firstTouch locationInView:self];
    if(_theApp)
    {
        _theApp->OnTouchEnded(location.x, location.y);
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches
               withEvent:(UIEvent *)event;
{
    UITouch *firstTouch = touches.allObjects[0];
    CGPoint location = [firstTouch locationInView:self];
    if(_theApp)
    {
        _theApp->OnTouchEnded(location.x, location.y);
    }
}

@end