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 (sdl2:with-event-loop (:method :poll)
51 (:keyup (:keysym keysym)
52 (when (sdl2:scancode= (sdl2:scancode-value keysym) :scancode-q)
53 (format t "pressed q~%")))
54 (:mousebuttondown (:x x :y y :state state)
55 (activate x y state +plane+))
57 ;; clean window texture
58 (sdl2:render-clear *renderer*)
60 (sdl2:render-copy *renderer* (texture +plane+)
61 :source-rect (screen-source +plane+)
62 :dest-rect (screen-destination +plane+ t))
63 (loop :for entities :in (entities-list +plane+)
65 (when (displayp entities)
66 (sdl2:render-copy *renderer* (texture entities)
67 :source-rect (screen-source entities)
68 :dest-rect (screen-destination entities +plane+))))
71 (grid-render *renderer* +plane+))
74 (indexes-render *renderer* +plane+))
78 (sdl2:set-render-draw-color *renderer* 128 250 33 255)
79 (let ((select-rectangle (screen-destination (nth *pointer* (entities-list +plane+)) +plane+)))
80 (sdl2:render-draw-rect *renderer* select-rectangle))
81 (sdl2:set-render-draw-color *renderer* 0 0 0 255)
83 (sdl2:render-present *renderer*)