mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Refactoring: Add NSAutoreleasePool
This commit is contained in:
parent
adbd0b1dac
commit
8ff6e4efa0
@ -33,7 +33,7 @@ func main() {
|
|||||||
textureFactory := cocoa.TextureFactory()
|
textureFactory := cocoa.TextureFactory()
|
||||||
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
|
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
|
||||||
|
|
||||||
drawing := make(chan *graphics.LazyContext)
|
drawing := make(chan struct{})
|
||||||
quit := make(chan struct{})
|
quit := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(quit)
|
defer close(quit)
|
||||||
@ -54,14 +54,17 @@ func main() {
|
|||||||
}
|
}
|
||||||
case <-tick:
|
case <-tick:
|
||||||
game.Update()
|
game.Update()
|
||||||
case context := <-drawing:
|
case <-drawing:
|
||||||
game.Draw(context)
|
window.Draw(func(context graphics.Context) {
|
||||||
drawing <- context
|
game.Draw(context)
|
||||||
|
})
|
||||||
|
drawing <- struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
u.RunMainLoop()
|
u.Start()
|
||||||
|
defer u.Terminate()
|
||||||
|
|
||||||
s := make(chan os.Signal, 1)
|
s := make(chan os.Signal, 1)
|
||||||
signal.Notify(s, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(s, os.Interrupt, syscall.SIGTERM)
|
||||||
@ -69,15 +72,8 @@ func main() {
|
|||||||
u.DoEvents()
|
u.DoEvents()
|
||||||
select {
|
select {
|
||||||
default:
|
default:
|
||||||
drawing <- graphics.NewLazyContext()
|
drawing <- struct{}{}
|
||||||
context := <-drawing
|
<-drawing
|
||||||
|
|
||||||
window.Draw(func(actualContext graphics.Context) {
|
|
||||||
context.Flush(actualContext)
|
|
||||||
})
|
|
||||||
after := time.After(time.Duration(int64(time.Second) / 120))
|
|
||||||
u.DoEvents()
|
|
||||||
<-after
|
|
||||||
case <-s:
|
case <-s:
|
||||||
return
|
return
|
||||||
case <-quit:
|
case <-quit:
|
||||||
|
@ -57,11 +57,11 @@ void ebiten_WindowClosed(void* nativeWindow);
|
|||||||
- (BOOL)windowShouldClose:(id)sender {
|
- (BOOL)windowShouldClose:(id)sender {
|
||||||
if ([sender isDocumentEdited]) {
|
if ([sender isDocumentEdited]) {
|
||||||
// TODO: add the application's name
|
// TODO: add the application's name
|
||||||
NSAlert* alert = [NSAlert alertWithMessageText:@"Quit the game?"
|
NSAlert* alert = [NSAlert new];
|
||||||
defaultButton:@"Quit"
|
[alert setMessageText:@"Quit the game?"];
|
||||||
alternateButton:nil
|
[alert addButtonWithTitle:@"Quit"];
|
||||||
otherButton:@"Cancel"
|
[alert addButtonWithTitle:@"Cancel"];
|
||||||
informativeTextWithFormat:@""];
|
[alert setAlertStyle:NSWarningAlertStyle];
|
||||||
SEL selector = @selector(alertDidEnd:returnCode:contextInfo:);
|
SEL selector = @selector(alertDidEnd:returnCode:contextInfo:);
|
||||||
[alert beginSheetModalForWindow:sender
|
[alert beginSheetModalForWindow:sender
|
||||||
modalDelegate:self
|
modalDelegate:self
|
||||||
@ -75,9 +75,7 @@ void ebiten_WindowClosed(void* nativeWindow);
|
|||||||
- (void)alertDidEnd:(NSAlert*)alert
|
- (void)alertDidEnd:(NSAlert*)alert
|
||||||
returnCode:(NSInteger)returnCode
|
returnCode:(NSInteger)returnCode
|
||||||
contextInfo:(void*)contextInfo {
|
contextInfo:(void*)contextInfo {
|
||||||
(void)alert;
|
if (returnCode == NSAlertFirstButtonReturn) {
|
||||||
(void)contextInfo;
|
|
||||||
if (returnCode == NSAlertDefaultReturn) {
|
|
||||||
[self close];
|
[self close];
|
||||||
ebiten_WindowClosed(self);
|
ebiten_WindowClosed(self);
|
||||||
}
|
}
|
||||||
|
@ -98,14 +98,17 @@ func (w *GameWindow) loop(context *opengl.Context, glContext *C.NSOpenGLContext)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *GameWindow) Draw(f func(graphics.Context)) {
|
func (w *GameWindow) Draw(f func(graphics.Context)) {
|
||||||
|
//after := time.After(time.Duration(int64(time.Second) / 120))
|
||||||
|
//defer <-after
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-w.closed:
|
case <-w.closed:
|
||||||
return
|
return
|
||||||
default:
|
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)) {
|
func (w *GameWindow) useGLContext(f func(*opengl.Context)) {
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
#import "ebiten_game_window.h"
|
#import "ebiten_game_window.h"
|
||||||
|
|
||||||
|
static NSAutoreleasePool* pool = NULL;
|
||||||
|
|
||||||
void initMenu(void) {
|
void initMenu(void) {
|
||||||
NSString* processName = [[NSProcessInfo processInfo] processName];
|
NSString* processName = [[NSProcessInfo processInfo] processName];
|
||||||
|
|
||||||
@ -25,16 +27,9 @@ void initMenu(void) {
|
|||||||
keyEquivalent:@"q"];
|
keyEquivalent:@"q"];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Run(void) {
|
|
||||||
NSAutoreleasePool * pool = [NSAutoreleasePool new];
|
|
||||||
NSApplication* app = [NSApplication sharedApplication];
|
|
||||||
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
||||||
//initMenu();
|
|
||||||
[app run];
|
|
||||||
[pool drain];
|
|
||||||
}
|
|
||||||
|
|
||||||
void StartApplication(void) {
|
void StartApplication(void) {
|
||||||
|
pool = [NSAutoreleasePool new];
|
||||||
|
|
||||||
NSApplication* app = [NSApplication sharedApplication];
|
NSApplication* app = [NSApplication sharedApplication];
|
||||||
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
|
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
|
||||||
|
|
||||||
@ -43,6 +38,32 @@ void StartApplication(void) {
|
|||||||
[app finishLaunching];
|
[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) {
|
NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext) {
|
||||||
NSOpenGLPixelFormatAttribute attributes[] = {
|
NSOpenGLPixelFormatAttribute attributes[] = {
|
||||||
NSOpenGLPFAWindow,
|
NSOpenGLPFAWindow,
|
||||||
@ -77,24 +98,6 @@ EbitenGameWindow* CreateGameWindow(size_t width, size_t height, const char* titl
|
|||||||
return window;
|
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) {
|
void UseGLContext(NSOpenGLContext* glContext) {
|
||||||
CGLContextObj cglContext = [glContext CGLContextObj];
|
CGLContextObj cglContext = [glContext CGLContextObj];
|
||||||
CGLLockContext(cglContext);
|
CGLLockContext(cglContext);
|
||||||
|
@ -3,9 +3,9 @@ package cocoa
|
|||||||
// #cgo CFLAGS: -x objective-c
|
// #cgo CFLAGS: -x objective-c
|
||||||
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL
|
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL
|
||||||
//
|
//
|
||||||
// void Run(void);
|
|
||||||
// void StartApplication(void);
|
// void StartApplication(void);
|
||||||
// void DoEvents(void);
|
// void DoEvents(void);
|
||||||
|
// void TerminateApplication(void);
|
||||||
//
|
//
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
@ -46,18 +46,12 @@ func (u *cocoaUI) DoEvents() {
|
|||||||
C.DoEvents()
|
C.DoEvents()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *cocoaUI) RunMainLoop() {
|
func (u *cocoaUI) Start() {
|
||||||
C.StartApplication()
|
C.StartApplication()
|
||||||
currentUI.sharedContext.run()
|
currentUI.sharedContext.run()
|
||||||
|
|
||||||
// TODO: Enable the loop
|
|
||||||
//C.Run()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) {
|
func (u *cocoaUI) Terminate() {
|
||||||
t.sharedContext.CreateTexture(tag, img, filter)
|
// TODO: Close existing windows
|
||||||
|
C.TerminateApplication()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) {
|
|
||||||
t.sharedContext.CreateRenderTarget(tag, width, height)
|
|
||||||
}*/
|
|
||||||
|
5
ui/ui.go
5
ui/ui.go
@ -33,9 +33,10 @@ type WindowClosedEvent struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UI interface {
|
type UI interface {
|
||||||
DoEvents()
|
|
||||||
CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow
|
CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow
|
||||||
RunMainLoop()
|
Start()
|
||||||
|
DoEvents()
|
||||||
|
Terminate()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Window interface {
|
type Window interface {
|
||||||
|
Loading…
Reference in New Issue
Block a user