diff --git a/.gitignore b/.gitignore index 9104efe..2702bfa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /build-* -/xcode-* +/xcode* /.swiftpm /.swift /.vscode /.idea +.DS_Store diff --git a/CMakeLists.txt b/CMakeLists.txt index 38ca47c..32ac31e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Sources/SDL3/CMakeLists.txt b/Sources/SDL3/CMakeLists.txt index 3fc394a..c7e0c21 100644 --- a/Sources/SDL3/CMakeLists.txt +++ b/Sources/SDL3/CMakeLists.txt @@ -4,4 +4,6 @@ set_property(TARGET SDL3 PROPERTY CXX_STANDARD 20) target_compile_options(SDL3 PUBLIC "$<$:-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}) diff --git a/Sources/SwiftFrontend/Application.swift b/Sources/SwiftFrontend/Application.swift new file mode 100644 index 0000000..f7a24ec --- /dev/null +++ b/Sources/SwiftFrontend/Application.swift @@ -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 + } +} diff --git a/Sources/SwiftFrontend/CMakeLists.txt b/Sources/SwiftFrontend/CMakeLists.txt index 6ecc7bb..503d9e2 100644 --- a/Sources/SwiftFrontend/CMakeLists.txt +++ b/Sources/SwiftFrontend/CMakeLists.txt @@ -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) diff --git a/Sources/SwiftFrontend/main.swift b/Sources/SwiftFrontend/main.swift index 520f343..8abc5c1 100644 --- a/Sources/SwiftFrontend/main.swift +++ b/Sources/SwiftFrontend/main.swift @@ -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())