duangan4070
duangan4070
2019-08-13 08:30

在屏幕上绘制缓冲区时绑定的不匹配(golang.org/x/exp/shiny/screen)

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

I am trying to use golang.org/x/exp/shiny/screen to create a windows and then paint a buffer on the screen.

I have assigned same bounds (height and width) to the window and the buffer I am publishing on the window, but there seems to be a mismatch when I see the results.

You can see in the picture, that the blue buffer which is uploaded to the window is not covering it completely.

enter image description here

package main

import (
    "image"
    "image/color"
    "log"

    "golang.org/x/exp/shiny/driver"
    "golang.org/x/exp/shiny/screen"
    "golang.org/x/mobile/event/key"
    "golang.org/x/mobile/event/lifecycle"
)

const (
    Row = 32
    Col = 64
)

// Colors
var (
    Black = color.RGBA{0, 0, 0, 1.0}
    White = color.RGBA{255, 255, 255, 1.0}
    Blue  = color.RGBA{0, 0, 255, 1.0}
)

func main() {

    go driver.Main(func(s screen.Screen) {
        opts := screen.NewWindowOptions{
            Height: Row,
            Width:  Col,
            Title:  "Chip-8 VM",
        }

        window, err := s.NewWindow(&opts)
        if err != nil {
            log.Print("Unable to create display window: ")
            log.Fatal(err)
            return
        }

        defer window.Release()

        dim := image.Point{Col, Row}
        drawBuff, err := s.NewBuffer(dim)
        if err != nil {
            log.Fatal(err)
        }

        defaultDrawToBuffer(drawBuff.RGBA())

        log.Print("Window bounds: ", opts)
        log.Printf("Buffer bounds: %s", drawBuff.Bounds())
        log.Printf("Buffer size: %s", drawBuff.Size())

        window.Upload(image.Point{}, drawBuff, drawBuff.Bounds())
        window.Publish()

        for {
            e := window.NextEvent()
            switch e := e.(type) {

            case lifecycle.Event:
                if e.To == lifecycle.StageDead {
                    return
                } else if e.To == lifecycle.StageFocused {
                    log.Print("Focus back on the screen!")
                }

            case key.Event:
                log.Print("pressed key: ", e.Code)
                // exit game
                if e.Code == key.CodeEscape {
                    return
                }
            case error:
                log.Print(e)
            }

        }
    })

    for {
    }

}

// Bounds: (0,0)-(64,32)
func defaultDrawToBuffer(img *image.RGBA) {
    b := img.Bounds()

    log.Printf("Bounds: %s", b.String())

    for x := b.Min.X; x < b.Max.X; x++ {
        for y := b.Min.Y; y < b.Max.Y; y++ {
            img.SetRGBA(x, y, Blue)
        }
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