-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.swift
68 lines (53 loc) · 1.76 KB
/
App.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// File.swift
//
//
// Created by Christophe Bronner on 2021-11-30.
//
import RaylibKit
@main struct LetterboxedWindow: Applet {
var target: RenderTexture
var virtualMouse = Vector2.zero
var transformation = Vector2.zero
let game = Vector2(640, 480)
var scale: Float = 1
var colors: [Color]
init() {
Window.create(800, by: 450, title: "Example - Core - Letterboxed Window")
Window.enable(.resizable, .vsync)
Window.resize(minimum: 320, by: 240)
Renderer.background = .black
Application.target(fps: 60)
target = RenderTexture(size: game)
target.texture.filter(using: .bilinear)
colors = [Color](repeat: 10, value: .random)
}
mutating func update() {
let scaled = Window.size / game
scale = min(scaled.x, scaled.y)
if Keyboard.space.isPressed {
for i in colors.indices {
colors[i] = .random
}
}
transformation = -(Window.size - game * scale) / 2
virtualMouse = (Mouse.position + transformation) / scale
virtualMouse.clamp(between: .zero, and: game)
Mouse.offset(by: transformation)
Mouse.scale(by: .one / scale)
}
func render() {
Renderer.target(target) {
Renderer.clear(to: .raywhite)
for (i, color) in colors.enumerated() {
Renderer2D.rectangle(at: 0, (game.y / 10).toInt * i, size: game.x.toInt, (game.y / 10).toInt, color: color)
}
Renderer2D.text("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", at: 10, 25, color: .white)
Renderer2D.text("Default Mouse: [\(Mouse.x), \(Mouse.y)]", at: 350, 25, color: .green)
Renderer2D.text("Virtual Mouse: [\(virtualMouse.x.toInt), \(virtualMouse.y.toInt)]", at: 350, 55, color: .yellow)
}
}
func draw() {
Renderer2D.target(target, to: Rectangle(at: -transformation, size: game * scale))
}
}