2017-09-06 11:05:19 +03:00
|
|
|
(use IO)
|
|
|
|
(use System)
|
|
|
|
(use Int)
|
|
|
|
(use Double)
|
|
|
|
(use Array)
|
2016-03-16 00:44:44 +03:00
|
|
|
|
2018-03-20 14:26:27 +03:00
|
|
|
(load "SDL.carp")
|
2018-03-20 14:49:46 +03:00
|
|
|
(load "SDL_image.carp")
|
2016-03-16 00:44:44 +03:00
|
|
|
|
2018-03-14 22:48:23 +03:00
|
|
|
(Project.config "title" "Game")
|
|
|
|
|
2018-01-29 09:14:41 +03:00
|
|
|
(def rand-max 400)
|
2016-03-18 09:40:57 +03:00
|
|
|
|
2017-06-26 12:15:03 +03:00
|
|
|
(defn r []
|
2018-01-29 09:14:41 +03:00
|
|
|
(the Int (random-between 0 rand-max)))
|
2016-03-18 09:40:57 +03:00
|
|
|
|
2017-06-26 12:15:03 +03:00
|
|
|
(defn random-lines []
|
2018-03-20 14:49:46 +03:00
|
|
|
(let [p1 (SDL.point (r) (r))
|
|
|
|
p2 (SDL.point (r) (r))
|
|
|
|
p3 (SDL.point (r) (r))]
|
2017-06-26 12:15:03 +03:00
|
|
|
[p1 p2 p3 p1]))
|
2016-03-18 09:40:57 +03:00
|
|
|
|
2017-06-26 12:15:03 +03:00
|
|
|
(deftype Images
|
|
|
|
[img1 (Ptr SDL_Texture)
|
|
|
|
img2 (Ptr SDL_Texture)])
|
2016-03-16 00:44:44 +03:00
|
|
|
|
2018-03-20 14:26:27 +03:00
|
|
|
(def images (Images.init NULL NULL))
|
|
|
|
|
2018-03-20 18:38:50 +03:00
|
|
|
(defn draw [app rend state-ref]
|
2018-03-20 14:49:46 +03:00
|
|
|
(let [rect (SDL.rect 32 32 (- 512 64) (- 512 64))]
|
2017-06-26 12:15:03 +03:00
|
|
|
(do
|
2018-03-20 14:49:46 +03:00
|
|
|
(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)
|
2021-05-27 23:04:46 +03:00
|
|
|
(SDL.render-fill-rect rend (Pointer.address &rect))
|
2018-03-20 14:49:46 +03:00
|
|
|
(SDL.set-render-draw-color rend 100 50 255 155)
|
2018-03-24 12:29:22 +03:00
|
|
|
(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)]
|
2018-05-20 10:57:51 +03:00
|
|
|
n (length &rects)]
|
2018-03-20 14:49:46 +03:00
|
|
|
(SDL.render-fill-rects rend (raw rects) n))
|
|
|
|
(SDL.set-render-draw-color rend 255 50 100 255)
|
2017-06-26 12:15:03 +03:00
|
|
|
(for [x 0 512 16]
|
|
|
|
(do
|
2018-03-20 14:49:46 +03:00
|
|
|
(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)
|
2017-06-30 10:37:23 +03:00
|
|
|
(let [lines (random-lines)
|
2018-05-20 10:57:51 +03:00
|
|
|
n (length &lines)]
|
2018-03-20 14:49:46 +03:00
|
|
|
(SDL.render-draw-lines rend (raw lines) n))
|
2018-03-20 14:26:27 +03:00
|
|
|
(let [img @(Images.img1 &images)]
|
2018-03-20 14:49:46 +03:00
|
|
|
(SDL.render-copy-ex rend
|
|
|
|
img
|
2021-05-27 23:04:46 +03:00
|
|
|
(Pointer.address &(SDL.dimensions img))
|
|
|
|
(Pointer.address &(SDL.rect 100 100 300 300))
|
2018-03-20 14:49:46 +03:00
|
|
|
(* 0.1 (from-int (SDL.get-ticks)))
|
2021-05-27 23:04:46 +03:00
|
|
|
(Pointer.address &(SDL.point 150 150))
|
2018-03-20 14:49:46 +03:00
|
|
|
SDL.flip-none)))))
|
2016-03-18 09:40:57 +03:00
|
|
|
|
2019-09-13 13:09:11 +03:00
|
|
|
(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)))
|
2016-03-16 00:44:44 +03:00
|
|
|
|
2018-03-20 18:38:50 +03:00
|
|
|
(defn tick [state] state)
|
|
|
|
|
2017-06-26 12:15:03 +03:00
|
|
|
(defn main []
|
2018-03-20 18:38:50 +03:00
|
|
|
(let [app (SDLApp.create "~ CARP ~" 512 512)
|
|
|
|
rend @(SDLApp.renderer &app)
|
|
|
|
initial-state 0]
|
2017-10-11 17:22:14 +03:00
|
|
|
(do
|
2020-11-20 09:52:59 +03:00
|
|
|
(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"))))
|
2019-09-13 13:09:11 +03:00
|
|
|
(SDLApp.run-with-callbacks &app event-handler tick draw initial-state))))
|