Nicer rendering of font.

This commit is contained in:
Erik Svedäng 2018-03-22 13:36:16 +01:00
parent 99132e3926
commit 482170ccd7
4 changed files with 36 additions and 18 deletions

View File

@ -53,7 +53,7 @@ SDL_Point SDL_point(int x, int y) {
return p;
}
SDL_Color SDL_color(int r, int g, int b) {
SDL_Color SDL_rgb(int r, int g, int b) {
SDL_Color p;
p.r = r;
p.g = g;
@ -61,3 +61,12 @@ SDL_Color SDL_color(int r, int g, int b) {
p.a = 255;
return p;
}
SDL_Color SDL_rgba(int r, int g, int b, int a) {
SDL_Color p;
p.r = r;
p.g = g;
p.b = b;
p.a = a;
return p;
}

View File

@ -22,7 +22,16 @@
;; TTF_STYLE_STRIKETHROUGH
;; Render
;; SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg)
(register render-text-solid (Fn [(Ptr TTF_Font) (Ptr Char) SDL_Color] (Ptr SDL_Surface)) "TTF_RenderText_Solid")
(register render-text-solid
(Fn [(Ptr TTF_Font) (Ptr Char) SDL_Color] (Ptr SDL_Surface))
"TTF_RenderText_Solid")
(register render-text-shaded
(Fn [(Ptr TTF_Font) (Ptr Char) SDL_Color SDL_Color] (Ptr SDL_Surface))
"TTF_RenderText_Shaded")
(register render-text-blended
(Fn [(Ptr TTF_Font) (Ptr Char) SDL_Color] (Ptr SDL_Surface))
"TTF_RenderText_Blended")
)

View File

@ -5,8 +5,8 @@
;; Types
(register-type SDL_Keycode)
(register-type SDL_Rect)
(register-type SDL_Point)
(register-type SDL_Rect [x Int, y Int, w Int, h Int])
(register-type SDL_Point [x Int, y Int])
(register-type SDL_Event)
(register-type SDL_EventType)
(register-type SDL_Texture)
@ -59,7 +59,8 @@
;; 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 color (Fn [Int Int Int] SDL_Color)) ;; 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]

View File

@ -1,27 +1,26 @@
(load "SDL.carp")
(load "SDL_ttf.carp")
(def font (the (Ptr TTF_Font) NULL))
(def texture (the (Ptr SDL_Texture) NULL))
(def text1 (the (Ptr SDL_Texture) NULL))
(defn draw [app rend state-ref]
(do (SDL.set-render-draw-color rend 240 240 220 255)
(SDL.render-clear rend)
(SDL.render-copy rend
texture
(address (SDL.dimensions texture))
(address (SDL.rect 100 100 300 300)))
))
(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)))))
(defn main []
(let [app (SDLApp.create "Font Rendering with SDL_ttf" 800 600)
(let [app (SDLApp.create "Font Rendering with SDL_ttf" 400 300)
rend @(SDLApp.renderer &app)]
(do
(if (= 0 (TTF.init))
(do
(set! font (TTF.open-font (cstr "resources/Hasklig.otf") 20))
(let [surface (TTF.render-text-solid font (cstr "Carp!") (SDL.color 0 0 0))]
(set! texture (SDL.create-texture-from-surface rend surface)))
(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))
(SDLApp.run-with-callbacks &app SDLApp.default-event-handler id draw 0))
(println* "Failed to initialize SDL_ttf: " &(str (TTF.get-error))))
0)))