OSDN Git Service

f59c2a9d838e28f7617e88c10aa21a8975176b48
[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 :graphics)
18 #| -------------------------------------------
19  | This file manipulate the window and the
20  | sdl initialization.
21  | -------------------------------------------
22  |#
23
24 (defmacro with-playground ((window renderer &key (title "RuLP") (width 740) (height 480)) &body body)
25   `(sdl2:with-window (,window :title title :w width :h height :flags '(:resizable))
26      (sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated :presentvsync)) ;later add delta
27        (sdl2-image:init '(:png :jpg))
28        (sdl2-ttf:init)
29        ,@body
30        (sdl2-ttf:quit)
31        (sdl2-image:quit)
32        )))
33
34 ;(defparameter +grid-span+ 50)
35
36 ;; renderer exists only inside this function, so you cannot create a texture outside
37 ;; (at least for now), more on this later os
38 (defun playground (width height title &optional (fps 60) (debug-info nil))
39   (sdl2:with-init (:video)
40     (with-playground (window *renderer* :title title :width width :height height)
41       (setf *tr-texture* (let* ((font (sdl2-ttf:open-font "media/IBMPlex.ttf" 100)) ;; this line throw fault, works anyway
42                                 (font-surface (sdl2-ttf:render-text-solid font
43                                                                           *tr-string*
44                                                                           0 0 0 0))
45                                 (font-texture (sdl2:create-texture-from-surface *renderer*
46                                                                                 font-surface)))
47                            (sdl2:free-surface font-surface)
48                            font-texture))
49       (setf +plane+ (make-instance 'plane :image (truename "media/mappa.png")))
50       (setf (entities-list +plane+) (list (make-instance 'entity)))
51       (push (make-instance 'entity :image (truename "media/hellsteir_pg70.png")) (entities-list +plane+))
52       (push (make-instance 'entity :image (truename "media/nell_pg70.png")) (entities-list +plane+))
53       (push (make-instance 'entity :image (truename "media/rednex_pg70.png")) (entities-list +plane+))
54       (push (make-instance 'entity :image (truename "media/aduial_pg70.png")) (entities-list +plane+))
55       (sdl2:with-event-loop (:method :poll)
56         (:quit () t)
57         (:mousebuttondown (:x x :y y :state state)
58                           (activate x y state +plane+))
59         (:idle ()
60                ;; clean window texture
61                (sdl2:render-clear *renderer*)
62                (when +plane+
63                  (sdl2:render-copy *renderer* (texture +plane+)
64                                    :source-rect (screen-source +plane+)
65                                    :dest-rect (screen-destination +plane+ t))
66                  (loop :for entities :in (entities-list +plane+)
67                        :do
68                           (when (displayp entities)
69                             (sdl2:render-copy *renderer* (texture entities)
70                                               :source-rect (screen-source entities)
71                                               :dest-rect (screen-destination entities +plane+))))
72                  ;; Grid creation
73                  (when *is-grid*
74                    (grid-render *renderer* +plane+))
75                  ;; Indexes creation
76                  (when *is-indexes*
77                    (indexes-render *renderer* +plane+))
78                  )
79                ;; pointer section
80                (when *pointer*
81                  (sdl2:set-render-draw-color *renderer* 128 250 33 255)
82                  (let ((select-rectangle (screen-destination (nth *pointer* (entities-list +plane+)) +plane+)))
83                    (sdl2:render-draw-rect *renderer* select-rectangle))
84                  (sdl2:set-render-draw-color *renderer* 0 0 0 255)
85                  )
86                (sdl2:render-present *renderer*)
87                )))))