From 0bcbaa6404a0a8a50664c4de1fe1456138b3eeb6 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Mon, 5 Aug 2024 14:01:01 +1000 Subject: [PATCH] application config for window settings --- Sources/Voxelotl/Application.swift | 48 ++++++++++++++++++++++++++---- Sources/Voxelotl/main.swift | 9 +++++- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/Sources/Voxelotl/Application.swift b/Sources/Voxelotl/Application.swift index 6e9de9c..20c0a22 100644 --- a/Sources/Voxelotl/Application.swift +++ b/Sources/Voxelotl/Application.swift @@ -1,12 +1,15 @@ import Foundation import SDL3 -class Application { +public class Application { + private let cfg: ApplicationConfiguration + private var window: OpaquePointer? = nil private var lastCounter: UInt64 = 0 - private static let windowWidth: Int32 = 1280 - private static let windowHeight: Int32 = 720 + public init(configuration: ApplicationConfiguration) { + self.cfg = configuration + } private func initialize() -> ApplicationExecutionState { guard SDL_Init(SDL_INIT_VIDEO) >= 0 else { @@ -14,9 +17,11 @@ class Application { return .exitFailure } - window = SDL_CreateWindow("Voxelotl", - Self.windowWidth, Self.windowHeight, - SDL_WindowFlags(SDL_WINDOW_RESIZABLE) | SDL_WindowFlags(SDL_WINDOW_HIGH_PIXEL_DENSITY)) + var windowFlags = SDL_WindowFlags(SDL_WINDOW_HIGH_PIXEL_DENSITY) + if (cfg.flags.contains(.resizable)) { + windowFlags |= SDL_WindowFlags(SDL_WINDOW_RESIZABLE) + } + window = SDL_CreateWindow(cfg.title, cfg.width, cfg.height, windowFlags) guard window != nil else { print("SDL_CreateWindow() error: \(String(cString: SDL_GetError()))") return .exitFailure @@ -81,6 +86,37 @@ class Application { } } +public struct ApplicationConfiguration { + public struct Flags: OptionSet { + public let rawValue: Int + public init(rawValue: Int) { + self.rawValue = rawValue + } + + static let resizable = Flags(rawValue: 1 << 0) + } + + public enum VSyncMode { + case off + case on(interval: UInt) + case adaptive + } + + let width: Int32 + let height: Int32 + let title: String + let flags: Flags + let vsyncMode: VSyncMode + + public init(width: Int32, height: Int32, title: String, flags: Flags, vsyncMode: VSyncMode) { + self.width = width + self.height = height + self.title = title + self.flags = flags + self.vsyncMode = vsyncMode + } +} + fileprivate enum ApplicationExecutionState { case exitFailure case exitSuccess diff --git a/Sources/Voxelotl/main.swift b/Sources/Voxelotl/main.swift index 0bd9bbf..5a7b502 100644 --- a/Sources/Voxelotl/main.swift +++ b/Sources/Voxelotl/main.swift @@ -1,4 +1,11 @@ import Darwin -let app = Application() +let app = Application( + configuration: ApplicationConfiguration( + width: 1280, + height: 720, + title: "Voxelotl Demo", + flags: .resizable, + vsyncMode: .on(interval: 1))) + exit(app.run())