Skip to content

Commit

Permalink
Split asset management mess into assets, resources and loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
borodust committed Apr 6, 2017
1 parent 826d619 commit 22545c7
Show file tree
Hide file tree
Showing 60 changed files with 519 additions and 565 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Experimental **bod**acious **g**ame **e**ngine written in **C**ommon **L**isp.
Abstraction layer over host OS-dependent functionality: windows, OS resource
management, system and input events, etc. See `cl-bodge/host`.

* ***Assets***
* ***Resources***

Asset management routines for asynchronous resource loading, preparation, retrieving and
releasing. See `cl-bodge/assets`.
releasing. See `cl-bodge/resources`.

* ***Graphics***

Expand Down Expand Up @@ -70,10 +70,10 @@ Experimental **bod**acious **g**ame **e**ngine written in **C**ommon **L**isp.
passes (rendering, simulation, etc); graphics-oriented, physics-oriented, transformation,
animation, generic model and other types of nodes. See `cl-bodge/scenegraph`.

* ***Resources***
* ***Assets***

Engine's universal Bodge Resource File and various external formats parsing and loading: images,
audio, fonts, meshes, skeletons, animations, etc. See `cl-bodge/resources`
audio, fonts, meshes, skeletons, animations, etc. See `cl-bodge/assets`

* ***Distribution***

Expand Down
33 changes: 19 additions & 14 deletions assets/audio.lisp
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
(in-package :cl-bodge.assets)


(deftype pcm-data ()
'(or (simple-array (unsigned-byte 8) (*))
(simple-array (signed-byte 16) (*))
(simple-array (signed-byte 32) (*))
(simple-array single-float (*))
(simple-array double-float (*))))
(defclass audio ()
((channel-format :initarg :channel-format :initform nil
:type channel-format :reader audio-channel-format-of)
(sample-depth :initarg :sample-depth :initform nil
:type sample-depth :reader audio-sample-depth-of)
(sampling-rate :initarg :sampling-rate :initform nil
:type positive-integer :reader audio-sampling-rate-of)))


