Skip to content

Commit cf502fb

Browse files
committed
WIP
1 parent 0bb8f06 commit cf502fb

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

core/shared/src/main/scala/eu/joaocosta/minart/graphics/Kernel.scala

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,19 @@ object Kernel {
9898
private val minX = -width / 2
9999
private val minY = -height / 2
100100

101+
// "Division by Invariant Integers using Multiplication" https://gmplib.org/~tege/divcnst-pldi94.pdf
102+
private val reciprocal: Long = (0xffffffffL / normalization) + 1
103+
private val bias: Long = normalization / 2
104+
101105
/** Convolution function to use.
102106
* All color channels are handled the same way, with the alpha channel being forced to 255.
103107
*
104108
* Use as `plane.coflatMap(kernel)`
105109
*/
106110
def apply(getPixel: (Int, Int) => Color): Color = {
107-
var accR: Int = 0
108-
var accG: Int = 0
109-
var accB: Int = 0
111+
var accR: Long = 0
112+
var accG: Long = 0
113+
var accB: Long = 0
110114

111115
var dMY = 0
112116
while (dMY < height) {
@@ -126,14 +130,19 @@ object Kernel {
126130
dMY += 1
127131
}
128132

129-
if (normalization != 1)
133+
if (normalization != 1) {
134+
/*Color(
135+
constant + (((accR + bias) * reciprocal) >> 32).toInt,
136+
constant + (((accG + bias) * reciprocal) >> 32).toInt,
137+
constant + (((accB + bias) * reciprocal) >> 32).toInt
138+
)*/
130139
Color(
131-
constant + (accR / normalization),
132-
constant + (accG / normalization),
133-
constant + (accB / normalization)
140+
constant + (accR.toInt / normalization),
141+
constant + (accG.toInt / normalization),
142+
constant + (accB.toInt / normalization)
134143
)
135-
else
136-
Color(constant + accR, constant + accG, constant + accB)
144+
} else
145+
Color(constant + accR.toInt, constant + accG.toInt, constant + accB.toInt)
137146
}
138147

139148
/** Creates a kernel equivalent to this one, but resized (keeping the center unchanged).

examples/snapshot/09-surface-views.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ We could use a simple function, but Minart already provides some optimized kerne
6464
out of the box.
6565

6666
```scala
67-
val blurKernel = Kernel.averageBlur(3, 3)
67+
val blurKernel = Kernel.averageBlur(9, 9)
6868

6969
```
7070

@@ -77,15 +77,7 @@ def application(t: Double, canvas: Canvas): Unit = {
7777
val zoom = 1.0 / (frameSin + 2.0)
7878

7979
val image = updatedBitmap.view.repeating // Create an infinite Plane from our surface
80-
.scale(zoom, zoom) // Scale
8180
.coflatMap(blurKernel) // Average blur
82-
.rotate(t) // Rotate
83-
.contramap((x, y) => (x + (5 * Math.sin(t + y / 10.0)).toInt, y)) // Wobbly effect
84-
.flatMap(color =>
85-
(x, y) => // Checkerboard effect
86-
if (x % 32 < 16 != y % 32 < 16) color.invert
87-
else color
88-
)
8981

9082
canvas.blitPlane(image)(0, 0)
9183
}
@@ -94,16 +86,31 @@ def application(t: Double, canvas: Canvas): Unit = {
9486
### Putting it all together
9587

9688
```scala
97-
val canvasSettings = Canvas.Settings(width = 128, height = 128, scale = Some(4), clearColor = Color(0, 0, 0))
89+
val frameCounter = {
90+
var frameNumber: Int = 0
91+
var timer = System.currentTimeMillis
92+
() => {
93+
frameNumber += 1
94+
if (frameNumber % 10 == 0) {
95+
val currTime = System.currentTimeMillis()
96+
val fps = 10.0 / ((currTime - timer) / 1000.0)
97+
println("FPS:" + fps)
98+
timer = System.currentTimeMillis()
99+
}
100+
}
101+
}
102+
103+
val canvasSettings = Canvas.Settings(width = 512, height = 512, scale = Some(1), clearColor = Color(0, 0, 0))
98104

99105
AppLoop
100106
.statefulRenderLoop((t: Double) => (canvas: Canvas) => {
107+
frameCounter()
101108
canvas.clear()
102109
application(t, canvas)
103110
canvas.redraw()
104111
t + 0.01
105112
}
106113
)
107-
.configure(canvasSettings, LoopFrequency.hz60, 0)
114+
.configure(canvasSettings, LoopFrequency.Uncapped, 0)
108115
.run()
109116
```

0 commit comments

Comments
 (0)