Refactoring: Add NSAutoreleasePool

This commit is contained in:
Hajime Hoshi 2014-01-13 15:26:20 +09:00
parent adbd0b1dac
commit 8ff6e4efa0
6 changed files with 60 additions and 65 deletions

View File

@ -33,7 +33,7 @@ func main() {
textureFactory := cocoa.TextureFactory()
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
drawing := make(chan *graphics.LazyContext)
drawing := make(chan struct{})
quit := make(chan struct{})
go func() {
defer close(quit)
@ -54,14 +54,17 @@ func main() {
}
case <-tick:
game.Update()
case context := <-drawing:
game.Draw(context)
drawing <- context
case <-drawing:
window.Draw(func(context graphics.Context) {
game.Draw(context)
})
drawing <- struct{}{}
}
}
}()
u.RunMainLoop()
u.Start()
defer u.Terminate()
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, syscall.SIGTERM)
@ -69,15 +72,8 @@ func main() {
u.DoEvents()
select {
default:
drawing <- graphics.NewLazyContext()
context := <-drawing
window.Draw(func(actualContext graphics.Context) {
context.Flush(actualContext)
})
after := time.After(time.Duration(int64(time.Second) / 120))
u.DoEvents()
<-after
drawing <- struct{}{}
<-drawing
case <-s:
return
case <-quit:

View File

@ -57,11 +57,11 @@ void ebiten_WindowClosed(void* nativeWindow);
- (BOOL)windowShouldClose:(id)sender {
if ([sender isDocumentEdited]) {
// TODO: add the application's name
NSAlert* alert = [NSAlert alertWithMessageText:@"Quit the game?"
defaultButton:@"Quit"
alternateButton:nil
otherButton:@"Cancel"
informativeTextWithFormat:@""];
NSAlert* alert = [NSAlert new];
[alert setMessageText:@"Quit the game?"];
[alert addButtonWithTitle:@"Quit"];
[alert addButtonWithTitle:@"Cancel"];
[alert setAlertStyle:NSWarningAlertStyle];
SEL selector = @selector(alertDidEnd:returnCode:contextInfo:);
[alert beginSheetModalForWindow:sender
modalDelegate:self
@ -75,9 +75,7 @@ void ebiten_WindowClosed(void* nativeWindow);
- (void)alertDidEnd:(NSAlert*)alert
returnCode:(NSInteger)returnCode
contextInfo:(void*)contextInfo {
(void)alert;
(void)contextInfo;
if (returnCode == NSAlertDefaultReturn) {
if (returnCode == NSAlertFirstButtonReturn) {
[self close];
ebiten_WindowClosed(self);
}

View File

@ -98,14 +98,17 @@ func (w *GameWindow) loop(context *opengl.Context, glContext *C.NSOpenGLContext)
}
func (w *GameWindow) Draw(f func(graphics.Context)) {
//after := time.After(time.Duration(int64(time.Second) / 120))
//defer <-after
select {
case <-w.closed:
return
default:
w.useGLContext(func(context *opengl.Context) {
context.Update(f)
})
}
w.useGLContext(func(context *opengl.Context) {
context.Update(f)
})
}
func (w *GameWindow) useGLContext(f func(*opengl.Context)) {

View File

@ -5,6 +5,8 @@
#import "ebiten_game_window.h"
static NSAutoreleasePool* pool = NULL;
void initMenu(void) {
NSString* processName = [[NSProcessInfo processInfo] processName];
@ -25,16 +27,9 @@ void initMenu(void) {
keyEquivalent:@"q"];
}
void Run(void) {
NSAutoreleasePool * pool = [NSAutoreleasePool new];
NSApplication* app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
//initMenu();
[app run];
[pool drain];
}
void StartApplication(void) {
pool = [NSAutoreleasePool new];
NSApplication* app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
@ -43,6 +38,32 @@ void StartApplication(void) {
[app finishLaunching];
}
void DoEvents(void) {
for (;;) {
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event == nil) {
break;
}
[NSApp sendEvent:event];
}
static BOOL initialBoot = YES;
if (initialBoot) {
[NSApp activateIgnoringOtherApps:YES];
initialBoot = NO;
}
[pool drain];
pool = [NSAutoreleasePool new];
}
void TerminateApplication(void) {
[pool drain];
}
NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext) {
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow,
@ -77,24 +98,6 @@ EbitenGameWindow* CreateGameWindow(size_t width, size_t height, const char* titl
return window;
}
void DoEvents(void) {
for (;;) {
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event == nil) {
break;
}
[NSApp sendEvent:event];
}
static BOOL initialBoot = YES;
if (initialBoot) {
[NSApp activateIgnoringOtherApps:YES];
initialBoot = NO;
}
}
void UseGLContext(NSOpenGLContext* glContext) {
CGLContextObj cglContext = [glContext CGLContextObj];
CGLLockContext(cglContext);

View File

@ -3,9 +3,9 @@ package cocoa
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL
//
// void Run(void);
// void StartApplication(void);
// void DoEvents(void);
// void TerminateApplication(void);
//
import "C"
import (
@ -46,18 +46,12 @@ func (u *cocoaUI) DoEvents() {
C.DoEvents()
}
func (u *cocoaUI) RunMainLoop() {
func (u *cocoaUI) Start() {
C.StartApplication()
currentUI.sharedContext.run()
// TODO: Enable the loop
//C.Run()
}
/*func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) {
t.sharedContext.CreateTexture(tag, img, filter)
func (u *cocoaUI) Terminate() {
// TODO: Close existing windows
C.TerminateApplication()
}
func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) {
t.sharedContext.CreateRenderTarget(tag, width, height)
}*/

View File

@ -33,9 +33,10 @@ type WindowClosedEvent struct {
}
type UI interface {
DoEvents()
CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow
RunMainLoop()
Start()
DoEvents()
Terminate()
}
type Window interface {