doudang1890 2019-05-30 01:18
浏览 131

如何在2D游戏中全屏处理?

I'm making a game using Go with the rendering library "pixel".

I am trying to get fullscreen/resizing working, but I have an issue, and that's how to deal with stretching of images

I've seen and have used the solution of a "letterbox" effect for the game.

That is, drawing the game in the same aspect ratio, so stretching wouldn't be an issue, leaving the extra space as black bars.

My issue is, when trying to do this in this rendering library, I can only scale the matrix of the "Canvas" I'm drawing on.

I'm used to SFML with C++ where I can just define a fixed size for the "View" [what's being drawn on], not scaling it.

This is how I'm getting the current scaling for the matrix, it's incorrect, but it's what I have.

camZoom is 2.0, it's in there so the screen is bigger. If the camZoom is 1.0 [normal], the images are too small.

func letterBox(win *pixelgl.Window) {
windowRatio := winWidth / winHeight
viewRatio := win.Bounds().W() / win.Bounds().H()
sizeX := 1.
sizeY := 1.

horizontalSpacing := true
if windowRatio < viewRatio {
    horizontalSpacing = false
}

if horizontalSpacing {
    sizeX = viewRatio / windowRatio
} else {
    sizeY = windowRatio / viewRatio
}

viewMatrix = pixel.IM.
    Moved(pixel.V(win.Bounds().Center().X/camZoom, win.Bounds().Center().Y/camZoom)).
    ScaledXY(pixel.V(win.Bounds().Center().X/camZoom, win.Bounds().Center().Y/camZoom), pixel.V(sizeY, sizeX))

}

Here's what it currently looks like:

Normal [no resizing done, 1024x768]: normal

Width of window increased [shrinks] [x increased]2

Height of window increased [stretches on X, hiding most of 'Canvas'] [y increased]3

Fullscreen [just keeps it's original size, but the width of the 'canvas' is slightly shrunken] [fs]4

I just can't really figure out the math to it.

If this is not the best way to solve the full-screen issue I have, then let me know and I can make another question, but I was told this is how you should do it.

  • 写回答

1条回答 默认 最新

  • douhuireng4407 2019-05-30 04:40
    关注

    Ended up being super simple just a math error on my part.

    func letterBox(win *pixelgl.Window) {
    sizeX := 1.
    sizeY := 1.
    
    if win.Bounds().H()-winHeight > win.Bounds().W()-winWidth {
        sizeX = win.Bounds().W() / winWidth
        sizeY = win.Bounds().W() / winWidth
    } else {
        sizeX = win.Bounds().H() / winHeight
        sizeY = win.Bounds().H() / winHeight
    }
    
    viewMatrix = pixel.IM.
        Moved(pixel.V(win.Bounds().Center().X/camZoom, win.Bounds().Center().Y/camZoom)).
        ScaledXY(pixel.V(win.Bounds().Center().X/camZoom, win.Bounds().Center().Y/camZoom), pixel.V(sizeX, sizeY))
    

    }

    Just had to figure out if the width difference of the newly-sized window was greater, or the height, then apply the same ratio [new window width / width of old frame] to scale the matrix properly. Dumb answer to a dumb question.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料