OSDN Git Service

core.lisp: created cli interface and json maps
[rulp/rulp.git] / layers / screens.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 :layers)
18 ;; This is the most generic class in the program with model, this class define
19 ;; objects to be displayed on screen, whenever they are entities, planes text
20 ;; or icons.
21
22 (defgeneric x (s)
23   (:documentation "returns the x position of the screen, the optional plane provide a grid"))
24 (defgeneric y (s)
25   (:documentation "returns the y position of the screen, the optional plane provide a grid"))
26 (defgeneric width (s)
27   (:documentation "returns the width of the screen, the optional plane provide a grid"))
28 (defgeneric height (s)
29   (:documentation "returns the height of the screen, the optional plane provide a grid"))
30
31 (defgeneric texture (s))
32 (defgeneric surface (s))
33 (defgeneric screen-source (s))
34 (defgeneric screen-destination (s p)
35   (:documentation "returns a sdl2 rectangle where the surface in real space, it indicates
36 where the screen should be displayed"))
37
38 ;; space dependent means that the thing can change position based on what space is
39 ;; on. if it is indipendent (under no space) it will be default to real space (therefore
40 ;; pixel per pixel coordinates and dimensions)
41 ;; when instead the object is under something it will use it's space, thefore it will be
42 ;; set into a grid, which will be provided by the something itself.
43 (defclass screen ()
44   ((coordinate :accessor coordinate
45                :initarg :coordinate
46                :initform '(0 . 0)
47                :documentation "the position of the object in the space, space dependent"
48                :type list)
49    (size :accessor size
50          :initarg :size
51          :initform '(50 . 50)
52          :documentation "the size of the object, by default 50x50, space dependent"
53          :type list)
54    (texture :initform nil
55             :accessor texture)
56    (rotation :accessor rotation)
57    (surface :initform nil
58             :accessor surface)
59    (path :accessor image-path
60          :initarg :image
61          :initform (merge-pathnames parameters:*rulp-share* "test.png"))
62    ))
63
64 ;; Real space is the pixel grid of the screen, a position of (6 . 7) means
65 ;; the 6th pixel horizontally and 7th pixel vertically.
66 ;;
67 ;; while this is useful for planes and other screens, this is hurtful for
68 ;; entities which move in a grid. The optional p can be used for a plane, which
69 ;; can shift these 4 functions to be inside the grid.
70 ;;
71 ;; this is what's called entity or plane space. Here when the grid is
72 ;; set to 100 the position (6 . 7) means the 6th square horizontally and
73 ;; 7th square vertically, or 600 pixels horiz. and 700 pixels vert.
74 (defmethod x ((s screen))
75   "returns the x position in real space (or in a grid of 1 pixel span)"
76   (car (slot-value s 'coordinate)))
77 (defmethod y ((s screen))
78   "returns the y position in real space (or in a grid of 1 pixel span)"
79   (cdr (slot-value s 'coordinate)))
80 (defmethod width ((s screen))
81   "returns the width in real space (or in a grid of 1 pixel span)"
82   (car (slot-value s 'size)))
83 (defmethod height ((s screen))
84   "returns the height in real space (or in a grid of 1 pixel span)"
85   (cdr (slot-value s 'size)))
86
87 (defmethod (setf x) (value (s screen))
88   (setf (car (slot-value s 'coordinate)) value))
89 (defmethod (setf y) (value (s screen))
90   (setf (cdr (slot-value s 'coordinate)) value))
91 (defmethod (setf width) (value (s screen))
92   (setf (car (slot-value s 'size)) value))
93 (defmethod (setf height) (value (s screen))
94   (setf (cdr (slot-value s 'size)) value))
95
96 (defmethod initialize-instance :after ((s screen) &rest args)
97   (setf (surface s) (sdl2-image:load-image (slot-value s 'path))))
98
99 (defmethod screen-source ((s screen))
100   "return the source rectangle, the portion of texture to display (standard all)"
101   nil)
102
103 (defmethod screen-destination ((s screen) (p t))
104   "Without a plane of reference screens are printed full size offset of x and y
105 pixels from the upper left angle of the window"
106   (sdl2:make-rect (x s) (y s) (width s) (height s))
107   )
108
109 (defmethod screen-purge ((s screen))
110   "purge screens video data. This data is automatically generated during rendering"
111   (sdl2:destroy-texture (texture s))
112   (sdl2:free-surface (surface s)))