The Skung Cave commit
This commit is contained in:
@ -23,23 +23,21 @@ public struct ApplicationConfiguration
|
||||
let vSync: VSyncMode
|
||||
let windowWidth: Int32
|
||||
let windowHeight: Int32
|
||||
let windowTitle: String
|
||||
let bundle: Bundle
|
||||
|
||||
public init(resizable: Bool, vSync: VSyncMode, windowWidth: Int32, windowHeight: Int32, bundle: Bundle)
|
||||
public init(resizable: Bool, vSync: VSyncMode, windowWidth: Int32, windowHeight: Int32, title: String, bundle: Bundle)
|
||||
{
|
||||
self.resizable = resizable
|
||||
self.vSync = vSync
|
||||
self.windowWidth = windowWidth
|
||||
self.windowHeight = windowHeight
|
||||
self.windowTitle = title
|
||||
self.bundle = bundle
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//FIXME: Wrap these
|
||||
public var sdlPad: OpaquePointer? = nil
|
||||
var joyId: Int32 = -1
|
||||
|
||||
public class Application
|
||||
{
|
||||
private let implementation: ApplicationImplementation
|
||||
@ -52,14 +50,6 @@ public class Application
|
||||
typealias ResizeEvent = (width: Int32, height: Int32)
|
||||
private var resize: ResizeEvent? = nil
|
||||
|
||||
func openController(index: Int32) -> (pad: OpaquePointer, joy: Int32)?
|
||||
{
|
||||
guard let sdlPad = SDL_GameControllerOpen(index) else { return nil }
|
||||
let joyId = SDL_JoystickGetDeviceInstanceID(index)
|
||||
print("Using gamepad #\(joyId), \"\(String(cString: SDL_GameControllerName(sdlPad)))\"")
|
||||
return (pad: sdlPad, joy: joyId)
|
||||
}
|
||||
|
||||
public init(application: ApplicationImplementation, config: ApplicationConfiguration)
|
||||
{
|
||||
self.implementation = application
|
||||
@ -77,7 +67,7 @@ public class Application
|
||||
let winWidth: Int32 = configuration.windowWidth, winHeight: Int32 = configuration.windowHeight
|
||||
var winFlags = SDL_WINDOW_RESIZABLE.rawValue | SDL_WINDOW_ALLOW_HIGHDPI.rawValue
|
||||
if render is OpenGL { winFlags |= SDL_WINDOW_OPENGL.rawValue }
|
||||
window = SDL_CreateWindow("Something", winPos, winPos, winWidth, winHeight, winFlags)
|
||||
window = SDL_CreateWindow(configuration.windowTitle, winPos, winPos, winWidth, winHeight, winFlags)
|
||||
guard window != nil else { fatalError("SDL_CreateWindow: \(String(cString: SDL_GetError()))") }
|
||||
|
||||
do
|
||||
@ -91,15 +81,10 @@ public class Application
|
||||
}
|
||||
catch { fatalError("piss") }
|
||||
|
||||
for i in 0...SDL_NumJoysticks()
|
||||
for idx in 0...SDL_NumJoysticks()
|
||||
{
|
||||
if SDL_IsGameController(i) != SDL_TRUE { continue }
|
||||
if let open = openController(index: i)
|
||||
{
|
||||
sdlPad = open.pad
|
||||
joyId = open.joy
|
||||
break
|
||||
}
|
||||
if SDL_IsGameController(idx) != SDL_TRUE { continue }
|
||||
Input.instance.padConnectedEvent(index: idx)
|
||||
}
|
||||
|
||||
do
|
||||
@ -131,13 +116,6 @@ public class Application
|
||||
switch SDL_EventType(event.type)
|
||||
{
|
||||
case SDL_QUIT: break mainLoop
|
||||
case SDL_KEYDOWN:
|
||||
if event.key.repeat == 0
|
||||
{
|
||||
Input.instance.pressEvent(scan: event.key.keysym.scancode)
|
||||
}
|
||||
case SDL_KEYUP:
|
||||
Input.instance.releaseEvent(scan: event.key.keysym.scancode)
|
||||
case SDL_WINDOWEVENT:
|
||||
switch SDL_WindowEventID(UInt32(event.window.event))
|
||||
{
|
||||
@ -145,28 +123,27 @@ public class Application
|
||||
resize = displaySize
|
||||
default: break
|
||||
}
|
||||
case SDL_KEYDOWN:
|
||||
Input.instance.pressEvent(scan: event.key.keysym.scancode, repeat: event.key.repeat != 0)
|
||||
case SDL_KEYUP:
|
||||
Input.instance.releaseEvent(scan: event.key.keysym.scancode)
|
||||
case SDL_CONTROLLERDEVICEADDED:
|
||||
if sdlPad == nil && SDL_IsGameController(event.cdevice.which) == SDL_TRUE
|
||||
{
|
||||
if let open = openController(index: event.cdevice.which)
|
||||
{
|
||||
sdlPad = open.pad
|
||||
joyId = open.joy
|
||||
}
|
||||
}
|
||||
if SDL_IsGameController(event.cdevice.which) != SDL_TRUE { break }
|
||||
Input.instance.padConnectedEvent(index: event.cdevice.which)
|
||||
case SDL_CONTROLLERDEVICEREMOVED:
|
||||
if event.cdevice.which == joyId
|
||||
{
|
||||
SDL_GameControllerClose(sdlPad)
|
||||
sdlPad = nil
|
||||
joyId = -1
|
||||
}
|
||||
Input.instance.padRemovedEvent(index: event.cdevice.which)
|
||||
case SDL_CONTROLLERBUTTONDOWN:
|
||||
Input.instance.padButtonPressEvent(id: event.cbutton.which, btn: event.cbutton.button)
|
||||
case SDL_CONTROLLERBUTTONUP:
|
||||
Input.instance.padButtonReleaseEvent(id: event.cbutton.which, btn: event.cbutton.button)
|
||||
case SDL_CONTROLLERAXISMOTION:
|
||||
Input.instance.padAxisEvent(id: event.caxis.which, axis: event.caxis.axis, value: event.caxis.value)
|
||||
default: break
|
||||
}
|
||||
}
|
||||
|
||||
let time = SDL_GetPerformanceCounter();
|
||||
let deltaTime = Float(Double(time - prevTime) * timeMul)
|
||||
let deltaTime = Float(Double(time &- prevTime) * timeMul)
|
||||
prevTime = time
|
||||
|
||||
implementation.update(deltaTime: min(deltaTime, 1.0 / 15.0))
|
||||
|
Reference in New Issue
Block a user