Refactoring

This commit is contained in:
Hajime Hoshi 2013-12-08 23:03:00 +09:00
parent 05fbee10d4
commit 698d176ec3
3 changed files with 77 additions and 33 deletions

View File

@ -14,13 +14,23 @@ var TexturePaths = map[string]string{
"text": "images/text.png",
}
type Size struct {
Width int
Height int
}
var RenderTargetSizes = map[string]Size{
"whole": Size{256, 254},
}
type drawInfo struct {
textures map[string]graphics.TextureId
inputStr string
textureX int
textureY int
textureAngle float64
textureGeo matrix.Geometry
textures map[string]graphics.TextureId
renderTargets map[string]graphics.RenderTargetId
inputStr string
textureX int
textureY int
textureAngle float64
textureGeo matrix.Geometry
}
type Game struct {
@ -40,11 +50,12 @@ func NewGame() *Game {
inputPrevY: -1,
counter: 0,
drawInfo: drawInfo{
textures: map[string]graphics.TextureId{},
textureX: 0,
textureY: 0,
textureAngle: 0,
textureGeo: matrix.IdentityGeometry(),
textures: map[string]graphics.TextureId{},
renderTargets: map[string]graphics.RenderTargetId{},
textureX: 0,
textureY: 0,
textureAngle: 0,
textureGeo: matrix.IdentityGeometry(),
},
}
}
@ -56,11 +67,32 @@ func (game *Game) OnTextureCreated(e graphics.TextureCreatedEvent) {
game.textures[e.Tag.(string)] = e.Id
}
func (game *Game) OnRenderTargetCreated(e graphics.RenderTargetCreatedEvent) {
if e.Error != nil {
panic(e.Error)
}
game.renderTargets[e.Tag.(string)] = e.Id
}
func (game *Game) OnInputStateUpdated(e ui.InputStateUpdatedEvent) {
game.inputX, game.inputY = e.X, e.Y
}
func (game *Game) isInitialized() bool {
if len(game.drawInfo.textures) < len(TexturePaths) {
return false
}
if len(game.drawInfo.renderTargets) < len(RenderTargetSizes) {
return false
}
return true
}
func (game *Game) Update() {
if !game.isInitialized() {
return
}
const textureWidth = 57
const textureHeight = 26
@ -78,27 +110,37 @@ func (game *Game) Update() {
}
game.drawInfo.textureAngle = 2 * math.Pi * float64(game.counter) / 600
game.drawInfo.textureGeo = matrix.IdentityGeometry()
geo := matrix.IdentityGeometry()
geo.Translate(-textureWidth/2, -textureHeight/2)
geo.Rotate(game.drawInfo.textureAngle)
geo.Translate(textureWidth/2, textureHeight/2)
geo.Translate(float64(game.textureX), float64(game.textureY))
game.drawInfo.textureGeo.Translate(-textureWidth/2, -textureHeight/2)
game.drawInfo.textureGeo.Rotate(game.drawInfo.textureAngle)
game.drawInfo.textureGeo.Translate(textureWidth/2, textureHeight/2)
game.drawInfo.textureGeo.Translate(float64(game.textureX), float64(game.textureY))
game.drawInfo.textureGeo = geo
// Update for the next frame.
game.inputPrevX, game.inputPrevY = game.inputX, game.inputY
}
func (game *Game) Draw(g graphics.Canvas) {
if len(game.drawInfo.textures) < len(TexturePaths) {
if !game.isInitialized() {
return
}
g.Fill(128, 128, 255)
whole := game.drawInfo.renderTargets["whole"]
g.SetOffscreen(whole)
g.Fill(0x40, 0x60, 0xb0)
game.drawTexture(g, game.drawInfo.textureGeo, matrix.IdentityColor())
game.drawText(g, game.drawInfo.inputStr, 6, 6, &color.RGBA{0x0, 0x0, 0x0, 0x80})
game.drawText(g, game.drawInfo.inputStr, 5, 5, color.White)
g.ResetOffscreen()
g.DrawRenderTarget(whole, matrix.IdentityGeometry(), matrix.IdentityColor())
wholeGeo := matrix.IdentityGeometry()
wholeGeo.Scale(0.25, 0.25)
wholeGeo.Translate(256*3/4, 240*3/4)
g.DrawRenderTarget(whole, wholeGeo, matrix.IdentityColor())
}
func (game *Game) drawText(g graphics.Canvas, text string, x, y int, clr color.Color) {

View File

@ -42,6 +42,7 @@ func main() {
var u UI = cocoa.New(screenWidth, screenHeight, screenScale, title)
textureCreated := u.TextureCreated()
renderTargetCreated := u.RenderTargetCreated()
inputStateUpdated := u.InputStateUpdated()
screenSizeUpdated := u.ScreenSizeUpdated()
@ -57,6 +58,14 @@ func main() {
}()
}
for tag, size := range RenderTargetSizes {
tag := tag
size := size
go func() {
u.CreateRenderTarget(tag, size.Width, size.Height)
}()
}
drawing := make(chan *graphics.LazyCanvas)
go func() {
game := NewGame()
@ -64,18 +73,12 @@ func main() {
tick := time.Tick(frameTime)
for {
select {
case e, ok := <-textureCreated:
if ok {
game.OnTextureCreated(e)
} else {
textureCreated = nil
}
case e, ok := <-inputStateUpdated:
if ok {
game.OnInputStateUpdated(e)
} else {
inputStateUpdated = nil
}
case e := <-textureCreated:
game.OnTextureCreated(e)
case e := <-renderTargetCreated:
game.OnRenderTargetCreated(e)
case e := <-inputStateUpdated:
game.OnInputStateUpdated(e)
case _, ok := <-screenSizeUpdated:
if ok {
// Do nothing

View File

@ -66,8 +66,6 @@ void* CreateWindow(size_t width, size_t height, const char* title, void* glConte
return window;
}
static BOOL initialBoot = YES;
void PollEvents(void) {
for (;;) {
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
@ -79,6 +77,7 @@ void PollEvents(void) {
}
[NSApp sendEvent:event];
}
static BOOL initialBoot = YES;
if (initialBoot) {
[NSApp activateIgnoringOtherApps:YES];
initialBoot = NO;