Speed up drawing and experiment with using rotation on the draw operation to get the right coordinates

This commit is contained in:
2018-03-23 01:27:38 +00:00
parent 7111e2e89b
commit c69beafa76
2 changed files with 49 additions and 30 deletions

View File

@@ -28,6 +28,7 @@ type env struct {
gameMap *maps.GameMap
set *sets.MapSet
objects map[string]*conv.Object
sprites map[string][]*pixel.Sprite
}
type state struct {
@@ -39,6 +40,7 @@ type state struct {
camPos pixel.Vec
zoom float64
rot float64
zIdx int
}
@@ -93,14 +95,16 @@ func (e *env) run() {
// camPos: pixel.V(0, float64(-pWin.Bounds().Size().Y)),
// camPos: pixel.V(float64(3700), float64(0)),
zoom: 1.0,
rot: 0.45,
}
pWin.SetSmooth(true)
win.Run(func() {
oldState := *state
state = state.runStep(pWin)
if oldState != *state || oldState.step == 0 {
log.Printf("zoom=%.2f zIdx=%v camPos=%#v", state.zoom, state.zIdx, state.camPos)
log.Printf("zoom=%.2f rot=%.4f zIdx=%v camPos=%#v", state.zoom, state.rot, state.zIdx, state.camPos)
state.present(pWin)
}
@@ -126,7 +130,7 @@ func (e *env) getSprite(palette []string, ref maps.ObjRef) (*conv.Sprite, *conv.
var (
cellWidth = 128 // I think, anyway
cellHeight = 64
cellHeight = 63
)
// TODO: build all the sprites in the set into a single spritesheet so we can
@@ -142,45 +146,49 @@ func (s *state) present(pWin *pixelgl.Window) {
cam = cam.ScaledXY(center, pixel.Vec{1.0, -1.0}) // invert the Y axis
cam = cam.Scaled(pixel.ZV, s.zoom) // apply current zoom factor
cam = cam.Moved(center.Sub(s.camPos)) // Make it central
cam = cam.Rotated(center.Sub(s.camPos), -0.785) // Apply isometric angle
//cam = cam.Rotated(center.Sub(s.camPos), -0.785) // Apply isometric angle
s.cam = cam
pWin.SetMatrix(s.cam)
pWin.SetMatrix(cam)
// TODO: bounds clipping
z := int(s.zIdx)
for y := int(gameMap.MinLength); y < int(gameMap.MaxLength); y++ {
for x := int(gameMap.MinWidth); x < int(gameMap.MaxWidth); x++ {
for x := int(gameMap.MinWidth); x < int(gameMap.MaxWidth); x++ {
for y := int(gameMap.MinLength); y < int(gameMap.MaxLength); y++ {
cell := gameMap.Cells.At(x, y, z)
// TODO: optimize drawing, etc
surfaceSprite, surfaceObj := s.env.getSprite(s.env.set.SurfacePalette, cell.Surface)
if surfaceSprite == nil {
log.Printf("no surfaceSprite")
continue
}
surfaceSprite, obj := s.env.getSprite(s.env.set.SurfacePalette, cell.Surface)
if surfaceSprite.Width != cellWidth {
log.Printf("WARN: Surface sprite has wrong width: %v", surfaceSprite.Width)
}
fX := float64(x)
fY := float64(y)
fWidth := float64(cellWidth)
fLength := float64(cellHeight)
yPos := float64((y - int(gameMap.MinLength)) * cellHeight / 2)
xPos := float64((x - int(gameMap.MinWidth)) * cellWidth)
// numCellsToOffset := math.Abs(float64(gameMap.Width() / 2) - fY)
// Tiles should be flush to each other. Offset even-numbered tiles
// across to get this effect
if y%2 == 0 {
xPos += float64(cellWidth) / 2.0
}
// xOffset := numCellsToOffset * (fWidth/2)
//yOffset := 0.0 //numCellsToOffset * (fLength/2)
xPos := (fX - fY) * fWidth / 2 // - xOffset
yPos := fY * fLength // - yOffset
// The rotation translates the rectangular coordinates to diamond
// ones \o/
orig := pixel.V(xPos, yPos)
rotated := orig
rotated = rotated.Rotated(s.rot)
// rotated = rotated.Rotated(0.464) // 26.565'
// rotated = orig.Rotated(0.35)
// rotated = rotated.Rotated(0.524) // 30'
// rotated = rotated.Rotated(0.785) // 45'
// rotated = rotated.Rotated(1.571) // 90'
pic := surfaceSprite.Pic
spr := pixel.NewSprite(pic, pic.Bounds())
log.Printf(
"cell(%v,%v,%v): %s %d: pix(%v,%v - %v,%v)",
x, y, z, surfaceObj.Name, cell.Surface.Index(),
xPos, yPos, xPos+float64(surfaceSprite.Width), yPos+float64(surfaceSprite.Height),
"cell(%v,%v): %s %d: %#v -> %#v",
x, y, obj.Name, cell.Surface.Index(),
orig, rotated,
)
spr.Draw(pWin, pixel.IM.Moved(pixel.V(float64(xPos), float64(yPos))))
surfaceSprite.Spr.Draw(pWin, pixel.IM.Moved(rotated))
}
}
}
@@ -215,6 +223,14 @@ func (s *state) handleKeys(pWin *pixelgl.Window) {
}
}
if pWin.Pressed(pixelgl.KeyMinus) {
s.rot -= 0.001
}
if pWin.Pressed(pixelgl.KeyEqual) {
s.rot += 0.001
}
/* TODO: restore this
// Show details of clicked-on cell in termal
if pWin.JustPressed(pixelgl.MouseButtonLeft) {

View File

@@ -19,6 +19,7 @@ type Sprite struct {
Height int
Pic *pixel.PictureData
Spr *pixel.Sprite
}
type Object struct {
@@ -33,10 +34,12 @@ func ConvertObject(rawObj *data.Object, name string) *Object {
}
for i, rawSpr := range rawObj.Sprites {
pic := spriteToPic(rawSpr)
out.Sprites[i] = Sprite{
Width: int(rawSpr.Width),
Height: int(rawSpr.Height),
Pic: spriteToPic(rawSpr),
Pic: pic,
Spr: pixel.NewSprite(pic, pic.Bounds()),
}
}