OSDN Git Service

render/: created sdl2 interface
authorGiulio De Stasio <giuliodestasio98@gmail.com>
Sun, 11 Jun 2023 07:41:07 +0000 (09:41 +0200)
committerGiulio De Stasio <giuliodestasio98@gmail.com>
Sun, 11 Jun 2023 07:41:07 +0000 (09:41 +0200)
render/package.lisp [new file with mode: 0644]
render/render.lisp [new file with mode: 0644]
render/text-rendering.lisp [new file with mode: 0644]

diff --git a/render/package.lisp b/render/package.lisp
new file mode 100644 (file)
index 0000000..7fea389
--- /dev/null
@@ -0,0 +1,20 @@
+;;;; Ru*** roLeplay Playground virtual tabletop
+;;;; Copyright (C) 2022  Zull
+;;;;
+;;;; This program is free software: you can redistribute it and/or modify
+;;;; it under the terms of the GNU General Public License as published by
+;;;; the Free Software Foundation, either version 3 of the License, or
+;;;; (at your option) any later version.
+;;;;
+;;;; This program is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;;; GNU General Public License for more details.
+;;;;
+;;;; You should have received a copy of the GNU General Public License
+;;;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+(defpackage :rulp.render
+  (:use :cl)
+  (:export *renderer* render-line render-text color))
+
diff --git a/render/render.lisp b/render/render.lisp
new file mode 100644 (file)
index 0000000..c7e0075
--- /dev/null
@@ -0,0 +1,87 @@
+;;;; Ru*** roLeplay Playground virtual tabletop
+;;;; Copyright (C) 2022  Zull
+;;;;
+;;;; This program is free software: you can redistribute it and/or modify
+;;;; it under the terms of the GNU General Public License as published by
+;;;; the Free Software Foundation, either version 3 of the License, or
+;;;; (at your option) any later version.
+;;;;
+;;;; This program is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;;; GNU General Public License for more details.
+;;;;
+;;;; You should have received a copy of the GNU General Public License
+;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+(in-package :rulp.render)
+
+(defparameter *renderer* nil
+  "a variable containing the rendering informations. It is used for all
+SDL operations that require graphic acceleration. The variable shall not be
+used by itself, the functions in this package will use it for you.")
+
+;; Color functions
+(defun color ()
+  (sdl2:get-render-draw-color *renderer*))
+
+(defun (setf color) (red green blue alpha)
+  (sdl2:set-render-draw-color *renderer* red green blue alpha)
+  )
+
+;; line function
+(defun render-line (x y width height)
+  "render a line with hardware acceleration on screen"
+  (sdl2:render-draw-line *renderer* x y width height))
+
+;; rectangle functions
+;; NOTE: thread unfriendly
+(defparameter *cache-rectangle* (sdl2:make-rect 0 0 10 10)
+  "This is a helper rectangle, used to define and draw squares whenever
+  necessary without creating and destroying rectangles. Use this with
+the rectangle function.")
+
+(defun move-rectangle (x y width height rectangle)
+  "setup a rectangle and return it"
+  (setf (sdl2:rect-x rectangle) x)
+  (setf (sdl2:rect-y rectangle) y)
+  (setf (sdl2:rect-width rectangle) width)
+  (setf (sdl2:rect-height rectangle) height)
+  rectangle)
+
+(defun rectangle (x y width height)
+  (move-rectangle x y width height *cache-rectangle*))
+
+(defun render-square (x y width height &key (fill nil))
+  "render a square with hardware acceleration on screen"
+  (if fill
+      (sdl2:render-fill-rect *renderer* (move-rectangle x y width height *cache-rectangle*))
+      (sdl2:render-draw-rect *renderer* (move-rectangle x y width height *cache-rectangle*))))
+
+;; texture functions
+;; NOTE: Thread unfrendly
+(defparameter *texture-cache-rectangles*
+  (cons (sdl2:make-rect 0 0 10 10)
+        (sdl2:make-rect 0 0 10 10))
+  "These rectangles are cache used by render-texture to renderize without
+  creating and destroying them every time")
+
+(defun render-texture (source-x source-y source-width source-height
+                       dest-x dest-y dest-width dest-height
+                       texture)
+  (move-rectangle source-x source-y source-width source-height (car *texture-cache-rectangles*))
+  (move-rectangle dest-x dest-y dest-width dest-height (cdr *texture-cache-rectangles*))
+  (sdl2:render-copy *renderer* texture
+                    (car *texture-cache-rectangles*)
+                    (cdr *texture-cache-rectangles*)))
+
+;; collision detection
+(defun intersect-square (source-x source-y source-width source-height
+                         dest-x dest-y dest-width dest-height)
+  (move-rectangle source-x source-y source-width source-height (car *texture-cache-rectangles*))
+  (move-rectangle dest-x dest-y dest-width dest-height (cdr *texture-cache-rectangles*))
+  (sdl2:intersect-rect (car *texture-cache-rectangles*) (cdr *texture-cache-rectangles*)))
+
+(defun intersect-point (source-x source-y source-width source-height
+                        point-x point-y)
+  (intersect-square source-x source-y source-width source-height point-x point-y 10 10))
diff --git a/render/text-rendering.lisp b/render/text-rendering.lisp
new file mode 100644 (file)
index 0000000..6750acc
--- /dev/null
@@ -0,0 +1,70 @@
+;;;; Ru*** roLeplay Playground virtual tabletop
+;;;; Copyright (C) 2022  Zull
+;;;;
+;;;; This program is free software: you can redistribute it and/or modify
+;;;; it under the terms of the GNU General Public License as published by
+;;;; the Free Software Foundation, either version 3 of the License, or
+;;;; (at your option) any later version.
+;;;;
+;;;; This program is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;;; GNU General Public License for more details.
+;;;;
+;;;; You should have received a copy of the GNU General Public License
+;;;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+(in-package :rulp.render)
+
+(defparameter *source-rectangle* (sdl2:make-rect 0 0 10 10))
+(defparameter *destination-rectangle* (sdl2:make-rect 0 0 10 10))
+
+;; This texture needs to be deplayed on start
+(defparameter *texture* nil)
+
+;; FIXME: hardcoded, needs to be part of a configuration setting
+(defparameter *texture-path* "/home/giulio/Lavori/rulp/media/text.tga")
+(defparameter *texture-spacing* '(68 69 69 77 63 58 79 79 32 36
+                                  69 58 89 77 60 77 69 62 63 73
+                                  68 99 69 63 69
+                                  64 64 64 64 64 64 64 64 64 64
+                                  36))
+(defparameter *texture-height* 116.0)
+(defparameter *texture-text* "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
+
+;; FIXME: should be injected in a different manner
+(defun initialize ()
+  (setf *texture*
+        (sdl2:create-texture-from-surface *renderer*
+                                          (sdl2-image:load-image *texture-path*))))
+
+(defun get-position-from-character (character)
+  (loop :for i :from 0 :to (1- (position character *texture-text*))
+        :sum (nth i *texture-spacing*)))
+
+(defun get-size-from-character (character)
+  (nth (position character *texture-text*) *texture-spacing*))
+
+(defun render-text (x y size text)
+  (let ((pointer x))
+    (loop :for character :across text
+          :do
+             (move-rectangle (get-position-from-character character)
+                             0
+                             (get-size-from-character character)
+                             (floor *texture-height*)
+                             *source-rectangle*)
+             (move-rectangle pointer
+                             y
+                             (floor
+                              (* size (/ (get-size-from-character character) *texture-height*)))
+                             size
+                             *destination-rectangle*)
+             (sdl2:render-copy *renderer* *texture*
+                               :source-rect *source-rectangle*
+                               :dest-rect *destination-rectangle*)
+             (setf pointer (+ pointer
+                              (floor
+                               (* size (/ (get-size-from-character character) *texture-height*)))))
+          )
+  ))