Video
An intro before the title screen, a cutscene between levels, a looping background behind the menu: sometimes the thing to show is a video. I play MP4 and WebM files, sound and all.
Crash-tan’s Tip: The games in this book have a short video in their folder,
clip.mp4: three seconds of me under the stars, with no sound. Put a video of your own in your game’s folder to hear one play.
Loading and playing
LOADVIDEO loads a video from the game’s folder and gives it a name, the way
LOADIMG does for an image. PLAYVIDEO plays it on a layer, and your
program carries on while it plays. !! loop starts it again each time it
ends, and !! mute plays it without its sound.
SCREEN 320, 240
LOADVIDEO "clip.mp4", "menu"
PLAYVIDEO "menu", 1 !! loop mute
That’s a background for a menu: round and round, quietly, while the menu is drawn and the keys are read.
A cutscene
WAITVIDEO waits until a video has finished, so the program stops there and
starts again when the video’s over. That’s a cutscene: play it, wait, then
the next level.
When a video ends, its last picture stays, so the end of a cutscene doesn’t
flash to an empty screen. This one plays on layer 0, the layer PRINT
writes on, so “Level 1” goes on top of that last picture. On a higher layer,
the picture would cover it.
SCREEN 320, 240
LOADVIDEO "clip.mp4", "intro"
PLAYVIDEO "intro", 0
WAITVIDEO "intro"
PRINT "Level 1"
The controls
PAUSEVIDEO pauses a video and RESUMEVIDEO carries on from where it was.
SEEKVIDEO jumps to a time, in seconds, so SEEKVIDEO "intro", 0 goes back to
the start. STOPVIDEO stops it for good, and a WAITVIDEO that was waiting
for it stops waiting.
Players like to skip a cutscene they’ve seen. While the program waits on
WAITVIDEO, a thread of its own watches the keys: Space skips, P pauses and
R starts again.
SCREEN 320, 240
LOADVIDEO "clip.mp4", "intro"
FUNCTION controls()
paused = FALSE
DO
IF COOLDOWN(1)
IF keydown("Space") THEN STOPVIDEO "intro"
IF keydown("R") THEN SEEKVIDEO "intro", 0
END IF
IF COOLDOWN(15) AND keydown("P")
IF paused THEN RESUMEVIDEO "intro" ELSE PAUSEVIDEO "intro"
paused = NOT paused
END IF
LOOP
END FUNCTION
PLAYVIDEO "intro", 0
CALL THREAD controls, "controls"
WAITVIDEO "intro"
STOP THREAD "controls"
PRINT "Level 1"
The COOLDOWN(15) on P means holding it down doesn’t flick between paused
and playing sixty times a second.
When the video isn’t there
A game can check for its video before playing it, and go straight on without
one. There’s no intro.mp4 in the book’s games, so this one skips to the
level.
SCREEN 320, 240
IF fileexists("intro.mp4")
LOADVIDEO "intro.mp4", "intro"
PLAYVIDEO "intro", 0
WAITVIDEO "intro"
END IF
PRINT "Level 1"
Level 1
Videos aren’t layers
Every platform plays video its own way, to use the hardware that’s made for it: in the browser it’s the browser’s own video player. So a video doesn’t always take on what you’ve done to its layer. Don’t count on a layer’s shader, transparency or clipping to change how a video looks.