diff --git a/basis/ui/gadgets/worlds/worlds.factor b/basis/ui/gadgets/worlds/worlds.factor index 8a22107f3a..7e54b823e8 100644 --- a/basis/ui/gadgets/worlds/worlds.factor +++ b/basis/ui/gadgets/worlds/worlds.factor @@ -125,7 +125,8 @@ M: world request-focus-on ( child gadget -- ) [ T{ rgba f 0.0 0.0 0.0 0.0 } ] [ T{ rgba f 1.0 1.0 1.0 1.0 } ] if ; -: apply-world-attributes ( world attributes -- world ) +GENERIC# apply-world-attributes 1 ( world attributes -- world ) +M: world apply-world-attributes { [ title>> >>title ] [ status>> >>status ] diff --git a/extra/audio/engine/engine.factor b/extra/audio/engine/engine.factor index 6caff51ee3..176fc3c305 100644 --- a/extra/audio/engine/engine.factor +++ b/extra/audio/engine/engine.factor @@ -10,12 +10,16 @@ TUPLE: audio-source { position initial: { 0.0 0.0 0.0 } } { gain float initial: 1.0 } { velocity initial: { 0.0 0.0 0.0 } } - { relative? boolean initial: f } ; + { relative? boolean initial: f } + { distance float initial: 1.0 } + { rolloff float initial: 1.0 } ; TUPLE: audio-orientation { forward initial: { 0.0 0.0 -1.0 } } { up initial: { 0.0 1.0 0.0 } } ; +C: audio-orientation + : orientation>float-array ( orientation -- float-array ) [ forward>> first3 ] [ up>> first3 ] bi 6 float-array{ } nsequence ; inline @@ -26,6 +30,32 @@ TUPLE: audio-listener { velocity initial: { 0.0 0.0 0.0 } } { orientation initial: T{ audio-orientation } } ; +GENERIC: audio-position ( source/listener -- position ) +GENERIC: audio-gain ( source/listener -- gain ) +GENERIC: audio-velocity ( source/listener -- velocity ) +GENERIC: audio-relative? ( source -- relative? ) +GENERIC: audio-distance ( source -- distance ) +GENERIC: audio-rolloff ( source -- rolloff ) +GENERIC: audio-orientation ( listener -- orientation ) + +M: object audio-position drop { 0.0 0.0 0.0 } ; inline +M: object audio-gain drop 1.0 ; inline +M: object audio-velocity drop { 0.0 0.0 0.0 } ; inline +M: object audio-relative? drop f ; inline +M: object audio-distance drop 1.0 ; inline +M: object audio-rolloff drop 1.0 ; inline +M: object audio-orientation drop T{ audio-orientation } ; inline + +M: audio-source audio-position position>> ; inline +M: audio-source audio-gain gain>> ; inline +M: audio-source audio-velocity velocity>> ; inline +M: audio-source audio-relative? relative?>> ; inline + +M: audio-listener audio-position position>> ; inline +M: audio-listener audio-gain gain>> ; inline +M: audio-listener audio-velocity velocity>> ; inline +M: audio-listener audio-orientation orientation>> ; inline + TUPLE: audio-engine < disposable { voice-count integer } { buffer-size integer } @@ -33,7 +63,7 @@ TUPLE: audio-engine < disposable { al-device c-ptr } { al-context c-ptr } al-sources - { listener audio-listener } + listener { next-source integer } clips update-alarm ; @@ -41,7 +71,7 @@ TUPLE: audio-engine < disposable TUPLE: audio-clip < disposable { audio-engine audio-engine } { audio audio } - { source audio-source } + source { loop? boolean } { al-source integer } { al-buffers uint-array } @@ -152,18 +182,20 @@ ERROR: audio-context-not-available device-name ; : update-listener ( audio-engine -- ) listener>> { - [ AL_POSITION swap position>> first3 alListener3f ] - [ AL_GAIN swap gain>> alListenerf ] - [ AL_VELOCITY swap velocity>> first3 alListener3f ] - [ AL_ORIENTATION swap orientation>> orientation>float-array alListenerfv ] + [ AL_POSITION swap audio-position first3 alListener3f ] + [ AL_GAIN swap audio-gain alListenerf ] + [ AL_VELOCITY swap audio-velocity first3 alListener3f ] + [ AL_ORIENTATION swap audio-orientation orientation>float-array alListenerfv ] } cleave ; : update-source ( audio-clip -- ) [ al-source>> ] [ source>> ] bi { - [ AL_POSITION swap position>> first3 alSource3f ] - [ AL_GAIN swap gain>> alSourcef ] - [ AL_VELOCITY swap velocity>> first3 alSource3f ] - [ AL_SOURCE_RELATIVE swap relative?>> c:>c-bool alSourcei ] + [ AL_POSITION swap audio-position first3 alSource3f ] + [ AL_GAIN swap audio-gain alSourcef ] + [ AL_VELOCITY swap audio-velocity first3 alSource3f ] + [ AL_SOURCE_RELATIVE swap audio-relative? c:>c-bool alSourcei ] + [ AL_REFERENCE_DISTANCE swap audio-distance alSourcef ] + [ AL_ROLLOFF_FACTOR swap audio-rolloff alSourcef ] } 2cleave ; :: update-audio-clip ( audio-clip -- ) @@ -179,8 +211,8 @@ ERROR: audio-context-not-available device-name ; ] times ] if ; -: clip-sources ( clips -- length sources ) - [ length ] [ [ source>> ] uint-array{ } map-as ] bi ; +: clip-al-sources ( clips -- length sources ) + [ length ] [ [ al-source>> ] uint-array{ } map-as ] bi ; PRIVATE> @@ -263,7 +295,7 @@ M: audio-clip dispose* : play-clips ( audio-clips -- ) [ [ update-source ] each ] - [ clip-sources alSourcePlayv ] bi ; + [ clip-al-sources alSourcePlayv ] bi ; : ( audio-engine audio source loop? -- audio-clip/f ) (audio-clip) dup play-clip ; @@ -272,19 +304,19 @@ M: audio-clip dispose* al-source>> alSourcePause ; : pause-clips ( audio-clip -- ) - clip-sources alSourcePausev ; + clip-al-sources alSourcePausev ; : stop-clip ( audio-clip -- ) dispose ; : stop-clips ( audio-clip -- ) - [ clip-sources alSourceStopv ] + [ clip-al-sources alSourceStopv ] [ [ dispose ] each ] bi ; : update-audio ( audio-engine -- ) { [ make-engine-current ] [ update-listener ] - [ clips>> [ update-audio-clip ] each ] + [ clips>> clone [ update-audio-clip ] each ] } cleave ; diff --git a/extra/game/worlds/worlds-docs.factor b/extra/game/worlds/worlds-docs.factor index faa21a18dc..75aed4dbda 100644 --- a/extra/game/worlds/worlds-docs.factor +++ b/extra/game/worlds/worlds-docs.factor @@ -1,50 +1,54 @@ ! (c)2009 Joe Groff bsd license -USING: game.loop help.markup help.syntax kernel math method-chains +USING: audio.engine game.loop help.markup help.syntax kernel math method-chains ui ui.gadgets.worlds words ; IN: game.worlds -HELP: GAME: -{ $syntax """GAME: word { attributes } - attribute-code ;""" } -{ $description "Similar to " { $link POSTPONE: MAIN-WINDOW: } ", defines a main entry point " { $snippet "word" } " for the current vocabulary that opens a UI window with the provided " { $snippet "attributes" } ". In addition to the standard " { $link world-attributes } ", additional " { $link game-attributes } " can be specified to specify game-specific attributes. Unlike " { $link POSTPONE: MAIN-WINDOW: } ", the " { $snippet "attributes" } " for " { $snippet "GAME:" } " must provide values for the " { $snippet "world-class" } " and " { $snippet "tick-interval-micros" } " slots." } ; - HELP: game-attributes { $class-description "Extends the " { $link world-attributes } " tuple class with extra attributes for " { $link game-world } "s:" } { $list -{ { $snippet "tick-interval-micros" } " specifies the number of microseconds between consecutive calls to the world's " { $link tick* } " method by the game loop." } +{ { $snippet "tick-interval-micros" } " specifies the number of microseconds between consecutive calls to the world's " { $link tick-game-world } " method by the game loop. An integer greater than zero must be provided." } +{ { $snippet "use-game-input?" } " specifies whether the game world should initialize the " { $vocab-link "game.input" } " library for use by the game. False by default." } +{ { $snippet "use-audio-engine?" } " specifies whether the game world should manage an " { $link audio-engine } " instance. False by default." } +{ { $snippet "audio-engine-device" } " specifies the string name of the OpenAL device the audio engine, if any, should try to open. The default value of " { $link POSTPONE: f } " attempts to open the default OpenAL device." } +{ { $snippet "audio-engine-voice-count" } " determines the number of independent voices the audio engine will make available. This determines how many individual audio clips can play simultaneously. This cannot exceed the OpenAL implementation's limit on supported voices." } +{ { $snippet "audio-engine-buffer-size" } " determines the size in bytes of the audio buffers the audio engine will stream to the sound card." } +{ { $snippet "audio-engine-buffer-count" } " determines the number of buffers the audio engine will allocate per audio clip played." } } ; HELP: game-world -{ $class-description "A subclass of " { $link world } " that automatically sets up and manages connections to the " { $vocab-link "game.loop" } " and " { $vocab-link "game.input" } " libraries. It does this by providing methods on " { $link begin-world } ", " { $link end-world } ", and " { $link draw* } ". Subclasses can provide their own world setup and teardown code by adding methods to the " { $link begin-game-world } " and " { $link end-game-world } " generic words." } ; +{ $class-description "A subclass of " { $link world } " that automatically sets up and manages connections to the " { $vocab-link "game.loop" } ", " { $vocab-link "game.input" } ", and " { $vocab-link "audio.engine" } " libraries. It does this by providing methods on " { $link begin-world } ", " { $link end-world } ", and " { $link draw* } ". Subclasses can provide their own world setup and teardown code by adding methods to the " { $link begin-game-world } " and " { $link end-game-world } " generic words." +$nl +"The game-world tuple has the following publicly accessible slots:" +{ $list +{ { $snippet "game-loop" } " contains the " { $link game-loop } " instance managed by the game world. If the world is inactive, this slot will contain " { $link POSTPONE: f } "." } +{ { $snippet "audio-engine" } " contains the " { $link audio-engine } " instance managed by the game world. If the world is inactive, or the " { $snippet "use-audio-engine?" } " slot of the " { $link game-attributes } " object used to initialize the world was false, this slot will contain " { $link POSTPONE: f } "." } +} } ; HELP: begin-game-world { $values { "world" game-world } } -{ $description "This generic word is called by the " { $link begin-world } " method for " { $link game-world } " subclasses immediately before the game world starts the game loop." } ; +{ $description "This generic word is called by the " { $link begin-world } " method for " { $link game-world } " subclasses immediately before the game world starts the game loop. If the game world has an " { $link audio-engine } ", it will be initialized and started before " { $snippet "begin-game-world" } " is called." } ; HELP: end-game-world { $values { "world" game-world } } { $description "This generic word is called by the " { $link end-world } " method for " { $link game-world } " subclasses immediately after the game world stops the game loop." } ; -{ game-world begin-game-world end-game-world } related-words +HELP: tick-game-world +{ $values { "world" game-world } } +{ $description "This generic word is called by the " { $link tick* } " method for " { $link game-world } " subclasses every time the game loop's tick interval occurs." } ; -HELP: tick-interval-micros -{ $values - { "world" game-world } - { "micros" integer } -} -{ $description "Subclasses of " { $link game-world } " can override this class to specify the number of microseconds between consecutive calls to the game world's " { $link tick* } " method by the game loop. Using the " { $link POSTPONE: GAME: } " syntax will define this method for you." } ; +{ game-world begin-game-world end-game-world tick-game-world } related-words ARTICLE: "game.worlds" "Game worlds" -"The " { $vocab-link "game.worlds" } " vocabulary provides a " { $link world } " subclass that integrates with " { $vocab-link "game.loop" } " and " { $vocab-link "game.input" } " to quickly provide game infrastructure." +"The " { $vocab-link "game.worlds" } " vocabulary provides a " { $link world } " subclass that integrates with " { $vocab-link "game.loop" } " and optionally " { $vocab-link "game.input" } " and " { $vocab-link "audio.engine" } " to quickly provide game infrastructure." { $subsections game-world game-attributes - POSTPONE: GAME: } -"Subclasses of " { $link game-world } " can provide their own setup and teardown code by providing methods for these generic words:" +"Subclasses of " { $link game-world } " can provide their own setup, teardown, and update code by providing methods for these generic words:" { $subsections begin-game-world end-game-world + tick-game-world } ; ABOUT: "game.worlds" diff --git a/extra/game/worlds/worlds.factor b/extra/game/worlds/worlds.factor index d624a1b41e..cf75d37b39 100644 --- a/extra/game/worlds/worlds.factor +++ b/extra/game/worlds/worlds.factor @@ -1,71 +1,86 @@ ! (c)2009 Joe Groff bsd license USING: accessors combinators fry game.input game.loop generic kernel math parser sequences ui ui.gadgets ui.gadgets.worlds ui.gestures threads -words ; +words audio.engine destructors ; IN: game.worlds TUPLE: game-world < world game-loop + audio-engine + { tick-interval-micros fixnum } + { use-game-input? boolean } + { use-audio-engine? boolean } + { audio-engine-device initial: f } + { audio-engine-voice-count initial: 16 } + { audio-engine-buffer-size initial: 8192 } + { audio-engine-buffer-count initial: 2 } { tick-slice float initial: 0.0 } ; -GENERIC: tick-interval-micros ( world -- micros ) - GENERIC: begin-game-world ( world -- ) M: object begin-game-world drop ; GENERIC: end-game-world ( world -- ) M: object end-game-world drop ; +GENERIC: tick-game-world ( world -- ) +M: object tick-game-world drop ; + +M: game-world tick* + [ tick-game-world ] + [ audio-engine>> [ update-audio ] when* ] bi ; + M: game-world draw* swap >>tick-slice relayout-1 yield ; +> ] + [ audio-engine-voice-count>> ] + [ audio-engine-buffer-size>> ] + [ audio-engine-buffer-count>> ] + } cleave + [ start-audio* ] keep ; inline + +PRIVATE> + M: game-world begin-world - open-game-input + dup use-game-input?>> [ open-game-input ] when + dup use-audio-engine?>> [ dup open-game-audio-engine >>audio-engine ] when dup begin-game-world - dup [ tick-interval-micros ] [ ] bi [ >>game-loop ] keep start-loop + dup [ tick-interval-micros>> ] [ ] bi [ >>game-loop ] keep start-loop drop ; M: game-world end-world [ [ stop-loop ] when* f ] change-game-loop - end-game-world - close-game-input ; + [ end-game-world ] + [ audio-engine>> [ dispose ] when* ] + [ use-game-input?>> [ close-game-input ] when ] tri ; TUPLE: game-attributes < world-attributes - { tick-interval-micros fixnum read-only } ; + { tick-interval-micros fixnum } + { use-game-input? boolean initial: f } + { use-audio-engine? boolean initial: f } + { audio-engine-device initial: f } + { audio-engine-voice-count initial: 16 } + { audio-engine-buffer-size initial: 8192 } + { audio-engine-buffer-count initial: 2 } ; -> { f world } member? - [ "GAME: must be given a custom world-class" throw ] when - ] - [ - tick-interval-micros>> 0 <= - [ "GAME: must be given a nonzero tick-interval-micros" throw ] when - ] + [ tick-interval-micros>> >>tick-interval-micros ] + [ use-game-input?>> >>use-game-input? ] + [ use-audio-engine?>> >>use-audio-engine? ] + [ audio-engine-device>> >>audio-engine-device ] + [ audio-engine-voice-count>> >>audio-engine-voice-count ] + [ audio-engine-buffer-size>> >>audio-engine-buffer-size ] + [ audio-engine-buffer-count>> >>audio-engine-buffer-count ] + [ call-next-method ] } cleave ; -: define-game-tick-interval-micros ( attributes -- ) - [ world-class>> \ tick-interval-micros create-method ] - [ tick-interval-micros>> '[ drop _ ] ] bi - define ; - -: define-game-methods ( attributes -- ) - { - [ verify-game-attributes ] - [ define-game-tick-interval-micros ] - } cleave ; - -: define-game ( word attributes quot -- ) - [ define-main-window ] - [ drop nip define-game-methods ] 3bi ; - -PRIVATE> - SYNTAX: GAME: CREATE game-attributes parse-main-window-attributes parse-definition - define-game ; + define-main-window ; diff --git a/extra/gpu/demos/bunny/bunny.factor b/extra/gpu/demos/bunny/bunny.factor index f56be88c78..b2f619c3d1 100644 --- a/extra/gpu/demos/bunny/bunny.factor +++ b/extra/gpu/demos/bunny/bunny.factor @@ -306,6 +306,7 @@ GAME: bunny-game { T{ depth-bits { value 24 } } } } { grab-input? t } + { use-game-input? t } { pref-dim { 1024 768 } } { tick-interval-micros $[ 60 fps ] } } ; diff --git a/extra/gpu/demos/raytrace/green-ball.aiff b/extra/gpu/demos/raytrace/green-ball.aiff new file mode 100644 index 0000000000..a7c03723c5 Binary files /dev/null and b/extra/gpu/demos/raytrace/green-ball.aiff differ diff --git a/extra/gpu/demos/raytrace/mirror-ball.aiff b/extra/gpu/demos/raytrace/mirror-ball.aiff new file mode 100644 index 0000000000..a1cd1632eb Binary files /dev/null and b/extra/gpu/demos/raytrace/mirror-ball.aiff differ diff --git a/extra/gpu/demos/raytrace/raytrace.factor b/extra/gpu/demos/raytrace/raytrace.factor index 0b44ac2e23..54912544f1 100644 --- a/extra/gpu/demos/raytrace/raytrace.factor +++ b/extra/gpu/demos/raytrace/raytrace.factor @@ -1,9 +1,9 @@ ! (c)2009 Joe Groff bsd license USING: accessors arrays combinators.tuple game.loop game.worlds generalizations gpu gpu.render gpu.shaders gpu.util gpu.util.wasd -kernel literals math math.matrices math.order math.vectors +kernel literals math math.libm math.matrices math.order math.vectors method-chains sequences ui ui.gadgets ui.gadgets.worlds -ui.pixel-formats ; +ui.pixel-formats audio.engine audio.loader locals ; IN: gpu.demos.raytrace GLSL-SHADER-FILE: raytrace-vertex-shader vertex-shader "raytrace.v.glsl" @@ -49,6 +49,9 @@ TUPLE: raytrace-world < wasd-world [ [ axis>> ] [ theta>> ] bi rotation-matrix4 ] [ home>> ] bi m.v ; +M: sphere audio-position sphere-center ; inline +M: sphere audio-distance radius>> fsqrt 2.0 * ; inline + : ( world -- uniforms ) [ wasd-mv-inv-matrix ] [ fov>> ] @@ -69,19 +72,36 @@ CONSTANT: initial-spheres { T{ sphere f { 1.0 0.0 0.0 } { 0.0 5.0 0.0 } 0.025 1.0 { 1.0 1.0 0.0 1.0 } } } +:: set-up-audio ( world -- ) + world audio-engine>> :> audio-engine + world spheres>> :> spheres + + audio-engine world >>listener update-audio + + audio-engine "vocab:gpu/demos/raytrace/mirror-ball.aiff" read-audio + spheres first t (audio-clip) + audio-engine "vocab:gpu/demos/raytrace/red-ball.aiff" read-audio + spheres second t (audio-clip) + audio-engine "vocab:gpu/demos/raytrace/green-ball.aiff" read-audio + spheres third t (audio-clip) + audio-engine "vocab:gpu/demos/raytrace/yellow-ball.aiff" read-audio + spheres fourth t (audio-clip) + + 4array play-clips ; + M: raytrace-world begin-game-world init-gpu { -2.0 6.25 10.0 } 0.19 0.55 set-wasd-view initial-spheres [ clone ] map >>spheres raytrace-program >>vertex-array - drop ; + set-up-audio ; CONSTANT: fov 0.7 AFTER: raytrace-world resize-world dup dim>> dup first2 min >float v/n fov v*n >>fov drop ; -AFTER: raytrace-world tick* +AFTER: raytrace-world tick-game-world spheres>> [ tick-sphere ] each ; M: raytrace-world draw-world* @@ -102,6 +122,9 @@ GAME: raytrace-game { double-buffered } } { grab-input? t } + { use-game-input? t } + { use-audio-engine? t } + { audio-engine-buffer-count 4 } { pref-dim { 1024 768 } } { tick-interval-micros $[ 60 fps ] } } ; diff --git a/extra/gpu/demos/raytrace/red-ball.aiff b/extra/gpu/demos/raytrace/red-ball.aiff new file mode 100644 index 0000000000..cd08e8880d Binary files /dev/null and b/extra/gpu/demos/raytrace/red-ball.aiff differ diff --git a/extra/gpu/demos/raytrace/yellow-ball.aiff b/extra/gpu/demos/raytrace/yellow-ball.aiff new file mode 100644 index 0000000000..63b3790acf Binary files /dev/null and b/extra/gpu/demos/raytrace/yellow-ball.aiff differ diff --git a/extra/gpu/util/wasd/wasd.factor b/extra/gpu/util/wasd/wasd.factor index b5ed28cc3d..8251fe21b6 100644 --- a/extra/gpu/util/wasd/wasd.factor +++ b/extra/gpu/util/wasd/wasd.factor @@ -4,7 +4,7 @@ game.input.scancodes game.loop game.worlds gpu.render gpu.state kernel literals locals math math.constants math.functions math.matrices math.order math.vectors opengl.gl sequences -ui ui.gadgets.worlds specialized-arrays ; +ui ui.gadgets.worlds specialized-arrays audio.engine ; FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float IN: gpu.util.wasd @@ -87,6 +87,9 @@ CONSTANT: fov 0.7 [ yaw>> ] [ ?pitch ] [ wasd-movement-speed ] tri { 1.0 0.0 0.0 } n*v eye-rotate ; +M: wasd-world audio-position location>> ; inline +M: wasd-world audio-orientation forward-vector { 0.0 1.0 0.0 } ; inline + : walk-forward ( world -- ) dup forward-vector [ v+ ] curry change-location drop ; : walk-backward ( world -- ) @@ -121,7 +124,7 @@ CONSTANT: fov 0.7 : wasd-mouse-input ( world -- ) read-mouse rotate-with-mouse ; -M: wasd-world tick* +M: wasd-world tick-game-world dup focused?>> [ [ wasd-keyboard-input ] [ wasd-mouse-input ] bi reset-mouse diff --git a/extra/terrain/terrain.factor b/extra/terrain/terrain.factor index f28e685c66..27bd7df403 100644 --- a/extra/terrain/terrain.factor +++ b/extra/terrain/terrain.factor @@ -217,7 +217,7 @@ terrain-world H{ [ tick-player-reverse ] [ tick-player-forward ] if ; -M: terrain-world tick* +M: terrain-world tick-game-world [ dup focused?>> [ handle-input ] [ drop ] if ] [ dup player>> tick-player ] bi ; @@ -295,6 +295,7 @@ GAME: terrain-game { double-buffered T{ depth-bits { value 24 } } } } + { use-game-input? t } { grab-input? t } { pref-dim { 1024 768 } } { tick-interval-micros $[ 60 fps ] }