Carp/examples/carp_demo.carp
Erik Svedäng 889f55fe8f
feat: Remove address (#1223)
* chore: Abuse deftemplate to get rid of Address

* chore: Avoid semicolon at end of define

* fix: address should be useable as an argument to a HOF

* chore: adapt examples to new address signature

* fix: Remove Address from typeOf

* fix: Remove more uses of Address

* fix: Remove more mentions of address in the Haskell code

* fix: Remove test that ensure you can't take the address of non-symbols

* refactor: Moved `address` to Pointer.carp

* refactor: Move address into Pointer module

Co-authored-by: Jorge Acereda <jacereda@gmail.com>
2021-05-27 22:04:46 +02:00

93 lines
2.9 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 (Pointer.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
(Pointer.address &(SDL.dimensions img))
(Pointer.address &(SDL.rect 100 100 300 300))
(* 0.1 (from-int (SDL.get-ticks)))
(Pointer.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))))