Skip to content

Commit

Permalink
Initial external display support, touch controls don't work yet
Browse files Browse the repository at this point in the history
  • Loading branch information
ckosmic committed Oct 20, 2021
1 parent b35242c commit 892f25e
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 59 deletions.
Binary file not shown.
6 changes: 4 additions & 2 deletions sm64ios/sm64ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
5918D479271C223E0085DDED /* ini.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = ini.c; sourceTree = "<group>"; };
5918D47B271C223E0085DDED /* discordrpc.c.disabled */ = {isa = PBXFileReference; lastKnownFileType = text; path = discordrpc.c.disabled; sourceTree = "<group>"; };
5918D47C271C223E0085DDED /* discordrpc.h.disabled */ = {isa = PBXFileReference; lastKnownFileType = text; path = discordrpc.h.disabled; sourceTree = "<group>"; };
5918D47D271C223E0085DDED /* pc_main.m.orig */ = {isa = PBXFileReference; lastKnownFileType = text; path = pc_main.m.orig; sourceTree = "<group>"; };
5918D47E271C223E0085DDED /* ultra_reimplementation.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = ultra_reimplementation.c; sourceTree = "<group>"; };
5918D47F271C223E0085DDED /* configfile.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = configfile.h; sourceTree = "<group>"; };
5918D480271C223E0085DDED /* platform.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = platform.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -537,6 +536,8 @@
5918D684271C223F0085DDED /* level_script.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = level_script.c; sourceTree = "<group>"; };
5918D685271C223F0085DDED /* behavior_script.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = behavior_script.c; sourceTree = "<group>"; };
5918D686271C223F0085DDED /* geo_layout.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = geo_layout.c; sourceTree = "<group>"; };
59A67E08271EBCDC00FF5D6C /* ExternalGameViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ExternalGameViewController.m; sourceTree = "<group>"; };
59A67E09271EBCEE00FF5D6C /* ExternalGameViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ExternalGameViewController.h; sourceTree = "<group>"; };
59AA60412716655100B9AF30 /* libSDL2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libSDL2.a; sourceTree = BUILT_PRODUCTS_DIR; };
59AA60432716655100B9AF30 /* libSDLmain.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libSDLmain.a; sourceTree = BUILT_PRODUCTS_DIR; };
59AA60472716655C00B9AF30 /* d3d12sdklayers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = d3d12sdklayers.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -761,7 +762,6 @@
5918D478271C223E0085DDED /* README-n64-fast32-engine.md */,
5918D479271C223E0085DDED /* ini.c */,
5918D47A271C223E0085DDED /* discord */,
5918D47D271C223E0085DDED /* pc_main.m.orig */,
5918D47E271C223E0085DDED /* ultra_reimplementation.c */,
5918D47F271C223E0085DDED /* configfile.h */,
5918D480271C223E0085DDED /* platform.c */,
Expand Down Expand Up @@ -1245,6 +1245,8 @@
5918D622271C223F0085DDED /* TouchControlsEnabledViewController.h */,
5918D623271C223F0085DDED /* haptics_controller.m */,
5918D624271C223F0085DDED /* TextureFilterViewController.h */,
59A67E08271EBCDC00FF5D6C /* ExternalGameViewController.m */,
59A67E09271EBCEE00FF5D6C /* ExternalGameViewController.h */,
);
path = ios;
sourceTree = "<group>";
Expand Down
17 changes: 17 additions & 0 deletions src/ios/ExternalGameViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ExternalGameViewController.h
// sm64ios
//
// Created by Christian Kosman on 10/19/21.
//

#ifndef ExternalGameViewController_h
#define ExternalGameViewController_h

#import <UIKit/UIKit.h>

@interface ExternalGameViewController : UIViewController

@end

#endif /* ExternalGameViewController_h */
17 changes: 17 additions & 0 deletions src/ios/ExternalGameViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ExternalGameViewController.m
// sm64ios
//
// Created by Christian Kosman on 10/19/21.
//

#import "ExternalGameViewController.h"


@implementation ExternalGameViewController

- (void)viewDidLoad {
[super viewDidLoad];
}