(deftype sample-depth ()
'(member 8 16))
(defclass pcm-16-audio (audio) ()
(:default-initargs :sample-depth 16))


(defenum channel-format
:mono :stereo)
(defclass cached-pcm-16-audio (pcm-16-audio)
((samples :initarg :samples :reader pcm-audio-data-of)))


(defgeneric pcm-audio-data-of (resource))
(defgeneric audio-channel-format-of (resource))
(defgeneric audio-sample-depth-of (resource))
(defgeneric audio-sampling-rate-of (resource))
(defun load-ogg-vorbis-audio (path)
(with-open-sound-file (file path)
(make-instance 'cached-pcm-16-audio
:samples (read-short-samples-into-array file)
:sampling-rate (sound-sample-rate file)
:channel-format (ecase (sound-channels file)
(1 :mono)
(2 :stereo)))))
2 changes: 1 addition & 1 deletion resources/basic-chunks.lisp → assets/basic-chunks.lisp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(in-package :cl-bodge.resources)
(in-package :cl-bodge.assets)

;;;
;;;
Expand Down
4 changes: 2 additions & 2 deletions resources/converters.lisp → assets/converters.lisp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(in-package :cl-bodge.resources)
(in-package :cl-bodge.assets)


(defun chunk->animation (chunk)
Expand Down Expand Up @@ -107,7 +107,7 @@


(defmethod chunk-asset-flow ((this font-atlas-chunk) loader)
(>> (load-asset loader (font-atlas-chunk-image-name this))
(>> (get-resource (font-atlas-chunk-image-name this))
(-> ((ge.gx:graphics) :important-p t) (image)
(chunk->font this image))))

Expand Down
17 changes: 0 additions & 17 deletions assets/distribution.lisp

This file was deleted.

6 changes: 6 additions & 0 deletions assets/encoded.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(in-package :cl-bodge.assets)


(defmethod read-chunk ((type (eql :encoded)) parameters stream)
(destructuring-bind (&key name asset-class &allow-other-keys) parameters
(decode-asset asset-class stream)))
2 changes: 1 addition & 1 deletion resources/font.lisp → assets/font.lisp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(in-package :cl-bodge.resources)
(in-package :cl-bodge.assets)


(define-chunk-structure glyph-metrics
Expand Down
6 changes: 3 additions & 3 deletions resources/image.lisp → assets/image.lisp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(in-package :cl-bodge.resources)
(in-package :cl-bodge.assets)


(defclass image ()
Expand Down Expand Up @@ -54,14 +54,14 @@
(data nil :read-only t))


(defmethod read-chunk-data ((type (eql :image)) parameters stream)
(defmethod read-chunk ((type (eql :image)) parameters stream)
(destructuring-bind (&key size &allow-other-keys) parameters
(let* ((image-data (make-array size :element-type '(unsigned-byte 8)))
(bytes-read (read-sequence image-data stream)))
(unless (= size bytes-read)
(error "Incorrect :size provided for image chunk data: ~a supplied, but ~a read"
size bytes-read))
image-data)))
(parse-chunk type parameters image-data))))


(defmethod parse-chunk ((type (eql :image)) parameters data)
Expand Down
41 changes: 9 additions & 32 deletions assets/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,15 @@

(defpackage :cl-bodge.assets
(:nicknames :ge.as)
(:use :cl :cl-bodge.utils :cl-bodge.engine)
(:export asset-system
asset-registry-of
assets
copy-assets
asset-flow
(:use :cl :cl-bodge.utils :cl-bodge.engine :bodge-sndfile :cl-bodge.resources)
(:export make-resource-loader

engine-asset-id
mesh-asset-mesh
mesh-asset-transform
mesh-asset-bones

register-asset-loader
release-assets
load-ogg-vorbis-audio
load-png-image

asset-names
load-asset
release-asset
path-to

assets-root

pixel-format
pixel-format-p

pixel-format-of
foreign-array-of
width-of
height-of

pcm-data
sample-depth
channel-format

pcm-audio-data-of
audio-channel-format-of
audio-sample-depth-of
audio-sampling-rate-of))
font-atlas-asset-id
font-asset-id))
38 changes: 0 additions & 38 deletions assets/registry.lisp

This file was deleted.

24 changes: 12 additions & 12 deletions resources/resource-loader.lisp → assets/resource-loader.lisp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(in-package :cl-bodge.resources)
(in-package :cl-bodge.assets)


(declaim (special *objects*
Expand All @@ -11,10 +11,11 @@
(error "Unknown chunk type: ~a" chunk-type)))


(defgeneric read-chunk-data (chunk-type parameters stream)
(defgeneric read-chunk (chunk-type parameters stream)
(:method (chunk-type parameters stream)
(read-preserving-whitespace
(flexi-streams:make-flexi-stream stream :external-format :utf-8))))
(parse-chunk chunk-type parameters
(read-preserving-whitespace
(flexi-streams:make-flexi-stream stream :external-format :utf-8)))))


(defun push-object (id obj)
Expand Down Expand Up @@ -129,13 +130,13 @@
parameters
(let* ((start-position (file-position in))
(inflated (chipz:make-decompressing-stream 'chipz:deflate in)))
(prog1 (read-chunk-data chunk-type parameters inflated)
(prog1 (read-chunk chunk-type parameters inflated)
;; fixme: dirty hack? mb better to allocate buffer from source stream
(file-position in (+ start-position compressed-size)))))))
(read-chunk-data chunk-type parameters in)))
(read-chunk chunk-type parameters in)))


(defun load-resource (path)
(defun load-asset (path)
(flet ((resolve-references (resolvers)
(dolist (fn resolvers)
(funcall fn))))
Expand All @@ -161,8 +162,7 @@
(error "Nameless chunk header of type ~A found" chunk-type))
((not (null chunk))
(error "Duplicate chunk with name ~A found" name)))
(let ((chunk-data (read-deflated in chunk-type parameters compression)))
(setf chunk (parse-chunk chunk-type parameters chunk-data))))))
(setf chunk (read-deflated in chunk-type parameters compression)))))
(resolve-references *resolvers*)
(make-instance 'resource :chunks chunk-table)))))))

Expand All @@ -183,7 +183,7 @@
(defmethod initialize-instance :after ((this resource-loader) &key resource-paths)
(with-slots (chunk-table) this
(loop for path in resource-paths
for chunks = (list-chunks (load-resource path))
for chunks = (list-chunks (load-asset path))
do (loop for (name . chunk) in chunks
do (with-hash-entries ((cached name)) chunk-table
(when cached
Expand All @@ -195,7 +195,7 @@
(make-instance 'resource-loader :resource-paths resource-paths))


(defmethod asset-names ((this resource-loader))
(defmethod list-resource-names ((this resource-loader))
(with-slots (chunk-table) this
(loop for name being the hash-key of chunk-table
collect name)))
Expand All @@ -206,7 +206,7 @@
(:method (chunk asset loader)))


(defmethod load-asset ((this resource-loader) name)
(defmethod load-resource ((this resource-loader) name)
(with-slots (chunk-table) this
(if-let ((cached (gethash name chunk-table)))
(with-instance-lock-held (this)
Expand Down
22 changes: 0 additions & 22 deletions assets/system.lisp

This file was deleted.

3 changes: 1 addition & 2 deletions audio/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@


(defpackage :cl-bodge.audio
(:use :cl-bodge.engine :cl-bodge.utils :cl-bodge.assets
:cl :cl-muth)
(:use :cl :cl-bodge.engine :cl-bodge.utils :cl-muth)
(:nicknames :ge.snd)
(:export audio-system
audio
Expand Down
2 changes: 1 addition & 1 deletion canvas/canvas.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
(define-system-function make-canvas graphics-system (width height &key antialiased)
(let ((opts (append (list :stencil-strokes)
(when antialiased (list :antialias))
(when-debugging (list :debug)))))
(in-development-mode (list :debug)))))
(make-instance 'canvas
:handle (make-canvas-handle (apply #'bodge-nanovg:make-context opts))
:width width
Expand Down
3 changes: 1 addition & 2 deletions canvas/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

(defpackage :cl-bodge.canvas
(:nicknames :ge.vg)
(:use :cl :cl-bodge.engine :cl-bodge.utils :cl-bodge.graphics :cl-bodge.assets
:autowrap :plus-c)
(:use :cl :cl-bodge.engine :cl-bodge.utils :cl-bodge.graphics :autowrap :plus-c)
(:export make-canvas
update-canvas-size
begin-canvas
Expand Down
Loading

0 comments on commit 22545c7

Please sign in to comment.