I made this for background animation in my own game, thought I'd package it up for release... hope it's useful for someone. Essentially it's a video playing plugin, with an internal ogg/theora decoder. Theora is a free video codec targetted to compete with well known MPEG4 codecs (xvid, divx etc). Quality isn't quite there yet but close enough, and the encoder is still being improved.
Generally be careful with it because as I said, it's for my own use and may not work in unusual situations. For a start it only supports videos encoded in YUV420 which happens to be most of them. Also seeking doesn't align to keyframes so there will be momentary tearing when seeking to a non keyframe, tell me if you need that fixed. Doesn't interpolate chroma though I suppose it should. If anyone actually uses it they can tell me what they want adjusted for their purposes.
plugin download / demonstration download (try it in d3d mode to see the rotating etc if you care)
Interface
Code: ags
Advantages of this over regular video playing are
. doesn't require user to have installed any codecs, while providing better compression than available by default
. completely free format for free and commercial use
. draw videos within your scene and onto sprites for special effects
. (hopefully) no black frames or other signs of switching between game and video
Disadvantages
. is a plugin, therefore only works on windows engine right now
. decoding is fairly slow, YUV->RGB conversion needs to be done in software which is an additional overhead, not sure what the minimum specs for a full screen 640x480 30fps video would be, but somewhat higher than the regular video player. However, I did have someone test the demo on an ASUS EEE PC with 900mhz processor and it seemed fine.
. does not do audio. AGS already does audio, if you want you can sync up a sound/music file with the video with script. The main purpose of the plugin is background animation which does not require audio.
. requires some scripting to get it going, videos don't play on their own (although I could make them if people really want)
. only supports 32 bit mode at present
. software mode doesn't scale videos, therefore playing your game at the wrong resolution (scale filters are fine) will look bad. I don't think AGS should have this option on the setup though, personally.
. one unpredictable lockup issue which occurs from time to time when exiting in d3d mode, which I will fix some time, but it is fairly rare for me.
Details
It's quite basic as it is, I only need it for background animations, but if anyone uses it and would like features go ahead and suggest them. Support for 16 bit mode and video file obfuscation might be worth doing.
Here's the room script from the demo game, as an example of how to play a video on the background (and how to scale and rotate one in D3D mode.
Code: ags
I should also say, if you have a video open and go to another room, that video is still open! When you no longer need a video set the pointer to null. You can do this on exit, and load it on enter if you want. Just in case someone loads a video background in every room and wonders why it's taking 100s of MB of RAM.
Generally be careful with it because as I said, it's for my own use and may not work in unusual situations. For a start it only supports videos encoded in YUV420 which happens to be most of them. Also seeking doesn't align to keyframes so there will be momentary tearing when seeking to a non keyframe, tell me if you need that fixed. Doesn't interpolate chroma though I suppose it should. If anyone actually uses it they can tell me what they want adjusted for their purposes.
plugin download / demonstration download (try it in d3d mode to see the rotating etc if you care)
Interface
/* values specifying the "layer" of the scene to draw the video over */
enum RenderStage {
RsBackground,
RsScene,
RsUI,
RsOverlay
};
enum RelativeTo {
RtRoom,
RtScreen,
RtScreenPixels
}
/* Each Theora instance represents an open video file. */
struct Theora {
// opening
static Theora* open(string name);
// positioning
void NextFrame(); // move to next frame
void Sync(float time); // advance 0 or more frames to sync with time
void Seek(float time); // attempt to seek to a position
// status
readonly bool broken; // the video is bad and can no longer render properly
readonly bool ended; // end of video was hit, no more new frames will appear unless looped
readonly float time; // current time of video (may be inaccurate)
readonly float fps; // fps of encoded video, you don't have to stick to it if you don't want to
readonly int width;
readonly int height;
bool loop; // enable/disable automatic looping
// rendering
void Draw(RenderStage stage, int left, int top, RelativeTo);
void DrawEx(RenderStage stage, float cx, float cy, float xscale, float yscale, float rotation, RelativeTo); // scale/rotation can only be used if d3d renderer is being used!
}
Advantages of this over regular video playing are
. doesn't require user to have installed any codecs, while providing better compression than available by default
. completely free format for free and commercial use
. draw videos within your scene and onto sprites for special effects
. (hopefully) no black frames or other signs of switching between game and video
Disadvantages
. is a plugin, therefore only works on windows engine right now
. decoding is fairly slow, YUV->RGB conversion needs to be done in software which is an additional overhead, not sure what the minimum specs for a full screen 640x480 30fps video would be, but somewhat higher than the regular video player. However, I did have someone test the demo on an ASUS EEE PC with 900mhz processor and it seemed fine.
. does not do audio. AGS already does audio, if you want you can sync up a sound/music file with the video with script. The main purpose of the plugin is background animation which does not require audio.
. requires some scripting to get it going, videos don't play on their own (although I could make them if people really want)
. only supports 32 bit mode at present
. software mode doesn't scale videos, therefore playing your game at the wrong resolution (scale filters are fine) will look bad. I don't think AGS should have this option on the setup though, personally.
. one unpredictable lockup issue which occurs from time to time when exiting in d3d mode, which I will fix some time, but it is fairly rare for me.
Details
It's quite basic as it is, I only need it for background animations, but if anyone uses it and would like features go ahead and suggest them. Support for 16 bit mode and video file obfuscation might be worth doing.
Here's the room script from the demo game, as an example of how to play a video on the background (and how to scale and rotate one in D3D mode.
Theora* video;
float rot = 0.0;
function repeatedly_execute_always() {
if(video) {
video.DrawEx(DsBackground, 160.0, 120.0, 0.5, 0.5, 0.0, RtScreen);
if(System.HardwareAcceleration) video.DrawEx(DsScene, 260.0, 120.0, 0.25, 0.25, rot, RtScreen);
video.NextFrame();
rot += 0.2;
}
}
function room_Load() {
video = Theora.Open("theora.ogg");
video.loop = true;
SetGameSpeed(24); // you don't need to use the same fps as the video like this, but it's easiest this way
}
I should also say, if you have a video open and go to another room, that video is still open! When you no longer need a video set the pointer to null. You can do this on exit, and load it on enter if you want. Just in case someone loads a video background in every room and wonders why it's taking 100s of MB of RAM.