Skip to content

Commit

Permalink
compatible with old games
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed May 13, 2024
1 parent 4926c4a commit 645c183
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
8 changes: 6 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,22 @@ type mapConfig struct {
}

const (
mapModeFillRatio = iota
mapModeFill = iota
mapModeRepeat
mapModeFillRatio
mapModeFillCut
)

func toMapMode(mode string) int {
switch mode {
case "repeat":
return mapModeRepeat
case "fillCut":
return mapModeFillCut
case "fillRatio":
return mapModeFillRatio
}
return mapModeFillRatio
return mapModeFill
}

type projConfig struct {
Expand Down
33 changes: 24 additions & 9 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ func (p *Game) drawBackground(dc drawContext) {
}
}

} else if p.mapMode == mapModeFillRatio {
} else {
var imgW, imgH, dstW, dstH float32
imgW = float32(img.Bounds().Dx())
imgH = float32(img.Bounds().Dy())
Expand All @@ -1000,16 +1000,31 @@ func (p *Game) drawBackground(dc drawContext) {

options.Address = ebiten.AddressClampToZero
imgRadio := (imgW / imgH)
winRadio := (worldW / worldH)
// scale image's height to fit window's height
isScaleHeight := imgRadio > winRadio
if isScaleHeight {
dstH = worldH
dstW = dstH * imgRadio
} else {
worldRadio := (worldW / worldH)
// scale image's height to fit world's height
isScaleHeight := imgRadio > worldRadio
switch p.mapMode {
default:
dstW = worldW
dstH = dstW / imgRadio
dstH = worldH
case mapModeFillCut:
if isScaleHeight {
dstW = worldW
dstH = dstW / imgRadio
} else {
dstH = worldH
dstW = dstH * imgRadio
}
case mapModeFillRatio:
if isScaleHeight {
dstH = worldH
dstW = dstH * imgRadio
} else {
dstW = worldW
dstH = dstW / imgRadio
}
}

var cx, cy float32
cx = (worldW - dstW) / 2.0
cy = (worldH - dstH) / 2.0
Expand Down

0 comments on commit 645c183

Please sign in to comment.