hack hack hack
git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1165 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
170
OpenRa.Game/Actor.cs
Normal file
170
OpenRa.Game/Actor.cs
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Drawing;
|
||||||
|
using OpenRa.FileFormats;
|
||||||
|
using BluntDirectX.Direct3D;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
|
namespace OpenRa.Game
|
||||||
|
{
|
||||||
|
class Actor
|
||||||
|
{
|
||||||
|
public PointF location;
|
||||||
|
public SheetRectangle<Sheet>[] currentImages;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Tree : Actor
|
||||||
|
{
|
||||||
|
public Tree( TreeReference r, TreeRenderer renderer, Map map )
|
||||||
|
{
|
||||||
|
location = new PointF(24 * (r.X - map.XOffset), 24 * (r.Y - map.YOffset));
|
||||||
|
currentImages = new SheetRectangle<Sheet>[] { renderer.GetImage( r.Image ) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TreeRenderer
|
||||||
|
{
|
||||||
|
Dictionary<string, SheetRectangle<Sheet>> trees = new Dictionary<string, SheetRectangle<Sheet>>();
|
||||||
|
|
||||||
|
public readonly Sheet sh;
|
||||||
|
|
||||||
|
public TreeRenderer(GraphicsDevice device, Map map, Package package, Palette pal)
|
||||||
|
{
|
||||||
|
Size pageSize = new Size( 1024, 512 );
|
||||||
|
List<Sheet> sheets = new List<Sheet>();
|
||||||
|
|
||||||
|
Provider<Sheet> sheetProvider = delegate
|
||||||
|
{
|
||||||
|
Sheet sheet = new Sheet(new Bitmap(pageSize.Width, pageSize.Height));
|
||||||
|
sheets.Add(sheet);
|
||||||
|
return sheet;
|
||||||
|
};
|
||||||
|
|
||||||
|
TileSheetBuilder<Sheet> builder = new TileSheetBuilder<Sheet>(pageSize, sheetProvider);
|
||||||
|
|
||||||
|
foreach (TreeReference r in map.Trees)
|
||||||
|
{
|
||||||
|
if (trees.ContainsKey( r.Image ))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ShpReader reader = new ShpReader(package.GetContent(r.Image + "." + map.Theater.Substring(0,3)));
|
||||||
|
Bitmap bitmap = BitmapBuilder.FromBytes(reader[0].Image, reader.Width, reader.Height, pal);
|
||||||
|
|
||||||
|
SheetRectangle<Sheet> rect = builder.AddImage(bitmap.Size);
|
||||||
|
using (Graphics g = Graphics.FromImage(rect.sheet.bitmap))
|
||||||
|
g.DrawImage(bitmap, rect.origin);
|
||||||
|
|
||||||
|
trees.Add(r.Image, rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Sheet sheet in sheets)
|
||||||
|
sheet.LoadTexture(device);
|
||||||
|
|
||||||
|
sh = sheets[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public SheetRectangle<Sheet> GetImage( string tree )
|
||||||
|
{
|
||||||
|
return trees[tree];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class World
|
||||||
|
{
|
||||||
|
const int spritesPerBatch = 1024;
|
||||||
|
|
||||||
|
List<Actor> actors = new List<Actor>();
|
||||||
|
FvfVertexBuffer<Vertex> vb;
|
||||||
|
IndexBuffer ib;
|
||||||
|
GraphicsDevice device;
|
||||||
|
|
||||||
|
public World(GraphicsDevice device)
|
||||||
|
{
|
||||||
|
this.device = device;
|
||||||
|
this.vb = new FvfVertexBuffer<Vertex>(device, spritesPerBatch * 4, Vertex.Format);
|
||||||
|
this.ib = new IndexBuffer(device, spritesPerBatch * 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(Actor a)
|
||||||
|
{
|
||||||
|
actors.Add(a); //todo: protect from concurrent modification
|
||||||
|
}
|
||||||
|
|
||||||
|
// assumption: there is only one sheet!
|
||||||
|
// some noob needs to fix this!
|
||||||
|
|
||||||
|
// assumption: its not going to hurt, to draw *all* units.
|
||||||
|
// in reality, 500 tanks is going to hurt our perf.
|
||||||
|
|
||||||
|
public void Draw()
|
||||||
|
{
|
||||||
|
int sprites = 0;
|
||||||
|
List<Vertex> vertices = new List<Vertex>();
|
||||||
|
List<ushort> indices = new List<ushort>();
|
||||||
|
|
||||||
|
foreach (Actor a in actors)
|
||||||
|
{
|
||||||
|
if (a.currentImages == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
foreach (SheetRectangle<Sheet> image in a.currentImages)
|
||||||
|
{
|
||||||
|
int offset = vertices.Count;
|
||||||
|
vertices.Add(new Vertex(a.location.X, a.location.Y, 0, U(image, 0), V(image, 0)));
|
||||||
|
vertices.Add(new Vertex(a.location.X + image.size.Width, a.location.Y, 0, U(image, 1), V(image, 0)));
|
||||||
|
vertices.Add(new Vertex(a.location.X, a.location.Y + image.size.Height, 0, U(image, 0), V(image, 1)));
|
||||||
|
vertices.Add(new Vertex(a.location.X + image.size.Width, a.location.Y + image.size.Height, 0, U(image, 1), V(image, 1)));
|
||||||
|
|
||||||
|
indices.Add((ushort)offset);
|
||||||
|
indices.Add((ushort)(offset + 1));
|
||||||
|
indices.Add((ushort)(offset + 2));
|
||||||
|
|
||||||
|
indices.Add((ushort)(offset + 1));
|
||||||
|
indices.Add((ushort)(offset + 3));
|
||||||
|
indices.Add((ushort)(offset + 2));
|
||||||
|
|
||||||
|
if (++sprites >= spritesPerBatch)
|
||||||
|
{
|
||||||
|
DrawBatch(vertices, indices);
|
||||||
|
|
||||||
|
vertices = new List<Vertex>();
|
||||||
|
indices = new List<ushort>();
|
||||||
|
sprites = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sprites > 0)
|
||||||
|
DrawBatch(vertices, indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DrawBatch(List<Vertex> vertices, List<ushort> indices)
|
||||||
|
{
|
||||||
|
vb.SetData(vertices.ToArray());
|
||||||
|
ib.SetData(indices.ToArray());
|
||||||
|
|
||||||
|
vb.Bind(0);
|
||||||
|
ib.Bind();
|
||||||
|
|
||||||
|
device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
|
||||||
|
vertices.Count, indices.Count / 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float U(SheetRectangle<Sheet> s, float u)
|
||||||
|
{
|
||||||
|
float u0 = (float)(s.origin.X + 0.5f) / (float)s.sheet.bitmap.Width;
|
||||||
|
float u1 = (float)(s.origin.X + s.size.Width) / (float)s.sheet.bitmap.Width;
|
||||||
|
|
||||||
|
return (u > 0) ? u1 : u0;// (1 - u) * u0 + u * u1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float V(SheetRectangle<Sheet> s, float v)
|
||||||
|
{
|
||||||
|
float v0 = (float)(s.origin.Y + 0.5f) / (float)s.sheet.bitmap.Height;
|
||||||
|
float v1 = (float)(s.origin.Y + s.size.Height) / (float)s.sheet.bitmap.Height;
|
||||||
|
|
||||||
|
return (v > 0) ? v1 : v0;// return (1 - v) * v0 + v * v1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@ namespace OpenRa.Game
|
|||||||
Package TileMix;
|
Package TileMix;
|
||||||
string TileSuffix;
|
string TileSuffix;
|
||||||
|
|
||||||
const string mapName = "scg11eb.ini";
|
const string mapName = "scm12ea.ini";
|
||||||
const string shaderName = "diffuse.fx";
|
const string shaderName = "diffuse.fx";
|
||||||
|
|
||||||
Dictionary<TileReference, SheetRectangle<Sheet>> tileMapping =
|
Dictionary<TileReference, SheetRectangle<Sheet>> tileMapping =
|
||||||
@@ -33,6 +33,9 @@ namespace OpenRa.Game
|
|||||||
Effect effect;
|
Effect effect;
|
||||||
IntPtr texture, scroll, r1h, r2h;
|
IntPtr texture, scroll, r1h, r2h;
|
||||||
|
|
||||||
|
World world;
|
||||||
|
TreeRenderer treeRenderer;
|
||||||
|
|
||||||
void LoadTextures()
|
void LoadTextures()
|
||||||
{
|
{
|
||||||
List<Sheet> tempSheets = new List<Sheet>();
|
List<Sheet> tempSheets = new List<Sheet>();
|
||||||
@@ -66,6 +69,12 @@ namespace OpenRa.Game
|
|||||||
|
|
||||||
foreach (Sheet s in tempSheets)
|
foreach (Sheet s in tempSheets)
|
||||||
s.LoadTexture(device);
|
s.LoadTexture(device);
|
||||||
|
|
||||||
|
world = new World(device);
|
||||||
|
treeRenderer = new TreeRenderer(device, map, TileMix, pal);
|
||||||
|
|
||||||
|
foreach (TreeReference treeReference in map.Trees)
|
||||||
|
world.Add(new Tree(treeReference, treeRenderer, map));
|
||||||
}
|
}
|
||||||
|
|
||||||
float U(SheetRectangle<Sheet> s, float u)
|
float U(SheetRectangle<Sheet> s, float u)
|
||||||
@@ -127,7 +136,7 @@ namespace OpenRa.Game
|
|||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
ClientSize = new Size(1280, 800);
|
ClientSize = new Size(1280,800);
|
||||||
|
|
||||||
Visible = true;
|
Visible = true;
|
||||||
|
|
||||||
@@ -249,6 +258,18 @@ namespace OpenRa.Game
|
|||||||
effect.EndPass();
|
effect.EndPass();
|
||||||
effect.End();
|
effect.End();
|
||||||
|
|
||||||
|
effect.Quality = ShaderQuality.High;
|
||||||
|
effect.Begin();
|
||||||
|
effect.BeginPass(0);
|
||||||
|
|
||||||
|
effect.SetTexture(texture, treeRenderer.sh.texture);
|
||||||
|
effect.Commit();
|
||||||
|
|
||||||
|
world.Draw();
|
||||||
|
|
||||||
|
effect.EndPass();
|
||||||
|
effect.End();
|
||||||
|
|
||||||
device.End();
|
device.End();
|
||||||
device.Present();
|
device.Present();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Actor.cs" />
|
||||||
<Compile Include="Clock.cs" />
|
<Compile Include="Clock.cs" />
|
||||||
<Compile Include="MainWindow.cs">
|
<Compile Include="MainWindow.cs">
|
||||||
<SubType>Form</SubType>
|
<SubType>Form</SubType>
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace OpenRa.Game
|
|||||||
|
|
||||||
public void LoadTexture(GraphicsDevice device)
|
public void LoadTexture(GraphicsDevice device)
|
||||||
{
|
{
|
||||||
string tempFile = string.Format("../../../block-cache-{0}.bmp", suffix++);
|
string tempFile = string.Format("../../../block-cache-{0}.png", suffix++);
|
||||||
bitmap.Save(tempFile);
|
bitmap.Save(tempFile);
|
||||||
|
|
||||||
using( Stream s = File.OpenRead(tempFile) )
|
using( Stream s = File.OpenRead(tempFile) )
|
||||||
|
|||||||
14
diffuse.fx
14
diffuse.fx
@@ -56,3 +56,17 @@ technique low_quality {
|
|||||||
PixelShader = compile ps_2_0 Simple_fp();
|
PixelShader = compile ps_2_0 Simple_fp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
technique high_quality {
|
||||||
|
pass p0 {
|
||||||
|
AlphaBlendEnable = true;
|
||||||
|
ZWriteEnable = true;
|
||||||
|
ZEnable = false;
|
||||||
|
CullMode = None;
|
||||||
|
VertexShader = compile vs_2_0 Simple_vp();
|
||||||
|
PixelShader = compile ps_2_0 Simple_fp();
|
||||||
|
|
||||||
|
SrcBlend = SrcAlpha;
|
||||||
|
DestBlend = InvSrcAlpha;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user