Skip to content

Commit

Permalink
Add a rasterization example
Browse files Browse the repository at this point in the history
  • Loading branch information
JD557 committed Sep 7, 2024
1 parent e826b61 commit c799a85
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
92 changes: 92 additions & 0 deletions examples/snapshot/10-vector-shapes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# 10. Vector shapes

Besides surfaces, Minart also allows you to render some basic vector shapes.

## Drawing shapes

### Dependencies and imports

The relevant methods are in the `eu.joaocosta.minart.geometry` package

```scala
//> using scala "3.3.3"
//> using dep "eu.joaocosta::minart::0.6.2-SNAPSHOT"

import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.geometry.*
import eu.joaocosta.minart.runtime.*
```

### Shapes

Vectorial shapes are represented by the `Shape` abstraction.

Minart already comes with some basic shapes, such as circle and convex polygons, with helper methods in the `Shape` companion object.

First, let's create a few shapes with those methods.

```scala
import eu.joaocosta.minart.geometry.Shape.Point

val triangle = Shape.triangle(Point(-16, 16), Point(0, -16), Point(16, 16))
val square = Shape.rectangle(Point(-16, -16), Point(16, 16))
val octagon = Shape.convexPolygon(
Point(-8, -16),
Point(8, -16),
Point(16, -8),
Point(16, 8),
Point(8, 16),
Point(-8, 16),
Point(-16, 8),
Point(-16, -8)
)
val circle = Shape.circle(Point(0, 0), 16)
```

### Faces

Notice that all shapes are defined with points in clockwise fashion.

All shapes have two faces: A front face and a back face.

Depending on the way they are defined (or transformed), different faces might be shown.

Minart allows you to set different colors for each face, and even no color at all!
This is helpful if, for some reason, you know you don't want to draw back faces.

### Rasterizing

Now we just need to use the `rasterize` operation, just like we did with `blit`.

In this example we will also scale our images with time, to show how the color changes when the face flips.

```scala
val frontfaceColor = Color(255, 0, 0)
val backfaceColor = Color(0, 255, 0)

def application(t: Double, canvas: Canvas): Unit = {
val scale = math.sin(t)
canvas.rasterize(triangle.scale(scale, 1.0), Some(frontfaceColor), Some(backfaceColor))(32, 32)
canvas.rasterize(square.scale(scale, 1.0), Some(frontfaceColor), Some(backfaceColor))(64, 32)
canvas.rasterize(octagon.scale(scale, 1.0), Some(frontfaceColor), Some(backfaceColor))(32, 64)
canvas.rasterize(circle.scale(scale, 1.0), Some(frontfaceColor), Some(backfaceColor))(64, 64)
}
```

### Putting it all together

```scala
val canvasSettings = Canvas.Settings(width = 128, height = 128, scale = Some(4), clearColor = Color(0, 0, 0))

AppLoop
.statefulRenderLoop((t: Double) => (canvas: Canvas) => {
canvas.clear()
application(t, canvas)
canvas.redraw()
t + 0.01
}
)
.configure(canvasSettings, LoopFrequency.hz60, 0)
.run()
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 10. Audio playback
# 11. Audio playback

Besides graphics and input, Minart also supports loading and playing back audio.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 11. Loading sounds
# 12. Loading sounds

Just like we did with images, we can also load audio clips from files.

Expand Down

0 comments on commit c799a85

Please sign in to comment.