1 ;;;; Ru*** roLeplay Playground virtual tabletop
2 ;;;; Copyright (C) 2022 Zull
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.
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.
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/>.
17 (in-package :graphics)
18 #| -------------------------------------------
19 | This file manipulate the window and the
21 | -------------------------------------------
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))
34 ;(defparameter +grid-span+ 50)
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
45 (font-texture (sdl2:create-texture-from-surface *renderer*
47 (sdl2:free-surface font-surface)
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)
57 (:mousebuttondown (:x x :y y :state state)
58 (activate x y state +plane+))
60 ;; clean window texture
61 (sdl2:render-clear *renderer*)
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+)
68 (when (displayp entities)
69 (sdl2:render-copy *renderer* (texture entities)
70 :source-rect (screen-source entities)
71 :dest-rect (screen-destination entities +plane+))))
74 (grid-render *renderer* +plane+))
77 (indexes-render *renderer* +plane+))
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)
86 (sdl2:render-present *renderer*)