From: jsancho Date: Mon, 12 Sep 2011 18:25:53 +0000 (+0000) Subject: (no commit message) X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=8d9d9e6ef939fbfc939cdd5c86a31a7ca919899e;p=gacela.git --- diff --git a/src/gacela.scm b/src/gacela.scm index f14b837..f8dcaf4 100644 --- a/src/gacela.scm +++ b/src/gacela.scm @@ -171,6 +171,17 @@ (define resources-cache (make-weak-value-hash-table)) +(define get-resource-from-cache #f) +(define insert-resource-into-cache #f) + +(let () + (set! get-resource-from-cache + (lambda (key) + (hash-ref resources-cache key))) + + (set! insert-resource-into-cache + (lambda (key res) + (hash-set! resources-cache key res)))) ;;; GaCeLa Functions diff --git a/src/gacela_draw.scm b/src/gacela_draw.scm index f72c751..595efe9 100644 --- a/src/gacela_draw.scm +++ b/src/gacela_draw.scm @@ -86,22 +86,27 @@ (zoomSurface surface zoomx zoomy 0)))))) (define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR)) - (progn-textures - (receive - (image real-w real-h) (load-image-for-texture filename) - (cond (image - (let ((width (surface-w image)) (height (surface-h image)) - (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN) - (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA) - (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA))) - (texture (car (glGenTextures 1)))) - - (glBindTexture GL_TEXTURE_2D texture) - (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image)) - (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter) - (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter) - (set-texture-size! texture real-w real-h) - texture)))))) + (let* ((key (list filename min-filter mag-filter)) + (res (get-resource-from-cache key))) + (cond (res res) + (else + (progn-textures + (receive + (image real-w real-h) (load-image-for-texture filename) + (cond (image + (let ((width (surface-w image)) (height (surface-h image)) + (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN) + (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA) + (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA))) + (texture (car (glGenTextures 1)))) + + (glBindTexture GL_TEXTURE_2D texture) + (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image)) + (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter) + (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter) + (set-texture-size! texture real-w real-h) + (insert-resource-into-cache key texture) + texture))))))))) (define* (draw-image filename #:optional (zoom 1)) (let ((texture (load-texture filename))) diff --git a/src/gacela_ttf.scm b/src/gacela_ttf.scm index 2281532..a9e64c0 100644 --- a/src/gacela_ttf.scm +++ b/src/gacela_ttf.scm @@ -16,7 +16,11 @@ (define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode)) - (let ((font (ftglCreateTextureFont font-file))) + (let* ((key (list font-file)) + (font (get-resource-from-cache key))) + (cond ((not font) + (set! font (ftglCreateTextureFont font-file)) + (insert-resource-into-cache key font))) (ftglSetFontFaceSize font size 72) (ftglSetFontCharMap font encoding) font))