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");
52 SDL_iOSSetEventPump(SDL_TRUE);
53 int argc = NSProcessInfo.processInfo.arguments.count;
54 char **argv =
new char *[argc];
55 for (
int i = 0; i < argc; i++) {
56 NSString *arg = NSProcessInfo.processInfo.arguments[i];
57 const char *cString = [arg UTF8String];
58 argv[i] =
new char[strlen(cString) + 1];
59 strcpy(argv[i], cString);
62 std::string rom_filename =
"";
64 rom_filename = argv[1];
66 SDL_iOSSetEventPump(SDL_FALSE);
69 SDL_SetHint(SDL_HINT_IME_SHOW_UI,
"1");
70 if (!_controller->CreateWindow().ok()) {
71 printf(
"Error creating window: %s\n", SDL_GetError());
74 if (!_controller->CreateRenderer().ok()) {
75 printf(
"Error creating renderer: %s\n", SDL_GetError());
81 ImGui::CreateContext();
82 ImGuiIO &io = ImGui::GetIO();
84 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
85 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
89 ImGui_ImplSDL2_InitForSDLRenderer(_controller->window(), _controller->renderer());
90 ImGui_ImplSDLRenderer2_Init(_controller->renderer());
92 if (!_controller->LoadFontFamilies().ok()) {
95 _controller->SetupScreen(rom_filename);
96 _controller->set_active(
true);
98 _hoverGestureRecognizer =
99 [[UIHoverGestureRecognizer alloc] initWithTarget:self action:@selector(HoverGesture:)];
100 [
self.view addGestureRecognizer:_hoverGestureRecognizer];
102 _pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self
103 action:@selector(HandlePinch:)];
104 [
self.view addGestureRecognizer:_pinchRecognizer];
106 _longPressRecognizer =
107 [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
108 [
self.view addGestureRecognizer:_longPressRecognizer];
110 _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
111 action:@selector(HandleSwipe:)];
112 _swipeRecognizer.direction =
113 UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
114 [
self.view addGestureRecognizer:_swipeRecognizer];
118- (MTKView *)mtkView {
119 return (MTKView *)self.view;
123 self.view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 1200, 720)];
129 self.mtkView.device = self.device;
130 self.mtkView.delegate = self;
133 ImGui_ImplOSX_Init(self.view);
134 [NSApp activateIgnoringOtherApps:YES];
138- (void)drawInMTKView:(MTKView *)view {
139 ImGuiIO &io = ImGui::GetIO();
140 io.DisplaySize.x = view.bounds.size.width;
141 io.DisplaySize.y = view.bounds.size.height;
144 CGFloat framebufferScale =
145 view.window.screen.backingScaleFactor ?: NSScreen.mainScreen.backingScaleFactor;
147 CGFloat framebufferScale = view.window.screen.scale ?: UIScreen.mainScreen.scale;
149 io.DisplayFramebufferScale = ImVec2(framebufferScale, framebufferScale);
151 ImGui_ImplSDLRenderer2_NewFrame();
152 ImGui_ImplSDL2_NewFrame();
154 ImGui_ImplOSX_NewFrame(view);
158 ImGui::SetNextWindowPos(ImVec2(0, 0));
159 ImVec2 dimensions(io.DisplaySize.x, io.DisplaySize.y);
160 ImGui::SetNextWindowSize(dimensions, ImGuiCond_Always);
161 if (ImGui::Begin(
"##YazeMain",
nullptr,
162 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse |
163 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_MenuBar |
164 ImGuiWindowFlags_NoBringToFrontOnFocus)) {
165 auto controller_status = _controller->OnLoad();
166 if (!controller_status.ok()) {
171 _controller->DoRender();
174- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
183- (void)viewWillAppear {
184 [
super viewWillAppear];
185 self.view.window.delegate = self;
188- (void)windowWillClose:(NSNotification *)notification {
189 ImGui_ImplMetal_Shutdown();
190 ImGui_ImplOSX_Shutdown();
191 ImGui::DestroyContext();
201- (void)UpdateIOWithTouchEvent:(UIEvent *)event {
202 UITouch *anyTouch =
event.allTouches.anyObject;
203 CGPoint touchLocation = [anyTouch locationInView:self.view];
204 ImGuiIO &io = ImGui::GetIO();
205 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
206 io.AddMousePosEvent(touchLocation.x, touchLocation.y);
208 BOOL hasActiveTouch = NO;
209 for (UITouch *touch in event.allTouches) {
210 if (touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled) {
211 hasActiveTouch = YES;
215 io.AddMouseButtonEvent(0, hasActiveTouch);
218- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
219 [
self UpdateIOWithTouchEvent:event];
221- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
222 [
self UpdateIOWithTouchEvent:event];
224- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
225 [
self UpdateIOWithTouchEvent:event];
227- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
228 [
self UpdateIOWithTouchEvent:event];
231- (void)HoverGesture:(UIHoverGestureRecognizer *)gesture {
232 ImGuiIO &io = ImGui::GetIO();
233 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
235 UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer *)gesture;
236 if (gesture.zOffset < 0.50) {
237 io.AddMousePosEvent([gestureRecognizer locationInView:self.view].x,
238 [gestureRecognizer locationInView:self.view].y);
242- (void)HandlePinch:(UIPinchGestureRecognizer *)gesture {
243 ImGuiIO &io = ImGui::GetIO();
244 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
245 io.AddMouseWheelEvent(0.0f, gesture.scale);
248- (void)HandleSwipe:(UISwipeGestureRecognizer *)gesture {
249 ImGuiIO &io = ImGui::GetIO();
250 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
251 if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
252 io.AddMouseWheelEvent(1.0f, 0.0f);
253 }
else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
254 io.AddMouseWheelEvent(-1.0f, 0.0f);
258- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
259 ImGuiIO &io = ImGui::GetIO();
260 io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
261 io.AddMouseButtonEvent(1, gesture.state == UIGestureRecognizerStateBegan);
274@interface AppDelegate : NSObject <NSApplicationDelegate>
275@property(nonatomic, strong) NSWindow *window;
280- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
284- (instancetype)init {
285 if (self = [super init]) {
286 NSViewController *rootViewController = [[
AppViewController alloc] initWithNibName:nil
288 self.window = [[NSWindow alloc]
289 initWithContentRect:NSZeroRect
290 styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
291 NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable
292 backing:NSBackingStoreBuffered
294 self.window.contentViewController = rootViewController;
295 [
self.window center];
296 [
self.window makeKeyAndOrderFront:self];
307- (BOOL)application:(UIApplication *)application
308 didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,
id> *)launchOptions {
310 self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
311 self.window.rootViewController = rootViewController;
312 [
self.window makeKeyAndVisible];
316- (void)applicationWillTerminate:(UIApplication *)application {
317 ImGui_ImplSDLRenderer2_Shutdown();
318 ImGui_ImplSDL2_Shutdown();
319 ImGui::DestroyContext();
323- (void)PresentDocumentPickerWithCompletionHandler:
324 (
void (^)(NSString *selectedFile))completionHandler {
325 self.completionHandler = completionHandler;
327 NSArray *documentTypes = @[ [UTType typeWithIdentifier:@"org.halext.sfc"] ];
328 UIViewController *rootViewController = self.window.rootViewController;
330 [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:documentTypes];
331 _documentPicker.delegate = self;
332 _documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
334 [rootViewController presentViewController:_documentPicker animated:YES completion:nil];
337- (void)documentPicker:(UIDocumentPickerViewController *)controller
338 didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
339 NSURL *selectedFileURL = [urls firstObject];
341 if (self.completionHandler) {
342 if (selectedFileURL) {
343 self.completionHandler(selectedFileURL.path);
344 std::string fileName = std::string([selectedFileURL.path UTF8String]);
347 [selectedFileURL startAccessingSecurityScopedResource];
349 auto data = [NSData dataWithContentsOfURL:selectedFileURL];
353 size_t size = [data length];
356 std::string filename = std::string([selectedFileURL.path UTF8String]);
358 [selectedFileURL stopAccessingSecurityScopedResource];
361 self.completionHandler(
@"");
364 self.completionHandler = nil;
365 [controller dismissViewControllerAnimated:YES completion:nil];
368- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
369 if (self.completionHandler) {
370 self.completionHandler(
@"");
372 self.completionHandler = nil;
373 [controller dismissViewControllerAnimated:YES completion:nil];
385int main(
int argc,
const char *argv[]) {
return NSApplicationMain(argc, argv); }
388int main(
int argc,
char *argv[]) {
390 return UIApplicationMain(argc, argv, nil, NSStringFromClass([
AppDelegate class]));
static std::shared_ptr< Rom > shared_rom_
Main controller for the application.
#define PRINT_IF_ERROR(expression)
int main(int argc, char **argv)
Main entry point for the application.