2017-02-24 08:28:44 +00:00
|
|
|
#include "VISIRenderer.hpp"
|
|
|
|
#include <AppKit/AppKit.h>
|
|
|
|
#include "athena/Global.hpp"
|
|
|
|
#include "logvisor/logvisor.hpp"
|
2021-03-28 18:07:38 +00:00
|
|
|
#include "../version.h"
|
2017-02-24 08:28:44 +00:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
#error ARC Required
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static std::thread s_task;
|
|
|
|
|
|
|
|
static const NSOpenGLPixelFormatAttribute PF_RGBA8_Z24_ATTRS[] =
|
|
|
|
{
|
|
|
|
NSOpenGLPFAAccelerated,
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
|
|
|
//NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAColorSize, 24,
|
|
|
|
NSOpenGLPFAAlphaSize, 8,
|
|
|
|
NSOpenGLPFADepthSize, 24,
|
|
|
|
0, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
@interface OpenGLView : NSOpenGLView
|
|
|
|
{
|
|
|
|
VISIRenderer* m_renderer;
|
|
|
|
}
|
|
|
|
- (id)initWithFrame:(NSRect)frame renderer:(VISIRenderer*)renderer;
|
|
|
|
@end
|
|
|
|
|
|
|
|
static NSWindow* s_Window;
|
|
|
|
static void UpdatePercent(float percent)
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
s_Window.title = [NSString stringWithFormat:@"VISIGen [%g%%]", percent * 100.f];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation OpenGLView
|
|
|
|
- (id)initWithFrame:(NSRect)frame renderer:(VISIRenderer*)renderer;
|
|
|
|
{
|
|
|
|
NSOpenGLPixelFormat* pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:PF_RGBA8_Z24_ATTRS];
|
|
|
|
self = [super initWithFrame:frame pixelFormat:pf];
|
|
|
|
m_renderer = renderer;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
- (void)prepareOpenGL
|
|
|
|
{
|
2019-06-03 07:51:44 +00:00
|
|
|
[super prepareOpenGL];
|
2017-02-24 08:28:44 +00:00
|
|
|
s_task = std::thread([self](){
|
|
|
|
[[self openGLContext] makeCurrentContext];
|
|
|
|
m_renderer->Run(UpdatePercent);
|
|
|
|
[NSApp terminate:nil];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface AppDelegate : NSObject <NSApplicationDelegate>
|
|
|
|
{
|
|
|
|
VISIRenderer* m_renderer;
|
|
|
|
NSWindow* m_window;
|
|
|
|
NSOpenGLView* m_glView;
|
2017-10-22 06:11:22 +00:00
|
|
|
int m_instanceIdx;
|
2017-02-24 08:28:44 +00:00
|
|
|
}
|
2017-10-22 06:11:22 +00:00
|
|
|
- (id)initWithRenderer:(VISIRenderer*)renderer instIdx:(int)instIdx;
|
2017-02-24 08:28:44 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AppDelegate
|
2017-10-22 06:11:22 +00:00
|
|
|
- (id)initWithRenderer:(VISIRenderer*)renderer instIdx:(int)instIdx
|
2017-02-24 08:28:44 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
m_renderer = renderer;
|
2017-10-22 06:11:22 +00:00
|
|
|
m_instanceIdx = instIdx;
|
2017-02-24 08:28:44 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification*)notification
|
|
|
|
{
|
2017-10-22 06:11:22 +00:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
if (m_instanceIdx != -1)
|
|
|
|
{
|
|
|
|
x = (m_instanceIdx & 1) != 0;
|
|
|
|
y = (m_instanceIdx & 2) != 0;
|
|
|
|
}
|
|
|
|
NSRect cRect = NSMakeRect(x * 768, y * 534, 768, 512);
|
2017-02-24 08:28:44 +00:00
|
|
|
m_window = [[NSWindow alloc] initWithContentRect:cRect
|
|
|
|
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:NO];
|
|
|
|
m_window.releasedWhenClosed = NO;
|
|
|
|
m_window.title = @"VISIGen";
|
|
|
|
s_Window = m_window;
|
|
|
|
m_glView = [[OpenGLView alloc] initWithFrame:cRect renderer:m_renderer];
|
|
|
|
m_window.contentView = m_glView;
|
|
|
|
[m_window makeKeyAndOrderFront:nil];
|
|
|
|
}
|
|
|
|
- (void)applicationWillTerminate:(NSNotification*)notification
|
|
|
|
{
|
|
|
|
m_renderer->Terminate();
|
|
|
|
if (s_task.joinable())
|
|
|
|
s_task.join();
|
|
|
|
exit(m_renderer->ReturnVal());
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
static logvisor::Module AthenaLog("Athena");
|
2019-08-31 20:40:07 +00:00
|
|
|
static void AthenaExc(athena::error::Level level, const char* /*file*/, const char*, int /*line*/,
|
|
|
|
fmt::string_view fmt, fmt::format_args args) {
|
|
|
|
AthenaLog.vreport(logvisor::Level(level), fmt, args);
|
2017-02-24 08:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
2018-01-02 01:04:23 +00:00
|
|
|
if (argc > 1 && !strcmp(argv[1], "--dlpackage"))
|
|
|
|
{
|
2020-04-11 22:51:39 +00:00
|
|
|
fmt::print(FMT_STRING("{}\n"), URDE_DLPACKAGE);
|
2018-01-02 01:04:23 +00:00
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
2017-02-24 08:28:44 +00:00
|
|
|
logvisor::RegisterStandardExceptions();
|
|
|
|
logvisor::RegisterConsoleLogger();
|
|
|
|
atSetExceptionHandler(AthenaExc);
|
|
|
|
VISIRenderer renderer(argc, argv);
|
2017-10-22 06:11:22 +00:00
|
|
|
int instIdx = -1;
|
|
|
|
if (argc > 3)
|
|
|
|
instIdx = atoi(argv[3]);
|
2017-02-24 08:28:44 +00:00
|
|
|
@autoreleasepool
|
|
|
|
{
|
|
|
|
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
|
|
|
|
|
/* Delegate (OS X callbacks) */
|
2017-10-22 06:11:22 +00:00
|
|
|
AppDelegate* appDelegate = [[AppDelegate alloc] initWithRenderer:&renderer instIdx:instIdx];
|
2017-02-24 08:28:44 +00:00
|
|
|
[[NSApplication sharedApplication] setDelegate:appDelegate];
|
|
|
|
[[NSApplication sharedApplication] run];
|
|
|
|
}
|
|
|
|
return renderer.ReturnVal();
|
|
|
|
}
|