Some helper functions to make code much cleaner.

This commit is contained in:
Erik Svedäng 2018-03-22 14:17:26 +01:00
parent 482170ccd7
commit e037a481cd
5 changed files with 70 additions and 23 deletions

View File

@ -70,3 +70,19 @@ SDL_Color SDL_rgba(int r, int g, int b, int a) {
p.a = a;
return p;
}
int *SDL_Color_r(SDL_Color *col) {
return (int*)&col->r;
}
int *SDL_Color_g(SDL_Color *col) {
return (int*)&col->g;
}
int *SDL_Color_b(SDL_Color *col) {
return (int*)&col->b;
}
int *SDL_Color_a(SDL_Color *col) {
return (int*)&col->a;
}

View File

@ -34,4 +34,12 @@
(Fn [(Ptr TTF_Font) (Ptr Char) SDL_Color] (Ptr SDL_Surface))
"TTF_RenderText_Blended")
;; Helpers to make everything a bit nicer
(defn ok? [error-code]
(= 0 error-code))
(defn render-to-texture [rend font str]
(let [surface (render-text-blended font (cstr str) (SDL.rgb 0 0 0))]
(SDL.create-texture-from-surface rend surface)))
)

View File

@ -14,8 +14,15 @@
(register-type SDL_BlendMode)
(register-type SDL_RendererFlip)
(register-type SDL_Window)
(register-type Uint8)
(register-type SDL_Color)
(defmodule SDL_Color
(register r (Fn [&SDL_Color] &Int))
(register g (Fn [&SDL_Color] &Int))
(register b (Fn [&SDL_Color] &Int))
(register a (Fn [&SDL_Color] &Int)))
(defmodule SDL
;; Setup and teardown
(register INIT_EVERYTHING Int) ;; Weird macro
@ -56,17 +63,6 @@
(register query-texture (Fn [(Ptr SDL_Texture) (Ptr Int) (Ptr Int) (Ptr Int) (Ptr Int)] ()) "SDL_QueryTexture") ;; ? ? w h
(register create-texture-from-surface (Fn [(Ptr SDL_Renderer) (Ptr SDL_Surface)] (Ptr SDL_Texture)) "SDL_CreateTextureFromSurface")
;; Helpers (not part of SDL)
(register rect (Fn [Int Int Int Int] SDL_Rect)) ;; x y w h
(register point (Fn [Int Int] SDL_Point)) ;; x y
(register rgb (Fn [Int Int Int] SDL_Color)) ;; x y
(register rgba (Fn [Int Int Int] SDL_Color)) ;; x y
(defn dimensions [texture]
(let-do [w 0 h 0]
(query-texture texture NULL NULL (address w) (address h)) ;; TODO: Can't qualify 'query-texture' ??!
(SDL.rect 0 0 w h)))
;; Blend modes
(register blend-mode-none SDL_BlendMode "SDL_BLENDMODE_NONE")
(register blend-mode-blend SDL_BlendMode "SDL_BLENDMODE_BLEND")
@ -120,6 +116,34 @@
;; Time
(register get-ticks (Fn [] Int) "SDL_GetTicks")
;; Helpers (not part of SDL)
(register rect (Fn [Int Int Int Int] SDL_Rect)) ;; x y w h
(register point (Fn [Int Int] SDL_Point)) ;; x y
(register rgb (Fn [Int Int Int] SDL_Color)) ;; x y
(register rgba (Fn [Int Int Int] SDL_Color)) ;; x y
(defn dimensions [texture]
(let-do [w 0 h 0]
(query-texture texture NULL NULL (address w) (address h)) ;; TODO: Can't qualify 'query-texture' ??!
(SDL.rect 0 0 w h)))
(defn draw-texture-at [rend texture point]
(let [dims (SDL.dimensions texture)
dest (SDL.rect @(SDL_Point.x point)
@(SDL_Point.y point)
@(SDL_Rect.w &dims)
@(SDL_Rect.h &dims))]
(render-copy rend texture (address dims) (address dest))))
(defn-do bg [rend color]
(with Int
(set-render-draw-color rend
@(SDL_Color.r color)
@(SDL_Color.g color)
@(SDL_Color.b color)
255))
(render-clear rend))
)
;; App helper

View File

@ -9,6 +9,7 @@
* [0.3] References must keep track of their origin and prevent usage of them if the origin has been given away.
* [0.3] Can set a ref so it points to a value in a more short-lived scope, leads to 'stack-use-after-scope' error in clang-sanitizer.
* [0.3] The error reporting in Eval is a mess, must make it possible to return errors with correct location for all kinds of errors.
* [0.4] When using external functions registered in the *same* module their overridden name is ignored.
## Big Language Features
* [0.4] Doc strings and type annotations.

View File

@ -3,24 +3,22 @@
(def text1 (the (Ptr SDL_Texture) NULL))
(defn shifting-bg-color []
(SDL.color (/ (SDL.get-ticks) 10) 240 220 255))
(defn draw [app rend state-ref]
(do (SDL.set-render-draw-color rend 240 240 220 255)
(SDL.render-clear rend)
(let [dims (SDL.dimensions text1)
dest (SDL.rect (- 200 (/ @(SDL_Rect.w &dims) 2))
(- 150 @(SDL_Rect.h &dims))
@(SDL_Rect.w &dims)
@(SDL_Rect.h &dims))]
(SDL.render-copy rend text1 (address dims) (address dest)))))
(do (SDL.bg rend (shifting-bg-color))
(let [dims (SDL.dimensions text1)
point (SDL.point (- 200 (/ @(SDL_Rect.w &dims) 2)) (- 150 @(SDL_Rect.h &dims)))]
(SDL.draw-texture-at rend text1 &point))))
(defn main []
(let [app (SDLApp.create "Font Rendering with SDL_ttf" 400 300)
rend @(SDLApp.renderer &app)]
(do
(if (= 0 (TTF.init))
(let-do [font (TTF.open-font (cstr "resources/Hasklig.otf") 20)
surface (TTF.render-text-blended font (cstr "Carp!") (SDL.rgb 0 0 0))]
(set! text1 (SDL.create-texture-from-surface rend surface))
(if (TTF.ok? (TTF.init))
(let-do [font (TTF.open-font (cstr "resources/Hasklig.otf") 20)]
(set! text1 (TTF.render-to-texture rend font "Carp!"))
(SDLApp.run-with-callbacks &app SDLApp.default-event-handler id draw 0))
(println* "Failed to initialize SDL_ttf: " &(str (TTF.get-error))))
0)))