Hacky ingame video widget

This commit is contained in:
Paul Chote
2010-08-11 11:45:37 +12:00
parent bf8e3645cb
commit 44d7aa14a4
6 changed files with 88 additions and 31 deletions

View File

@@ -22,9 +22,10 @@ namespace OpenRA.FileFormats
Stream stream; Stream stream;
ushort flags; ushort flags;
public readonly ushort numFrames; public readonly ushort numFrames;
public readonly byte framerate;
ushort numColors; ushort numColors;
ushort width; public readonly ushort width;
ushort height; public readonly ushort height;
ushort blockWidth; ushort blockWidth;
ushort blockHeight; ushort blockHeight;
byte cbParts; byte cbParts;
@@ -51,12 +52,12 @@ namespace OpenRA.FileFormats
if (new String(reader.ReadChars(4)) != "FORM") if (new String(reader.ReadChars(4)) != "FORM")
throw new InvalidDataException("Invalid vqa (invalid FORM section)"); throw new InvalidDataException("Invalid vqa (invalid FORM section)");
/*var formlength = */ reader.ReadUInt32(); /*var length = */ reader.ReadUInt32();
if (new String(reader.ReadChars(8)) != "WVQAVQHD") if (new String(reader.ReadChars(8)) != "WVQAVQHD")
throw new InvalidDataException("Invalid vqa (not WVQAVQHD)"); throw new InvalidDataException("Invalid vqa (not WVQAVQHD)");
/* var len = */reader.ReadUInt32(); /* var length = */reader.ReadUInt32();
var version = reader.ReadUInt16(); var version = reader.ReadUInt16();
flags = reader.ReadUInt16(); flags = reader.ReadUInt16();
numFrames = reader.ReadUInt16(); numFrames = reader.ReadUInt16();
@@ -65,7 +66,7 @@ namespace OpenRA.FileFormats
blockWidth = reader.ReadByte(); blockWidth = reader.ReadByte();
blockHeight = reader.ReadByte(); blockHeight = reader.ReadByte();
var framerate = reader.ReadByte(); framerate = reader.ReadByte();
cbParts = reader.ReadByte(); cbParts = reader.ReadByte();
blocks = new int2(width / blockWidth, height / blockHeight); blocks = new int2(width / blockWidth, height / blockHeight);
@@ -74,7 +75,7 @@ namespace OpenRA.FileFormats
/*var unknown1 = */reader.ReadUInt16(); /*var unknown1 = */reader.ReadUInt16();
/*var unknown2 = */reader.ReadUInt32(); /*var unknown2 = */reader.ReadUInt32();
cbf = new byte[8*blocks.X*blocks.Y]; cbf = new byte[width*height];
palette = new Color[numColors]; palette = new Color[numColors];
framedata = new byte[2*blocks.X*blocks.Y]; framedata = new byte[2*blocks.X*blocks.Y];
@@ -86,15 +87,6 @@ namespace OpenRA.FileFormats
/*var unknown3 = */reader.ReadChars(14); /*var unknown3 = */reader.ReadChars(14);
Console.WriteLine("FORM Info");
Console.WriteLine("\tVersion: {0}",version);
Console.WriteLine("\tFlags: {0}",flags);
Console.WriteLine("\tFrames: {0}",numFrames);
Console.WriteLine("\tFramerate: {0}",framerate);
Console.WriteLine("\tSize: {0}x{1}",width,height);
Console.WriteLine("\tBlocksize: {0}x{1}",blockWidth,blockHeight);
Console.WriteLine("\tAudio: {0}hz, {1} channel(s), {2} bit",freq, channels, bits);
// Decode FINF chunk // Decode FINF chunk
if (new String(reader.ReadChars(4)) != "FINF") if (new String(reader.ReadChars(4)) != "FINF")
throw new InvalidDataException("Invalid vqa (invalid FINF section)"); throw new InvalidDataException("Invalid vqa (invalid FINF section)");
@@ -117,7 +109,7 @@ namespace OpenRA.FileFormats
} }
public void AdvanceFrame() public void AdvanceFrame()
{ {
// Seek to the start of the frame // Seek to the start of the frame
stream.Seek(frames[currentFrame], SeekOrigin.Begin); stream.Seek(frames[currentFrame], SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream); BinaryReader reader = new BinaryReader(stream);
@@ -144,15 +136,14 @@ namespace OpenRA.FileFormats
// Chunks are aligned on even bytes; advance by a byte if the next one is null // Chunks are aligned on even bytes; advance by a byte if the next one is null
if (reader.PeekChar() == 0) reader.ReadByte(); if (reader.PeekChar() == 0) reader.ReadByte();
} }
currentFrame++; if (++currentFrame == numFrames)
currentFrame = 0;
} }
public Bitmap FrameData() public void FrameData(ref Bitmap frame)
{ {
Bitmap frame = new Bitmap(width, height);
var bitmapData = frame.LockBits(new Rectangle(0, 0, width, height), var bitmapData = frame.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe unsafe
{ {
int* c = (int*)bitmapData.Scan0; int* c = (int*)bitmapData.Scan0;
@@ -172,7 +163,6 @@ namespace OpenRA.FileFormats
} }
} }
frame.UnlockBits(bitmapData); frame.UnlockBits(bitmapData);
return frame;
} }
// VQA Frame // VQA Frame

