aboutsummaryrefslogtreecommitdiff
path: root/tests/renderer-test.scm
blob: a8fdeedaf8ff8aee845efb7446535c619d9da253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;; Load base deps
(import scheme
        (chicken base)
        (chicken keyword)
        (only srfi-1 fold iota for-each)
        defstruct
        srfi-64)

;; Mock tilemap module
(module tilemap *
  (import scheme (chicken base) defstruct)
  (defstruct tileset tilewidth tileheight spacing tilecount columns image-source image)
  (defstruct layer name width height map)
  (defstruct tilemap width height tilewidth tileheight tileset-source tileset layers objects)
  (defstruct tile id rect)
  (define (tileset-tile ts id) (make-tile id: id rect: #f))
  (define (tile-rect t) #f))
(import tilemap)

;; Mock sdl2
(module sdl2 *
  (import scheme (chicken base))
  (define (make-rect x y w h) (list x y w h))
  (define (render-copy! . args) #f)
  (define (render-copy-ex! . args) #f)
  (define (create-texture-from-surface . args) #f))
(import (prefix sdl2 "sdl2:"))

;; Mock sdl2-ttf
(module sdl2-ttf *
  (import scheme (chicken base))
  (define (render-text-solid . args) #f)
  (define (size-utf8 . args) (values 0 0)))
(import (prefix sdl2-ttf "ttf:"))

;; Load entity module
(include "entity.scm")
(import entity)

;; Load world module
(include "world.scm")
(import world)

;; Load renderer module
(include "renderer.scm")
(import renderer)

(test-begin "renderer")

(test-group "entity-screen-coords"
  (let* ((cam (make-camera x: 10 y: 20))
         (e   (list #:x 50 #:y 80 #:width 16 #:height 16)))
    (test-equal "subtracts camera offset from x"
                40
                (car (entity-screen-coords e cam)))
    (test-equal "subtracts camera offset from y"
                60
                (cadr (entity-screen-coords e cam)))
    (test-equal "preserves width"
                16
                (caddr (entity-screen-coords e cam)))
    (test-equal "preserves height"
                16
                (cadddr (entity-screen-coords e cam))))

  (let* ((cam (make-camera x: 0 y: 0))
         (e   (list #:x 100.7 #:y 200.3 #:width 16 #:height 16)))
    (test-equal "floors fractional x"
                100
                (car (entity-screen-coords e cam)))
    (test-equal "floors fractional y"
                200
                (cadr (entity-screen-coords e cam))))

  (let* ((cam (make-camera x: 0 y: 0))
         (e   (list #:x 0 #:y 0 #:width 32 #:height 32)))
    (test-equal "zero camera, zero position"
                '(0 0 32 32)
                (entity-screen-coords e cam))))

(test-group "entity-flip"
  (test-equal "facing 1: no flip"
              '()
              (entity-flip (list #:facing 1)))
  (test-equal "facing -1: horizontal flip"
              '(horizontal)
              (entity-flip (list #:facing -1)))
  (test-equal "no facing key: defaults to no flip"
              '()
              (entity-flip (list #:x 0))))

(test-end "renderer")