]> git.jsancho.org Git - gacela.git/blob - gacela/image.scm
Display images using OpenGL and textures
[gacela.git] / gacela / image.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2016 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 (define-module (gacela image)
19   #:use-module (gacela scene)
20   #:use-module (gacela game)
21   #:use-module ((sdl2) #:prefix sdl2:)
22   #:use-module ((sdl2 image) #:prefix sdl2:)
23   #:use-module ((sdl2 render) #:prefix sdl2:)
24   #:use-module ((sdl2 surface) #:prefix sdl2:)
25   #:use-module (gl)
26   #:export (bitmap
27             move-xy))
28
29 (define (bitmap filename)
30   (make-scene
31    "bitmap"
32    (let ((image (sdl2:load-image filename))
33          (texture #f)
34          (w/2 0)
35          (h/2 0))
36      (lambda* ()
37        (when (not texture)
38          (set! texture (sdl2:surface->texture %sdl-renderer image))
39          (set! w/2 (/ (sdl2:surface-width image) 2))
40          (set! h/2 (/ (sdl2:surface-height image) 2)))
41        (gl-enable (oes-framebuffer-object texture-2d))
42        (sdl2:bind-texture texture)
43        (gl-begin (begin-mode quads)
44          (gl-texture-coordinates 0 0)
45          (gl-vertex (- w/2) h/2 0)
46          (gl-texture-coordinates 1 0)
47          (gl-vertex w/2 h/2 0)
48          (gl-texture-coordinates 1 1)
49          (gl-vertex w/2 (- h/2) 0)
50          (gl-texture-coordinates 0 1)
51          (gl-vertex (- w/2) (- h/2) 0))
52        (gl-disable (oes-framebuffer-object texture-2d))))))
53
54 (define (move-xy x y scene)
55   (define (to-integer n)
56     (inexact->exact (round n)))
57   (make-scene
58    "move-xy"
59    (lambda ()
60      (let ((xy (list (to-integer (if (procedure? x) (x) x))
61                      (to-integer (if (procedure? y) (y) y)))))
62        (display-scene scene #:xy xy)))))