X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=layers%2Fscreens.lisp;fp=layers%2Fscreens.lisp;h=1cccb837125343be2760ff07d536ffb692c0dec9;hb=b78f4e89d75549517bda2c08084348e73ca5c8cb;hp=a251f93dd6d4631c3a8be3955c5b1f251093cfa5;hpb=79b68cf42ad28b5663fff9dd563bc05d46cac082;p=rulp%2Frulp.git diff --git a/layers/screens.lisp b/layers/screens.lisp index a251f93..1cccb83 100644 --- a/layers/screens.lisp +++ b/layers/screens.lisp @@ -28,6 +28,9 @@ (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)) @@ -42,21 +45,21 @@ where the screen should be displayed")) ;; 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 @@ -99,13 +102,12 @@ interact directly with this but use screen-destination instead") (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)" @@ -129,6 +131,10 @@ interact directly with this but use screen-destination instead") ;; )) ;; ) +(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 @@ -141,3 +147,18 @@ top left of the window" (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*)))) +