func (v *Viewport) ScreenCoordinatesToTile(sx, sy int, config DisplayConfig) (float64, float64) { // I have never been more ashamed of code I have written w, h := float64(config.TileW), float64(config.TileH) fx, fy := float32(sx), float32(sy) var trans allegro.Transform trans.Identity() // Builds the viewport alignment matrix trans.Build(float32(-v.x), float32(-v.y), float32(v.xZoom), float32(v.yZoom), 0) // Invert it to get back to pixel coordinates trans.Invert() // We need to translate back half a width to get to the pivot of the tiles trans.Translate(-float32(w/2), 0) x, y := trans.Apply(fx, fy) // Then we manually rotate it (because I'm bad at maths I guess) tx := float64(float64(y)*w-float64(x)*h) / (w * h) ty := float64(float64(y)*w+float64(x)*h) / (w * h) return tx, ty }
func (d *DisplayEngine) drawFrame() { toDraw := make([][]*resources.Bitmap, d.config.MapW*d.config.MapH) drawPasses := 0 for x := 0; x < d.config.MapW; x++ { for y := 0; y < d.config.MapH; y++ { toDraw[x*d.config.MapW+y] = (*d.gameEngine).GetTile(x, y) length := len(toDraw[x*d.config.MapW+y]) if length > drawPasses { drawPasses = length } } } viewport := d.viewport font := allegro.CreateBuiltinFont() // Don't want anyone changing the viewport mid frame or any such highjinks d.Display.SetTargetBackbuffer() allegro.RunInThread(func() { r, g, b, a := d.config.BGColor.GetRGBA() gl.ClearColor( gl.GLclampf(r)/255.0, gl.GLclampf(g)/255.0, gl.GLclampf(b)/255.0, gl.GLclampf(a)/255.0) gl.Clear(gl.COLOR_BUFFER_BIT) viewport.SetupTransform() for p := 0; p < drawPasses; p++ { m := d.config.MapW n := d.config.MapH for s := 0; s < m+n; s++ { for x := 0; x < s; x++ { y := s - x - 1 if x >= m || y < 0 || y >= n { continue } if len(toDraw[x*d.config.MapW+y]) < p { continue } // Coordinates in terms of pixels px := (y - x) * d.config.TileW / 2 py := (x + y) * d.config.TileH / 2 bmp := toDraw[x*d.config.MapW+y][p] /* ox := bmp.OffX oy := bmp.OffY*/ bw, bh := bmp.W, bmp.H if viewport.OnScreen(px, py, bw, bh) { gl.Begin(gl.QUADS) bmp.Tex.Bind(gl.TEXTURE_2D) gl.TexCoord2f(0, 0) gl.Vertex3i(px, py, 0) gl.TexCoord2f(0, 1) gl.Vertex3i(px, py+bw, 0) gl.TexCoord2f(1, 1) gl.Vertex3i(px+bh, py+bw, 0) gl.TexCoord2f(1, 0) gl.Vertex3i(px+bh, py, 0) gl.End() } } } } gl.Flush() }) var trans allegro.Transform trans.Identity() trans.Use() font.Draw(allegro.CreateColor(0, 255, 0, 255), 0, 0, 0, fmt.Sprint(int(d.fps))) allegro.Flip() d.frameDrawing.Unlock() }