/
Main.scala
146 lines (115 loc) · 2.62 KB
/
Main.scala
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package demo
import pdapi.all._
import pdapi.enumerations.PDButtons.kButtonA
import pdapi.enumerations.PDSystemEvent
import pdapi.enumerations.PDSystemEvent.kEventInit
import scalanative.unsafe._
import scala.scalanative.unsigned.UInt
object Main {
var x = 50
var y = 50
val VelocityX = 5
val VelocityY = 2
enum DirectionX {
case Left, Right
}
enum DirectionY {
case Up, Down
}
var dirX = DirectionX.Left
var dirY = DirectionY.Down
val LCD_COLUMNS = 400
val LCD_ROWS = 240
var w = 100.0f
val h = 100.0f
var state = false
extension [A](
inline ptr: Ptr[A]
) {
inline def !(
using inline tag: Tag[A]
): A = !ptr
}
@exported("sn_event")
def event(
pd: Ptr[PlaydateAPI],
event: PDSystemEvent,
arg: UInt,
): Int = {
val f: CFuncPtr1[Ptr[Byte], CInt] = update
val ptr: Ptr[PDCallbackFunction] = CFuncPtr.toPtr(f).asInstanceOf[Ptr[PDCallbackFunction]]
if (event == kEventInit)
pd.!.system.!.setUpdateCallback(ptr, pd.asInstanceOf[Ptr[Byte]])
0
}
def update(
arg: Ptr[Byte]
): Int = {
val pd = arg.asInstanceOf[Ptr[PlaydateAPI]]
val current = stackalloc[PDButtons](1)
val pressed = stackalloc[PDButtons](1)
val released = stackalloc[PDButtons](1)
pd.!.system.!.getButtonState(current, pressed, released)
if (pressed.!.is(kButtonA)) {
pd.!.system.!.logToConsole(c"Button A is pressed")
state = !state
}
if (dirX == DirectionX.Left) {
x -= VelocityX
if (x < 0) {
dirX = DirectionX.Right
}
} else {
x += VelocityX
if (x > LCD_COLUMNS - w) {
dirX = DirectionX.Left
}
}
x = 0 max ((LCD_COLUMNS - w.toInt) min x.toInt)
if (dirY == DirectionY.Up) {
y -= VelocityY
if (y < 0) {
dirY = DirectionY.Down
}
} else {
y += VelocityY
if (y > (LCD_ROWS - h)) {
dirY = DirectionY.Up
}
}
y = 0 max ((LCD_ROWS - h.toInt) min y.toInt)
val crankDelta = pd.!.system.!.getCrankChange()
if (crankDelta != 0) {
w += crankDelta.toInt
}
pd.!
.graphics
.!
.clear(
LCDColor(uintptr_t(LCDSolidColor.kColorWhite.value))
)
if (state)
pd.!
.graphics
.!
.fillRect(
x,
y,
w.toInt,
h.toInt,
LCDColor(uintptr_t(LCDSolidColor.kColorBlack.value)),
)
else
pd.!
.graphics
.!
.drawRect(
x,
y,
w.toInt,
h.toInt,
LCDColor(uintptr_t(LCDSolidColor.kColorBlack.value)),
)
1
}
}