OSDN Git Service

986acefaac4237ac1842fe98ddf601246d3a931d
[rulp/rulp.git] / graphics / grid.lisp
1 ;;;; Ru*** roLeplay Playground virtual tabletop
2 ;;;; Copyright (C) 2022  Zull
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 <https://www.gnu.org/licenses/>.
16
17 (in-package :graphics)
18
19 (defparameter +grid-span+ 50)
20 (defparameter +is-grid-letters+ t)
21 (defparameter +is-grid+ t)
22
23
24 ; these two functions can be edited when different grids will be added
25 (defun position-actor-to-view (a)
26   (* a +grid-span+))
27 (defun position-view-to-actor (a)
28   (floor (/ a +grid-span+)))
29
30 (defparameter +letters-list+
31   '("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
32     "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"))
33
34 ;; this function cycle throughout the letters
35 (defun letter-cycle (n)
36   (nth (mod n (length +letters-list+)) +letters-list+))
37
38 (defun toggle-grid ()
39   (if +is-grid+
40       (setf +is-grid+ nil)
41       (setf +is-grid+ t)))
42
43 ;; NOTE: horrible
44 (defun toggle-grid-letters ()
45   (if +is-grid-letters+
46       (setf +is-grid-letters+ nil)
47       (setf +is-grid-letters+ t)))
48
49 (defmacro grid-render (renderer plane &optional (x 0) (y 0) (w 100) (h 100))
50   "renderize a grid in where required"
51   `(let ((neg-x (* ,x -1))
52          (neg-y (* ,y -1))
53          (plane-x (x ,plane))
54          (plane-y (y ,plane))
55          (x-iterations (/ (- ,w ,x) (grid-dimension ,plane)))
56          (y-iterations (/ (- ,h ,y) (grid-dimension ,plane)))
57          (grid-spacing (grid-dimension ,plane)))
58     (progn
59       ;; (sdl2:set-render-draw-color ,renderer ,red ,green ,blue 255)
60       (loop :for i :from (ceiling (/ neg-x grid-spacing)) :to x-iterations
61             :do
62                (sdl2:render-draw-line ,renderer
63                                       (+ (* i grid-spacing) plane-x)
64                                       (+ plane-y neg-y)
65                                       (+ (* i grid-spacing) plane-x)
66                                       (+ ,h plane-y neg-y))
67             )
68       (loop :for j :from (ceiling (/ neg-y grid-spacing)) :to y-iterations
69             :do
70                (sdl2:render-draw-line ,renderer
71                                       (+ plane-x neg-x)
72                                       (+ (* j grid-spacing) plane-y)
73                                       (+ plane-x ,w neg-x)
74                                       (+ (* j grid-spacing) plane-y))
75             ))
76     (sdl2:set-render-draw-color ,renderer 0 0 0 255)
77      ))
78
79 (defmacro indexes-render (renderer plane &optional (x 0) (y 0) (w 0) (h 0))
80   `(let ((neg-x (* ,x -1))
81          (neg-y (* ,y -1))
82          (x-offset (x ,plane))
83          (y-offset (y ,plane))
84          (x-iterations (/ (- ,w ,x) (grid-dimension ,plane)))
85          (y-iterations (/ (- ,h ,y) (grid-dimension ,plane)))
86          (grid-spacing (grid-dimension ,plane)))
87      (loop :for k :from (floor (/ neg-x grid-spacing)) :to x-iterations
88            :do (loop :for l :from (floor (/ neg-y grid-spacing)) :to y-iterations
89                      :do (tr-write (coordinate-to-grid-index (cons l (+ k 1)))
90                                    (+ x-offset (* k grid-spacing))
91                                    (+ y-offset (* l grid-spacing))
92                                    (floor (/ grid-spacing 3))
93                                    (floor (/ grid-spacing 3))
94                                    ,renderer)))
95      ;; (sdl2:set-render-draw-color ,renderer 0 255 0 100) ; remember to uncomment both
96      ;; (sdl2:render-fill-rect ,renderer (sdl2:make-rect ,x ,y ,w ,h)) ; test for x y w and h
97      ))
98
99 ;;; BUG: the coordinates given to the macros are incorrect. The render-fill-rect test
100 ;;; proven that the coordinates are not faithful of the view point visual. These
101 ;;; coordinate must be adjusted so the (now commented) render-fill-rect line will
102 ;;; always fill all the screen