From 77603b582c11a82c5cee8a316707edb18c35b510 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Sun, 22 Sep 2024 00:59:58 +1000 Subject: [PATCH] restore c++ backend using swift 6's improved interop --- CMakeLists.txt | 5 +++- Sources/CMakeLists.txt | 4 --- Sources/CppBackend/ball.cpp | 2 ++ Sources/CppBackend/ball.hpp | 3 +++ Sources/SDLSwift/module.modulemap | 2 +- Sources/SwiftBackend/Backend.swift | 7 ----- Sources/SwiftBackend/CMakeLists.txt | 2 -- Sources/SwiftFrontend/Application.swift | 34 +++++++++++++++++-------- Sources/SwiftFrontend/CMakeLists.txt | 2 +- 9 files changed, 35 insertions(+), 26 deletions(-) delete mode 100644 Sources/CMakeLists.txt delete mode 100644 Sources/SwiftBackend/Backend.swift delete mode 100644 Sources/SwiftBackend/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index fe9e8e9..4603799 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.24) set(CMAKE_OSX_DEPLOYMENT_TARGET "13.6" CACHE STRING "Minimum OSX version" FORCE) +set(CMAKE_Swift_LANGUAGE_VERSION 6) project(cxxswift LANGUAGES CXX Swift) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") @@ -8,4 +9,6 @@ include(AddSwift) find_library(SDL3 SDL3 REQUIRED PATHS "${CMAKE_SOURCE_DIR}/Frameworks" NO_DEFAULT_PATH) -add_subdirectory(Sources) +add_subdirectory(Sources/CppBackend) +add_subdirectory(Sources/SDLSwift) +add_subdirectory(Sources/SwiftFrontend) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt deleted file mode 100644 index 752afcb..0000000 --- a/Sources/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -add_subdirectory(SwiftFrontend) -add_subdirectory(SDLSwift) -add_subdirectory(CppBackend) -add_subdirectory(SwiftBackend) diff --git a/Sources/CppBackend/ball.cpp b/Sources/CppBackend/ball.cpp index 6078a2b..2022a05 100644 --- a/Sources/CppBackend/ball.cpp +++ b/Sources/CppBackend/ball.cpp @@ -30,6 +30,8 @@ void Ball::update(float deltaTime) noexcept { } +BallWorld::BallWorld() noexcept : balls() {} + void BallWorld::add(Ball::vec2f pos, float angle, float ballSize) noexcept { balls.emplace_back(Ball{ pos, angle, ballSize }); } diff --git a/Sources/CppBackend/ball.hpp b/Sources/CppBackend/ball.hpp index 7faca3d..c006257 100644 --- a/Sources/CppBackend/ball.hpp +++ b/Sources/CppBackend/ball.hpp @@ -34,6 +34,9 @@ public: struct BallWorld { std::vector balls; + BallWorld() noexcept; + virtual ~BallWorld() noexcept = default; + void add(Ball::vec2f pos, float angle, float ballSize) noexcept; void update(float deltaTime) noexcept; }; diff --git a/Sources/SDLSwift/module.modulemap b/Sources/SDLSwift/module.modulemap index 687dc63..77aad28 100644 --- a/Sources/SDLSwift/module.modulemap +++ b/Sources/SDLSwift/module.modulemap @@ -1,3 +1,3 @@ -module SDL3 [extern_c] { +module SDL3 { header "shim.h" } 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..654bc41 100644 --- a/Sources/SwiftFrontend/Application.swift +++ b/Sources/SwiftFrontend/Application.swift @@ -1,13 +1,6 @@ -// -// Application.swift -// cxxswift -// -// Created by Alex Zenla on 8/3/24. -// - import Foundation import SDL3 -import SwiftBackend +import CppBackend class Application { private var window: OpaquePointer? = nil @@ -15,9 +8,9 @@ class Application { private var lastCounter: UInt64 = 0 + private var balls = BallWorld() + 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 +33,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( + ballOrigin, + Float(arc4random()) / Float(UInt32.max), + Float(arc4random_uniform(32 - 3) + 3) + ) + } + lastCounter = SDL_GetPerformanceCounter() return .running @@ -71,10 +73,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.pointee.x - size, + y: position.pointee.y - size, + w: size * 2.0, + h: size * 2.0 + ) + SDL_RenderFillRect(renderer, &rect) + } SDL_RenderPresent(renderer) return .running diff --git a/Sources/SwiftFrontend/CMakeLists.txt b/Sources/SwiftFrontend/CMakeLists.txt index abe9f17..0924a14 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) +target_link_libraries(SwiftFrontend PRIVATE CppBackend SDLSwift) set_target_properties(SwiftFrontend PROPERTIES XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME NO XCODE_EMBED_FRAMEWORKS "${SDL3}"