Speed up drawing and experiment with using rotation on the draw operation to get the right coordinates
This commit is contained in:
@@ -28,6 +28,7 @@ type env struct {
|
|||||||
gameMap *maps.GameMap
|
gameMap *maps.GameMap
|
||||||
set *sets.MapSet
|
set *sets.MapSet
|
||||||
objects map[string]*conv.Object
|
objects map[string]*conv.Object
|
||||||
|
sprites map[string][]*pixel.Sprite
|
||||||
}
|
}
|
||||||
|
|
||||||
type state struct {
|
type state struct {
|
||||||
@@ -39,6 +40,7 @@ type state struct {
|
|||||||
camPos pixel.Vec
|
camPos pixel.Vec
|
||||||
|
|
||||||
zoom float64
|
zoom float64
|
||||||
|
rot float64
|
||||||
|
|
||||||
zIdx int
|
zIdx int
|
||||||
}
|
}
|
||||||
@@ -93,14 +95,16 @@ func (e *env) run() {
|
|||||||
// camPos: pixel.V(0, float64(-pWin.Bounds().Size().Y)),
|
// camPos: pixel.V(0, float64(-pWin.Bounds().Size().Y)),
|
||||||
// camPos: pixel.V(float64(3700), float64(0)),
|
// camPos: pixel.V(float64(3700), float64(0)),
|
||||||
zoom: 1.0,
|
zoom: 1.0,
|
||||||
|
rot: 0.45,
|
||||||
}
|
}
|
||||||
|
pWin.SetSmooth(true)
|
||||||
|
|
||||||
win.Run(func() {
|
win.Run(func() {
|
||||||
oldState := *state
|
oldState := *state
|
||||||
state = state.runStep(pWin)
|
state = state.runStep(pWin)
|
||||||
|
|
||||||
if oldState != *state || oldState.step == 0 {
|
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)
|
state.present(pWin)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +130,7 @@ func (e *env) getSprite(palette []string, ref maps.ObjRef) (*conv.Sprite, *conv.
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
cellWidth = 128 // I think, anyway
|
cellWidth = 128 // I think, anyway
|
||||||
cellHeight = 64
|
cellHeight = 63
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: build all the sprites in the set into a single spritesheet so we can
|
// 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.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.Scaled(pixel.ZV, s.zoom) // apply current zoom factor
|
||||||
cam = cam.Moved(center.Sub(s.camPos)) // Make it central
|
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
|
s.cam = cam
|
||||||
pWin.SetMatrix(s.cam)
|
pWin.SetMatrix(cam)
|
||||||
|
|
||||||
// TODO: bounds clipping
|
// TODO: bounds clipping
|
||||||
z := int(s.zIdx)
|
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)
|
cell := gameMap.Cells.At(x, y, z)
|
||||||
|
|
||||||
// TODO: optimize drawing, etc
|
surfaceSprite, obj := s.env.getSprite(s.env.set.SurfacePalette, cell.Surface)
|
||||||
surfaceSprite, surfaceObj := s.env.getSprite(s.env.set.SurfacePalette, cell.Surface)
|
|
||||||
if surfaceSprite == nil {
|
|
||||||
log.Printf("no surfaceSprite")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if surfaceSprite.Width != cellWidth {
|
fX := float64(x)
|
||||||
log.Printf("WARN: Surface sprite has wrong width: %v", surfaceSprite.Width)
|
fY := float64(y)
|
||||||
}
|
fWidth := float64(cellWidth)
|
||||||
|
fLength := float64(cellHeight)
|
||||||
|
|
||||||
yPos := float64((y - int(gameMap.MinLength)) * cellHeight / 2)
|
// numCellsToOffset := math.Abs(float64(gameMap.Width() / 2) - fY)
|
||||||
xPos := float64((x - int(gameMap.MinWidth)) * cellWidth)
|
|
||||||
|
|
||||||
// Tiles should be flush to each other. Offset even-numbered tiles
|
// xOffset := numCellsToOffset * (fWidth/2)
|
||||||
// across to get this effect
|
//yOffset := 0.0 //numCellsToOffset * (fLength/2)
|
||||||
if y%2 == 0 {
|
|
||||||
xPos += float64(cellWidth) / 2.0
|
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(
|
log.Printf(
|
||||||
"cell(%v,%v,%v): %s %d: pix(%v,%v - %v,%v)",
|
"cell(%v,%v): %s %d: %#v -> %#v",
|
||||||
x, y, z, surfaceObj.Name, cell.Surface.Index(),
|
x, y, obj.Name, cell.Surface.Index(),
|
||||||
xPos, yPos, xPos+float64(surfaceSprite.Width), yPos+float64(surfaceSprite.Height),
|
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
|
/* TODO: restore this
|
||||||
// Show details of clicked-on cell in termal
|
// Show details of clicked-on cell in termal
|
||||||
if pWin.JustPressed(pixelgl.MouseButtonLeft) {
|
if pWin.JustPressed(pixelgl.MouseButtonLeft) {
|
||||||
|
@@ -19,6 +19,7 @@ type Sprite struct {
|
|||||||
Height int
|
Height int
|
||||||
|
|
||||||
Pic *pixel.PictureData
|
Pic *pixel.PictureData
|
||||||
|
Spr *pixel.Sprite
|
||||||
}
|
}
|
||||||
|
|
||||||
type Object struct {
|
type Object struct {
|
||||||
@@ -33,10 +34,12 @@ func ConvertObject(rawObj *data.Object, name string) *Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, rawSpr := range rawObj.Sprites {
|
for i, rawSpr := range rawObj.Sprites {
|
||||||
|
pic := spriteToPic(rawSpr)
|
||||||
out.Sprites[i] = Sprite{
|
out.Sprites[i] = Sprite{
|
||||||
Width: int(rawSpr.Width),
|
Width: int(rawSpr.Width),
|
||||||
Height: int(rawSpr.Height),
|
Height: int(rawSpr.Height),
|
||||||
Pic: spriteToPic(rawSpr),
|
Pic: pic,
|
||||||
|
Spr: pixel.NewSprite(pic, pic.Bounds()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user