mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-03 13:11:33 +00:00
spritebatch viewport changes
This commit is contained in:
@ -439,7 +439,7 @@ public class Renderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createSpriteBatch() -> SpriteBatch {
|
func createSpriteBatch() -> SpriteBatch {
|
||||||
return SpriteBatch(self)
|
return SpriteBatch(self, Rect(origin: .zero, size: Size<Float>(self.backBufferSize)))
|
||||||
}
|
}
|
||||||
|
|
||||||
internal func setupBatch(environment: Environment, camera: Camera) {
|
internal func setupBatch(environment: Environment, camera: Camera) {
|
||||||
|
@ -13,11 +13,12 @@ public struct SpriteBatch {
|
|||||||
private var _mesh: RendererDynamicMesh<VertexType, IndexType>
|
private var _mesh: RendererDynamicMesh<VertexType, IndexType>
|
||||||
private var _instances = [SpriteInstance]()
|
private var _instances = [SpriteInstance]()
|
||||||
|
|
||||||
public var viewport: Rect<Float>? = nil
|
public var viewport: Rect<Float>
|
||||||
|
|
||||||
internal init(_ renderer: Renderer) {
|
internal init(_ renderer: Renderer, _ viewport: Rect<Float>) {
|
||||||
self._renderer = renderer
|
self._renderer = renderer
|
||||||
self._mesh = renderer.createDynamicMesh(vertexCapacity: 4096, indexCapacity: 4096)!
|
self._mesh = renderer.createDynamicMesh(vertexCapacity: 4096, indexCapacity: 4096)!
|
||||||
|
self.viewport = viewport
|
||||||
}
|
}
|
||||||
|
|
||||||
//MARK: - Public API
|
//MARK: - Public API
|
||||||
@ -89,14 +90,14 @@ public struct SpriteBatch {
|
|||||||
|
|
||||||
public mutating func draw(_ texture: RendererTexture2D, destination: Rect<Float>?) {
|
public mutating func draw(_ texture: RendererTexture2D, destination: Rect<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(destination ?? self.viewport ?? Rect<Float>(self._renderer.frame)))
|
self.drawQuad(texture, .positions(destination ?? self.viewport))
|
||||||
}
|
}
|
||||||
|
|
||||||
public mutating func draw(_ texture: RendererTexture2D, destination: Rect<Float>?,
|
public mutating func draw(_ texture: RendererTexture2D, destination: Rect<Float>?,
|
||||||
angle: Float = 0.0, center: Point<Float>? = .zero, flip: Sprite.Flip = .none, color: Color<Float> = .white
|
angle: Float = 0.0, center: Point<Float>? = .zero, 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")
|
||||||
let dst = destination ?? self.viewport ?? Rect<Float>(self._renderer.frame)
|
let dst = destination ?? self.viewport
|
||||||
if dst.size.w.isZero || dst.size.h.isZero { return }
|
if dst.size.w.isZero || dst.size.h.isZero { return }
|
||||||
let texCoord = Quad.texcoords(flip)
|
let texCoord = Quad.texcoords(flip)
|
||||||
let color = color.linear
|
let color = color.linear
|
||||||
@ -182,17 +183,14 @@ public struct SpriteBatch {
|
|||||||
|
|
||||||
public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>, destination: Rect<Float>?) {
|
public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>, destination: Rect<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,
|
self.drawQuad(texture, .positions(destination ?? self.viewport), .texcoords(texture.size, source))
|
||||||
.positions(destination ?? self.viewport ?? Rect<Float>(self._renderer.frame)),
|
|
||||||
.texcoords(texture.size, source))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>, destination: Rect<Float>?,
|
public mutating func draw(_ texture: RendererTexture2D, source: Rect<Float>, destination: Rect<Float>?,
|
||||||
color: Color<Float> = .white
|
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,
|
self.drawQuad(texture, .positions(destination ?? self.viewport),
|
||||||
.positions(destination ?? self.viewport ?? Rect<Float>(self._renderer.frame)),
|
|
||||||
.texcoords(texture.size, source), color: color.linear)
|
.texcoords(texture.size, source), color: color.linear)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +198,7 @@ public struct SpriteBatch {
|
|||||||
angle: Float = 0.0, center: Point<Float>? = .zero, flip: Sprite.Flip = .none, color: Color<Float> = .white
|
angle: Float = 0.0, center: Point<Float>? = .zero, 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")
|
||||||
let dst = destination ?? self.viewport ?? Rect<Float>(self._renderer.frame)
|
let dst = destination ?? self.viewport
|
||||||
if dst.size.w.isZero || dst.size.h.isZero { return }
|
if dst.size.w.isZero || dst.size.h.isZero { return }
|
||||||
let texCoord = Quad.texcoords(texture.size, source, flip)
|
let texCoord = Quad.texcoords(texture.size, source, flip)
|
||||||
let color = color.linear
|
let color = color.linear
|
||||||
@ -255,7 +253,7 @@ public struct SpriteBatch {
|
|||||||
assert(self._instances.count > 0)
|
assert(self._instances.count > 0)
|
||||||
|
|
||||||
if self._active == .begin {
|
if self._active == .begin {
|
||||||
self._renderer.setupBatch(blendMode: self._blendMode, frame: self.viewport ?? .init(self._renderer.frame))
|
self._renderer.setupBatch(blendMode: self._blendMode, frame: self.viewport)
|
||||||
self._active = .active
|
self._active = .active
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,8 +10,6 @@ internal class SpriteTestGame: GameDelegate {
|
|||||||
|
|
||||||
func create(_ renderer: Renderer) {
|
func create(_ renderer: Renderer) {
|
||||||
self.spriteBatch = renderer.createSpriteBatch()
|
self.spriteBatch = renderer.createSpriteBatch()
|
||||||
// Uncomment to squeesh
|
|
||||||
//self.spriteBatch.viewport = .init(renderer.frame)
|
|
||||||
renderer.clearColor = .init(hue: 301.2, saturation: 0.357, value: 0.488).linear // .magenta.mix(.white, 0.4).mix(.black, 0.8)
|
renderer.clearColor = .init(hue: 301.2, saturation: 0.357, value: 0.488).linear // .magenta.mix(.white, 0.4).mix(.black, 0.8)
|
||||||
self.texture = renderer.loadTexture(resourcePath: "test.png")
|
self.texture = renderer.loadTexture(resourcePath: "test.png")
|
||||||
self.wireShark = renderer.loadTexture(resourcePath: "wireshark.png")
|
self.wireShark = renderer.loadTexture(resourcePath: "wireshark.png")
|
||||||
@ -59,7 +57,7 @@ internal class SpriteTestGame: GameDelegate {
|
|||||||
self.spriteBatch.draw(self.texture,
|
self.spriteBatch.draw(self.texture,
|
||||||
source: .init(
|
source: .init(
|
||||||
origin: .init(scalar: fmod(Float(time.total), 32)),
|
origin: .init(scalar: fmod(Float(time.total), 32)),
|
||||||
size: (spriteBatch.viewport?.size ?? Size<Float>(renderer.frame.size)) * 0.01),
|
size: spriteBatch.viewport.size * 0.01),
|
||||||
destination: nil,
|
destination: nil,
|
||||||
color: .init(renderer.clearColor).setAlpha(0.7))
|
color: .init(renderer.clearColor).setAlpha(0.7))
|
||||||
|
|
||||||
@ -92,9 +90,9 @@ internal class SpriteTestGame: GameDelegate {
|
|||||||
|
|
||||||
// Draw mouse cursor
|
// Draw mouse cursor
|
||||||
var mpos = Mouse.position
|
var mpos = Mouse.position
|
||||||
if self.spriteBatch.viewport != nil {
|
if self.spriteBatch.viewport.size != Size<Float>(renderer.frame.size) {
|
||||||
mpos /= SIMD2(Size<Float>(renderer.frame.size))
|
mpos /= SIMD2(Size<Float>(renderer.frame.size))
|
||||||
mpos *= SIMD2(self.spriteBatch.viewport!.size)
|
mpos *= SIMD2(self.spriteBatch.viewport.size)
|
||||||
}
|
}
|
||||||
let inter = 0.5 + sin(Float(time.total) * 10) * 0.5
|
let inter = 0.5 + sin(Float(time.total) * 10) * 0.5
|
||||||
let color = Color<Float>.green.mix(.white, 0.3)
|
let color = Color<Float>.green.mix(.white, 0.3)
|
||||||
@ -112,6 +110,10 @@ internal class SpriteTestGame: GameDelegate {
|
|||||||
|
|
||||||
self.spriteBatch.end()
|
self.spriteBatch.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resize(_ size: Size<Int>) {
|
||||||
|
self.spriteBatch.viewport.size = Size<Float>(size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate struct TestLevel {
|
fileprivate struct TestLevel {
|
||||||
|
Reference in New Issue
Block a user