Refactoring

This commit is contained in:
Hajime Hoshi 2013-10-13 23:33:22 +09:00
parent 983aeec03b
commit ea6e88d06e
6 changed files with 46 additions and 50 deletions

View File

@ -4,13 +4,13 @@ package cocoa
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL -framework QuartzCore
//
// #include <stdlib.h>
// #include "input.h"
//
// void Run(size_t width, size_t height, size_t scale, const char* title);
//
import "C"
import (
"github.com/hajimehoshi/go.ebiten"
// "github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl"
"time"
"unsafe"
@ -55,8 +55,26 @@ func ebiten_EbitenOpenGLView_Updating() {
}
//export ebiten_EbitenOpenGLView_InputUpdated
func ebiten_EbitenOpenGLView_InputUpdated(x, y C.int) {
currentUI.input <- ebiten.InputState{int(x), int(y)}
func ebiten_EbitenOpenGLView_InputUpdated(inputType C.int, cx, cy C.int) {
if inputType == C.InputTypeMouseUp {
currentUI.input <- ebiten.InputState{-1, -1}
return
}
x, y := int(cx), int(cy)
x /= currentUI.screenScale
y /= currentUI.screenScale
if x < 0 {
x = 0
} else if currentUI.screenWidth <= x {
x = currentUI.screenWidth - 1
}
if y < 0 {
y = 0
} else if currentUI.screenHeight <= y {
y = currentUI.screenHeight - 1
}
currentUI.input <- ebiten.InputState{x, y}
}
func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int,

View File

@ -1,10 +1,11 @@
// -*- objc -*-
#include "ebiten_opengl_view.h"
#include "input.h"
void ebiten_EbitenOpenGLView_Initialized(void);
void ebiten_EbitenOpenGLView_Updating(void);
void ebiten_EbitenOpenGLView_InputUpdated(int x, int y);
void ebiten_EbitenOpenGLView_InputUpdated(enum InputType inputType, int x, int y);
// Reference:
// http://developer.apple.com/library/mac/#qa/qa1385/_index.html
@ -32,9 +33,6 @@ EbitenDisplayLinkCallback(CVDisplayLinkRef displayLink,
@implementation EbitenOpenGLView {
@private
CVDisplayLinkRef displayLink_;
size_t screenWidth_;
size_t screenHeight_;
size_t screenScale_;
}
- (void)dealloc {
@ -85,50 +83,26 @@ EbitenDisplayLinkCallback(CVDisplayLinkRef displayLink,
- (void)mouseDown:(NSEvent*)theEvent {
NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
int x = location.x / self->screenScale_;
int y = location.y / self->screenScale_;
if (x < 0) {
x = 0;
} else if (self->screenWidth_<= x) {
x = self->screenWidth_ - 1;
}
if (y < 0) {
y = 0;
} else if (self->screenHeight_<= y) {
y = self->screenHeight_ - 1;
}
ebiten_EbitenOpenGLView_InputUpdated(x, y);
int x = location.x;
int y = location.y;
ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDown, x, y);
}
- (void)mouseUp:(NSEvent*)theEvent {
(void)theEvent;
ebiten_EbitenOpenGLView_InputUpdated(-1, -1);
NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
int x = location.x;
int y = location.y;
ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseUp, x, y);
}
- (void)mouseDragged:(NSEvent*)theEvent {
NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
int x = location.x / self->screenScale_;
int y = location.y / self->screenScale_;
if (x < 0) {
x = 0;
} else if (self->screenWidth_<= x) {
x = self->screenWidth_ - 1;
}
if (y < 0) {
y = 0;
} else if (self->screenHeight_<= y) {
y = self->screenHeight_ - 1;
}
ebiten_EbitenOpenGLView_InputUpdated(x, y);
}
- (void)setScreenWidth:(size_t)screenWidth
screenHeight:(size_t)screenHeight
screenScale:(size_t)screenScale {
self->screenWidth_ = screenWidth;
self->screenHeight_ = screenHeight;
self->screenScale_ = screenScale;
int x = location.x;
int y = location.y;
ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDragged, x, y);
}
@end

View File

@ -9,9 +9,6 @@
@interface EbitenOpenGLView : NSOpenGLView
- (CVReturn)getFrameForTime:(CVTimeStamp const*)outputTime;
- (void)setScreenWidth:(size_t)screenWidth
screenHeight:(size_t)screenHeight
screenScale:(size_t)screenScale;
@end

10
ui/cocoa/input.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef GO_EBITEN_UI_COCOA_INPUT_H_
#define GO_EBITEN_UI_COCOA_INPUT_H_
enum InputType {
InputTypeMouseUp,
InputTypeMouseDragged,
InputTypeMouseDown,
};
#endif

View File

@ -24,9 +24,6 @@ static NSWindow* generateWindow(size_t width, size_t height, size_t scale, const
EbitenOpenGLView* glView =
[[EbitenOpenGLView alloc] initWithFrame:rect
pixelFormat:format];
[glView setScreenWidth:width
screenHeight:height
screenScale:scale];
[window setContentView:glView];
[window setTitle: [[NSString alloc] initWithUTF8String:title]];
//[window makeFirstResponder:glView];

View File

@ -117,7 +117,7 @@ func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int, title str
screenWidth, screenHeight, screenScale)
ui.graphicsDevice = graphicsDevice
graphicsDevice.Init()
game.Init(ui.graphicsDevice.TextureFactory())
input := make(chan ebiten.InputState)
@ -169,7 +169,7 @@ func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int, title str
// Set the callbacks
C.setGlutFuncs()
C.glutMainLoop()
}