4#import <Foundation/Foundation.h>
12#import <Metal/Metal.h>
13#import <MetalKit/MetalKit.h>
15#import <MobileCoreServices/MobileCoreServices.h>
16#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
28#include "imgui/backends/imgui_impl_metal.h"
29#include "imgui/imgui.h"
37- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil
38 bundle:(nullable NSBundle *)nibBundleOrNil {
39 self = [
super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
41 _device = MTLCreateSystemDefaultDevice();
42 _commandQueue = [
_device newCommandQueue];
45 NSLog(
@"Metal is not supported");
53 ImGui::CreateContext();
54 ImGuiIO &io = ImGui::GetIO();
56 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
57 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
62 SDL_iOSSetEventPump(SDL_TRUE);
63 int argc = NSProcessInfo.processInfo.arguments.count;
64 char **argv =
new char *[
argc];
65 for (
int i = 0; i < argc; i++) {
66 NSString *arg = NSProcessInfo.processInfo.arguments[
i];
67 const char *cString = [
arg UTF8String];
68 argv[
i] =
new char[
strlen(cString) + 1];
69 strcpy(argv[i], cString);
72 std::string rom_filename =
"";
74 rom_filename = argv[1];
76 SDL_iOSSetEventPump(SDL_FALSE);
79 SDL_SetHint(SDL_HINT_IME_SHOW_UI,
"1");
81 ImGui_ImplSDL2_InitForSDLRenderer(_controller->window(),
85 if (!LoadPackageFonts().ok()) {
88 _controller->set_active(
true);
90 _hoverGestureRecognizer =
92 [
self.
view addGestureRecognizer:_hoverGestureRecognizer];
95 action:@selector(HandlePinch:)];
96 [
self.
view addGestureRecognizer:_pinchRecognizer];
98 _longPressRecognizer =
100 [
self.
view addGestureRecognizer:_longPressRecognizer];
103 action:@selector(HandleSwipe:)];
104 _swipeRecognizer.direction =
105 UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
106 [
self.
view addGestureRecognizer:_swipeRecognizer];
110- (MTKView *)mtkView {
111 return (MTKView *)self.view;
115 self.view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 1200, 720)];
121 self.mtkView.device = self.device;
122 self.mtkView.delegate = self;
125 ImGui_ImplOSX_Init(self.view);
126 [NSApp activateIgnoringOtherApps:YES];
130- (void)drawInMTKView:(MTKView *)view {
131 ImGuiIO &io = ImGui::GetIO();
132 io.DisplaySize.x = view.bounds.size.width;
133 io.DisplaySize.y = view.bounds.size.height;
136 CGFloat framebufferScale =
137 view.window.screen.backingScaleFactor ?: NSScreen.mainScreen.backingScaleFactor;
139 CGFloat framebufferScale = view.window.screen.scale ?: UIScreen.mainScreen.scale;
141 io.DisplayFramebufferScale = ImVec2(framebufferScale, framebufferScale);
143 ImGui_ImplSDLRenderer2_NewFrame();
144 ImGui_ImplSDL2_NewFrame();
146 ImGui_ImplOSX_NewFrame(view);
150 ImGui::SetNextWindowPos(ImVec2(0, 0));
151 ImVec2 dimensions(io.DisplaySize.x, io.DisplaySize.y);
152 ImGui::SetNextWindowSize(dimensions, ImGuiCond_Always);
153 if (ImGui::Begin(
"##YazeMain",
nullptr,
154 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse |
155 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_MenuBar |
156 ImGuiWindowFlags_NoBringToFrontOnFocus)) {
157 auto controller_status = _controller->OnLoad();
158 if (!controller_status.ok()) {
163 _controller->DoRender();
166- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
175- (void)viewWillAppear {
176 [
super viewWillAppear];
177 self.view.window.delegate = self;
180- (void)windowWillClose:(NSNotification *)notification {
181 ImGui_ImplMetal_Shutdown();
182 ImGui_ImplOSX_Shutdown();
183 ImGui::DestroyContext();
193- (void)UpdateIOWithTouchEvent:(UIEvent *)event {
194 UITouch *anyTouch =
event.allTouches.anyObject;
195 CGPoint touchLocation = [anyTouch locationInView:
self.view];
196 ImGuiIO &io = ImGui::GetIO();
197 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
198 io.AddMousePosEvent(touchLocation.x, touchLocation.y);
200 BOOL hasActiveTouch = NO;
201 for (UITouch *touch in event.allTouches) {
202 if (touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled) {
203 hasActiveTouch = YES;
207 io.AddMouseButtonEvent(0, hasActiveTouch);
210- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
211 [
self UpdateIOWithTouchEvent:event];
213- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
214 [
self UpdateIOWithTouchEvent:event];
216- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
217 [
self UpdateIOWithTouchEvent:event];
219- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
220 [
self UpdateIOWithTouchEvent:event];
223- (void)HoverGesture:(UIHoverGestureRecognizer *)gesture {
224 ImGuiIO &io = ImGui::GetIO();
225 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
227 UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer *)gesture;
228 if (gesture.zOffset < 0.50) {
229 io.AddMousePosEvent([gestureRecognizer locationInView:self.view].x,
230 [gestureRecognizer locationInView:self.view].y);
234- (void)HandlePinch:(UIPinchGestureRecognizer *)gesture {
235 ImGuiIO &io = ImGui::GetIO();
236 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
237 io.AddMouseWheelEvent(0.0f, gesture.scale);
240- (void)HandleSwipe:(UISwipeGestureRecognizer *)gesture {
241 ImGuiIO &io = ImGui::GetIO();
242 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
243 if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
244 io.AddMouseWheelEvent(1.0f, 0.0f);
245 }
else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
246 io.AddMouseWheelEvent(-1.0f, 0.0f);
250- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
251 ImGuiIO &io = ImGui::GetIO();
252 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
253 io.AddMouseButtonEvent(1, gesture.state == UIGestureRecognizerStateBegan);
266@interface AppDelegate : NSObject <NSApplicationDelegate>
267@property(nonatomic, strong) NSWindow *window;
270@implementation AppDelegate
272- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
276- (instancetype)init {
277 if (self = [super init]) {
278 NSViewController *rootViewController = [[
AppViewController alloc] initWithNibName:nil
280 self.window = [[NSWindow alloc]
281 initWithContentRect:NSZeroRect
282 styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
283 NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable
284 backing:NSBackingStoreBuffered
286 self.window.contentViewController = rootViewController;
287 [
self.window center];
288 [
self.window makeKeyAndOrderFront:
self];
299- (BOOL)application:(UIApplication *)application
300 didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,
id> *)launchOptions {
302 self.window = [[
UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
303 self.window.rootViewController = rootViewController;
304 [
self.
window makeKeyAndVisible];
308- (void)applicationWillTerminate:(UIApplication *)application {
309 ImGui_ImplSDLRenderer2_Shutdown();
310 ImGui_ImplSDL2_Shutdown();
311 ImGui::DestroyContext();
315- (void)PresentDocumentPickerWithCompletionHandler:
316 (
void (^)(NSString *selectedFile))completionHandler {
317 self.completionHandler = completionHandler;
319 NSArray *documentTypes = @[ [UTType typeWithIdentifier:@"org.halext.sfc"] ];
320 UIViewController *rootViewController = self.window.rootViewController;
322 [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:documentTypes];
323 _documentPicker.delegate = self;
324 _documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
326 [rootViewController presentViewController:_documentPicker animated:YES completion:nil];
329- (void)documentPicker:(UIDocumentPickerViewController *)controller
330 didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
331 NSURL *selectedFileURL = [urls firstObject];
333 if (self.completionHandler) {
334 if (selectedFileURL) {
335 self.completionHandler(selectedFileURL.path);
336 std::string fileName = std::string([selectedFileURL.path UTF8String]);
339 [selectedFileURL startAccessingSecurityScopedResource];
341 auto data = [NSData dataWithContentsOfURL:selectedFileURL];
343 uint8_t *bytes = (uint8_t *)[data bytes];
345 size_t size = [data length];
347 std::vector<uint8_t> rom_data;
348 rom_data.resize(size);
349 std::copy(bytes, bytes + size, rom_data.begin());
353 std::string filename = std::string([selectedFileURL.path UTF8String]);
355 [selectedFileURL stopAccessingSecurityScopedResource];
358 self.completionHandler(
@"");
361 self.completionHandler = nil;
362 [controller dismissViewControllerAnimated:YES completion:nil];
365- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
366 if (self.completionHandler) {
367 self.completionHandler(
@"");
369 self.completionHandler = nil;
370 [controller dismissViewControllerAnimated:YES completion:nil];
382int main(
int argc,
const char *argv[]) {
return NSApplicationMain(argc, argv); }
385int main(
int argc,
char *argv[]) {
387 return UIApplicationMain(argc, argv, nil, NSStringFromClass([
AppDelegate class]));
Main controller for the application.
int main(int argc, char **argv)