PDA

View Full Version : Pascal and XNA - Loading and displaying an Image



cairnswm
23-01-2007, 08:17 PM
I'm keeping this seperate from the base "Pascal and XNA" discussion as its effectivly the next step in making a game work.

XNA loads all resources through the ContentManager - this enables a single method of loading the game resources and makes it closely compatible with the XBox as the ContentManager is supported on the XBox360 as well.

In C# you add a graphic resource to the project - for example a PNG file. When the project is compiled a ".xnb" file is created for the resource. This .xnb file is the resource that needs to be distributed. Now I dont know how to compile the png/jpg to a .xnb but I can show how to load and display a xnb file.

http://www.cairnsgames.co.za/files/xnatemp.jpg

I created a C# project - added a resource, and then copied the xnb file to my Pascal XNA project directory. In the example below the cursor is displayed on the screen. The resource file is saved in the XNADemo directory as Cursor.xnb



namespace XNADemo;

interface

uses
Microsoft.Xna.Framework,
Microsoft.Xna.Framework.Content,
Microsoft.Xna.Framework.Audio,
Microsoft.Xna.Framework.Graphics,
Microsoft.Xna.Framework.Input,
Microsoft.Xna.Framework.Storage;

type
TXNAGame = class(Microsoft.Xna.Framework.Game)
private
graphics : GraphicsDeviceManager;

// these are the size of the output window, ignored on Xbox 360
preferredWindowWidth : integer;
preferredWindowHeight : integer;

content : ContentManager;
SpriteEngine : SpriteBatch;
T : Texture2D;
protected
method Initialize; override;
method BeginRun; override;
method LoadGraphicsContent(loadAllContent: Boolean); Override;
method Update( gameTime : Gametime ); override;
method BeginDraw : boolean; override;
method Draw( gameTime : Gametime ); override;
method EndDraw; override;
public
constructor;

// Entry point into this object.
class method Main;
end;

implementation

constructor TXNAGame;
begin
inherited;
preferredWindowWidth := 800;//1280;
preferredWindowHeight := 600;//720;

Self.graphics := new Microsoft.Xna.Framework.GraphicsDeviceManager(Self );
Self.graphics.PreferredBackBufferWidth := preferredWindowWidth;
Self.graphics.PreferredBackBufferHeight := preferredWindowHeight;
Content := new ContentManager(Services);
end;

class method TXNAGame.Main;
begin
try
// probably should not be instantiating an object within itself :).
with lGame := new TXNAGame() do
lGame.Run();
except
on E: Exception do
begin
// MessageBox.Show(E.Message);
end;
end;
end;

method TXNAGame.Initialize;
begin
// Your Initialize Code here
Window.Title := 'Pascal and XNA';

inherited;
end;

method TXNAGame.BeginRun;
begin
// Your BeginRun Code here
inherited;
end;

method TXNAGame.Update( gameTime : Gametime );
begin
// Your Update Code here

inherited Update( gameTime );
end;

method TXNAGame.LoadGraphicsContent(loadAllContent: Boolean);
Begin
SpriteEngine := new SpriteBatch(graphics.GraphicsDevice);
T &#58;= Content.Load<Texture2D>&#40;'Cursor'&#41;;
End;

method TXNAGame.BeginDraw &#58; boolean;
begin
if not &#40; inherited BeginDraw &#41; then
begin
result &#58;= false;
exit;
end;

// Your BeginDraw Code here

result &#58;= true;
end;

method TXNAGame.Draw&#40; gameTime &#58; Gametime &#41;;
Var
V &#58; Vector2;
begin
V &#58;= new Vector2&#40;100,100&#41;;
graphics.GraphicsDevice.Clear&#40; Color.CornflowerBlue &#41;;
SpriteEngine.Begin&#40;&#41;;
SpriteEngine.Draw&#40;T,V,Color.White&#41;;
SpriteEngine.End&#40;&#41;;

inherited Draw&#40; gameTime &#41;;

// Your Draw Code here

end;

method TXNAGame.EndDraw;
begin
// Your EndDraw Code here

inherited;
end;

end.


Note that xnd files are VERY big! A 66kb jpg file makes a 4mb xnb file that compresses in zip format to 1mb. Not exactly download friendly.

NecroDOME
23-01-2007, 09:35 PM
Therefor you can use procedural textures. Never done it before, but it's very efficient on disk usage! DirectX supports this feature, found it somewhere on MSDN.

EDIT: http://en.wikipedia.org/wiki/Procedural_textures

savage
23-01-2007, 10:06 PM
Hi cairnswm,
I tried following the first tutorial that shows how to load a 3D textured object , but could not get the code...


myModel &#58;= content.Load<Model>&#40;'Content\Models\p1_wedge'&#41;;


working. The game compiles but just bombs out when it runs, on the line above. If I comment out that line it runs but obviously nothing is shown. Any ideas?

I suppose it would be cool to port those 3 tutorials and submit them to the Chrome team so that they can at least say that it all works with XNA.

bigsofty
24-01-2007, 03:03 AM
This may be a silly question but is there any reason you could not use Turbo Delphi for .Net for this?

savage
24-01-2007, 06:39 AM
This may be a silly question but is there any reason you could not use Turbo Delphi for .Net for this?

I did not think that Turbo Delphi supports .NET 2.0.

cairnswm
24-01-2007, 07:07 AM
Code:
myModel := content.Load<Model>('Content\Models\p1_wedge');


Has the model already been compiled to xnb format?
Is you xnd file in the 'Content\Models' folder?

I seemed to have some issues loading resources that were not in the project directory - ie in the same dir as the exe.

savage
24-01-2007, 10:38 AM
Code:
myModel := content.Load<Model>('Content\Models\p1_wedge');


Has the model already been compiled to xnb format?
Is you xnd file in the 'Content\Models' folder?

I seemed to have some issues loading resources that were not in the project directory - ie in the same dir as the exe.

p1_wedge is from the SpaceWar demo ( great little example of XNA ) and I had already copied it and created the sub-directory structure under the XNADemo folder and also added the Textures sub-dir as well. But still no joy.

cairnswm
24-01-2007, 10:44 AM
Have you tried putting it int he same dir as the exe and then loading it?(Obviously only by name then) Like I said - I had some problems until I put it in the same dir as the EXE.