From 89f104d8abe4cd57fd73233aba54ce01b3e5676a Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 16 Jan 2010 23:06:54 -0800 Subject: [PATCH] add begin-game-world and end-game-world generics so method-chains aren't needed just to do game setup and teardown at the right time --- extra/game/worlds/worlds-docs.factor | 21 ++++++++++++++++++--- extra/game/worlds/worlds.factor | 11 +++++++++-- extra/gpu/demos/bunny/bunny.factor | 2 +- extra/gpu/demos/raytrace/raytrace.factor | 2 +- extra/terrain/terrain.factor | 6 +++--- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/extra/game/worlds/worlds-docs.factor b/extra/game/worlds/worlds-docs.factor index e021ed5718..faa21a18dc 100644 --- a/extra/game/worlds/worlds-docs.factor +++ b/extra/game/worlds/worlds-docs.factor @@ -1,5 +1,6 @@ ! (c)2009 Joe Groff bsd license -USING: game.loop help.markup help.syntax kernel math ui ui.gadgets.worlds words ; +USING: game.loop help.markup help.syntax kernel math method-chains +ui ui.gadgets.worlds words ; IN: game.worlds HELP: GAME: @@ -14,7 +15,17 @@ HELP: game-attributes } ; HELP: game-world -{ $class-description "" } ; +{ $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." } ; + +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." } ; + +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-interval-micros { $values @@ -30,6 +41,10 @@ ARTICLE: "game.worlds" "Game worlds" game-attributes POSTPONE: GAME: } -; +"Subclasses of " { $link game-world } " can provide their own setup and teardown code by providing methods for these generic words:" +{ $subsections + begin-game-world + end-game-world +} ; ABOUT: "game.worlds" diff --git a/extra/game/worlds/worlds.factor b/extra/game/worlds/worlds.factor index 1fd7f53905..d624a1b41e 100644 --- a/extra/game/worlds/worlds.factor +++ b/extra/game/worlds/worlds.factor @@ -10,18 +10,25 @@ TUPLE: game-world < world 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 ; + M: game-world draw* swap >>tick-slice relayout-1 yield ; M: game-world begin-world open-game-input + dup begin-game-world dup [ tick-interval-micros ] [ ] bi [ >>game-loop ] keep start-loop drop ; M: game-world end-world [ [ stop-loop ] when* f ] change-game-loop - close-game-input - drop ; + end-game-world + close-game-input ; TUPLE: game-attributes < world-attributes { tick-interval-micros fixnum read-only } ; diff --git a/extra/gpu/demos/bunny/bunny.factor b/extra/gpu/demos/bunny/bunny.factor index e18d76ed8d..3199cdcffd 100644 --- a/extra/gpu/demos/bunny/bunny.factor +++ b/extra/gpu/demos/bunny/bunny.factor @@ -214,7 +214,7 @@ CONSTANT: bunny-model-url "http://factorcode.org/bun_zipper.ply" dup 0 "vocab:gpu/demos/bunny/loading.tiff" load-image allocate-texture-image >>texture ; -BEFORE: bunny-world begin-world +M: bunny-world begin-game-world init-gpu { -0.2 0.13 0.1 } 1.1 0.2 set-wasd-view diff --git a/extra/gpu/demos/raytrace/raytrace.factor b/extra/gpu/demos/raytrace/raytrace.factor index b474ca1f09..a58388fc00 100644 --- a/extra/gpu/demos/raytrace/raytrace.factor +++ b/extra/gpu/demos/raytrace/raytrace.factor @@ -69,7 +69,7 @@ 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 } } } -BEFORE: raytrace-world begin-world +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 diff --git a/extra/terrain/terrain.factor b/extra/terrain/terrain.factor index 39bc7f3ec8..370c7634a7 100644 --- a/extra/terrain/terrain.factor +++ b/extra/terrain/terrain.factor @@ -6,7 +6,7 @@ math.vectors opengl opengl.capabilities opengl.gl opengl.shaders opengl.textures opengl.textures.private sequences sequences.product specialized-arrays terrain.generation terrain.shaders typed ui ui.gadgets -ui.gadgets.worlds ui.pixel-formats game.worlds method-chains +ui.gadgets.worlds ui.pixel-formats game.worlds math.matrices.simd noise ui.gestures combinators.short-circuit destructors grid-meshes math.vectors.simd ; QUALIFIED-WITH: alien.c-types c @@ -233,7 +233,7 @@ M: terrain-world tick* : sky-theta ( world -- theta ) game-loop>> tick-number>> SKY-SPEED * ; -BEFORE: terrain-world begin-world +M: terrain-world begin-game-world "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" } require-gl-version-or-extensions GL_DEPTH_TEST glEnable @@ -254,7 +254,7 @@ BEFORE: terrain-world begin-world terrain-vertex-size >>terrain-mesh drop ; -AFTER: terrain-world end-world +M: terrain-world end-game-world { [ terrain-mesh>> dispose ] [ terrain-program>> delete-gl-program ]