mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 02:59:37 +00:00 
			
		
		
		
	spritebatch affine draw command
This commit is contained in:
		@ -124,6 +124,20 @@ public struct SpriteBatch {
 | 
				
			|||||||
      .texcoords(flip), color: color.linear)
 | 
					      .texcoords(flip), color: color.linear)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public mutating func draw(_ texture: RendererTexture2D, position: SIMD2<Float>, transform: simd_float2x2) {
 | 
				
			||||||
 | 
					    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
				
			||||||
 | 
					    self.drawQuad(texture, .positions(position, transform, Size<Float>(texture.size)))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public mutating func draw(_ texture: RendererTexture2D, position: SIMD2<Float>, transform: simd_float2x2,
 | 
				
			||||||
 | 
					    flip: Sprite.Flip = .none, color: Color<Float> = .white
 | 
				
			||||||
 | 
					  ) {
 | 
				
			||||||
 | 
					    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
				
			||||||
 | 
					    self.drawQuad(texture,
 | 
				
			||||||
 | 
					      .positions(position, transform, Size<Float>(texture.size)),
 | 
				
			||||||
 | 
					      .texcoords(flip), color: color.linear)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>, position: SIMD2<Float>) {
 | 
					  public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>, position: SIMD2<Float>) {
 | 
				
			||||||
    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
					    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
				
			||||||
    self.drawQuad(texture, .positions(position, source.size), .texcoords(texture.size, source))
 | 
					    self.drawQuad(texture, .positions(position, source.size), .texcoords(texture.size, source))
 | 
				
			||||||
@ -220,7 +234,25 @@ public struct SpriteBatch {
 | 
				
			|||||||
    flip: Sprite.Flip = .none, color: Color<Float> = .white
 | 
					    flip: Sprite.Flip = .none, color: Color<Float> = .white
 | 
				
			||||||
  ) {
 | 
					  ) {
 | 
				
			||||||
    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
					    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
				
			||||||
    self.drawQuad(texture, .positions(transform, source.size), .texcoords(texture.size, source), color: color.linear)
 | 
					    self.drawQuad(texture,
 | 
				
			||||||
 | 
					      .positions(transform, source.size),
 | 
				
			||||||
 | 
					      .texcoords(texture.size, source, flip), color: color.linear)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>,
 | 
				
			||||||
 | 
					    position: SIMD2<Float>, transform: simd_float2x2
 | 
				
			||||||
 | 
					  ) {
 | 
				
			||||||
 | 
					    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
				
			||||||
 | 
					    self.drawQuad(texture, .positions(position, transform, source.size), .texcoords(texture.size, source))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>,
 | 
				
			||||||
 | 
					    position: SIMD2<Float>, transform: simd_float2x2, flip: Sprite.Flip = .none, color: Color<Float> = .white
 | 
				
			||||||
 | 
					  ) {
 | 
				
			||||||
 | 
					    assert(self._active != .inactive, "call to SpriteBatch.draw without calling begin")
 | 
				
			||||||
 | 
					    self.drawQuad(texture,
 | 
				
			||||||
 | 
					      .positions(position, transform, source.size),
 | 
				
			||||||
 | 
					      .texcoords(texture.size, source, flip), color: color.linear)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public mutating func draw(_ texture: RendererTexture2D, vertices: [VertexType]) {
 | 
					  public mutating func draw(_ texture: RendererTexture2D, vertices: [VertexType]) {
 | 
				
			||||||
@ -338,6 +370,14 @@ fileprivate extension SpriteBatch.Quad {
 | 
				
			|||||||
      v11: (transform * .init(w, h, 1)).xy)
 | 
					      v11: (transform * .init(w, h, 1)).xy)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  static func positions(_ position: SIMD2<Float>, _ transform: simd_float2x2, _ size: Size<Float>) -> Self {
 | 
				
			||||||
 | 
					    .init(
 | 
				
			||||||
 | 
					      v00: position + .init(     0,      0) * transform,
 | 
				
			||||||
 | 
					      v01: position + .init(size.w,      0) * transform,
 | 
				
			||||||
 | 
					      v10: position + .init(     0, size.h) * transform,
 | 
				
			||||||
 | 
					      v11: position + .init(size.w, size.h) * transform)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static let texcoordsDefault = Self(
 | 
					  static let texcoordsDefault = Self(
 | 
				
			||||||
    v00: SIMD2<Float>(0, 1), v01: SIMD2<Float>(1, 1),
 | 
					    v00: SIMD2<Float>(0, 1), v01: SIMD2<Float>(1, 1),
 | 
				
			||||||
    v10: SIMD2<Float>(0, 0), v11: SIMD2<Float>(1, 0))
 | 
					    v10: SIMD2<Float>(0, 0), v11: SIMD2<Float>(1, 0))
 | 
				
			||||||
 | 
				
			|||||||
@ -81,12 +81,16 @@ internal class SpriteTestGame: GameDelegate {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Sliding door test
 | 
					    // Sliding door test
 | 
				
			||||||
    let doorAngle = max(sin(player.rotate * 2.6) - 0.75, 0) * .pi
 | 
					    let doorAngle = max(sin(player.rotate * 2.6) - 0.75, 0) * .pi
 | 
				
			||||||
 | 
					    let doorScale = SIMD2<Float>(24, 12)
 | 
				
			||||||
 | 
					    let doorAffine = simd_float2x2(
 | 
				
			||||||
 | 
					      .init( cos(doorAngle) * doorScale.x, sin(doorAngle) * doorScale.y),
 | 
				
			||||||
 | 
					      .init(-sin(doorAngle) * doorScale.x, cos(doorAngle) * doorScale.y))
 | 
				
			||||||
 | 
					    let doorOrigin = SIMD2<Float>(repeating: 1)
 | 
				
			||||||
 | 
					    let doorPosition = SIMD2<Float>(704 + 24, 1152 + 12) - doorOrigin * doorAffine
 | 
				
			||||||
    self.spriteBatch.draw(self.texture, source: Rect<Float>(
 | 
					    self.spriteBatch.draw(self.texture, source: Rect<Float>(
 | 
				
			||||||
        origin: .init(4 + cos(player.rotate / 1.2) * 4, 0),
 | 
					        origin: .init(4 + cos(player.rotate / 1.2) * 4, 0),
 | 
				
			||||||
        size:   .init(4 + cos(player.rotate / 1.3) * 4, 16)),
 | 
					        size:   .init(4 + cos(player.rotate / 1.3) * 4, 16)),
 | 
				
			||||||
      position: .init(704 + 24, 1152 + 12), scale: .init(24, 12),
 | 
					      position: doorPosition, transform: doorAffine, color: .red.mix(.white, 0.3))
 | 
				
			||||||
      angle: doorAngle, origin: SIMD2<Float>(repeating: 1),
 | 
					 | 
				
			||||||
      flip: .none, color: .red.mix(.white, 0.3))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Draw mouse cursor
 | 
					    // Draw mouse cursor
 | 
				
			||||||
    var mpos = Mouse.position
 | 
					    var mpos = Mouse.position
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user