@end
33 changes: 8 additions & 25 deletions src/ios/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19162" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="14B-gy-VWT">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19162" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_0" orientation="landscape" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19144"/>
Expand All @@ -25,37 +25,20 @@
</objects>
<point key="canvasLocation" x="-887" y="-663"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="VW5-K1-QVv">
<objects>
<navigationController storyboardIdentifier="GameNav" id="14B-gy-VWT" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="qe4-Kg-Zu5">
<rect key="frame" x="0.0" y="0.0" width="844" height="32"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="kZX-El-NPb" kind="relationship" relationship="rootViewController" id="Cpw-FX-79c"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="rJH-ka-csd" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-887" y="-1498"/>
</scene>
<!--View Controller-->
<scene sceneID="rRJ-Xf-8TV">
<!--External Game View Controller-->
<scene sceneID="17T-7e-92n">
<objects>
<viewController id="kZX-El-NPb" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="9EL-dv-u1b">
<viewController storyboardIdentifier="ExternalScreen" id="MJ1-u5-bGf" customClass="ExternalGameViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="cAe-uc-z9k">
<rect key="frame" x="0.0" y="0.0" width="844" height="390"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="WvC-0D-hfe"/>
<viewLayoutGuide key="safeArea" id="r1x-LD-FRT"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</view>
<navigationItem key="navigationItem" id="BN9-gR-nkd"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="jSl-dW-Fk9" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="aXJ-90-WZf" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-176" y="-1498"/>
<point key="canvasLocation" x="-176" y="-1531"/>
</scene>
<!--Main Menu-->
<scene sceneID="1G3-Zx-8E0">
Expand Down
5 changes: 5 additions & 0 deletions src/ios/native_ui_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

struct ScreenData {
int index;
UIWindow *uiwindow;
};

UIViewController *present_viewcontroller(NSString *vcName, bool animated);

bool paused_by_menu;
Expand Down
3 changes: 2 additions & 1 deletion src/pc/gfx/gfx_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#define GFX_SDL_H

#include "gfx_window_manager_api.h"
#include <UIKit/UIKit.h>

extern struct GfxWindowManagerAPI gfx_sdl;
long* get_sdl_viewcontroller();
UIViewController *get_sdl_viewcontroller();

#endif
33 changes: 22 additions & 11 deletions src/pc/gfx/gfx_sdl2.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
#include <SDL2/SDL.h>
#define GL_GLEXT_PROTOTYPES 1

#ifdef OSX_BUILD
#include <SDL2/SDL_opengles2.h>
#else
#include <SDL2/SDL_opengles2.h>
#endif

#include <SDL2/SDL_syswm.h>

#endif // End of OS-Specific GL defines
Expand All @@ -40,8 +35,6 @@
#include "src/pc/controller/controller_keyboard.h"
#include "src/pc/controller/controller_touchscreen.h"

#import <UIKit/UIKit.h>

// TODO: figure out if this shit even works
#ifdef VERSION_EU
# define FRAMERATE 25
Expand Down Expand Up @@ -131,13 +124,13 @@ - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
}
@end

