Carp/examples/game.carp

95 lines
2.9 KiB
Plaintext
Raw Normal View History

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
2017-06-26 12:15:03 +03:00
(load "core/sdl.carp")
(load "core/sdl_image.carp")
2017-09-06 11:05:19 +03:00
(use Keycode)
2016-03-16 00:44:44 +03:00
2017-06-26 12:15:03 +03:00
(def max 400)
2016-03-18 09:40:57 +03:00
2017-06-26 12:15:03 +03:00
(defn r []
(the Int (random-between 0 max)))
2016-03-18 09:40:57 +03:00
2017-06-26 12:15:03 +03:00
(defn random-lines []
(let [p1 (make-point (r) (r))
p2 (make-point (r) (r))
p3 (make-point (r) (r))]
[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
2017-06-26 12:15:03 +03:00
(defn dimensions [texture]
(let [w 0
h 0]
(do
(SDL_QueryTexture texture NULL NULL (address w) (address h))
(make-rect 0 0 w h))))
2016-03-16 00:44:44 +03:00
2017-06-26 12:15:03 +03:00
(defn draw [rend images]
(let [rect (make-rect 32 32 (- 512 64) (- 512 64))]
(do
(SDL_SetRenderDrawBlendMode rend SDL_BLENDMODE_ADD)
(SDL_SetRenderDrawColor rend 0 0 0 255)
(SDL_RenderClear rend)
(SDL_SetRenderDrawColor rend 200 250 255 255)
(SDL_RenderFillRect rend (address rect))
(SDL_SetRenderDrawColor rend 100 50 255 155)
(let [rects [(make-rect 48 48 16 16)
(make-rect 48 80 16 16)
(make-rect 48 112 16 16)
(make-rect 48 144 16 16)]
2017-09-04 12:42:09 +03:00
n (count &rects)]
(SDL_RenderFillRects rend (raw rects) n))
2017-06-26 12:15:03 +03:00
(SDL_SetRenderDrawColor rend 255 50 100 255)
(for [x 0 512 16]
(do
(SDL_RenderDrawLine rend x 0 512 512)
(SDL_RenderDrawLine rend 512 (+ 256 (/ x 2)) 0 512)))
(SDL_SetRenderDrawColor rend 0 0 0 255)
(let [lines (random-lines)
2017-09-04 12:42:09 +03:00
n (count &lines)]
(SDL_RenderDrawLines rend (raw lines) n))
2017-06-26 12:15:03 +03:00
(let [img (Images.img1 (the (Ref Images) images))]
(SDL_RenderCopyEx rend
img
(address (dimensions img))
(address (make-rect 100 100 300 300))
(* 0.1 (from-int (SDL_GetTicks)))
2017-06-26 12:15:03 +03:00
(address (make-point 150 150))
SDL_FLIP_NONE))
(SDL_RenderPresent rend)
)))
2016-03-18 09:40:57 +03:00
2017-06-26 12:15:03 +03:00
(defn handle-events [app rend]
(let [event (SDL_Event_init)]
(while (SDL_PollEvent (address event))
2017-09-04 12:40:55 +03:00
(let [et (event-type &event)]
(cond (= et SDL_QUIT) (quit &app)
(= et SDL_KEYDOWN) (let [key (event-keycode &event)]
2017-06-26 12:15:03 +03:00
(cond
2017-09-04 12:40:55 +03:00
(= key SDLK_ESCAPE) (quit &app)
(println "Unrecognized key.")))
(println "Some other event happened..."))))))
2016-03-16 00:44:44 +03:00
2017-06-26 12:15:03 +03:00
(defn main []
(let [app (app-init (String.copy "~ CARP ~") 512 512)
2017-06-26 12:15:03 +03:00
rend (app-renderer app)
img1 (IMG_LoadTexture rend (cstr "./img/square.png"))
img2 (IMG_LoadTexture rend (cstr "./img/carp_logo_969_no_texture.png"))
2017-06-26 12:15:03 +03:00
images (Images.init img1 img2)]
(do
(println (ref (Images.str &images)))
(while true
(do
(handle-events app rend)
(draw rend &images)
(SDL_Delay 30))))))
2017-09-08 13:24:57 +03:00
(build)
(run)