Skip to content

Commit

Permalink
Merge pull request #3 from quephird/improve_app_protocol
Browse files Browse the repository at this point in the history
Output filename is now that of the class name with the ppm extension.
  • Loading branch information
quephird committed Sep 25, 2022
2 parents cc633ec + afe612d commit 703bfce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions Sources/ScintillaLib/ScintillaApp.swift
Expand Up @@ -8,16 +8,18 @@
import Foundation

public protocol ScintillaApp {
var filename: String { get set }
@WorldBuilder var body: (Light, Camera, [Shape]) { get }
@WorldBuilder var body: World { get }

init()
}

extension ScintillaApp {
public static func main() {
let instance = Self()
let canvas = World { return instance.body }.render()
canvas.save(to: instance.filename)
let instanceBody = instance.body
let canvas = instanceBody.render()

let outputFilename = String(describing: self) + ".ppm"
canvas.save(to: outputFilename)
}
}
7 changes: 2 additions & 5 deletions Sources/ScintillaLib/World.swift
Expand Up @@ -14,11 +14,8 @@ public struct World {
var camera: Camera
var objects: [Shape]

public init(@WorldBuilder builder: () -> (Light, Camera, [Shape])) {
let (light, camera, shapes) = builder()
self.light = light
self.camera = camera
self.objects = shapes
public init(@WorldBuilder builder: () -> World) {
self = builder()
}

public init(_ light: Light, _ camera: Camera, @ShapeBuilder builder: () -> [Shape]) {
Expand Down
5 changes: 5 additions & 0 deletions Sources/ScintillaLib/WorldBuilder.swift
Expand Up @@ -7,6 +7,11 @@

@resultBuilder
public enum WorldBuilder {
public static func buildFinalResult(_ component: (Light, Camera, [Shape])) -> World {
let (light, camera, shapes) = component
return World(light, camera, shapes)
}

public static func buildBlock(_ light: Light, _ camera: Camera, _ shapes: [Shape]...) -> (Light, Camera, [Shape]) {
return (light, camera, Array(shapes.joined()))
}
Expand Down

0 comments on commit 703bfce

Please sign in to comment.