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/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)
})
}
}

View File

@ -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}
}

View File

@ -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

View File

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

View File

@ -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];
}