From 3bbfa0719e413e2d53356ebc5d13560b825f49c5 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Mon, 5 Aug 2024 10:32:40 +1000 Subject: [PATCH] fix build by converting cxx backend to swift --- CMakeLists.txt | 2 +- Sources/CMakeLists.txt | 2 - Sources/CppBackend/CMakeLists.txt | 6 --- Sources/CppBackend/ball.cpp | 41 ------------------ Sources/CppBackend/ball.hpp | 39 ------------------ Sources/CppBackend/module.modulemap | 3 -- Sources/SwiftBackend/Backend.swift | 7 ---- Sources/SwiftBackend/CMakeLists.txt | 2 - Sources/SwiftFrontend/Application.swift | 25 +++++++++-- Sources/SwiftFrontend/Ball.swift | 55 +++++++++++++++++++++++++ Sources/SwiftFrontend/CMakeLists.txt | 4 +- 11 files changed, 80 insertions(+), 106 deletions(-) delete mode 100644 Sources/CppBackend/CMakeLists.txt delete mode 100644 Sources/CppBackend/ball.cpp delete mode 100644 Sources/CppBackend/ball.hpp delete mode 100644 Sources/CppBackend/module.modulemap delete mode 100644 Sources/SwiftBackend/Backend.swift delete mode 100644 Sources/SwiftBackend/CMakeLists.txt create mode 100644 Sources/SwiftFrontend/Ball.swift diff --git a/CMakeLists.txt b/CMakeLists.txt index fe9e8e9..759b693 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.24) set(CMAKE_OSX_DEPLOYMENT_TARGET "13.6" CACHE STRING "Minimum OSX version" FORCE) -project(cxxswift LANGUAGES CXX Swift) +project(cxxswift LANGUAGES C Swift) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") include(InitializeSwift) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index 752afcb..a4c9a9a 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -1,4 +1,2 @@ add_subdirectory(SwiftFrontend) add_subdirectory(SDLSwift) -add_subdirectory(CppBackend) -add_subdirectory(SwiftBackend) diff --git a/Sources/CppBackend/CMakeLists.txt b/Sources/CppBackend/CMakeLists.txt deleted file mode 100644 index a5f92b8..0000000 --- a/Sources/CppBackend/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(CppBackend STATIC ball.hpp ball.cpp) -set_property(TARGET CppBackend PROPERTY Swift_MODULE_NAME "CppBackend") -set_property(TARGET CppBackend PROPERTY CXX_STANDARD 20) - -target_include_directories(CppBackend PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") -target_compile_options(CppBackend PUBLIC "$<$:-cxx-interoperability-mode=default>") diff --git a/Sources/CppBackend/ball.cpp b/Sources/CppBackend/ball.cpp deleted file mode 100644 index 6078a2b..0000000 --- a/Sources/CppBackend/ball.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "ball.hpp" -#include - - -Ball::Ball(vec2f pos, float angle, float ballSize) noexcept : - _position(pos), - _velocity(simd::make( - std::cos(angle * M_PI * 2.0f), - -std::sin(angle * M_PI * 2.0f) - )), - _size(ballSize) { -} - -void Ball::update(float deltaTime) noexcept { - _position += _velocity * speed * deltaTime; - if (_position.x < _size) { - _velocity.x = -_velocity.x; - _position.x = _size; - } else if (_position.x > worldSize - _size) { - _velocity.x = -_velocity.x; - _position.x = worldSize - _size; - } - if (_position.y < _size) { - _velocity.y = -_velocity.y; - _position.y = _size; - } else if (_position.y > worldSize - _size) { - _velocity.y = -_velocity.y; - _position.y = worldSize - _size; - } -} - - -void BallWorld::add(Ball::vec2f pos, float angle, float ballSize) noexcept { - balls.emplace_back(Ball{ pos, angle, ballSize }); -} - -void BallWorld::update(float deltaTime) noexcept { - for (auto& ball : balls) { - ball.update(deltaTime); - } -} diff --git a/Sources/CppBackend/ball.hpp b/Sources/CppBackend/ball.hpp deleted file mode 100644 index 7faca3d..0000000 --- a/Sources/CppBackend/ball.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include -#include - -struct Ball { - using vec2f = simd::float2; - -private: - constexpr static float speed = 80.0f; - constexpr static float worldSize = 512.0f; - - vec2f _position, _velocity; - float _size; - -public: - Ball(vec2f pos, float angle, float ballSize) noexcept; - virtual ~Ball() noexcept = default; - - void update(float deltaTime) noexcept; - - [[nodiscard]] constexpr const vec2f& position() const noexcept { - return _position; - } - [[nodiscard]] constexpr const vec2f& velocity() const noexcept { - return _velocity; - } - [[nodiscard]] constexpr const float size() const noexcept { - return _size; - } -}; - - -struct BallWorld { - std::vector balls; - - void add(Ball::vec2f pos, float angle, float ballSize) noexcept; - void update(float deltaTime) noexcept; -}; diff --git a/Sources/CppBackend/module.modulemap b/Sources/CppBackend/module.modulemap deleted file mode 100644 index 5361cb5..0000000 --- a/Sources/CppBackend/module.modulemap +++ /dev/null @@ -1,3 +0,0 @@ -module CppBackend { - header "ball.hpp" -} diff --git a/Sources/SwiftBackend/Backend.swift b/Sources/SwiftBackend/Backend.swift deleted file mode 100644 index 363d114..0000000 --- a/Sources/SwiftBackend/Backend.swift +++ /dev/null @@ -1,7 +0,0 @@ -import CppBackend - -public struct FuckYou { - public static func fuck() { - let _ = BallWorld() - } -} diff --git a/Sources/SwiftBackend/CMakeLists.txt b/Sources/SwiftBackend/CMakeLists.txt deleted file mode 100644 index 01d617a..0000000 --- a/Sources/SwiftBackend/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_library(SwiftBackend STATIC Backend.swift) -target_link_libraries(SwiftBackend PRIVATE CppBackend) diff --git a/Sources/SwiftFrontend/Application.swift b/Sources/SwiftFrontend/Application.swift index 94ec74d..c14a65c 100644 --- a/Sources/SwiftFrontend/Application.swift +++ b/Sources/SwiftFrontend/Application.swift @@ -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 diff --git a/Sources/SwiftFrontend/Ball.swift b/Sources/SwiftFrontend/Ball.swift new file mode 100644 index 0000000..bc33629 --- /dev/null +++ b/Sources/SwiftFrontend/Ball.swift @@ -0,0 +1,55 @@ +import Foundation +import simd + +struct BallWorld { + var balls: [Ball] = [] + + mutating func add(position: SIMD2, 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, _velocity: SIMD2 + var _size: Float + + init(position: SIMD2, angle: Float, size ballSize: Float) { + _position = position + _velocity = SIMD2( + 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 { return _position } + var velocity: SIMD2 { return _velocity } + var size: Float { return _size } +} diff --git a/Sources/SwiftFrontend/CMakeLists.txt b/Sources/SwiftFrontend/CMakeLists.txt index abe9f17..35555d2 100644 --- a/Sources/SwiftFrontend/CMakeLists.txt +++ b/Sources/SwiftFrontend/CMakeLists.txt @@ -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}"