OSDN Git Service

correcting input and rulp main loop
[rulp/rulp.git] / graphics / view.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 :rulp.graphics)
18 #| -------------------------------------------
19  | This file manipulate the window and the
20  | sdl initialization.
21  | -------------------------------------------
22  |#
23
24 (defun create-plane (map-info map-path &key (number 0))
25   "convert the map informations into a functioning plane, without number it convert the
26 first element"
27   (let ((plane-info (nth number (cdr (assoc :planes map-info)))))
28     (values
29      (make-instance 'rulp.layers:screen
30                     :image (merge-pathnames map-path (cdr (assoc :image-path plane-info))))
31      (cdr (assoc :grid-dimension plane-info))
32      )
33     ;; FIXME: generalize... somehow
34     )
35   )
36
37 ;; FIXME: search alternative
38 (defun create-entities (map-info map-path)
39   (let ((entities-info (cdr (assoc :entities map-info))))
40     (loop :for entity-info :in entities-info
41           :collect
42           (make-instance 'entity
43                          :image (merge-pathnames map-path (cdr (assoc :image-path entity-info)))
44                          :name (assoc :name entity-info)
45                          )
46           )
47     )
48   )
49
50 (defun grid-layout (x y &key (size 1))
51   "converts natural coordinates and size into pixel positions. This function can be replaced to change
52 the actual grid layout"
53   (values (cons (* x *plane-grid*)
54                 (* y *plane-grid*))
55           (* size *plane-grid*)))
56
57 (defun actors-layout (x y)
58   (cons (floor (/ x *plane-grid*))
59         (floor (/ y *plane-grid*)))
60   )
61
62 (defmacro with-playground ((window renderer &key (title "RuLP")) &body body)
63   `(sdl2:with-window (,window :title title :w *window-width* :h *window-height* :flags '(:resizable))
64      ;; NOTE: here is where you should set the window icon, if there's the method to do that
65      (sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated :presentvsync)) ;later add delta
66        (sdl2-image:init '(:png :jpg))
67        (sdl2-ttf:init)
68        ,@body
69        (sdl2-ttf:quit)
70        (sdl2-image:quit)
71        )))
72
73 ;; FIXME: temporary place
74 ;; set boundaries so the plane doesn't go away with the panning. fix boundaries to -window width, -window height, plane width, plane height
75 (defparameter *viewpoint-offset* '(0 . 0))
76 (defparameter *viewpoint-zoom* 1)
77 (defparameter *mouse-previous* '(0 . 0))
78
79 (defparameter *framerule* 20
80   "every 20 frames the parameter *changep* is set to t, so it updates informations
81 even when away from keyboard")
82
83 (defparameter *active-entries* nil
84   "contain the active menu in form of a entry class")
85
86 (defparameter *changep* t
87   "set to t when a change on the plane is made, if nil the viewpoint is not updated")
88
89 ;; renderer exists only inside this function, so you cannot create a texture outside
90 ;; (at least for now), more on this later os
91 (defun playground (title &optional (fps 60) (debug-info nil))
92   "initialize and run the game loop. it takes a TITLE to display and FPS for framerate.
93 DEBUG-INFO can be used to display the content on screen for test and debug purposes."
94   (declare (ignore fps))
95   (sdl2:with-init (:video)
96     (with-playground (window *renderer* :title title)
97       (setf *tr-texture* (let* ((font (sdl2-ttf:open-font "media/IBMPlex.ttf" 100)) ;; FIXME: this crashes the program under windows, throws error on linux but works anyway
98                                 (font-surface (sdl2-ttf:render-utf8-solid font *tr-string* 0 0 0 0))
99                                 (font-texture (sdl2:create-texture-from-surface *renderer* font-surface)))
100                            (sdl2:free-surface font-surface)
101                            font-texture))
102       (multiple-value-bind (p g) (create-plane *map-info* *map-path*)
103         (setf *plane* p)
104         (setf *plane-grid* g))
105       (setf *entities-list* (create-entities *map-info* *map-path*))
106       (let (;; (mouse-button-previous nil)
107             (window-texture (sdl2:get-render-target *renderer*))
108             (viewpoint-texture (sdl2:create-texture *renderer* (sdl2:get-window-pixel-format window)
109                                                     2 (width *plane*) (height *plane*))) ;camping into the ramhog
110             (viewpoint-rectangle (sdl2:make-rect 0 0 10 10)))
111         (sdl2:with-event-loop (:method :poll)
112           (:quit () t)
113           (:keydown ()
114                     ;; FIXME: incorporate into input.lisp. check for input every now and then outside keydown
115                     (when (sdl2:keyboard-state-p :scancode-up) (setf (cdr *viewpoint-offset*) (+ (cdr *viewpoint-offset*) 10)))
116                     (when (sdl2:keyboard-state-p :scancode-down) (setf (cdr *viewpoint-offset*) (+ (cdr *viewpoint-offset*) -10)))
117                     (when (sdl2:keyboard-state-p :scancode-left) (setf (car *viewpoint-offset*) (+ (car *viewpoint-offset*) 10)))
118                     (when (sdl2:keyboard-state-p :scancode-right) (setf (car *viewpoint-offset*) (+ (car *viewpoint-offset*) -10)))
119                     (when (sdl2:keyboard-state-p :scancode-p) (setf *viewpoint-zoom* (+ *viewpoint-zoom* 0.2))) ; FIXME: find the scancode for the plus sign
120                     (when (sdl2:keyboard-state-p :scancode-m) (setf *viewpoint-zoom* (+ *viewpoint-zoom* -0.2)))
121                     )
122           (:mousebuttondown ()
123                             (setf *is-mouse-hold* t)
124                             ;; this routine seems identical to the one in idle, instead
125                             ;; of calling the second element of the keybind list it calls
126                             ;; the third element. If there is something there it would
127                             ;; execute a special function for when the mouse is released.
128                             (mouse-event *mouse-position*)
129                             )
130           (:mousebuttonup ()
131                           ;; what about functions that require a single press? just create
132                           ;; a keybind (+key+ nil (...)) and it will execute just on
133                           ;; release and not on hold
134                           ;; (setf mouse-button-previous nil)
135                           (setf *changep* t)
136                           (setf *is-mouse-hold* nil)
137                           )
138           (:windowevent ()
139                         ;; bug prevention method. SDL2 can look the :mousebuttonup event
140                         ;; only when the mouse is on the window. This makes press,
141                         ;; alt+tab and release a way to keep the value to t. This
142                         ;; event is a prevention method designed to release the hold
143                         ;; when alt+tab or other window shortcuts are used.
144                         (setf *is-mouse-hold* nil))
145           (:multigesture ()
146                          ;; same as for windowevent
147                          (setf *is-mouse-hold* nil))
148           ;; NOTE: When the user prefear to use only the keyboard or joystick
149           ;; the *cursor-position* is set default, this is set when a key or
150           ;; koystick button is pressed, then menues and actions are chosed with
151           ;; the cursor position. When a mouse motion is detected the
152           ;; mouse-position is set default. the cursor follows the grid on each
153           ;; step and it is an alternative to the mouse movement to do
154           ;; everything.
155           (:idle ()
156                  (setf *mouse-previous-position* *mouse-position*)
157                  (multiple-value-bind (x y) (sdl2:mouse-state)
158                    (setf *mouse-position* (cons x y)))
159                  ;; mouse-holding-event
160                  ;; (unless *active-entries*
161                  ;;   (when *is-mouse-hold*
162                  ;;     (loop :for action :in (mouse-actions *mouse-keybinds*)
163                  ;;           :do (when action (eval (car action))) ; FIXME: replace eval with a more safer DSL eval
164                  ;;           )
165                  ;;     ;; (setf mouse-button-previous (mouse-actions *mouse-keybinds*))
166                  ;;     ))
167                  (when *is-mouse-hold*
168                    (mouse-event *mouse-position* :dragp t))
169
170                  ;; trick to avoid functions to change the global draw-color
171                  (sdl2:set-render-draw-color *renderer* 0 0 0 255)
172
173                  ;; local viewpoint
174                  ;; NOTE: generalize
175                  (when *changep*
176                    (sdl2:destroy-texture viewpoint-texture)
177                    (setf viewpoint-texture (sdl2:create-texture *renderer*
178                                                                 (sdl2:get-window-pixel-format window)
179                                                                 2
180                                                                 (width *plane*)
181                                                                 (height *plane*)))
182                    (sdl2:set-render-target *renderer* viewpoint-texture)
183                    (sdl2:render-clear *renderer*)
184                    (render-plane-and-entities *renderer*)
185                    (when *is-grid*
186                      (grid-render *renderer* 0 0 (width *plane*) (height *plane*))
187                      )
188                    (when *is-indexes*
189                      (indexes-render *renderer* 0 0 (width *plane*) (height *plane*)))
190                    (sdl2:set-render-draw-color *renderer* 0 0 0 255)
191                    (setf *changep* nil)
192                    ;; pointer section
193                    (when *pointer* ;; NOTE: to test this out
194                      (sdl2:set-render-draw-color *renderer* 128 250 33 255)
195                      (multiple-value-bind (coordinates size) (grid-layout (car (coordinate (nth *pointer* *entities-list*)))
196                                                                           (cdr (coordinate (nth *pointer* *entities-list*)))
197                                                                           :size (size (nth *pointer* *entities-list*)))
198                        (let ((select-rectangle (sdl2:make-rect (car coordinates) (cdr coordinates) size size)))
199                          (sdl2:render-draw-rect *renderer* select-rectangle)
200                          (sdl2:free-rect select-rectangle)))
201                      (sdl2:set-render-draw-color *renderer* 0 0 0 255)
202                      ))
203
204
205
206                  ;; display viewpoint on window
207                  (sdl2:set-render-target *renderer* window-texture)
208                  (sdl2:render-clear *renderer*)
209                  (setf (sdl2:rect-x viewpoint-rectangle) (car *viewpoint-offset*))
210                  (setf (sdl2:rect-y viewpoint-rectangle) (cdr *viewpoint-offset*))
211                  (setf (sdl2:rect-width viewpoint-rectangle) (floor (* (width *plane*) *viewpoint-zoom*)))
212                  (setf (sdl2:rect-height viewpoint-rectangle) (floor (* (height *plane*) *viewpoint-zoom*)))
213                  (sdl2:render-copy *renderer* viewpoint-texture
214                                    :source-rect nil
215                                    :dest-rect viewpoint-rectangle)
216
217                  ;; entries visualization
218                  (loop :for entry :in rulp.entries:*entries-list*
219                        :do (rulp.entries:render-entry
220                             *renderer*
221                             (rulp.entries:make-plist entry)))
222                  ;; (loop :for entry :in *active-entries*
223                  ;;       :do (display-entry *renderer* entry))
224
225                  ;; debug infos
226                  (when debug-info
227                    (tr-write (format nil "~A" debug-info) 0 0 10 15 *renderer*))
228                  (sdl2:render-present *renderer*)
229                  ;; updating grids and dimension
230                  ;;  FIXME: i hate this, find a better way to dinamically change the window dimension
231                  (multiple-value-bind (new-width new-height) (sdl2:get-window-size window)
232                    (setf *window-width* new-width)
233                    (setf *window-height* new-height))
234                  (1- *framerule*)
235                  (when (< *framerule* 1)
236                    (setf *changep* t)
237                    (setf *framerule* 20))))
238         (setf *changep* t)              ; useful in the repl where parameters are not reset
239         )))
240   )