git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1119 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
chrisf
2007-07-07 03:29:45 +00:00
parent ac94527e91
commit 34fc937db2
5 changed files with 117 additions and 0 deletions

Binary file not shown.

View File

@@ -6,6 +6,7 @@ using System.Drawing;
using BluntDirectX.Direct3D;
using OpenRa.FileFormats;
using System.IO;
using System.Runtime.InteropServices;
namespace OpenRa.Game
{
@@ -21,6 +22,58 @@ namespace OpenRa.Game
const string mapName = "scm12ea.ini";
Dictionary<TileReference, SheetRectangle<Sheet>> tileMapping =
new Dictionary<TileReference, SheetRectangle<Sheet>>();
FvfVertexBuffer<Vertex> vertexBuffer;
IndexBuffer indexBuffer;
void LoadTextures()
{
List<Sheet> tempSheets = new List<Sheet>();
Provider<Sheet> sheetProvider = delegate
{
Sheet t = new Sheet( new Bitmap(256, 256));
tempSheets.Add(t);
return t;
};
TileSheetBuilder<Sheet> builder = new TileSheetBuilder<Sheet>( new Size(256,256), sheetProvider );
for( int i = 0; i < 128; i++ )
for (int j = 0; j < 128; j++)
{
TileReference tileRef = map.MapTiles[i, j];
if (!tileMapping.ContainsKey(tileRef))
{
SheetRectangle<Sheet> rect = builder.AddImage(new Size(24, 24));
Bitmap srcImage = tileSet.tiles[ tileRef.tile ].GetTile( tileRef.image );
using (Graphics g = Graphics.FromImage(rect.sheet.bitmap))
g.DrawImage(srcImage, rect.origin);
tileMapping.Add(tileRef, rect);
}
}
foreach (Sheet s in tempSheets)
s.LoadTexture(device);
}
void LoadVertexBuffer()
{
Vertex[] vertices = new Vertex[4 * map.Width * map.Height];
vertexBuffer = new FvfVertexBuffer<Vertex>(device, vertices.Length, Vertex.Format);
vertexBuffer.SetData(vertices);
ushort[] indices = new ushort[6 * map.Width * map.Height];
indexBuffer = new IndexBuffer(device, indices.Length);
indexBuffer.SetData(indices);
}
public MainWindow()
{
ClientSize = new Size(640, 480);
@@ -35,6 +88,9 @@ namespace OpenRa.Game
Text = string.Format("OpenRA - {0} - {1}", map.Title, mapName);
tileSet = LoadTileSet(map);
LoadTextures();
LoadVertexBuffer();
}
internal void Run()
@@ -53,6 +109,11 @@ namespace OpenRa.Game
// render something :)
//vertexBuffer.Bind(0);
//indexBuffer.Bind();
//device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 2 * map.Width * map.Height);
device.End();
device.Present();
}
@@ -81,5 +142,7 @@ namespace OpenRa.Game
}
return new TileSet(TileMix, TileSuffix, pal);
}
}
}

View File

@@ -44,6 +44,8 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sheet.cs" />
<Compile Include="Vertex.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRa.FileFormats\OpenRa.FileFormats.csproj">

29
OpenRa.Game/Sheet.cs Normal file
View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using BluntDirectX.Direct3D;
using System.IO;
using System.Drawing.Imaging;
namespace OpenRa.Game
{
class Sheet
{
public readonly Bitmap bitmap;
public Texture texture;
public Sheet(Bitmap b) { bitmap = b; }
public void LoadTexture(GraphicsDevice device)
{
string tempFile = string.Format("../../../block-cache-{0}.bmp", suffix++);
bitmap.Save(tempFile);
using( Stream s = File.OpenRead(tempFile) )
texture = Texture.Create(s, device);
}
static int suffix = 0;
}
}

23
OpenRa.Game/Vertex.cs Normal file
View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using BluntDirectX.Direct3D;
namespace OpenRa.Game
{
[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public float x, y, z, u, v;
public Vertex(float x, float y, float z, float u, float v)
{
this.x = x; this.y = y; this.z = z;
this.u = u;
this.v = v;
}
public const VertexFormat Format = VertexFormat.Position | VertexFormat.Texture;
}
}