long* get_sdl_viewcontroller() {
UIViewController *get_sdl_viewcontroller() {
SDL_SysWMinfo systemWindowInfo;
SDL_VERSION(&systemWindowInfo.version);
SDL_GetWindowWMInfo(wnd, &systemWindowInfo);

UIWindow *uiWindow = systemWindowInfo.info.uikit.window;
return (int*)uiWindow.rootViewController;
return uiWindow.rootViewController;
}

static inline void sys_sleep(const uint64_t us) {
Expand All @@ -154,7 +147,7 @@ static int test_vsync(void) {
// This method will fail if the refresh rate is changed, which, in
// combination with that we can't control the queue size (i.e. lag)
// is a reason this generic SDL2 backend should only be used as last resort.

for (int i = 0; i < 8; ++i)
SDL_GL_SwapWindow(wnd);

Expand All @@ -179,6 +172,9 @@ static int test_vsync(void) {
static inline void gfx_sdl_set_vsync(const bool enabled) {
if (enabled) {
// try to detect refresh rate
if(SDL_GL_SetSwapInterval(1) < 0) {
printf("Error with swap interval: %s\n", SDL_GetError());
}
const int vblanks = gCLIOpts.SyncFrames ? (int)gCLIOpts.SyncFrames : test_vsync();
if (vblanks) {
printf("determined swap interval: %d\n", vblanks);
Expand Down Expand Up @@ -464,6 +460,20 @@ static void gfx_sdl_shutdown(void) {
}
}

static void gfx_sdl_set_screen_state(long *screenDataPointer) {
struct ScreenData screenData = *((struct ScreenData *)screenDataPointer);
printf("rendering to screen: %d\n", screenData.index);

SDL_Rect bounds;
SDL_GetDisplayBounds(screenData.index, &bounds);
configWindow.x = bounds.x;
configWindow.y = bounds.y;
configWindow.w = bounds.w;
configWindow.h = bounds.h;
SDL_SetWindowPosition(wnd, configWindow.x, configWindow.y);
SDL_SetWindowSize(wnd, configWindow.w, configWindow.h);
}

struct GfxWindowManagerAPI gfx_sdl = {
gfx_sdl_init,
gfx_sdl_set_keyboard_callbacks,
Expand All @@ -476,7 +486,8 @@ static void gfx_sdl_shutdown(void) {
gfx_sdl_swap_buffers_end,
gfx_sdl_get_time,
gfx_sdl_shutdown,
gfx_sdl_reset_dimension_and_pos
gfx_sdl_reset_dimension_and_pos,
gfx_sdl_set_screen_state
};

#endif // BACKEND_WM
5 changes: 3 additions & 2 deletions src/pc/gfx/gfx_uikit.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "../../ios/native_ui_controller.h"

@interface OverlayView : UIView
- (id)initWithFrame:(CGRect)frame;
- (void)drawRect:(CGRect)rect;
Expand All @@ -30,9 +32,8 @@

OverlayView *overlayView;

void gfx_uikit_init(long *viewControllerPointer);
void gfx_uikit_init(UIViewController *viewControllerPointer, struct ScreenData *screenData);
OverlayImageView *add_image_subview(CGImageRef imageRef, CGRect rect);
CGImageRef create_imageref(const char *path);
void setup_game_viewcontroller(UIViewController *subvc);

#endif /* gfx_uikit_h */
36 changes: 19 additions & 17 deletions src/pc/gfx/gfx_uikit.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
//

#include <math.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "../../ios/GameViewController.h"
#import "../../ios/native_ui_controller.h"

#include "gfx_uikit.h"
#import "gfx_uikit.h"
#import "../../ios/ExternalGameViewController.h"

OverlayView *overlayView;
UIWindow *externalWindow;
Expand Down Expand Up @@ -89,26 +85,32 @@ CGImageRef create_imageref(const char *path) {
return v;
}

void gfx_uikit_init(long *viewControllerPointer) {
// There's probably a better way to do this than pointer casting
UIViewController *sdlViewController = (UIViewController *)viewControllerPointer;
void gfx_uikit_init(UIViewController *viewControllerPointer, struct ScreenData *screenData) {
UIViewController *sdlViewController = viewControllerPointer;

UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];

CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
overlayView = [[OverlayView alloc] initWithFrame:mainScreenBounds];

[sdlViewController.view addSubview:overlayView];
screenData->uiwindow = mainWindow;
screenData->index = 0;

if([[UIScreen screens] count] > 1) {
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:overlayView];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ExternalGameViewController *externalVc = [storyboard instantiateViewControllerWithIdentifier:@"ExternalScreen"];
[externalVc.view addSubview:sdlViewController.view.superview];
UIScreen *screen = [UIScreen screens][1];
externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
externalWindow.rootViewController = sdlViewController;
externalWindow.rootViewController = externalVc;
externalWindow.screen = screen;
externalWindow.screen.overscanCompensation = UIScreenOverscanCompensationScale;
externalWindow.hidden = false;
screenData->index = 1;
screenData->uiwindow = externalWindow;
} else {
[sdlViewController.view addSubview:overlayView];
}
}

void setup_game_viewcontroller(UIViewController *subvc) {
GameViewController *vc = (GameViewController *)present_viewcontroller(@"GameNav", false);

[vc.view addSubview:subvc.view];
}
1 change: 1 addition & 0 deletions src/pc/gfx/gfx_window_manager_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct GfxWindowManagerAPI {
double (*get_time)(void); // For debug
void (*shutdown)(void);
void (*reset_dimension_and_pos)(void);
void (*set_screen_state)(long *screenData);
};

#endif
4 changes: 3 additions & 1 deletion src/pc/pc_main.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,11 @@ void main_func(void) {
wm_api->set_touchscreen_callbacks((void*)touch_down, (void*)touch_motion, (void*)touch_up);

UIViewController *gfxVc = get_sdl_viewcontroller();
gfx_uikit_init(gfxVc);
struct ScreenData screenData;
gfx_uikit_init(gfxVc, &screenData);
configWindow.settings_changed = true;
wm_api->reset_dimension_and_pos();
wm_api->set_screen_state((long *)(&screenData));

menu_button_pressed = &present_first_screen;

Expand Down

0 comments on commit 892f25e

Please sign in to comment.