Use SDL3 in a way that is xcframework compatible.

This commit is contained in:
Alex Zenla 2024-08-04 01:04:02 -07:00
parent 50f4f02f85
commit dad9a7c762
Signed by: alex
GPG Key ID: C0780728420EBFE5
6 changed files with 65 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
/build-*
/xcode-*
/xcode*
/.swiftpm
/.swift
/.vscode
/.idea
.DS_Store

View File

@ -5,6 +5,4 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
include(InitializeSwift)
include(AddSwift)
find_package(SDL3 CONFIG REQUIRED)
add_subdirectory(Sources)

View File

@ -4,4 +4,6 @@ set_property(TARGET SDL3 PROPERTY CXX_STANDARD 20)
target_compile_options(SDL3 PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
target_include_directories(SDL3 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(SDL3 PUBLIC SDL3::SDL3)
find_library(SDL3_LIB SDL3 REQUIRED)
target_link_libraries(SDL3 PUBLIC ${SDL3_LIB})

View File

@ -0,0 +1,58 @@
//
// Application.swift
// cxxswift
//
// Created by Alex Zenla on 8/3/24.
//
import Foundation
import SDL3
struct Application {
func run() -> Int32 {
guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
print("SDL init failed.")
return 1
}
defer {
SDL_Quit()
}
guard let window = SDL_CreateWindow("Hello World", 512, 512, 0) else {
return 1
}
defer {
SDL_DestroyWindow(window)
}
let renderer = SDL_CreateRenderer(window, nil)
defer {
SDL_DestroyRenderer(renderer)
}
quit: while true {
var event = SDL_Event()
while SDL_PollEvent(&event) > 0 {
switch SDL_EventType(event.type) {
case SDL_EVENT_QUIT:
break quit
default:
break
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255)
SDL_RenderClear(renderer)
var rect = SDL_FRect(x: 0, y: 0, w: 100, h: 100)
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255)
SDL_RenderFillRect(renderer, &rect)
SDL_RenderPresent(renderer)
}
return 0
}
}

View File

@ -1,2 +1,2 @@
add_executable(SwiftFrontend main.swift)
add_executable(SwiftFrontend MACOSX_BUNDLE main.swift Application.swift)
target_link_libraries(SwiftFrontend PRIVATE CppBackend SDL3)

View File

@ -1,9 +1,4 @@
import Foundation
import SDL3
guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
print("SDL init failed.")
exit(0)
}
print("SDL init success.")
SDL_Quit()
exit(Application().run())