OSDN Git Service

inputs.lisp: better input system for mouse
[rulp/rulp.git] / graphics / grid.lisp
index 81b5a07..986acef 100644 (file)
       (setf +is-grid-letters+ nil)
       (setf +is-grid-letters+ t)))
 
-;;; FIXME: grid renders always from top and not from actual position, grid doesn't move with view
-(defmacro grid-render (renderer plane &optional (r 0) (g 0) (b 0))
-  `(let ((x-offset (x ,plane))
-         (y-offset (y ,plane))
-         (x-iterations (/ *window-width* (grid-dimension ,plane)))
-         (y-iterations (/ *window-height* (grid-dimension ,plane)))
+(defmacro grid-render (renderer plane &optional (x 0) (y 0) (w 100) (h 100))
+  "renderize a grid in where required"
+  `(let ((neg-x (* ,x -1))
+         (neg-y (* ,y -1))
+         (plane-x (x ,plane))
+         (plane-y (y ,plane))
+         (x-iterations (/ (- ,w ,x) (grid-dimension ,plane)))
+         (y-iterations (/ (- ,h ,y) (grid-dimension ,plane)))
          (grid-spacing (grid-dimension ,plane)))
-     (progn
-       (sdl2:set-render-draw-color ,renderer ,r ,g ,b 255)
-       (loop :for i :from 1 :to x-iterations
-             :do
-                (sdl2:render-draw-line ,renderer
-                                       (+ (* i grid-spacing) x-offset)
-                                       y-offset
-                                       (+ (* i grid-spacing) x-offset)
-                                       (+ *window-height* y-offset))
-             )
-       (loop :for j :from 1 :to y-iterations
-             :do
-                 (sdl2:render-draw-line ,renderer
-                                        x-offset
-                                        (+ (* j grid-spacing) y-offset)
-                                        (+ *window-width* x-offset)
-                                        (+ (* j grid-spacing) y-offset))
-              ))
-       (sdl2:set-render-draw-color ,renderer 0 0 0 255)
-    ))
+    (progn
+      ;; (sdl2:set-render-draw-color ,renderer ,red ,green ,blue 255)
+      (loop :for i :from (ceiling (/ neg-x grid-spacing)) :to x-iterations
+            :do
+               (sdl2:render-draw-line ,renderer
+                                      (+ (* i grid-spacing) plane-x)
+                                      (+ plane-y neg-y)
+                                      (+ (* i grid-spacing) plane-x)
+                                      (+ ,h plane-y neg-y))
+            )
+      (loop :for j :from (ceiling (/ neg-y grid-spacing)) :to y-iterations
+            :do
+               (sdl2:render-draw-line ,renderer
+                                      (+ plane-x neg-x)
+                                      (+ (* j grid-spacing) plane-y)
+                                      (+ plane-x ,w neg-x)
+                                      (+ (* j grid-spacing) plane-y))
+            ))
+    (sdl2:set-render-draw-color ,renderer 0 0 0 255)
+     ))
 
-;;; FIXME: same as grid-render
-(defmacro indexes-render (renderer plane &optional (r 0) (g 0) (b 0))
-  `(let ((x-offset (x ,plane))
+(defmacro indexes-render (renderer plane &optional (x 0) (y 0) (w 0) (h 0))
+  `(let ((neg-x (* ,x -1))
+         (neg-y (* ,y -1))
+         (x-offset (x ,plane))
          (y-offset (y ,plane))
-         (x-iterations (/ *window-width* (grid-dimension ,plane)))
-         (y-iterations (/ *window-height* (grid-dimension ,plane)))
+         (x-iterations (/ (- ,w ,x) (grid-dimension ,plane)))
+         (y-iterations (/ (- ,h ,y) (grid-dimension ,plane)))
          (grid-spacing (grid-dimension ,plane)))
-     (loop :for k :from 0 :to x-iterations
-           :do (loop :for l :from 0 :to y-iterations
+     (loop :for k :from (floor (/ neg-x grid-spacing)) :to x-iterations
+           :do (loop :for l :from (floor (/ neg-y grid-spacing)) :to y-iterations
                      :do (tr-write (coordinate-to-grid-index (cons l (+ k 1)))
                                    (+ x-offset (* k grid-spacing))
                                    (+ y-offset (* l grid-spacing))
                                    (floor (/ grid-spacing 3))
                                    (floor (/ grid-spacing 3))
                                    ,renderer)))
+     ;; (sdl2:set-render-draw-color ,renderer 0 255 0 100) ; remember to uncomment both
+     ;; (sdl2:render-fill-rect ,renderer (sdl2:make-rect ,x ,y ,w ,h)) ; test for x y w and h
      ))
+
+;;; BUG: the coordinates given to the macros are incorrect. The render-fill-rect test
+;;; proven that the coordinates are not faithful of the view point visual. These
+;;; coordinate must be adjusted so the (now commented) render-fill-rect line will
+;;; always fill all the screen