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,8 +14,18 @@ var TexturePaths = map[string]string{
"text": "images/text.png", "text": "images/text.png",
} }
type Size struct {
Width int
Height int
}
var RenderTargetSizes = map[string]Size{
"whole": Size{256, 254},
}
type drawInfo struct { type drawInfo struct {
textures map[string]graphics.TextureId textures map[string]graphics.TextureId
renderTargets map[string]graphics.RenderTargetId
inputStr string inputStr string
textureX int textureX int
textureY int textureY int
@ -41,6 +51,7 @@ func NewGame() *Game {
counter: 0, counter: 0,
drawInfo: drawInfo{ drawInfo: drawInfo{
textures: map[string]graphics.TextureId{}, textures: map[string]graphics.TextureId{},
renderTargets: map[string]graphics.RenderTargetId{},
textureX: 0, textureX: 0,
textureY: 0, textureY: 0,
textureAngle: 0, textureAngle: 0,
@ -56,11 +67,32 @@ func (game *Game) OnTextureCreated(e graphics.TextureCreatedEvent) {
game.textures[e.Tag.(string)] = e.Id 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) { func (game *Game) OnInputStateUpdated(e ui.InputStateUpdatedEvent) {
game.inputX, game.inputY = e.X, e.Y 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() { func (game *Game) Update() {
if !game.isInitialized() {
return
}
const textureWidth = 57 const textureWidth = 57
const textureHeight = 26 const textureHeight = 26
@ -78,27 +110,37 @@ func (game *Game) Update() {
} }
game.drawInfo.textureAngle = 2 * math.Pi * float64(game.counter) / 600 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 = geo
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))
// Update for the next frame. // Update for the next frame.
game.inputPrevX, game.inputPrevY = game.inputX, game.inputY game.inputPrevX, game.inputPrevY = game.inputX, game.inputY
} }
func (game *Game) Draw(g graphics.Canvas) { func (game *Game) Draw(g graphics.Canvas) {
if len(game.drawInfo.textures) < len(TexturePaths) { if !game.isInitialized() {
return 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.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, 6, 6, &color.RGBA{0x0, 0x0, 0x0, 0x80})
game.drawText(g, game.drawInfo.inputStr, 5, 5, color.White) 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) { 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) var u UI = cocoa.New(screenWidth, screenHeight, screenScale, title)
textureCreated := u.TextureCreated() textureCreated := u.TextureCreated()
renderTargetCreated := u.RenderTargetCreated()
inputStateUpdated := u.InputStateUpdated() inputStateUpdated := u.InputStateUpdated()
screenSizeUpdated := u.ScreenSizeUpdated() 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) drawing := make(chan *graphics.LazyCanvas)
go func() { go func() {
game := NewGame() game := NewGame()
@ -64,18 +73,12 @@ func main() {
tick := time.Tick(frameTime) tick := time.Tick(frameTime)
for { for {
select { select {
case e, ok := <-textureCreated: case e := <-textureCreated:
if ok {
game.OnTextureCreated(e) game.OnTextureCreated(e)
} else { case e := <-renderTargetCreated:
textureCreated = nil game.OnRenderTargetCreated(e)
} case e := <-inputStateUpdated:
case e, ok := <-inputStateUpdated:
if ok {
game.OnInputStateUpdated(e) game.OnInputStateUpdated(e)
} else {
inputStateUpdated = nil
}
case _, ok := <-screenSizeUpdated: case _, ok := <-screenSizeUpdated:
if ok { if ok {
// Do nothing // Do nothing

View File

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