mirror of
https://github.com/carp-lang/Carp.git
synced 2024-10-11 20:49:05 +03:00
86 lines
2.8 KiB
Plaintext
86 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 (count &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 (count &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 handle-events [app]
|
|
(foreach [event &(SDL.Event.all)]
|
|
(case (SDL.Event.type event)
|
|
SDL.Event.quit (SDLApp.stop app)
|
|
SDL.Event.key-down (let [key (SDL.Event.keycode event)]
|
|
(case key
|
|
SDL.Keycode.escape (SDLApp.stop app)
|
|
SDL.Keycode.backspace (println "!")
|
|
(println "Unrecognized key.")))
|
|
() ;;(println "Some other event happened...")
|
|
)))
|
|
|
|
(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 "./img/square.png"))
|
|
(IMG.load-texture rend (cstr "./img/carp_logo_969_no_texture.png"))))
|
|
(SDLApp.run-with-callbacks &app handle-events tick draw initial-state))))
|