Skip to content

Commit

Permalink
[swift] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellocordeiro committed Jan 20, 2024
1 parent 74a49d8 commit 92519ec
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 48 deletions.
25 changes: 13 additions & 12 deletions core/gb-core-swift/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@

import PackageDescription

let packageDirectory = Context.packageDirectory
let rootDirectory = "\(packageDirectory)/../.."

#if DEBUG
let libDirectory = "\(rootDirectory)/target/debug"
let configDir = "debug"
#else
let libDirectory = "\(rootDirectory)/target/release"
let configDir = "release"
#endif

let staticLibPath = "\(libDirectory)/libgb_core_c.a"

let packageDir = Context.packageDirectory
let rootDir = "\(packageDir)/../.."
let libsDir = "\(rootDir)/target/\(configDir)"

let libName = "gb_core_c"
let staticLibFile = "lib\(libName).a"

let linkerSettings: [PackageDescription.LinkerSetting]
#if os(macOS)
linkerSettings = [
.unsafeFlags(["-L\(libDirectory)/"]),
.linkedLibrary("gb_core_c")
.unsafeFlags(["-L\(libsDir)/"]),
.linkedLibrary(libName)
]
#else
linkerSettings = [.linkedLibrary(staticLibPath)]
linkerSettings = [.linkedLibrary("\(libsDir)/\(staticLibFile)")]
#endif

let package = Package(
Expand All @@ -30,13 +33,11 @@ let package = Package(
.macOS(.v14)
],
products: [
.library(name: "CGameBoyCore", type: .static, targets: ["CGameBoyCore"]),
.library(name: "GameBoyCore", type: .static, targets: ["GameBoyCore"]),
.library(name: "GameBoyCore", targets: ["GameBoyCore"])
],
targets: [
.target(
name: "CGameBoyCore",
dependencies: [],
linkerSettings: linkerSettings
),
.target(
Expand Down
23 changes: 4 additions & 19 deletions core/gb-core-swift/Sources/GameBoyCore/GameBoyCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,8 @@ public final class GameBoy {
gameboy_destroy(gb)
}

public func load(_ url: URL) {
guard let data = NSData(contentsOf: url) else {
return
}

load(data)
}

public func load(_ data: NSData) {
gameboy_load_cartridge(gb, data.bytes, UInt(data.length))
public func load(_ rom: [UInt8]) {
gameboy_load_cartridge(gb, rom, UInt(rom.count))
}

public func runFrame() {
Expand All @@ -81,14 +73,7 @@ public final class GameBoy {
gameboy_set_key(gb, button.toGameBoyButton, value)
}

public func draw() -> [UInt8] {
var pixels = [UInt8](repeating: 0, count: Int(Self.width * Self.height) * 4)
gameboy_draw_into_frame_rgba8888(gb, &pixels)

return pixels
}

public func draw(pixels: inout [UInt8]) {
gameboy_draw_into_frame_rgba8888(gb, &pixels)
public func draw(frame: inout [UInt8]) {
gameboy_draw_into_frame_rgba8888(gb, &frame)
}
}
8 changes: 4 additions & 4 deletions platform/sdl2-swift/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

import PackageDescription

let packageDirectory = Context.packageDirectory
let rootDirectory = "\(packageDirectory)/../.."
let coreDirectory = "\(rootDirectory)/core/gb-core-swift"
let packageDir = Context.packageDirectory
let rootDir = "\(packageDir)/../.."
let coreDir = "\(rootDir)/core/gb-core-swift"

let package = Package(
name: "GameBoyEmulator",
platforms: [
.macOS(.v14)
],
dependencies: [
.package(path: coreDirectory),
.package(path: coreDir),
.package(url: "https://github.com/ctreffs/SwiftSDL2.git", from: "1.4.1")
],
targets: [
Expand Down
21 changes: 8 additions & 13 deletions platform/sdl2-swift/Sources/GameBoy/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import GameBoyCore
import SDL

let filePath = CommandLine.arguments[1]

guard let data = NSData(contentsOfFile: filePath) else {
exit(1)
}
let url = URL(filePath: filePath)
let rom = try [UInt8](Data(contentsOf: url))

let gb = GameBoy()
gb.load(data)

let SCREEN_WIDTH = Int32(160)
let SCREEN_HEIGHT = Int32(144)
gb.load(rom)

guard SDL_Init(SDL_INIT_VIDEO) == 0 else {
fatalError("SDL could not initialize! SDL_Error: \(String(cString: SDL_GetError()))")
Expand All @@ -31,16 +26,16 @@ let renderer = SDL_CreateRenderer(
SDL_RENDERER_PRESENTVSYNC.rawValue | SDL_RENDERER_ACCELERATED.rawValue
)

SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT)
SDL_RenderSetLogicalSize(renderer, Int32(GameBoy.width), Int32(GameBoy.height))

let texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_ABGR8888.rawValue,
Int32(SDL_TEXTUREACCESS_STREAMING.rawValue),
SCREEN_WIDTH, SCREEN_HEIGHT
Int32(GameBoy.width), Int32(GameBoy.height)
)

var framebuffer = [UInt8](repeating: 0, count: Int(SCREEN_WIDTH * SCREEN_HEIGHT) * 4)
var frame = [UInt8](repeating: 0, count: GameBoy.width * GameBoy.height * 4)

var quit = false
var event = SDL_Event()
Expand All @@ -58,9 +53,9 @@ while !quit {
}

gb.runFrame()
gb.draw(pixels: &framebuffer)
gb.draw(frame: &frame)

SDL_UpdateTexture(texture, nil, framebuffer, SCREEN_WIDTH * 4)
SDL_UpdateTexture(texture, nil, frame, Int32(GameBoy.width) * 4)
SDL_RenderCopy(renderer, texture, nil, nil)
SDL_RenderPresent(renderer)
}
Expand Down

0 comments on commit 92519ec

Please sign in to comment.