mirror of
https://github.com/XProger/OpenLara.git
synced 2025-08-16 10:04:28 +02:00
macOS improvements (retina display support, fps limit, save game fix, settings fix) (#162)
This commit is contained in:
@@ -5,6 +5,10 @@
|
|||||||
|
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
|
||||||
|
int MSAA = 0;
|
||||||
|
BOOL enableRetina = YES;
|
||||||
|
int frameLimit = 58 + 4;
|
||||||
|
|
||||||
// timing
|
// timing
|
||||||
int osGetTime() {
|
int osGetTime() {
|
||||||
static mach_timebase_info_data_t timebaseInfo;
|
static mach_timebase_info_data_t timebaseInfo;
|
||||||
@@ -356,7 +360,9 @@ NSWindow *window;
|
|||||||
unsigned short keyCode = theEvent.keyCode;
|
unsigned short keyCode = theEvent.keyCode;
|
||||||
Input::setDown(keyToInputKey(keyCode), false);
|
Input::setDown(keyToInputKey(keyCode), false);
|
||||||
if (keyCode == 36 && Input::down[ikAlt]) { // Alt + Enter
|
if (keyCode == 36 && Input::down[ikAlt]) { // Alt + Enter
|
||||||
|
allowFrameUpdate = NO;
|
||||||
[window toggleFullScreen:nil];
|
[window toggleFullScreen:nil];
|
||||||
|
allowFrameUpdate = YES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -372,9 +378,17 @@ NSWindow *window;
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)reshape {
|
- (void)reshape {
|
||||||
NSRect bounds = self.bounds;
|
CGSize drawableSize = self.bounds.size;
|
||||||
Core::width = bounds.size.width;
|
|
||||||
Core::height = bounds.size.height;
|
if (enableRetina)
|
||||||
|
{
|
||||||
|
NSScreen* screen = self.window.screen ?: [NSScreen mainScreen];
|
||||||
|
drawableSize.width *= screen.backingScaleFactor;
|
||||||
|
drawableSize.height *= screen.backingScaleFactor;
|
||||||
|
}
|
||||||
|
|
||||||
|
Core::width = drawableSize.width;
|
||||||
|
Core::height = drawableSize.height;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)prepareOpenGL {
|
- (void)prepareOpenGL {
|
||||||
@@ -382,21 +396,39 @@ NSWindow *window;
|
|||||||
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lastDeltaMs = 1;
|
||||||
|
BOOL allowFrameUpdate = YES;
|
||||||
|
|
||||||
- (void)drawRect:(NSRect)dirtyRect {
|
- (void)drawRect:(NSRect)dirtyRect {
|
||||||
|
|
||||||
|
if(!allowFrameUpdate) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int startDrawMs = osGetTime();
|
||||||
|
|
||||||
NSOpenGLContext *context = [self openGLContext];
|
NSOpenGLContext *context = [self openGLContext];
|
||||||
|
|
||||||
if (!Game::update())
|
if (!Game::update())
|
||||||
return;
|
return;
|
||||||
Game::render();
|
Game::render();
|
||||||
|
|
||||||
[context flushBuffer];
|
[context flushBuffer];
|
||||||
|
|
||||||
|
int endDrawMs = osGetTime();
|
||||||
|
int deltaMs = endDrawMs - startDrawMs;
|
||||||
|
float avgDelta = ((lastDeltaMs + deltaMs) / 2.0);
|
||||||
|
lastDeltaMs = deltaMs;
|
||||||
|
|
||||||
|
float nextDelaySec = (1000.0 / frameLimit) / 1000.0;
|
||||||
|
nextDelaySec = nextDelaySec - (avgDelta / 1000.0);
|
||||||
|
|
||||||
BOOL arg = YES;
|
BOOL arg = YES;
|
||||||
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(setNeedsDisplay:)]];
|
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(setNeedsDisplay:)]];
|
||||||
[inv setSelector:@selector(setNeedsDisplay:)];
|
[inv setSelector:@selector(setNeedsDisplay:)];
|
||||||
[inv setTarget:self];
|
[inv setTarget:self];
|
||||||
[inv setArgument:&arg atIndex:2];
|
[inv setArgument:&arg atIndex:2];
|
||||||
[inv performSelector:@selector(invoke) withObject:self afterDelay:0.01];
|
[inv performSelector:@selector(invoke) withObject:self afterDelay:nextDelaySec];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
@@ -413,6 +445,21 @@ NSWindow *window;
|
|||||||
@end
|
@end
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
|
//get and create support path
|
||||||
|
NSString *appName, *supportPath = nil;
|
||||||
|
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory,
|
||||||
|
NSUserDomainMask, YES );
|
||||||
|
if ( [paths count] > 0)
|
||||||
|
{
|
||||||
|
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"];
|
||||||
|
supportPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:appName];
|
||||||
|
|
||||||
|
if ( ![[NSFileManager defaultManager] fileExistsAtPath:supportPath] )
|
||||||
|
if ( ![[NSFileManager defaultManager] createDirectoryAtPath:supportPath attributes:nil] )
|
||||||
|
supportPath = nil;
|
||||||
|
}
|
||||||
|
|
||||||
cacheDir[0] = saveDir[0] = contentDir[0] = 0;
|
cacheDir[0] = saveDir[0] = contentDir[0] = 0;
|
||||||
|
|
||||||
NSApplication *application = [NSApplication sharedApplication];
|
NSApplication *application = [NSApplication sharedApplication];
|
||||||
@@ -424,28 +471,39 @@ int main() {
|
|||||||
window.acceptsMouseMovedEvents = YES;
|
window.acceptsMouseMovedEvents = YES;
|
||||||
window.delegate = [[OpenLaraWindowDelegate alloc] init];
|
window.delegate = [[OpenLaraWindowDelegate alloc] init];
|
||||||
|
|
||||||
|
|
||||||
// init OpenGL context
|
// init OpenGL context
|
||||||
NSOpenGLPixelFormatAttribute attribs[] = {
|
NSOpenGLPixelFormatAttribute attribs[] = {
|
||||||
|
NSOpenGLPFANoRecovery,
|
||||||
|
NSOpenGLPFAMultisample,
|
||||||
NSOpenGLPFADoubleBuffer,
|
NSOpenGLPFADoubleBuffer,
|
||||||
NSOpenGLPFAColorSize, 32,
|
NSOpenGLPFAColorSize, 32,
|
||||||
NSOpenGLPFADepthSize, 24,
|
NSOpenGLPFADepthSize, 24,
|
||||||
NSOpenGLPFAStencilSize, 8,
|
NSOpenGLPFAStencilSize, 8,
|
||||||
|
NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute)(MSAA == 0 ? 0 : 1),
|
||||||
|
NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute)MSAA,
|
||||||
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
||||||
|
|
||||||
OpenLaraGLView *view = [[OpenLaraGLView alloc] initWithFrame:window.contentLayoutRect pixelFormat:format];
|
OpenLaraGLView *view = [[OpenLaraGLView alloc] initWithFrame:window.contentLayoutRect pixelFormat:format];
|
||||||
view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
||||||
window.contentView = view;
|
window.contentView = view;
|
||||||
[view.openGLContext makeCurrentContext];
|
[view.openGLContext makeCurrentContext];
|
||||||
|
[view setWantsBestResolutionOpenGLSurface:enableRetina];
|
||||||
|
|
||||||
// get path to game content
|
// get path to game content
|
||||||
NSBundle *bundle = [NSBundle mainBundle];
|
NSBundle *bundle = [NSBundle mainBundle];
|
||||||
NSURL *resourceURL = bundle.resourceURL;
|
NSURL *resourceURL = bundle.resourceURL;
|
||||||
[resourceURL getFileSystemRepresentation:contentDir maxLength:sizeof(contentDir)];
|
[resourceURL getFileSystemRepresentation:contentDir maxLength:sizeof(contentDir)];
|
||||||
strcat(contentDir, "/");
|
strcat(contentDir, "/");
|
||||||
|
|
||||||
|
[supportPath getCString:saveDir maxLength:sizeof(saveDir) encoding:NSUTF8StringEncoding];
|
||||||
|
strcat(saveDir, "/");
|
||||||
|
[supportPath getCString:cacheDir maxLength:sizeof(cacheDir) encoding:NSUTF8StringEncoding];
|
||||||
|
strcat(cacheDir, "/");
|
||||||
|
|
||||||
// show window
|
// show window
|
||||||
[window center];
|
[window center];
|
||||||
[window makeKeyAndOrderFront:nil];
|
[window makeKeyAndOrderFront:nil];
|
||||||
|
Reference in New Issue
Block a user