mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 10:59:39 +00:00 
			
		
		
		
	application skeleton
This commit is contained in:
		
							
								
								
									
										90
									
								
								Sources/Voxelotl/Application.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								Sources/Voxelotl/Application.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,90 @@
 | 
			
		||||
import Foundation
 | 
			
		||||
import SDL3
 | 
			
		||||
 | 
			
		||||
class Application {
 | 
			
		||||
  private var window: OpaquePointer? = nil
 | 
			
		||||
  private var lastCounter: UInt64 = 0
 | 
			
		||||
 | 
			
		||||
  private static let windowWidth: Int32 = 1280
 | 
			
		||||
  private static let windowHeight: Int32 = 720
 | 
			
		||||
 | 
			
		||||
  private func initialize() -> ApplicationExecutionState {
 | 
			
		||||
    guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
 | 
			
		||||
      print("SDL_Init() error: \(String(cString: SDL_GetError()))")
 | 
			
		||||
      return .exitFailure
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let sdlWindowResizable:        SDL_WindowFlags = 0x0000000000000020
 | 
			
		||||
    let sdlWindowHighPixelDensity: SDL_WindowFlags = 0x0000000000002000
 | 
			
		||||
    window = SDL_CreateWindow("Voxelotl",
 | 
			
		||||
      Self.windowWidth, Self.windowHeight,
 | 
			
		||||
      sdlWindowResizable | sdlWindowHighPixelDensity)
 | 
			
		||||
    guard window != nil else {
 | 
			
		||||
      print("SDL_CreateWindow() error: \(String(cString: SDL_GetError()))")
 | 
			
		||||
      return .exitFailure
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    lastCounter = SDL_GetPerformanceCounter()
 | 
			
		||||
    return .running
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private func deinitialize() {
 | 
			
		||||
    SDL_DestroyWindow(window)
 | 
			
		||||
    SDL_Quit()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private func handleEvent(_ event: SDL_Event) -> ApplicationExecutionState {
 | 
			
		||||
    switch SDL_EventType(event.type) {
 | 
			
		||||
    case SDL_EVENT_QUIT:
 | 
			
		||||
      return .exitSuccess
 | 
			
		||||
 | 
			
		||||
    case SDL_EVENT_KEY_DOWN:
 | 
			
		||||
      switch event.key.key {
 | 
			
		||||
      case SDLK_ESCAPE:
 | 
			
		||||
        return .exitSuccess
 | 
			
		||||
      default:
 | 
			
		||||
        break
 | 
			
		||||
      }
 | 
			
		||||
      return .running
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
      return .running
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private func update(_ deltaTime: Double) -> ApplicationExecutionState {
 | 
			
		||||
    return .running
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private func getDeltaTime() -> Double {
 | 
			
		||||
    let counter = SDL_GetPerformanceCounter()
 | 
			
		||||
    let divisor = 1.0 / Double(SDL_GetPerformanceFrequency())
 | 
			
		||||
    defer { lastCounter = counter }
 | 
			
		||||
    return Double(counter &- lastCounter) * divisor
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  func run() -> Int32 {
 | 
			
		||||
    var res = initialize()
 | 
			
		||||
 | 
			
		||||
    quit: while res == .running {
 | 
			
		||||
      var event = SDL_Event()
 | 
			
		||||
      while SDL_PollEvent(&event) > 0 {
 | 
			
		||||
        res = handleEvent(event)
 | 
			
		||||
        if res != .running {
 | 
			
		||||
          break quit
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      let deltaTime = getDeltaTime()
 | 
			
		||||
      res = update(deltaTime)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return res == .exitSuccess ? 0 : 1
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fileprivate enum ApplicationExecutionState {
 | 
			
		||||
  case exitFailure
 | 
			
		||||
  case exitSuccess
 | 
			
		||||
  case running
 | 
			
		||||
}
 | 
			
		||||
@ -1,4 +1,7 @@
 | 
			
		||||
add_executable(Voxelotl MACOSX_BUNDLE main.swift)
 | 
			
		||||
add_executable(Voxelotl MACOSX_BUNDLE
 | 
			
		||||
  Application.swift
 | 
			
		||||
  main.swift)
 | 
			
		||||
 | 
			
		||||
target_link_libraries(Voxelotl PRIVATE SDLSwift)
 | 
			
		||||
set_target_properties(Voxelotl PROPERTIES
 | 
			
		||||
  XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME YES
 | 
			
		||||
 | 
			
		||||
@ -1,13 +1,4 @@
 | 
			
		||||
import Foundation
 | 
			
		||||
import SDL3
 | 
			
		||||
import Darwin
 | 
			
		||||
 | 
			
		||||
guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
 | 
			
		||||
  print("SDL init failed.")
 | 
			
		||||
  exit(1)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
defer {
 | 
			
		||||
  SDL_Quit()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("SDL init success.")
 | 
			
		||||
let app = Application()
 | 
			
		||||
exit(app.run())
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user