From cc7b78bb9a6eb8a6fca145fd374ce2311f9299b2 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 23 Nov 2013 19:31:10 +0900 Subject: [PATCH] Refactoring --- example/main.go | 24 +++++++++++++----- ui/cocoa/cocoa.go | 54 ++++++++++++++++++++-------------------- ui/cocoa/ebiten_window.c | 17 +++---------- ui/cocoa/ebiten_window.h | 5 +--- ui/cocoa/mainloop.c | 36 +++++++++++++++------------ 5 files changed, 70 insertions(+), 66 deletions(-) diff --git a/example/main.go b/example/main.go index 8a990d9e9..8ed7e4735 100644 --- a/example/main.go +++ b/example/main.go @@ -9,9 +9,11 @@ import ( "github.com/hajimehoshi/go-ebiten/example/game/rotating" "github.com/hajimehoshi/go-ebiten/example/game/sprites" "github.com/hajimehoshi/go-ebiten/example/game/testpattern" + "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/ui/cocoa" "os" "runtime" + "sync" "time" ) @@ -53,13 +55,23 @@ func main() { frameTime := time.Duration(int64(time.Second) / int64(ebiten.FPS)) tick := time.Tick(frameTime) + lock := sync.Mutex{} + go func() { + for { + <-tick + ui.Update(func(c ebiten.GameContext) { + lock.Lock() + defer lock.Unlock() + game.Update(c) + }) + } + }() for { ui.PollEvents() - select { - case <-tick: - ui.Update(game.Update) - default: - } - ui.Draw(game.Draw) + ui.Draw(func(c graphics.Context) { + lock.Lock() + defer lock.Unlock() + game.Draw(c) + }) } } diff --git a/ui/cocoa/cocoa.go b/ui/cocoa/cocoa.go index 5fee19802..ef8183e0b 100644 --- a/ui/cocoa/cocoa.go +++ b/ui/cocoa/cocoa.go @@ -44,13 +44,13 @@ func (context *GameContext) InputState() ebiten.InputState { } type UI struct { - screenWidth int - screenHeight int - screenScale int - title string - graphicsDevice *opengl.Device - lock sync.Mutex - gameContext *GameContext + screenWidth int + screenHeight int + screenScale int + title string + graphicsDevice *opengl.Device + gameContext *GameContext + gameContextLock sync.Mutex } var currentUI *UI @@ -69,6 +69,7 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI { screenHeight: screenHeight, inputState: ebiten.InputState{-1, -1}, }, + gameContextLock: sync.Mutex{}, } currentUI = ui return ui @@ -82,7 +83,10 @@ func (ui *UI) Start() { C.size_t(ui.screenHeight), C.size_t(ui.screenScale), cTitle) - C.PollEvents() + ui.graphicsDevice = opengl.NewDevice( + ui.screenWidth, + ui.screenHeight, + ui.screenScale) } func (ui *UI) PollEvents() { @@ -96,6 +100,8 @@ func (ui *UI) InitTextures(f func(graphics.TextureFactory)) { } func (ui *UI) Update(f func(ebiten.GameContext)) { + ui.gameContextLock.Lock() + defer ui.gameContextLock.Unlock() f(ui.gameContext) } @@ -105,36 +111,30 @@ func (ui *UI) Draw(f func(graphics.Context)) { C.EndDrawing() } -//export ebiten_Initialized -func ebiten_Initialized() { - if currentUI.graphicsDevice != nil { - panic("The graphics device is already initialized") - } - currentUI.graphicsDevice = opengl.NewDevice( - currentUI.screenWidth, - currentUI.screenHeight, - currentUI.screenScale) -} - //export ebiten_InputUpdated func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) { + ui := currentUI + + ui.gameContextLock.Lock() + defer ui.gameContextLock.Unlock() + if inputType == C.InputTypeMouseUp { - currentUI.gameContext.inputState = ebiten.InputState{-1, -1} + ui.gameContext.inputState = ebiten.InputState{-1, -1} return } x, y := int(cx), int(cy) - x /= currentUI.screenScale - y /= currentUI.screenScale + x /= ui.screenScale + y /= ui.screenScale if x < 0 { x = 0 - } else if currentUI.screenWidth <= x { - x = currentUI.screenWidth - 1 + } else if ui.screenWidth <= x { + x = ui.screenWidth - 1 } if y < 0 { y = 0 - } else if currentUI.screenHeight <= y { - y = currentUI.screenHeight - 1 + } else if ui.screenHeight <= y { + y = ui.screenHeight - 1 } - currentUI.gameContext.inputState = ebiten.InputState{x, y} + ui.gameContext.inputState = ebiten.InputState{x, y} } diff --git a/ui/cocoa/ebiten_window.c b/ui/cocoa/ebiten_window.c index 618c937d7..0ce487652 100644 --- a/ui/cocoa/ebiten_window.c +++ b/ui/cocoa/ebiten_window.c @@ -6,10 +6,7 @@ #import "ebiten_content_view.h" -void ebiten_Initialized(void); - -@implementation EbitenWindow -{ +@implementation EbitenWindow { NSOpenGLContext* glContext_; } @@ -44,10 +41,7 @@ void ebiten_Initialized(void); return self; } -- (NSOpenGLContext*)glContext { - if (self->glContext_ != nil) - return self->glContext_; - +- (void)initializeGLContext { NSOpenGLPixelFormatAttribute attributes[] = { NSOpenGLPFAWindow, NSOpenGLPFADoubleBuffer, @@ -61,11 +55,8 @@ void ebiten_Initialized(void); shareContext:nil]; [self->glContext_ setView:[self contentView]]; [self->glContext_ makeCurrentContext]; - ebiten_Initialized(); [format release]; - - return self->glContext_; } - (BOOL)windowShouldClose:(id)sender { @@ -97,12 +88,12 @@ void ebiten_Initialized(void); } - (void)beginDrawing { - [[self glContext] makeCurrentContext]; + [self->glContext_ makeCurrentContext]; glClear(GL_COLOR_BUFFER_BIT); } - (void)endDrawing { - [[self glContext] flushBuffer]; + [self->glContext_ flushBuffer]; } @end diff --git a/ui/cocoa/ebiten_window.h b/ui/cocoa/ebiten_window.h index b5bc4f961..5a911efe1 100644 --- a/ui/cocoa/ebiten_window.h +++ b/ui/cocoa/ebiten_window.h @@ -8,10 +8,7 @@ @interface EbitenWindow : NSWindow - (id)initWithSize:(NSSize)size; -- (void)alertDidEnd:(NSAlert*)alert - returnCode:(NSInteger)returnCode - contextInfo:(void*)contextInfo; -- (BOOL)windowShouldClose:(id)sender; +- (void)initializeGLContext; - (void)beginDrawing; - (void)endDrawing; diff --git a/ui/cocoa/mainloop.c b/ui/cocoa/mainloop.c index 66398e032..5db1c2105 100644 --- a/ui/cocoa/mainloop.c +++ b/ui/cocoa/mainloop.c @@ -7,22 +7,6 @@ static EbitenWindow* currentWindow = 0; -void Start(size_t width, size_t height, size_t scale, const char* title) { - NSSize size = NSMakeSize(width * scale, height * scale); - EbitenWindow* window = [[EbitenWindow alloc] - initWithSize:size]; - [window setTitle: [[NSString alloc] initWithUTF8String:title]]; - EbitenController* controller = [[EbitenController alloc] - initWithWindow:window]; - NSApplication* app = [NSApplication sharedApplication]; - [app setActivationPolicy:NSApplicationActivationPolicyRegular]; - [app setDelegate:controller]; - [app finishLaunching]; - [app activateIgnoringOtherApps:YES]; - - currentWindow = window; -} - void PollEvents(void) { for (;;) { NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask @@ -36,6 +20,26 @@ void PollEvents(void) { } } +void Start(size_t width, size_t height, size_t scale, const char* title) { + NSSize size = NSMakeSize(width * scale, height * scale); + EbitenWindow* window = [[EbitenWindow alloc] + initWithSize:size]; + [window setTitle: [[NSString alloc] initWithUTF8String:title]]; + EbitenController* controller = [[EbitenController alloc] + initWithWindow:window]; + NSApplication* app = [NSApplication sharedApplication]; + [app setActivationPolicy:NSApplicationActivationPolicyRegular]; + [app setDelegate:controller]; + [app finishLaunching]; + [app activateIgnoringOtherApps:YES]; + + currentWindow = window; + + PollEvents(); + + [window initializeGLContext]; +} + void BeginDrawing(void) { [currentWindow beginDrawing]; }