Skip to content

Commit

Permalink
ray example
Browse files Browse the repository at this point in the history
  • Loading branch information
melsman committed Jan 14, 2024
1 parent 11f9504 commit 120f6c2
Show file tree
Hide file tree
Showing 7 changed files with 596 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ test: prepare
$(MAKE) -C sml-examples/hello
$(MAKE) -C sml-examples/flags
$(MAKE) -C sml-examples/mandel
$(MAKE) -C sml-examples/nbody
$(MAKE) -C sml-examples/ray

.PHONY: clean
clean:
Expand All @@ -20,6 +22,8 @@ clean:
$(MAKE) -C sml-examples/hello clean
$(MAKE) -C sml-examples/flags clean
$(MAKE) -C sml-examples/mandel clean
$(MAKE) -C sml-examples/nbody clean
$(MAKE) -C sml-examples/ray clean

lib/github.com/diku-dk/sml-random:
smlpkg sync
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Standard ML package for interfacing with TIGR - the TIny GRaphics
library, which is a small graphics library that works on both Linux
and macOS platforms. In particular, this package contains bindings for
the [MLKit](https://github.com/melsman/mlkit) Standard ML compiler.
the [MLKit](https://github.com/melsman/mlkit) (> v4.7.8).

Notice that this repository **is a fork** of the repository for TIGR -
the [TIny GRaphics library](https://github.com/erkkah/tigr) - with an
Expand Down Expand Up @@ -66,17 +66,19 @@ setup MLKit to link with the dynamic library, consult
Standard ML examples include the [hello](sml-examples/hello/hello.sml)
and [flags](sml-examples/flags/flags.sml) examples ported to Standard
ML from C, the [mandel](sml-examples/mandel/mandel.sml) example
demonstrating an interactive Mandelbrot viewer, and the
demonstrating an interactive Mandelbrot viewer, the
[nbody](sml-examples/nbody/nbody.sml) example demonstrating an
interactive Nbody simulator.
interactive Nbody simulator, and the [ray](sml-examples/ray/ray.sml)
example demonstrating dynamic ray-tracing.

## The C Library (from https://github.com/erkkah/tigr)

TIGR is a tiny cross-platform graphics library, providing a unified
API for Windows, macOS, Linux, iOS and Android.

TIGR's core is a simple framebuffer library.
On top of that, we provide a few helpers for the common tasks that 2D programs generally need:
TIGR's core is a simple framebuffer library. On top of that, TIGR
provides a few helpers for the common tasks that 2D programs generally
need:

- Bitmap-backed windows.
- Direct access to bitmaps, no locking.
Expand All @@ -103,7 +105,8 @@ TIGR is free to copy with no restrictions; see [tigr.h](tigr.h).
## How do I program with TIGR?
![](./images/demo.gif)

Here's an example Hello World program. For more information, just read [tigr.h](tigr.h) to see the APIs available.
Here's an example Hello World program in C. For more information, just
read [tigr.h](tigr.h) to see the APIs available.

```C
#include "tigr.h"
Expand Down
4 changes: 4 additions & 0 deletions sml-examples/ray/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.exe
*~
MLB
libtigr.so
61 changes: 61 additions & 0 deletions sml-examples/ray/CommandLineArgs.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
structure CommandLineArgs :
sig
(* each takes a key K and a default value D, looks for -K V in the
* command-line arguments, and returns V if it finds it, or D otherwise. *)
val parseString : string -> string -> string
val parseInt : string -> int -> int
val parseBool : string -> bool -> bool

(* parseFlag K returns true if --K given on command-line *)
val parseFlag : string -> bool

val positional : unit -> string list
end =
struct

fun die msg =
( TextIO.output (TextIO.stdErr, msg ^ "\n")
; TextIO.flushOut TextIO.stdErr
; OS.Process.exit OS.Process.failure
)

fun positional () =
List.filter (not o String.isPrefix "-") (CommandLine.arguments ())

fun search key args =
case args of
[] => NONE
| x :: args' =>
if key = x
then SOME args'
else search key args'

fun parseString key default =
case search ("-" ^ key) (CommandLine.arguments ()) of
NONE => default
| SOME [] => die ("Missing argument of \"-" ^ key ^ "\" ")
| SOME (s :: _) => s

fun parseInt key default =
case search ("-" ^ key) (CommandLine.arguments ()) of
NONE => default
| SOME [] => die ("Missing argument of \"-" ^ key ^ "\" ")
| SOME (s :: _) =>
case Int.fromString s of
NONE => die ("Cannot parse integer from \"-" ^ key ^ " " ^ s ^ "\"")
| SOME x => x

fun parseBool key default =
case search ("-" ^ key) (CommandLine.arguments ()) of
NONE => default
| SOME [] => die ("Missing argument of \"-" ^ key ^ "\" ")
| SOME ("true" :: _) => true
| SOME ("false" :: _) => false
| SOME (s :: _) => die ("Cannot parse bool from \"-" ^ key ^ " " ^ s ^ "\"")

fun parseFlag key =
case search ("--" ^ key) (CommandLine.arguments ()) of
NONE => false
| SOME _ => true

end
27 changes: 27 additions & 0 deletions sml-examples/ray/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
PROGNAME=ray
MLKIT=mlkit
LIBS=
ifeq ($(OS),Windows_NT)
LDFLAGS += -s -lopengl32 -lgdi32
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
LDFLAGS += -arch x86_64 -framework OpenGL -framework Cocoa -L. -ltigr
else ifeq ($(UNAME_S),Linux)
LDFLAGS += -L.
LIBS += -libs 'm,c,dl,GLU,GL,X11,tigr'
endif
endif

.PHONY: all
all: $(PROGNAME).exe

$(PROGNAME).exe: $(PROGNAME).mlb $(PROGNAME).sml libtigr.so
MLCOMP=mlkit $(MLKIT) -ldexe '$(CC) $(LDFLAGS)' $(LIBS) -o $@ $<

libtigr.so: ../../lib/github.com/diku-dk/sml-tigr/clib/libtigr.so
cp $< $@

.PHONY: clean
clean:
rm -rf *~ *.exe MLB libtigr.so
7 changes: 7 additions & 0 deletions sml-examples/ray/ray.mlb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local
$(SML_LIB)/basis/basis.mlb
../../lib/github.com/diku-dk/sml-tigr/tigr.mlb
CommandLineArgs.sml
in
ray.sml
end
Loading

0 comments on commit 120f6c2

Please sign in to comment.