2017-08-25 18:34:26 -04:00
|
|
|
! Copyright (C) 2009 Joe Groff.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2010-06-21 13:33:34 -04:00
|
|
|
USING: accessors audio.engine combinators concurrency.promises
|
2015-08-12 09:49:02 -04:00
|
|
|
destructors game.input game.loop kernel math parser sequences
|
2016-04-22 09:21:56 -04:00
|
|
|
threads ui ui.gadgets ui.gadgets.worlds vocabs.parser words.constant ;
|
2009-10-08 02:42:54 -04:00
|
|
|
IN: game.worlds
|
2009-05-09 13:53:01 -04:00
|
|
|
|
|
|
|
TUPLE: game-world < world
|
2009-05-09 15:31:33 -04:00
|
|
|
game-loop
|
2010-01-19 18:02:47 -05:00
|
|
|
audio-engine
|
2010-05-22 00:50:42 -04:00
|
|
|
{ tick-interval-nanos integer }
|
2010-01-19 18:02:47 -05:00
|
|
|
{ use-game-input? boolean }
|
|
|
|
{ use-audio-engine? boolean }
|
|
|
|
{ audio-engine-device initial: f }
|
|
|
|
{ audio-engine-voice-count initial: 16 }
|
2009-05-09 15:31:33 -04:00
|
|
|
{ tick-slice float initial: 0.0 } ;
|
2009-05-09 13:53:01 -04:00
|
|
|
|
2010-01-17 02:06:54 -05:00
|
|
|
GENERIC: begin-game-world ( world -- )
|
|
|
|
M: object begin-game-world drop ;
|
|
|
|
|
|
|
|
GENERIC: end-game-world ( world -- )
|
|
|
|
M: object end-game-world drop ;
|
|
|
|
|
2010-01-19 18:02:47 -05:00
|
|
|
GENERIC: tick-game-world ( world -- )
|
|
|
|
M: object tick-game-world drop ;
|
|
|
|
|
|
|
|
M: game-world tick*
|
|
|
|
[ tick-game-world ]
|
|
|
|
[ audio-engine>> [ update-audio ] when* ] bi ;
|
|
|
|
|
2009-05-09 13:53:01 -04:00
|
|
|
M: game-world draw*
|
2009-06-18 23:33:09 -04:00
|
|
|
swap >>tick-slice relayout-1 yield ;
|
2009-05-09 13:53:01 -04:00
|
|
|
|
2010-01-19 18:02:47 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: open-game-audio-engine ( game-world -- audio-engine )
|
|
|
|
{
|
|
|
|
[ audio-engine-device>> ]
|
|
|
|
[ audio-engine-voice-count>> ]
|
|
|
|
} cleave <audio-engine>
|
|
|
|
[ start-audio* ] keep ; inline
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-05-09 13:53:01 -04:00
|
|
|
M: game-world begin-world
|
2010-01-19 18:02:47 -05:00
|
|
|
dup use-game-input?>> [ open-game-input ] when
|
|
|
|
dup use-audio-engine?>> [ dup open-game-audio-engine >>audio-engine ] when
|
2010-05-22 00:50:42 -04:00
|
|
|
dup [ tick-interval-nanos>> ] [ ] bi <game-loop>
|
2010-02-24 23:07:13 -05:00
|
|
|
[ >>game-loop begin-game-world ] keep start-loop ;
|
2009-05-09 13:53:01 -04:00
|
|
|
|
|
|
|
M: game-world end-world
|
2010-06-08 19:05:28 -04:00
|
|
|
dup game-loop>> [ stop-loop ] when*
|
2010-01-19 18:02:47 -05:00
|
|
|
[ end-game-world ]
|
|
|
|
[ audio-engine>> [ dispose ] when* ]
|
|
|
|
[ use-game-input?>> [ close-game-input ] when ] tri ;
|
2009-05-09 13:53:01 -04:00
|
|
|
|
2010-01-15 18:03:33 -05:00
|
|
|
TUPLE: game-attributes < world-attributes
|
2010-05-22 03:24:04 -04:00
|
|
|
{ tick-interval-nanos integer }
|
2010-01-19 18:02:47 -05:00
|
|
|
{ use-game-input? boolean initial: f }
|
|
|
|
{ use-audio-engine? boolean initial: f }
|
|
|
|
{ audio-engine-device initial: f }
|
2010-01-20 14:44:18 -05:00
|
|
|
{ audio-engine-voice-count initial: 16 } ;
|
2010-01-15 18:03:33 -05:00
|
|
|
|
2010-01-19 18:02:47 -05:00
|
|
|
M: game-world apply-world-attributes
|
2010-01-16 16:13:13 -05:00
|
|
|
{
|
2010-05-22 00:50:42 -04:00
|
|
|
[ tick-interval-nanos>> >>tick-interval-nanos ]
|
2010-01-19 18:02:47 -05:00
|
|
|
[ 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 ]
|
|
|
|
[ call-next-method ]
|
2010-01-16 16:13:13 -05:00
|
|
|
} cleave ;
|
2010-01-16 15:18:50 -05:00
|
|
|
|
2010-06-08 19:05:28 -04:00
|
|
|
: start-game ( attributes -- game-world )
|
|
|
|
f swap open-window* ;
|
|
|
|
|
2010-06-16 17:05:35 -04:00
|
|
|
: wait-game ( attributes -- game-world )
|
|
|
|
f swap open-window* dup promise>> ?promise drop ;
|
|
|
|
|
2010-06-08 19:05:28 -04:00
|
|
|
: define-attributes-word ( word tuple -- )
|
2015-06-08 15:38:38 -04:00
|
|
|
[ name>> "-attributes" append create-word-in ] dip define-constant ;
|
2010-06-08 19:05:28 -04:00
|
|
|
|
2010-01-16 15:18:50 -05:00
|
|
|
SYNTAX: GAME:
|
2011-09-27 16:20:07 -04:00
|
|
|
scan-new-word
|
2016-04-22 09:21:56 -04:00
|
|
|
game-attributes parse-window-attributes
|
2010-06-08 19:05:28 -04:00
|
|
|
2dup define-attributes-word
|
2010-01-16 16:13:13 -05:00
|
|
|
parse-definition
|
2016-04-22 09:21:56 -04:00
|
|
|
[ define-window ] [ 2drop current-vocab main<< ] 3bi ;
|