mirror of
https://github.com/GayPizzaSpecifications/cxx-swift-interop.git
synced 2025-08-03 05:10:55 +00:00
c and cpp mixed attempt
This commit is contained in:
parent
99d4d99d76
commit
93d3feca7e
@ -1,3 +1,4 @@
|
||||
add_subdirectory(SwiftFrontend)
|
||||
add_subdirectory(SDLSwift)
|
||||
add_subdirectory(CppBackend)
|
||||
add_subdirectory(SwiftBackend)
|
||||
|
@ -2,5 +2,5 @@ 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_compile_options(CppBackend PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
|
||||
target_include_directories(CppBackend PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
target_compile_options(CppBackend PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
|
||||
|
@ -1,7 +1,6 @@
|
||||
add_library(SDLSwift INTERFACE)
|
||||
set_property(TARGET SDLSwift PROPERTY Swift_MODULE_NAME "SDL3")
|
||||
|
||||
target_compile_options(SDLSwift INTERFACE "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
|
||||
target_include_directories(SDLSwift INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
target_link_libraries(SDLSwift INTERFACE ${SDL3})
|
||||
|
@ -1,3 +1,3 @@
|
||||
module SDL3 {
|
||||
module SDL3 [extern_c] {
|
||||
header "shim.h"
|
||||
}
|
||||
|
7
Sources/SwiftBackend/Backend.swift
Normal file
7
Sources/SwiftBackend/Backend.swift
Normal file
@ -0,0 +1,7 @@
|
||||
import CppBackend
|
||||
|
||||
public struct FuckYou {
|
||||
public static func fuck() {
|
||||
let _ = BallWorld()
|
||||
}
|
||||
}
|
2
Sources/SwiftBackend/CMakeLists.txt
Normal file
2
Sources/SwiftBackend/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
add_library(SwiftBackend STATIC Backend.swift)
|
||||
target_link_libraries(SwiftBackend PRIVATE CppBackend)
|
@ -6,17 +6,18 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CppBackend
|
||||
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
|
||||
@ -39,15 +40,6 @@ 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
|
||||
@ -79,22 +71,10 @@ 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().pointee, 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
|
||||
|
@ -1,5 +1,5 @@
|
||||
add_executable(SwiftFrontend MACOSX_BUNDLE main.swift Application.swift)
|
||||
target_link_libraries(SwiftFrontend PRIVATE CppBackend SDLSwift)
|
||||
target_link_libraries(SwiftFrontend PRIVATE SwiftBackend SDLSwift)
|
||||
set_target_properties(SwiftFrontend PROPERTIES
|
||||
XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME NO
|
||||
XCODE_EMBED_FRAMEWORKS "${SDL3}"
|
||||
|
Loading…
Reference in New Issue
Block a user