Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ To run these you will need to install Claudius via opam, and then you can `dune
These examples just show a single feature of Claudius, and are a good place to start if you're trying to understand how Claudius works.

* [Bounce](/bounce/) - example of a bouncing ball made from ellipses.
* [Flying Camels](/flying_camels/) - example of using image files to recreate a classic screen saver effect.
* [Polygons](/polygons/) - rotating filled polygons.
* [Red Hexagon](/red_hexagon/) - a different style of rotating polyons.
* [Screen saver](/screen_saver/) - a recreation of a classic screen saver effect from the early Macintosh era.
Expand Down
1 change: 1 addition & 0 deletions flying_camels/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This example uses the ability to load images as a Picture.t in Claudius to recreate the old [Flying Windows Screensaver](https://microsoft.fandom.com/wiki/Flying_Windows).
4 changes: 4 additions & 0 deletions flying_camels/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(public_name flying_camels)
(name main)
(libraries claudius))
56 changes: 56 additions & 0 deletions flying_camels/bin/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
open Claudius

type point = { x : float; y : float }

let generate_points dimensions =
let width, height = dimensions in
let f_width, f_height = (float_of_int width, float_of_int height) in
Random.init 42;
List.init 42 (fun _i ->
let x = Random.float f_width -. (f_width /. 2.)
and y = Random.float f_height -. (f_height /. 2.) in
{ x; y })

let move_points t points =
let ft = float_of_int t in
List.map
(fun p ->
let distance = Float.sqrt ((p.x *. p.x) +. (p.y *. p.y)) in
let angle = Float.atan2 p.y p.x in
let new_distance = distance +. ft in
let i_dist = int_of_float new_distance in
let wrapped_dist = i_dist mod 300 in
let f_wrapped_dist = float_of_int wrapped_dist in
let f_wrapped_dist = f_wrapped_dist *. f_wrapped_dist /. 100. in
let x = f_wrapped_dist *. Float.cos angle
and y = f_wrapped_dist *. Float.sin angle in
{ x; y })
points

let tick t s _f _e =
let fb = Framebuffer.init (Screen.dimensions s) (fun _ _ -> 0) in
let img = (Screen.pictures s).(0) in
let w, h = Screen.dimensions s in
let img_w = Picture.original_width img
and img_h = Picture.original_height img in

let points = generate_points (Screen.dimensions s) |> move_points t in

List.iter
(fun p ->
let x, y = (int_of_float p.x, int_of_float p.y) in
let distance_from_origin = Float.sqrt ((p.x *. p.x) +. (p.y *. p.y)) in
let scale = distance_from_origin /. 200. in
Framebuffer.draw_picture img ~scale
(x - (img_w / 2) + (w / 2))
(y - (img_h / 2) + (h / 2))
fb)
points;

fb

let () =
let image_filenames = [ "flying_camels/images/logo48.png" ] in
Palette.generate_classic_vga_palette ()
|> Screen.create ~image_filenames 640 480 1
|> Base.run "Flying camels" None tick
Binary file added flying_camels/images/logo48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.