View File

@@ -503,15 +503,7 @@ namespace OpenRA
// Load the default mod to access required files // Load the default mod to access required files
LoadModPackages(); LoadModPackages();
var video = new VqaReader(FileSystem.Open("crontest.vqa"));
video.FrameData().Save("test-0.bmp");
for (int i = 1; i < video.numFrames; i++)
{
video.AdvanceFrame();
video.FrameData().Save("test-{0}.bmp".F(i));
}
Renderer.SheetSize = Settings.SheetSize; Renderer.SheetSize = Settings.SheetSize;
var resolution = GetResolution(settings, Game.Settings.WindowMode); var resolution = GetResolution(settings, Game.Settings.WindowMode);

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -221,6 +221,7 @@
<Compile Include="Traits\Health.cs" /> <Compile Include="Traits\Health.cs" />
<Compile Include="Traits\RepairableBuilding.cs" /> <Compile Include="Traits\RepairableBuilding.cs" />
<Compile Include="Traits\Activities\Drag.cs" /> <Compile Include="Traits\Activities\Drag.cs" />
<Compile Include="Widgets\VqaPlayerWidget.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -0,0 +1,62 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
using OpenRA.Widgets;
using OpenRA.FileFormats;
namespace OpenRA.Widgets
{
public class VqaPlayerWidget : Widget
{
public string Video = "";
float timestep;
Sprite videoSprite;
Bitmap videoFrame;
VqaReader video = null;
public void LoadVideo(string filename)
{
video = new VqaReader(FileSystem.Open(filename));
timestep = 1e3f/video.framerate;
var size = OpenRA.Graphics.Util.NextPowerOf2(Math.Max(video.width, video.height));
videoFrame = new Bitmap(size,size);
video.FrameData(ref videoFrame);
videoSprite = new Sprite(new Sheet(new Size(size,size)), new Rectangle( 0, 0, video.width, video.height ), TextureChannel.Alpha);
videoSprite.sheet.Texture.SetData(videoFrame);
}
int lastTime;
public override void DrawInner(World world)
{
if (video == null)
LoadVideo(Video);
int t = Environment.TickCount;
int dt = t - lastTime;
if (dt > timestep)
{
lastTime = t;
video.AdvanceFrame();
video.FrameData(ref videoFrame);
videoSprite.sheet.Texture.SetData(videoFrame);
}
Game.Renderer.RgbaSpriteRenderer.DrawSprite(videoSprite, new int2(RenderBounds.X,RenderBounds.Y), "chrome");
}
}
}

View File

@@ -1,5 +1,11 @@
Container@ROOT: Container@ROOT:
Children: Children:
VqaPlayer:
X:WINDOW_RIGHT - 400
Y:WINDOW_BOTTOM - 200
Width:200
Height:200
Video:obel.vqa
Background@MAINMENU_BG: Background@MAINMENU_BG:
Id:MAINMENU_BG Id:MAINMENU_BG
X:(WINDOW_RIGHT - WIDTH)/2 X:(WINDOW_RIGHT - WIDTH)/2

View File

@@ -1,3 +1,9 @@
VqaPlayer:
X:WINDOW_RIGHT - 400
Y:WINDOW_BOTTOM - 200
Width:200
Height:200
Video:aagun.vqa
Background@MAINMENU_BG: Background@MAINMENU_BG:
Id:MAINMENU_BG Id:MAINMENU_BG
X:(WINDOW_RIGHT - WIDTH)/2 X:(WINDOW_RIGHT - WIDTH)/2