3. Sound & Sky!
The special warped sky image below is made to be textured onto a sphere that will be enlarged and flipped inside out to become our sky! Add a little music and sound and our world is really starting to come alive!
See full size!

If you haven't already downloaded the tutorial files do so now, unzip to your desktop and OPEN up lesson3.bb, functions3.bb, and loadworld3.bb (below) and RUN / LANUCH the lesson3.bb program by pressing the ROCKET ICON

and you should see this...

NEW CODE is BOLD.

Here's the CODE!
lesson3.bb

Include "functions3.bb"

Global escape_key = 1
Global screen_width = 640, screen_height = 480

Graphics3D screen_width, screen_height, 16, 2
SetBuffer BackBuffer()


AmbientLight 200, 200, 200
light1 = CreateLight(2)

cam1 = CreateCamera()
CameraViewport cam1, 0, 0, screen_width, screen_height

Include "loadworld3.bb"

LoadMusic()
LoadSFX()

MoveMouse screen_width/2, screen_height/2

While Not KeyHit( escape_key )

    object_key_control( cube1 )


    ChannelVolume runchannel, run_vol#

    PositionEntity sky, EntityX(cube1), EntityY(cube1), EntityZ(cube1)

    UpdateWorld
    RenderWorld

    
; 2D here


Flip
Wend

End


functions3.bb

Global up_key = 200, down_key=208, left_key=203, right_key=205
Global W_key = 17, S_key=31, A_key=30, D_key=32

Global runchannel, run_vol#

Function LoadMusic()
    chn_music = PlayMusic ("onlylove.mid")

    ChannelVolume chn_music,.5

End Function

Function LoadSFX()
    run = LoadSound ("water.wav")
    LoopSound run

    SoundVolume run, 0

    runchannel = PlaySound (run)

End Function

Function object_key_control( obj )
    run_snd = False
   
 If KeyDown( up_key ) = True Then MoveEntity obj, 0, 0, .2 : run_snd = True
    
If KeyDown( down_key ) = True Then MoveEntity obj, 0, 0, -.2 : run_snd = True
    If KeyDown( left_key ) =True Then TurnEntity obj, 0, 2, 0
    If KeyDown( right_key ) =True Then TurnEntity obj, 0, -2, 0
    If run_snd Then run_vol# = .5 Else run_vol# = 0
End Function


loadworld3.bb

; Make a sky!
sky = CreateSphere()
skytexture = LoadTexture("sky1.jpg")
EntityTexture sky,skytexture
ScaleEntity sky,200,200,200
FlipMesh sky

cube1 = CreateCube()
PositionEntity cube1, 0,-.5, 10
EntityColor cube1, 0, 255, 255

EntityParent cam1, cube1
. . .


NEW COMMANDS
lesson3.bb

; Add some extra light in the world!
AmbientLight 200, 200, 200
. . .
; New Sound Functions
LoadMusic()
LoadSFX()
. . .
    ; Keep player "running" volume updated
    ChannelVolume runchannel, run_vol#

    ; Keep our sky centered on player

    PositionEntity sky, EntityX(cube1), EntityY(cube1), EntityZ(cube1)
. . .

We can add some Ambient light so that if we get too far from our point light source things won't go dark on us :)
Next we add Music and Sound FX loader functions. Then, in the GAME LOOP, we update our player sound and keep the SKY sphere centered on our player!
EXPERIMENT !
Try changing the color of the ambient light

function3.bb

Global up_key = 200, down_key=208, left_key=203, right_key=205
Global W_key = 17, S_key=31, A_key=30, D_key=32

; Running sound effect handle and volume tags
Global runchannel, run_vol#

Function LoadMusic()
    chn_music = PlayMusic ("onlylove.mid")
; PlayMusic() required for midi files
    ChannelVolume chn_music,.5
; Set play volume to 50%
End Function

Function LoadSFX()
    run = LoadSound ("water.wav")
    LoopSound run
; keep it playing in a loop
    SoundVolume run, 0
; running sound effect volume set to start silent
    runchannel = PlaySound (run)
; using a channel allows for volume control, mixing, panning
End Function

Function object_key_control( obj )
    ; This is a "flag" we'll use to keep track of our state of movement
    run_snd = False
    ; If we move forward or backward then turn "flag" ON (TRUE)
    If KeyDown( up_key ) = True Then MoveEntity obj, 0, 0, .2 : run_snd = True
    If KeyDown( down_key ) = True Then MoveEntity obj, 0, 0, -.2 : run_snd = True
    ; No need to use sound when turning
    If KeyDown( left_key ) =True Then TurnEntity obj, 0, 2, 0
    If KeyDown( right_key ) =True Then TurnEntity obj, 0, -2, 0
    ; if the RUN "flag" is TRUE then turn volume up to 50% otherwise OFF
    If run_snd = True Then run_vol# = .5 Else run_vol# = 0
End Function

We have created 2 new sound LOADing functions here and made modifications to our object_key_control( obj ) so that the "water.wav" wading sound is played when we move our player object.
EXPERIMENT !
Find a different midi music file and replace the existing one!

loadworld3.bb

; Make a sky!
sky = CreateSphere()
skytexture = LoadTexture("sky1.jpg")
EntityTexture sky,skytexture
; Make it large!
ScaleEntity sky,200,200,200
; Make the texture visible from the inside of the sphere
FlipMesh sky

cube1 = CreateCube()
PositionEntity cube1, 0,-.5, 10
EntityColor cube1, 0, 255, 255

; "Glue" the camera to follow the cube
EntityParent cam1, cube1
. . .

Making a sky is a lot simpler than you probably thought. Just make a sphere, texture it, enlarge it, and FLIP it inside out! We are also going to make the camera follow us by sticking (Parenting) it to our character.
THE CHALLENGE!!!
Add sound and sky to your previous lesson!

NEXT : Lesson 4 - SPRITES