Carp/examples/carp_demo.carp
Erik Svedäng a873099640
chore: Move some examples to test/produces-output (#989)
* chore: Moved examples that work more as tests to folder 'test/produces-output'

* fix: Corrections to the release script

* fix: Correct filename on Windows

* fix: Move more files around

* fix: Remove check-malloc example

* fix: Apparently unicode example does not work

* chore: Move nested_lambdas.carp back to examples

* chore: Remove .DS_Store files

* fix: Bring back unicode test

* test: Make sure benchmark compile (had to remove mandelbrot and n-bodies)

* fix: Replacement implementation of clock_gettime on Windows

* chore: Trigger CI

* fix: Define CLOCK_REALTIME

Co-authored-by: Erik Svedang <erik@Eriks-iMac.local>
2020-11-23 06:30:43 +01:00

93 lines
2.8 KiB
Plaintext

(use IO)
(use System)
(use Int)
(use Double)
(use Array)
(load "SDL.carp")
(load "SDL_image.carp")
(Project.config "title" "Game")
(def rand-max 400)
(defn r []
(the Int (random-between 0 rand-max)))
(defn random-lines []
(let [p1 (SDL.point (r) (r))
p2 (SDL.point (r) (r))
p3 (SDL.point (r) (r))]
[p1 p2 p3 p1]))
(deftype Images
[img1 (Ptr SDL_Texture)
img2 (Ptr SDL_Texture)])
(def images (Images.init NULL NULL))
(defn draw [app rend state-ref]
(let [rect (SDL.rect 32 32 (- 512 64) (- 512 64))]
(do
(SDL.set-render-draw-blend-mode rend SDL.blend-mode-add)
(SDL.set-render-draw-color rend 0 0 0 255)
(SDL.render-clear rend)
(SDL.set-render-draw-color rend 200 250 255 255)
(SDL.render-fill-rect rend (address rect))
(SDL.set-render-draw-color rend 100 50 255 155)
(let [mouse-state (SDL.MouseState.get)
x @(SDL.MouseState.x &mouse-state)
rects [(SDL.rect x 48 16 16)
(SDL.rect (* x 2) 80 16 16)
(SDL.rect (* x 4) 112 16 16)
(SDL.rect (* x 8) 144 16 16)]
n (length &rects)]
(SDL.render-fill-rects rend (raw rects) n))
(SDL.set-render-draw-color rend 255 50 100 255)
(for [x 0 512 16]
(do
(SDL.render-draw-line rend x 0 512 512)
(SDL.render-draw-line rend 512 (+ 256 (/ x 2)) 0 512)))
(SDL.set-render-draw-color rend 0 0 0 255)
(let [lines (random-lines)
n (length &lines)]
(SDL.render-draw-lines rend (raw lines) n))
(let [img @(Images.img1 &images)]
(SDL.render-copy-ex rend
img
(address (SDL.dimensions img))
(address (SDL.rect 100 100 300 300))
(* 0.1 (from-int (SDL.get-ticks)))
(address (SDL.point 150 150))
SDL.flip-none)))))
(defn event-handler [app state event]
(let [et (SDL.Event.type event)]
(cond
;; Quit event
(= et SDL.Event.quit)
(SDLApp.stop app)
;; Key events
(= et SDL.Event.key-down)
(let [key (SDL.Event.keycode event)]
(cond
(= key SDL.Keycode.escape) (SDLApp.stop app)
(= key SDL.Keycode.backspace) (do (println "!")
state)
state))
;; Other event
state)))
(defn tick [state] state)
(defn main []
(let [app (SDLApp.create "~ CARP ~" 512 512)
rend @(SDLApp.renderer &app)
initial-state 0]
(do
(set! images (Images.init (IMG.load-texture rend (cstr "./resources/logo/square.png"))
(IMG.load-texture rend (cstr "./resources/logo/carp_logo_969_no_texture.png"))))
(SDLApp.run-with-callbacks &app event-handler tick draw initial-state))))