mirror of
				https://github.com/GayPizzaSpecifications/cxx-swift-interop.git
				synced 2025-11-04 04:09:37 +00:00 
			
		
		
		
	Use SDL3 in a way that is xcframework compatible.
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,6 +1,7 @@
 | 
				
			|||||||
/build-*
 | 
					/build-*
 | 
				
			||||||
/xcode-*
 | 
					/xcode*
 | 
				
			||||||
/.swiftpm
 | 
					/.swiftpm
 | 
				
			||||||
/.swift
 | 
					/.swift
 | 
				
			||||||
/.vscode
 | 
					/.vscode
 | 
				
			||||||
/.idea
 | 
					/.idea
 | 
				
			||||||
 | 
					.DS_Store
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,4 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
 | 
				
			|||||||
include(InitializeSwift)
 | 
					include(InitializeSwift)
 | 
				
			||||||
include(AddSwift)
 | 
					include(AddSwift)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
find_package(SDL3 CONFIG REQUIRED)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
add_subdirectory(Sources)
 | 
					add_subdirectory(Sources)
 | 
				
			||||||
 | 
				
			|||||||
@ -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_compile_options(SDL3 PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
 | 
				
			||||||
target_include_directories(SDL3 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
 | 
					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})
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										58
									
								
								Sources/SwiftFrontend/Application.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								Sources/SwiftFrontend/Application.swift
									
									
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -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)
 | 
					target_link_libraries(SwiftFrontend PRIVATE CppBackend SDL3)
 | 
				
			||||||
 | 
				
			|||||||
@ -1,9 +1,4 @@
 | 
				
			|||||||
import Foundation
 | 
					import Foundation
 | 
				
			||||||
import SDL3
 | 
					import SDL3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
 | 
					exit(Application().run())
 | 
				
			||||||
    print("SDL init failed.")
 | 
					 | 
				
			||||||
    exit(0)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
print("SDL init success.")
 | 
					 | 
				
			||||||
SDL_Quit()
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user