fix build by converting cxx backend to swift

This commit is contained in:
2024-08-05 10:32:40 +10:00
parent 93d3feca7e
commit 3bbfa0719e
11 changed files with 80 additions and 106 deletions

View File

@ -7,17 +7,15 @@
import Foundation
import SDL3
import SwiftBackend
class Application {
private var window: OpaquePointer? = nil
private var renderer: OpaquePointer? = nil
private var balls = BallWorld()
private var lastCounter: UInt64 = 0
private func initialize() -> ApplicationExecutionState {
FuckYou.fuck()
guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
print("SDL_Init() error: \(String(cString: SDL_GetError()))")
return .exitFailure
@ -40,6 +38,15 @@ class Application {
SDL_SetRenderVSync(renderer, 1)
SDL_SetRenderLogicalPresentation(renderer, 512, 512, SDL_LOGICAL_PRESENTATION_LETTERBOX, SDL_SCALEMODE_BEST)
let ballOrigin = SIMD2(Float(width), Float(height)) * 0.5
for _ in 0..<10 {
balls.add(
position: ballOrigin,
angle: Float(arc4random()) / Float(UInt32.max),
size: Float(arc4random_uniform(32 - 3) + 3)
)
}
lastCounter = SDL_GetPerformanceCounter()
return .running
@ -71,10 +78,22 @@ class Application {
}
private func paint(_ deltaTime: Float) -> ApplicationExecutionState {
balls.update(deltaTime)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255)
SDL_RenderClear(renderer)
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255)
for ball in balls.balls {
let position = ball.position, size = ball.size
var rect = SDL_FRect(
x: position.x - size,
y: position.y - size,
w: size * 2.0,
h: size * 2.0
)
SDL_RenderFillRect(renderer, &rect)
}
SDL_RenderPresent(renderer)
return .running

View File

@ -0,0 +1,55 @@
import Foundation
import simd
struct BallWorld {
var balls: [Ball] = []
mutating func add(position: SIMD2<Float>, angle: Float, size: Float) {
balls.append(Ball(position: position, angle: angle, size: size))
}
mutating func update(_ deltaTime: Float) {
for i in balls.indices {
balls[i].update(deltaTime)
}
}
}
struct Ball {
public static let speed: Float = 80.0
public static let worldSize: Float = 512.0
var _position: SIMD2<Float>, _velocity: SIMD2<Float>
var _size: Float
init(position: SIMD2<Float>, angle: Float, size ballSize: Float) {
_position = position
_velocity = SIMD2<Float>(
cos(angle * Float.pi * 2.0),
-sin(angle * Float.pi * 2.0)
)
_size = ballSize
}
public mutating func update(_ deltaTime: Float) {
_position += _velocity * Self.speed * deltaTime
if (_position.x < _size) {
_velocity.x = -_velocity.x
_position.x = _size
} else if (_position.x > Self.worldSize - _size) {
_velocity.x = -_velocity.x
_position.x = Self.worldSize - _size
}
if (_position.y < _size) {
_velocity.y = -_velocity.y
_position.y = _size
} else if (_position.y > Self.worldSize - _size) {
_velocity.y = -_velocity.y
_position.y = Self.worldSize - _size
}
}
var position: SIMD2<Float> { return _position }
var velocity: SIMD2<Float> { return _velocity }
var size: Float { return _size }
}

View File

@ -1,5 +1,5 @@
add_executable(SwiftFrontend MACOSX_BUNDLE main.swift Application.swift)
target_link_libraries(SwiftFrontend PRIVATE SwiftBackend SDLSwift)
add_executable(SwiftFrontend MACOSX_BUNDLE main.swift Application.swift Ball.swift)
target_link_libraries(SwiftFrontend PRIVATE SDLSwift)
set_target_properties(SwiftFrontend PROPERTIES
XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME NO
XCODE_EMBED_FRAMEWORKS "${SDL3}"