2018-03-22 15:25:30 +03:00
|
|
|
(load "SDL.carp")
|
|
|
|
(load "SDL_ttf.carp")
|
|
|
|
|
2018-06-05 11:05:33 +03:00
|
|
|
(Project.config "title" "Fonts")
|
2018-03-22 23:21:46 +03:00
|
|
|
|
|
|
|
(def font (the (Ptr TTF_Font) NULL))
|
2018-03-22 15:36:16 +03:00
|
|
|
(def text1 (the (Ptr SDL_Texture) NULL))
|
2018-03-22 15:25:30 +03:00
|
|
|
|
2018-03-22 16:17:26 +03:00
|
|
|
(defn shifting-bg-color []
|
2018-03-22 17:43:43 +03:00
|
|
|
(SDL.rgb (/ (SDL.get-ticks) 10) 240 220))
|
2018-03-22 16:17:26 +03:00
|
|
|
|
2018-03-22 15:25:30 +03:00
|
|
|
(defn draw [app rend state-ref]
|
2018-03-22 17:43:43 +03:00
|
|
|
(do (SDL.bg rend &(shifting-bg-color))
|
2018-03-26 12:54:20 +03:00
|
|
|
(let [dims (SDL.dimensions text1)]
|
|
|
|
(SDL.draw-texture-centered rend text1 &(SDL.point 200 150)))))
|
2018-03-22 15:25:30 +03:00
|
|
|
|
2018-03-22 23:21:46 +03:00
|
|
|
(defn change-text [rend]
|
|
|
|
(do (when (not (Pointer.eq NULL text1))
|
2018-03-23 09:28:51 +03:00
|
|
|
(SDL.destroy-texture text1))
|
2018-04-24 16:02:06 +03:00
|
|
|
(set! text1 (TTF.render-text-to-texture rend font &(str* "TICK: " (SDL.get-ticks)) (SDL.rgb 0 0 0)))))
|
2018-03-22 23:21:46 +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.return) (do (change-text @(SDLApp.renderer app))
|
|
|
|
state)
|
|
|
|
state))
|
|
|
|
|
|
|
|
;; Other event
|
|
|
|
state)))
|
2018-03-22 23:21:46 +03:00
|
|
|
|
2018-03-22 15:25:30 +03:00
|
|
|
(defn main []
|
2018-03-22 15:36:16 +03:00
|
|
|
(let [app (SDLApp.create "Font Rendering with SDL_ttf" 400 300)
|
2018-03-22 15:25:30 +03:00
|
|
|
rend @(SDLApp.renderer &app)]
|
|
|
|
(do
|
2018-03-22 16:17:26 +03:00
|
|
|
(if (TTF.ok? (TTF.init))
|
2018-03-22 23:21:46 +03:00
|
|
|
(do (set! font (TTF.open-font (cstr "resources/Hasklig.otf") 20))
|
2018-04-24 16:02:06 +03:00
|
|
|
(set! text1 (TTF.render-text-to-texture rend font "Carp!" (SDL.rgb 0 0 0)))
|
2018-03-22 23:21:46 +03:00
|
|
|
(SDLApp.run-with-callbacks &app event-handler id draw 0))
|
2020-05-11 17:10:35 +03:00
|
|
|
(println* "Failed to initialize SDL_ttf: " &(from-cstr (TTF.get-error)))))))
|