Skip to content

Commit 68d6554

Browse files
committed
WIP
1 parent 0bb8f06 commit 68d6554

2 files changed

Lines changed: 52 additions & 39 deletions

File tree

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,22 @@ object Kernel {
9999
private val minY = -height / 2
100100

101101
/** Convolution function to use.
102-
* All color channels are handled the same way, with the alpha channel being forced to 255.
102+
* All color channels are handled the same way, with the alpha channel being forced to 255.
103103
*
104104
* Use as `plane.coflatMap(kernel)`
105105
*/
106106
def apply(getPixel: (Int, Int) => Color): Color = {
107-
var accR: Int = 0
108-
var accG: Int = 0
109-
var accB: Int = 0
107+
var accR: Int = constant
108+
var accG: Int = constant
109+
var accB: Int = constant
110110

111111
var dMY = 0
112112
while (dMY < height) {
113-
val dy = minY + dMY
114-
var dMX = 0
113+
val dy = minY + dMY
114+
var dMX = 0
115+
val line = matrix(dMY)
115116
while (dMX < width) {
116-
val weight = matrix(dMY)(dMX)
117+
val weight = line(dMX)
117118
if (weight != 0) {
118119
val dx = minX + dMX
119120
val color = getPixel(dx, dy)
@@ -128,12 +129,11 @@ object Kernel {
128129

129130
if (normalization != 1)
130131
Color(
131-
constant + (accR / normalization),
132-
constant + (accG / normalization),
133-
constant + (accB / normalization)
132+
accR / normalization,
133+
accG / normalization,
134+
accB / normalization
134135
)
135-
else
136-
Color(constant + accR, constant + accG, constant + accB)
136+
else Color(accR, accG, accB)
137137
}
138138

139139
/** Creates a kernel equivalent to this one, but resized (keeping the center unchanged).
@@ -208,20 +208,24 @@ object Kernel {
208208
private val minY = -height / 2
209209

210210
def apply(getPixel: (Int, Int) => Color): Color = {
211-
var accR: Int = 0
212-
var accG: Int = 0
213-
var accB: Int = 0
214-
var accA: Int = 0
211+
var accR: Int = kernelR.constant
212+
var accG: Int = kernelG.constant
213+
var accB: Int = kernelB.constant
214+
var accA: Int = kernelA.constant
215215

216216
var dMY = 0
217217
while (dMY < height) {
218-
val dy = minY + dMY
219-
var dMX = 0
218+
val dy = minY + dMY
219+
var dMX = 0
220+
val lineR = matrixR(dMY)
221+
val lineG = matrixG(dMY)
222+
val lineB = matrixB(dMY)
223+
val lineA = matrixA(dMY)
220224
while (dMX < width) {
221-
val weightR = matrixR(dMY)(dMX)
222-
val weightG = matrixG(dMY)(dMX)
223-
val weightB = matrixB(dMY)(dMX)
224-
val weightA = matrixA(dMY)(dMX)
225+
val weightR = lineR(dMX)
226+
val weightG = lineG(dMX)
227+
val weightB = lineB(dMX)
228+
val weightA = lineA(dMX)
225229
if (weightR != 0 || weightG != 0 || weightB != 0 || weightA != 0) {
226230
val dx = minX + dMX
227231
val color = getPixel(dx, dy)
@@ -236,10 +240,10 @@ object Kernel {
236240
}
237241

238242
Color(
239-
kernelR.constant + (accR / kernelR.normalization),
240-
kernelG.constant + (accG / kernelG.normalization),
241-
kernelB.constant + (accB / kernelB.normalization),
242-
kernelA.constant + (accA / kernelA.normalization)
243+
accR / kernelR.normalization,
244+
accG / kernelG.normalization,
245+
accB / kernelB.normalization,
246+
accA / kernelA.normalization
243247
)
244248
}
245249
}

examples/snapshot/09-surface-views.md

Lines changed: 22 additions & 13 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,33 +77,42 @@ 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
80+
.toSurfaceView(canvasSettings.width, canvasSettings.height)
81+
.precompute
8182
.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-
)
89-
90-
canvas.blitPlane(image)(0, 0)
83+
84+
canvas.blit(image)(0, 0)
9185
}
9286
```
9387

9488
### Putting it all together
9589

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

99107
AppLoop
100108
.statefulRenderLoop((t: Double) => (canvas: Canvas) => {
109+
frameCounter()
101110
canvas.clear()
102111
application(t, canvas)
103112
canvas.redraw()
104113
t + 0.01
105114
}
106115
)
107-
.configure(canvasSettings, LoopFrequency.hz60, 0)
116+
.configure(canvasSettings, LoopFrequency.Uncapped, 0)
108117
.run()
109118
```

0 commit comments

Comments
 (0)