Revision | 9f21da385b7cdb222f226b9d5d22ef0c75154b24 (tree) |
---|---|
Time | 2023-01-17 21:57:21 |
Author | Giulio De Stasio <giuliodestasio98@gmai...> |
Commiter | Giulio De Stasio |
graphics: sdl2:rects freed after use
@@ -68,6 +68,7 @@ to render locally use sdl2:set-render-target before calling this macro." | ||
68 | 68 | :do (when (displayp entity) |
69 | 69 | (sdl2:render-copy ,renderer (texture entity) |
70 | 70 | :source-rect (screen-source entity) |
71 | - :dest-rect (screen-destination entity ,plane)))))) | |
71 | + :dest-rect (screen-destination entity ,plane)) | |
72 | + )))) | |
72 | 73 | |
73 | 74 | ;; NOTE: test if it is necessary to create render-plane-without-contents |
@@ -32,6 +32,10 @@ | ||
32 | 32 | `(let ((value-indexes (tr-parse-string ,value))) |
33 | 33 | (loop :for character :in value-indexes |
34 | 34 | :for character-position :from 0 :to (length value-indexes) |
35 | - :do (sdl2:render-copy ,renderer *tr-texture* | |
36 | - :source-rect (sdl2:make-rect (* character 60) 0 60 130) | |
37 | - :dest-rect (sdl2:make-rect (+ ,x (* character-position ,width)) ,y ,width ,height))))) | |
35 | + :do (let ((source-rectangle (sdl2:make-rect (* character 60) 0 60 130)) | |
36 | + (destination-rectangle (sdl2:make-rect (+ ,x (* character-position ,width)) ,y ,width ,height))) | |
37 | + (sdl2:render-copy ,renderer *tr-texture* | |
38 | + :source-rect source-rectangle | |
39 | + :dest-rect destination-rectangle) | |
40 | + (sdl2:free-rect source-rectangle) | |
41 | + (sdl2:free-rect destination-rectangle))))) |
@@ -46,7 +46,7 @@ DEBUG-INFO can be used to display the content on screen for test and debug purpo | ||
46 | 46 | (sdl2:with-init (:video) |
47 | 47 | (with-playground (window *renderer* :title title) |
48 | 48 | (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 |
49 | - (font-surface (sdl2-ttf:render-text-solid font *tr-string* 0 0 0 0)) | |
49 | + (font-surface (sdl2-ttf:render-utf8-solid font *tr-string* 0 0 0 0)) | |
50 | 50 | (font-texture (sdl2:create-texture-from-surface *renderer* font-surface))) |
51 | 51 | (sdl2:free-surface font-surface) |
52 | 52 | font-texture)) |
@@ -58,8 +58,7 @@ DEBUG-INFO can be used to display the content on screen for test and debug purpo | ||
58 | 58 | 2 (width +plane+) (height +plane+)))) |
59 | 59 | (sdl2:with-event-loop (:method :poll) |
60 | 60 | (:quit () t) |
61 | - (:keydown (:keysym keysym) | |
62 | - (declare (ignore keysym)) | |
61 | + (:keydown () | |
63 | 62 | ;; FIXME: incorporate into input.lisp. check for input every now and then outside keydown |
64 | 63 | (when (sdl2:keyboard-state-p :scancode-up) (setf (cdr *viewpoint-offset*) (+ (cdr *viewpoint-offset*) 10))) |
65 | 64 | (when (sdl2:keyboard-state-p :scancode-down) (setf (cdr *viewpoint-offset*) (+ (cdr *viewpoint-offset*) -10))) |
@@ -68,7 +67,7 @@ DEBUG-INFO can be used to display the content on screen for test and debug purpo | ||
68 | 67 | (when (sdl2:keyboard-state-p :scancode-p) (setf *viewpoint-zoom* (+ *viewpoint-zoom* 0.2))) ; FIXME: find the scancode for the plus sign |
69 | 68 | (when (sdl2:keyboard-state-p :scancode-m) (setf *viewpoint-zoom* (+ *viewpoint-zoom* -0.2))) |
70 | 69 | ) |
71 | - (:mousebuttondown (:x x :y y :state state) | |
70 | + (:mousebuttondown () | |
72 | 71 | (setf *is-mouse-hold* t) |
73 | 72 | ) |
74 | 73 | (:mousebuttonup () |
@@ -99,7 +98,6 @@ DEBUG-INFO can be used to display the content on screen for test and debug purpo | ||
99 | 98 | (multiple-value-bind (x y) (sdl2:mouse-state) |
100 | 99 | (setf *mouse-position* (cons x y))) |
101 | 100 | ;; mouse-holding-event |
102 | - ;; FIXME: absolutely horrible. to be moved inside input.lisp for better functionalities | |
103 | 101 | (when *is-mouse-hold* |
104 | 102 | (loop :for action :in (mouse-actions *mouse-keybinds*) |
105 | 103 | :do (when action (eval (car action))) ; FIXME: replace eval with a more safer DSL eval |
@@ -113,37 +111,36 @@ DEBUG-INFO can be used to display the content on screen for test and debug purpo | ||
113 | 111 | (sdl2:render-clear *renderer*) |
114 | 112 | |
115 | 113 | ;; local viewpoint |
114 | + ;; NOTE: generalize | |
116 | 115 | (sdl2:set-render-target *renderer* viewpoint-texture) |
117 | 116 | (sdl2:render-clear *renderer*) |
118 | 117 | (render-plane-and-contents *renderer* +plane+) |
119 | - ;; NOTE: grid-render supports multiple colors for grids | |
120 | - (grid-render *renderer* +plane+ (car *viewpoint-offset*) (cdr *viewpoint-offset*) | |
121 | - (floor (* (width +plane+) *viewpoint-zoom*)) | |
122 | - (floor (* (height +plane+) *viewpoint-zoom*))) | |
123 | - (indexes-render *renderer* +plane+ (car *viewpoint-offset*) (cdr *viewpoint-offset*) | |
124 | - (floor (* (width +plane+) *viewpoint-zoom*)) | |
125 | - (floor (* (height +plane+) *viewpoint-zoom*))) | |
118 | + (when *is-grid* | |
119 | + (grid-render *renderer* +plane+ (car *viewpoint-offset*) (cdr *viewpoint-offset*) | |
120 | + (floor (* (width +plane+) *viewpoint-zoom*)) | |
121 | + (floor (* (height +plane+) *viewpoint-zoom*)))) | |
122 | + (when *is-indexes* | |
123 | + (indexes-render *renderer* +plane+ (car *viewpoint-offset*) (cdr *viewpoint-offset*) | |
124 | + (floor (* (width +plane+) *viewpoint-zoom*)) | |
125 | + (floor (* (height +plane+) *viewpoint-zoom*)))) | |
126 | + (sdl2:set-render-draw-color *renderer* 0 255 0 155) | |
127 | + (sdl2:set-render-draw-color *renderer* 0 0 0 255) | |
126 | 128 | (sdl2:set-render-target *renderer* window-texture) |
127 | - (sdl2:render-copy *renderer* viewpoint-texture | |
128 | - :source-rect nil | |
129 | - :dest-rect (sdl2:make-rect (car *viewpoint-offset*) | |
130 | - (cdr *viewpoint-offset*) | |
131 | - (floor (* (width +plane+) *viewpoint-zoom*)) ; NOTE: hardcoded to +plane+, change to dynamic | |
132 | - (floor (* (height +plane+) *viewpoint-zoom*))) | |
133 | - ) | |
134 | - ;; Grid creation | |
135 | - ;; NOTE: grid creation should be done before, so to render on global, but it should be limited with actual viewpoint restrictions | |
136 | - ;; to reduce cycles | |
137 | - ;; (when *is-grid* | |
138 | - ;; (grid-render *renderer* +plane+)) | |
139 | - ;; Indexes creation | |
140 | - ;; (when *is-indexes* | |
141 | - ;; (indexes-render *renderer* +plane+)) | |
129 | + (let ((viewpoint-rectangle (sdl2:make-rect (car *viewpoint-offset*) | |
130 | + (cdr *viewpoint-offset*) | |
131 | + (floor (* (width +plane+) *viewpoint-zoom*)) ; NOTE: hardcoded +plane+ usage | |
132 | + (floor (* (height +plane+) *viewpoint-zoom*))))) | |
133 | + (sdl2:render-copy *renderer* viewpoint-texture | |
134 | + :source-rect nil | |
135 | + :dest-rect viewpoint-rectangle) | |
136 | + (sdl2:free-rect viewpoint-rectangle)) | |
137 | + | |
142 | 138 | ;; pointer section |
143 | 139 | (when *pointer* |
144 | 140 | (sdl2:set-render-draw-color *renderer* 128 250 33 255) |
145 | 141 | (let ((select-rectangle (screen-destination (nth *pointer* (entities-list +plane+)) +plane+))) |
146 | - (sdl2:render-draw-rect *renderer* select-rectangle)) | |
142 | + (sdl2:render-draw-rect *renderer* select-rectangle) | |
143 | + (sdl2:free-rect select-rectangle)) | |
147 | 144 | ) |
148 | 145 | ;; entries generation |
149 | 146 | ;; (loop :for entry :in (reverse *entries-list*) |
@@ -59,6 +59,12 @@ where the screen should be displayed")) | ||
59 | 59 | (path :accessor image-path |
60 | 60 | :initarg :image |
61 | 61 | :initform (merge-pathnames parameters:*rulp-share* "test.png")) |
62 | + (s-rect :initform nil | |
63 | + :documentation "this value contains the source rectangle to be used, don't | |
64 | +interact directly with this but use screen-source instead") | |
65 | + (d-rect :initform nil | |
66 | + :documentation "this value contains the destination rectangle to be used, don't | |
67 | +interact directly with this but use screen-destination instead") | |
62 | 68 | )) |
63 | 69 | |
64 | 70 | ;; Real space is the pixel grid of the screen, a position of (6 . 7) means |
@@ -100,9 +106,28 @@ where the screen should be displayed")) | ||
100 | 106 | "return the source rectangle, the portion of texture to display (standard all)" |
101 | 107 | nil) |
102 | 108 | |
109 | +;; FIXME: save and use the make-rect. destroy and create a new one when | |
110 | +;; info don't match | |
111 | +;; (defmethod screen-destination ((s screen) (p t)) | |
112 | +;; "Without a plane of reference screens are printed full size offset of x and y | |
113 | +;; pixels from the upper left angle of the window" | |
114 | +;; (let ((rect (slot-value s 'd-rect))) | |
115 | +;; (if (and | |
116 | +;; rect | |
117 | +;; (equal (x s) (sdl2:rect-x rect)) | |
118 | +;; (equal (y s) (sdl2:rect-y rect)) | |
119 | +;; (equal (width s) (sdl2:rect-width rect)) | |
120 | +;; (equal (height s) (sdl2:rect-height rect))) | |
121 | +;; (rect) | |
122 | +;; (progn (sdl2:free-rect (slot-value s 'd-rect)) | |
123 | +;; (sdl2:make-rect (x s) (y s) (width s) (height s))) | |
124 | +;; )) | |
125 | +;; ) | |
126 | + | |
127 | +;; FIXME: find a way to store rectangles to later use | |
103 | 128 | (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" | |
129 | + "without a plane of reference, a screen is just printed full size from the | |
130 | +top left of the window" | |
106 | 131 | (sdl2:make-rect (x s) (y s) (width s) (height s)) |
107 | 132 | ) |
108 | 133 |