(defgeneric height (s)
(:documentation "returns the height of the screen, the optional plane provide a grid"))
+(defgeneric display (s)
+ (:documentation "display the screen by using the defined coordinates and size"))
+
(defgeneric texture (s))
(defgeneric surface (s))
(defgeneric screen-source (s))
;; set into a grid, which will be provided by the something itself.
(defclass screen ()
((coordinate :accessor coordinate
- :initarg :coordinate
- :initform '(0 . 0)
- :documentation "the position of the object in the space, space dependent"
- :type list)
+ :initarg :coordinate
+ :initform '(0 . 0)
+ :documentation "the position of the object in the space, space dependent"
+ :type list)
(size :accessor size
- :initarg :size
- :initform '(50 . 50)
- :documentation "the size of the object, by default 50x50, space dependent"
- :type list)
+ :initarg :size
+ :initform '(50 . 50)
+ :documentation "the size of the object, by default 50x50, space dependent"
+ :type list)
(texture :initform nil
- :accessor texture)
+ :accessor texture)
(rotation :accessor rotation)
(surface :initform nil
- :accessor surface)
- (path :accessor image-path
+ :accessor surface)
+ (path :reader image-path
:initarg :image
:initform (merge-pathnames rulp.parameters:*rulp-share* "test.png"))
(s-rect :initform nil
(defmethod (setf height) (value (s screen))
(setf (cdr (slot-value s 'size)) value))
-;; FIXME: entities use size as a simple integer, change to make it work better
(defmethod initialize-instance :after ((s screen) &rest args)
(declare (ignore args))
- (setf (surface s) (sdl2-image:load-image (slot-value s 'path)))
- (setf (width s) (sdl2:surface-width (surface s))
- (height s) (sdl2:surface-height (surface s)))
- (setf (slot-value s 's-rect) (sdl2:make-rect 0 0 (width s) (height s))))
+ (setf (slot-value s 'texture) (rulp.render:load-texture (slot-value s 'path)))
+ (setf (width s) (rulp.render:texture-width (texture s)))
+ (setf (height s) (rulp.render:texture-height (texture s)))
+ )
(defmethod screen-source ((s screen))
"return the source rectangle, the portion of texture to display (standard all)"
;; ))
;; )
+(defmethod render ((s screen) x y width height)
+ (render-texture nil `(,x ,y ,width ,height) (texture s))
+ )
+
;; FIXME: find a way to store rectangles to later use
(defmethod screen-destination ((s screen) (p t))
"without a plane of reference, a screen is just printed full size from the
(sdl2:destroy-texture (texture s))
(when (surface s)
(sdl2:free-surface (surface s))))
+
+;; Flyweight zone, screens should be accessed only from the function get screen
+(defparameter *screen-list* nil)
+
+(defun path-equal (value screen)
+ (equal value (image-path screen)))
+
+(defun get-screen (path)
+ "A flyweight for screens. Given a path this function return an existing
+texture or, if no other exists, create a new one and return it."
+ (if (member path *screen-list* :test #'path-equal)
+ (car (member path *screen-list* :test #'path-equal))
+ (car (push (make-instance 'screen :path image)
+ *screen-list*))))
+