-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from paulcadman/2d-graphics
Add 2D Graphics library
- Loading branch information
Showing
14 changed files
with
186 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import Lens | |
namespace Types | ||
|
||
open Raylean.Types | ||
open Lens | ||
|
||
structure Player where | ||
position : Vector2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Raylean.Graphics2D.Basic | ||
import Raylean.Graphics2D.Render |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Raylean.Types | ||
|
||
open Raylean.Types | ||
|
||
namespace Raylean.Graphics2D | ||
|
||
inductive Picture : Type where | ||
| blank : Picture | ||
| line (path : Array Vector2) : Picture | ||
| circle (radius : Float) : Picture | ||
| rectangle (width : Float) (height : Float) | ||
| color : Color → Picture → Picture | ||
| translate : Vector2 → Picture → Picture | ||
| scale : Vector2 → Picture → Picture | ||
| pictures : Array Picture → Picture | ||
|
||
instance : Inhabited Picture where | ||
default := .blank | ||
|
||
instance : Append Picture where | ||
append : Picture → Picture → Picture | ||
| (.pictures ps1), (.pictures ps2) => .pictures <| ps1 ++ ps2 | ||
| (.pictures ps), p => .pictures <| ps.push p | ||
| p, (.pictures ps) => .pictures <| #[p] ++ ps | ||
| p1, p2 => .pictures #[p1, p2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Raylean.Core | ||
import Raylean.Types | ||
import Raylean.Math | ||
import Raylean.Graphics2D.Basic | ||
import Lens | ||
import Raylean.Lean | ||
|
||
open Raylean.Types | ||
open Lens | ||
|
||
namespace Raylean.Graphics2D | ||
|
||
structure RenderContext where | ||
scale : Vector2 | ||
color : Color | ||
translate : Vector2 | ||
center : Vector2 | ||
|
||
/-- Translate a point in screen coordinates -/ | ||
def RenderContext.screenTranslate (s : RenderContext) (v : Vector2) : Vector2 := | ||
⟨v.x + s.translate.x, v.y - s.translate.y⟩ | ||
|
||
/-- Convert a point in Picture coordinates to a point in screen coordinates -/ | ||
def RenderContext.toScreen (s : RenderContext) (v : Vector2) : Vector2 := | ||
s.screenTranslate ⟨s.center.x + s.scale.x * v.x, s.center.y - s.scale.y * v.y⟩ | ||
|
||
abbrev getContext : ReaderT RenderContext IO RenderContext := read | ||
|
||
makeLenses RenderContext | ||
|
||
open RenderContext.Lens | ||
|
||
def renderLine (points : Array Vector2) : ReaderT RenderContext IO Unit := do | ||
let ctx ← getContext | ||
drawLineStrip (points.map ctx.toScreen) ctx.color | ||
|
||
def renderCircle (radius : Float) : ReaderT RenderContext IO Unit := do | ||
let ctx ← getContext | ||
drawCircleV (ctx.screenTranslate ctx.center) (radius * (max 0 (max ctx.scale.x ctx.scale.y))) ctx.color | ||
|
||
def renderRectangle (width height : Float) : ReaderT RenderContext IO Unit := do | ||
let ctx ← getContext | ||
let topLeft : Vector2 := ⟨-width / 2, height / 2⟩ | ||
let p := ctx.toScreen topLeft | ||
let r : Rectangle := {x := p.x, y := p.y, width := ctx.scale.x * width, height := ctx.scale.y * height} | ||
drawRectangleRec r ctx.color | ||
|
||
-- This function is partial because lean cannot prove termination | ||
partial def renderPicture' : (picture : Picture) → ReaderT RenderContext IO Unit | ||
| .blank => return () | ||
| .line ps => renderLine ps | ||
| .circle radius => renderCircle radius | ||
| .rectangle width height => renderRectangle width height | ||
| .color c p => renderPicture' p |>.local (set color c) | ||
| .translate v p => renderPicture' p |>.local (over translate (·.add v)) | ||
| .scale v p => renderPicture' p |>.local (over scale (·.dot v)) | ||
| .pictures ps => (fun _ => ()) <$> ps.mapM renderPicture' | ||
|
||
def renderPicture (width height : Float) (picture : Picture) : IO Unit := | ||
let initState := {scale := ⟨1,1⟩, color := Color.transparent, translate := ⟨0,0⟩, center := ⟨width / 2, height / 2⟩} | ||
renderPicture' picture |>.run initState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
/-- Execute a computation in a modified environment --/ | ||
def ReaderT.local {ρ : Type u} (f : ρ → ρ) (r : ReaderT ρ m α) : ReaderT ρ m α := r.run ∘ f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters