Refactoring

This commit is contained in:
Hajime Hoshi 2013-11-23 19:31:10 +09:00
parent c3968a5aaa
commit cc7b78bb9a
5 changed files with 70 additions and 66 deletions

View File

@ -9,9 +9,11 @@ import (
"github.com/hajimehoshi/go-ebiten/example/game/rotating" "github.com/hajimehoshi/go-ebiten/example/game/rotating"
"github.com/hajimehoshi/go-ebiten/example/game/sprites" "github.com/hajimehoshi/go-ebiten/example/game/sprites"
"github.com/hajimehoshi/go-ebiten/example/game/testpattern" "github.com/hajimehoshi/go-ebiten/example/game/testpattern"
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/ui/cocoa" "github.com/hajimehoshi/go-ebiten/ui/cocoa"
"os" "os"
"runtime" "runtime"
"sync"
"time" "time"
) )
@ -53,13 +55,23 @@ func main() {
frameTime := time.Duration(int64(time.Second) / int64(ebiten.FPS)) frameTime := time.Duration(int64(time.Second) / int64(ebiten.FPS))
tick := time.Tick(frameTime) 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 { for {
ui.PollEvents() ui.PollEvents()
select { ui.Draw(func(c graphics.Context) {
case <-tick: lock.Lock()
ui.Update(game.Update) defer lock.Unlock()
default: game.Draw(c)
} })
ui.Draw(game.Draw)
} }
} }

View File

@ -44,13 +44,13 @@ func (context *GameContext) InputState() ebiten.InputState {
} }
type UI struct { type UI struct {
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
title string title string
graphicsDevice *opengl.Device graphicsDevice *opengl.Device
lock sync.Mutex gameContext *GameContext
gameContext *GameContext gameContextLock sync.Mutex
} }
var currentUI *UI var currentUI *UI
@ -69,6 +69,7 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI {
screenHeight: screenHeight, screenHeight: screenHeight,
inputState: ebiten.InputState{-1, -1}, inputState: ebiten.InputState{-1, -1},
}, },
gameContextLock: sync.Mutex{},
} }
currentUI = ui currentUI = ui
return ui return ui
@ -82,7 +83,10 @@ func (ui *UI) Start() {
C.size_t(ui.screenHeight), C.size_t(ui.screenHeight),
C.size_t(ui.screenScale), C.size_t(ui.screenScale),
cTitle) cTitle)
C.PollEvents() ui.graphicsDevice = opengl.NewDevice(
ui.screenWidth,
ui.screenHeight,
ui.screenScale)
} }
func (ui *UI) PollEvents() { func (ui *UI) PollEvents() {
@ -96,6 +100,8 @@ func (ui *UI) InitTextures(f func(graphics.TextureFactory)) {
} }
func (ui *UI) Update(f func(ebiten.GameContext)) { func (ui *UI) Update(f func(ebiten.GameContext)) {
ui.gameContextLock.Lock()
defer ui.gameContextLock.Unlock()
f(ui.gameContext) f(ui.gameContext)
} }
@ -105,36 +111,30 @@ func (ui *UI) Draw(f func(graphics.Context)) {
C.EndDrawing() 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 //export ebiten_InputUpdated
func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) { func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) {
ui := currentUI
ui.gameContextLock.Lock()
defer ui.gameContextLock.Unlock()
if inputType == C.InputTypeMouseUp { if inputType == C.InputTypeMouseUp {
currentUI.gameContext.inputState = ebiten.InputState{-1, -1} ui.gameContext.inputState = ebiten.InputState{-1, -1}
return return
} }
x, y := int(cx), int(cy) x, y := int(cx), int(cy)
x /= currentUI.screenScale x /= ui.screenScale
y /= currentUI.screenScale y /= ui.screenScale
if x < 0 { if x < 0 {
x = 0 x = 0
} else if currentUI.screenWidth <= x { } else if ui.screenWidth <= x {
x = currentUI.screenWidth - 1 x = ui.screenWidth - 1
} }
if y < 0 { if y < 0 {
y = 0 y = 0
} else if currentUI.screenHeight <= y { } else if ui.screenHeight <= y {
y = currentUI.screenHeight - 1 y = ui.screenHeight - 1
} }
currentUI.gameContext.inputState = ebiten.InputState{x, y} ui.gameContext.inputState = ebiten.InputState{x, y}
} }

View File

@ -6,10 +6,7 @@
#import "ebiten_content_view.h" #import "ebiten_content_view.h"
void ebiten_Initialized(void); @implementation EbitenWindow {
@implementation EbitenWindow
{
NSOpenGLContext* glContext_; NSOpenGLContext* glContext_;
} }
@ -44,10 +41,7 @@ void ebiten_Initialized(void);
return self; return self;
} }
- (NSOpenGLContext*)glContext { - (void)initializeGLContext {
if (self->glContext_ != nil)
return self->glContext_;
NSOpenGLPixelFormatAttribute attributes[] = { NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow, NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer, NSOpenGLPFADoubleBuffer,
@ -61,11 +55,8 @@ void ebiten_Initialized(void);
shareContext:nil]; shareContext:nil];
[self->glContext_ setView:[self contentView]]; [self->glContext_ setView:[self contentView]];
[self->glContext_ makeCurrentContext]; [self->glContext_ makeCurrentContext];
ebiten_Initialized();
[format release]; [format release];
return self->glContext_;
} }
- (BOOL)windowShouldClose:(id)sender { - (BOOL)windowShouldClose:(id)sender {
@ -97,12 +88,12 @@ void ebiten_Initialized(void);
} }
- (void)beginDrawing { - (void)beginDrawing {
[[self glContext] makeCurrentContext]; [self->glContext_ makeCurrentContext];
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
- (void)endDrawing { - (void)endDrawing {
[[self glContext] flushBuffer]; [self->glContext_ flushBuffer];
} }
@end @end

View File

@ -8,10 +8,7 @@
@interface EbitenWindow : NSWindow<NSWindowDelegate> @interface EbitenWindow : NSWindow<NSWindowDelegate>
- (id)initWithSize:(NSSize)size; - (id)initWithSize:(NSSize)size;
- (void)alertDidEnd:(NSAlert*)alert - (void)initializeGLContext;
returnCode:(NSInteger)returnCode
contextInfo:(void*)contextInfo;
- (BOOL)windowShouldClose:(id)sender;
- (void)beginDrawing; - (void)beginDrawing;
- (void)endDrawing; - (void)endDrawing;

View File

@ -7,22 +7,6 @@
static EbitenWindow* currentWindow = 0; 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) { void PollEvents(void) {
for (;;) { for (;;) {
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask 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) { void BeginDrawing(void) {
[currentWindow beginDrawing]; [currentWindow beginDrawing];
} }