创建跨平台c ++触摸管理器。在c ++中传递Objective-c对象涉及代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建跨平台c ++触摸管理器。在c ++中传递Objective-c对象涉及代码相关的知识,希望对你有一定的参考价值。
所以我正在尝试创建一个可以跨平台(ios,android等)使用的多点触控类。目标是引擎不需要知道平台以获得更新的触摸数据。
现在我正在进行一般设计,并且专门实现iOS方面。 Android等方面将会更晚。
为了做到这一点,我试图使用委托模式。实际上只有两个接口项。
有一个方法更新(浮点dt)用dt在每个新帧上调用。然后,此方法将返回有关所有触摸的信息的c ++结构,或者可能设置可以访问的属性。我不确定。
在iOS端,唯一需要从外部调用的其他函数是touchesDown,它传入UITouches的NSSet *和它们所在的UIView。
这是我到目前为止所使用的设计
Multitouch
PlatformTouchManager.h (C++ Abstract Class with Update)
iOS
iOSMultiTouch.cpp (Objective-c++)
iOSMultiTouch.hpp (Objective-c++ Class inheriting PlatformTouchManager)
MultTouch.cpp (C++)
MultiTouch.h (C++ class)
这是PlatformTouchManager.h
#define MAX_TOUCHES 5
#define PLATFORM_iOS
//#define PLATFORM_ANDROID
class PlatformTouchManager {
public:
// Update All The Events
virtual void update(float time) = 0;
};
当然还有Multitouch.h
class Multitouch {
private:
PlatformTouchManager* manager;
public:
Multitouch() {
#ifdef PLATFORM_iOS
manager = new iOSMultiTouch();
#endif
}
~Multitouch() {
delete manager;
}
Multitouch(const Multitouch&) = delete;
void update(float dt);
void* getManager() {
return manager;
}
};
最后是iOSMultiTouch.hpp
#include <stdio.h>
#include "../PlatformTouchManager.h"
#include <set>
#import <UIKit/UIKit.h>
class iOSMultiTouch: public PlatformTouchManager {
public:
// Initializer
iOSMultiTouch();
// Destructor
~iOSMultiTouch();
// Update All The Events
void update(float time);
// Touch Down Set
void touchesDown(id<NSSet> set, id<UIView> view);
};
值得注意的是,此代码包含在MultiTouch.h(c ++)中,而我的本机视图代码是objective-c ++。
因此,跨平台引擎创建了一个Multitouch对象,保持引用并且每个帧调用更新。
我想像iOS UIView将从跨平台引擎获得Multitouch*
。将其投射到iOSMultiTouch*
对象并在代表上调用touchesDown:(NSSet *)touches withEvent:(IUView *)view
。
我这样做有一个严重的问题。
如果我尝试在iOSMUltiTouch.hpp中导入UIKit,则会导致构建混乱,因此看起来我只能在源文件中导入它。因此我的降落方法需要无效touchesDown(void* touches, void* view);.
问题在于实现这一点,因为当我尝试将这些void*
指针转换为正确的类型时,我得到警告Cast of C pointer type void* to objective-c pointer type id requires a bridged cast
。
我知道这里有一些ARC问题,因为ARC在转换成原始指针时无法跟踪这些事情,但我该如何解决这个问题呢?
iOSTouchManager确实需要保持对UITouch对象的强引用,直到释放触摸的帧之后。
我该如何实现这个演员?有没有不同的方法我应该设计我的类来使这更容易?
对于那些想知道这些是弹出的错误你应该从iOSMultiTouch.hpp导入uikit的人
Parse Issue
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:512:1: Expected unqualified-id
../Multitouch/MultiTouch.cpp:9:10: In file included from ../Multitouch/MultiTouch.cpp:9:
../Multitouch/MultiTouch.h:15:10: In file included from ../Multitouch/MultiTouch.h:15:
../Multitouch/iOS/iOSMultiTouch.hpp:15:9: In file included from ../Multitouch/iOS/iOSMultiTouch.hpp:15:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:9:1: Expected unqualified-id
../Multitouch/MultiTouch.cpp:9:10: In file included from ../Multitouch/MultiTouch.cpp:9:
../Multitouch/MultiTouch.h:15:10: In file included from ../Multitouch/MultiTouch.h:15:
../Multitouch/iOS/iOSMultiTouch.hpp:15:9: In file included from ../Multitouch/iOS/iOSMultiTouch.hpp:15:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:
Semantic Issues
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:514:9: Unknown type name 'NSString'
../Multitouch/MultiTouch.cpp:9:10: In file included from ../Multitouch/MultiTouch.cpp:9:
../Multitouch/MultiTouch.h:15:10: In file included from ../Multitouch/MultiTouch.h:15:
../Multitouch/iOS/iOSMultiTouch.hpp:15:9: In file included from ../Multitouch/iOS/iOSMultiTouch.hpp:15:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
问题
你有很多错误,因为你在C ++文件(UIKit
)中导入了iOS框架(iOSMultiTouch.cpp
)。
解
对于需要导入iOS框架的实现文件,您必须使用.mm
而不是.cpp
扩展。 .cpp
是C ++扩展,而不是Objective-C ++扩展。
在这种情况下,您需要将iOSMultiTouch.cpp
重命名为iOSMultiTouch.mm
。用Objective-C指针替换void *
,它将按预期工作。
Utils.h
将保留与Objective-C无关的任何内容,并且可以在任何地方使用。 Utils.mm
用于处理Objective-C部件。
Utils.h
PlatformTouchManager* GetIOSTouchManager();
u替LS.妈妈
#include "Utils.h"
#include "iOSMultiTouch.hpp"
PlatformTouchManager* GetIOSTouchManager() {
return new iOSMultiTouch();
};
Multitouch.h
#include "Utils.h"
// Other include
class Multitouch {
private:
PlatformTouchManager* manager;
public:
Multitouch() {
#ifdef PLATFORM_iOS
manager = GetIOSTouchManager();
#endif
}
~Multitouch() {
delete manager;
}
Multitouch(const Multitouch&) = delete;
void update(float dt);
PlatformTouchManager* getManager() {
return manager;
}
};
以上是关于创建跨平台c ++触摸管理器。在c ++中传递Objective-c对象涉及代码的主要内容,如果未能解决你的问题,请参考以下文章