(define-module (gacela image)
#:use-module (gacela scene)
#:use-module (gacela game)
+ #:use-module ((sdl2) #:prefix sdl2:)
#:use-module ((sdl2 image) #:prefix sdl2:)
#:use-module ((sdl2 render) #:prefix sdl2:)
- #:export (import-bitmap
- move-xy))
+ #:use-module ((sdl2 surface) #:prefix sdl2:)
+ #:use-module (gl)
+ #:export (image
+ move
+ scale
+ over))
-(define-syntax-rule (import-bitmap filename)
+(define (calculate proc-or-value)
+ (if (procedure? proc-or-value)
+ (proc-or-value)
+ proc-or-value))
+
+(define (image filename)
(make-scene
- "bitmap"
+ "image"
(let ((image (sdl2:load-image filename))
- (texture #f))
- (let ((a 0))
- (lambda ()
- (if (not texture)
- (set! texture (sdl2:surface->texture %sdl-renderer image)))
- ;; (set! a (+ a 1))
- ;; (format #t "~a steps with texture ~a~%" a texture))))))
- (sdl2:clear-renderer %sdl-renderer)
- (sdl2:render-copy %sdl-renderer texture)
- (sdl2:present-renderer %sdl-renderer))))))
+ (texture #f)
+ (w/2 0)
+ (h/2 0))
+ (lambda ()
+ (when (not texture)
+ (set! texture (sdl2:surface->texture %sdl-renderer image))
+ (set! w/2 (/ (sdl2:surface-width image) 2))
+ (set! h/2 (/ (sdl2:surface-height image) 2)))
+ (gl-enable (oes-framebuffer-object texture-2d))
+ (sdl2:bind-texture texture)
+ (gl-enable (oes-framebuffer-object blend))
+ (set-gl-blend-function (blending-factor-src src-alpha)
+ (blending-factor-dest one-minus-src-alpha))
+ (gl-begin (begin-mode quads)
+ (gl-texture-coordinates 0 0)
+ (gl-vertex (- w/2) h/2 0)
+ (gl-texture-coordinates 1 0)
+ (gl-vertex w/2 h/2 0)
+ (gl-texture-coordinates 1 1)
+ (gl-vertex w/2 (- h/2) 0)
+ (gl-texture-coordinates 0 1)
+ (gl-vertex (- w/2) (- h/2) 0))
+ (gl-disable (oes-framebuffer-object blend))
+ (gl-disable (oes-framebuffer-object texture-2d))))))
+
+(define* (move scene x y #:optional (z 0))
+ (make-scene
+ "move"
+ (lambda ()
+ (gl-translate (calculate x)
+ (calculate y)
+ (calculate z))
+ (display-scene scene))))
-(define-syntax-rule (move-xy x y scene)
+(define* (scale scene x #:optional (y x) (z y))
(make-scene
- "move-xy"
+ "scale"
(lambda ()
+ (gl-scale (calculate x)
+ (calculate y)
+ (calculate z))
(display-scene scene))))
+
+(define (over . scenes)
+ (make-scene
+ "over"
+ (lambda ()
+ (let display ((sc scenes))
+ (cond ((not (null? sc))
+ (with-gl-push-matrix
+ (display-scene (car sc)))
+ (display (cdr sc))))))))