Refactoring

This commit is contained in:
Hajime Hoshi 2013-10-14 02:04:26 +09:00
parent a6ebcbbda6
commit 5828feadd7
4 changed files with 42 additions and 49 deletions

View File

@ -45,6 +45,8 @@ type Texture interface {
type TextureID int
// The interface of a render target. This is essentially same as a texture, but
// it is assumed that the all alpha of a render target is maximum.
type RenderTarget interface {
Texture() Texture
ID() RenderTargetID

View File

@ -17,14 +17,14 @@ import (
)
type UI struct {
game ebiten.Game
screenWidth int
screenHeight int
screenScale int
graphicsDevice *opengl.Device
inited chan bool
updating chan bool
updated chan bool
initializing chan ebiten.Game
initialized chan ebiten.Game
updating chan ebiten.Game
updated chan ebiten.Game
input chan ebiten.InputState
}
@ -42,16 +42,16 @@ func ebiten_EbitenOpenGLView_Initialized() {
currentUI.screenScale)
currentUI.graphicsDevice.Init()
currentUI.game.Init(currentUI.graphicsDevice.TextureFactory())
currentUI.inited <- true
game := <-currentUI.initializing
game.Init(currentUI.graphicsDevice.TextureFactory())
currentUI.initialized <- game
}
//export ebiten_EbitenOpenGLView_Updating
func ebiten_EbitenOpenGLView_Updating() {
<-currentUI.updating
currentUI.graphicsDevice.Update(currentUI.game.Draw)
currentUI.updated <- true
game := <-currentUI.updating
currentUI.graphicsDevice.Update(game.Draw)
currentUI.updated <- game
}
//export ebiten_EbitenOpenGLView_InputUpdated
@ -79,17 +79,14 @@ func ebiten_EbitenOpenGLView_InputUpdated(inputType C.InputType, cx, cy C.int) {
func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int,
title string) {
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
currentUI = &UI{
game: game,
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
inited: make(chan bool),
updating: make(chan bool),
updated: make(chan bool),
initializing: make(chan ebiten.Game),
initialized: make(chan ebiten.Game),
updating: make(chan ebiten.Game),
updated: make(chan ebiten.Game),
input: make(chan ebiten.InputState),
}
@ -102,18 +99,21 @@ func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int,
screenHeight: screenHeight,
inputState: ebiten.InputState{-1, -1},
}
<-currentUI.inited
currentUI.initializing <- game
game = <-currentUI.initialized
for {
select {
case gameContext.inputState = <-currentUI.input:
case <-tick:
game.Update(gameContext)
case currentUI.updating <- true:
<-currentUI.updated
case currentUI.updating <- game:
game = <-currentUI.updated
}
}
}()
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
C.Run(C.size_t(screenWidth),
C.size_t(screenHeight),
C.size_t(screenScale),

View File

@ -1,12 +1,12 @@
// -*- objc -*-
#import "ebiten_opengl_view.h"
#import "ebiten_window.h"
#import "ebiten_opengl_view.h"
@implementation EbitenWindow
- (id)initWithSize:(NSSize)size {
[NSApplication sharedApplication];
NSUInteger style = (NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask);
NSRect windowRect =
@ -27,6 +27,21 @@
[self setReleasedWhenClosed:YES];
[self setDelegate:self];
[self setDocumentEdited:YES];
NSRect rect = NSMakeRect(0, 0, size.width, size.height);
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, 32,
0,
};
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc]
initWithAttributes:attributes];
EbitenOpenGLView* glView =
[[EbitenOpenGLView alloc] initWithFrame:rect
pixelFormat:format];
[self setContentView:glView];
return self;
}

View File

@ -3,37 +3,13 @@
#include <stdlib.h>
#import "ebiten_controller.h"
#import "ebiten_opengl_view.h"
#import "ebiten_window.h"
static NSWindow* generateWindow(size_t width, size_t height, size_t scale, const char* title) {
EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:NSMakeSize(width * scale, height * scale)];
assert(window != nil);
NSRect const rect = NSMakeRect(0, 0, width * scale, height * scale);
NSOpenGLPixelFormatAttribute const attributes[] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, 32,
0,
};
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc]
initWithAttributes:attributes];
EbitenOpenGLView* glView =
[[EbitenOpenGLView alloc] initWithFrame:rect
pixelFormat:format];
[window setContentView:glView];
[window setTitle: [[NSString alloc] initWithUTF8String:title]];
//[window makeFirstResponder:glView];
return window;
}
void Run(size_t width, size_t height, size_t scale, const char* title) {
@autoreleasepool {
NSWindow* window = generateWindow(width, height, scale, title);
EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:NSMakeSize(width * scale, height * scale)];
[window setTitle: [[NSString alloc] initWithUTF8String:title]];
EbitenController* controller = [[EbitenController alloc]
initWithWindow:window];
NSApplication* app = [NSApplication sharedApplication];