OSDN Git Service

34139db7a6cc04716018389bb6b2733cb8844d05
[rulp/rulp.git] / graphics / render.lisp
1 (in-package :graphics)
2
3 (defgeneric texture (s))
4 (defgeneric surface (s))
5
6 (defmethod texture ((s screen))
7   "returns the screen texture, useful for accelerated enviroinments"
8   (unless (slot-value s 'texture)
9     (setf (slot-value s 'texture) (sdl2:create-texture-from-surface *renderer* (surface s))))
10   (slot-value s 'texture))
11
12 (defmethod surface ((s screen))
13   "returns the screen sdl2 surface, create if there is none"
14   (unless (slot-value s 'surface)
15     (setf (slot-value s 'surface) (sdl2-image:load-image (slot-value s 'path))))
16   (slot-value s 'surface))
17
18 (defun remove-nth (n list)
19   "given a list it returns the same list without the nth element"
20   (let ((k (mod n (length list))))
21     (if (< k 1)
22         (cdr list)
23         (cons (car list) (remove-nth (1- k) (cdr list)))
24        )))
25
26 (defun make-grid (span)
27   "create a square grid"
28   (loop :for i :from 0 :to *window-width* :by span
29         :do (sdl2:render-draw-line *renderer* i 0 i *window-height*))
30   (loop :for j :from 0 :to *window-height* :by span
31         :do (sdl2:render-draw-line *renderer* 0 j *window-width* j)))
32
33 (defun find-on-plane (x y plane)
34   "find the entity in real-coordinates (x,y) in plane"
35   (let ((mouse-point (sdl2:make-rect (- x 2) (- y 2) 2 2))
36         (entities (entities-list plane)))
37     (loop :for entity :in entities
38           :do (when (sdl2:has-intersect mouse-point
39                                         (screen-destination entity plane))
40                 entity))))
41
42 ;; NOTE: this doesn't directly uses +plane+ so it can be easily generalized
43 (defmacro render-plane-and-contents (renderer plane)
44   "given a PLANE it renders globally the plane images and entities. With
45 globally it means that this plane is rendered full on whatever texture is used.
46 to render locally use sdl2:set-render-target before calling this macro."
47   `(when ,plane
48      (sdl2:render-copy ,renderer (texture ,plane)
49                        :source-rect (screen-source ,plane)
50                        :dest-rect (screen-destination ,plane t))
51      (loop :for entity :in (entities-list ,plane)
52            :do (when (displayp entity)
53                  (sdl2:render-copy ,renderer (texture entity)
54                                    :source-rect (screen-source entity)
55                                    :dest-rect (screen-destination entity ,plane))))))
56
57 ;; NOTE: test if it is necessary to create render-plane-without-contents