Results 1 to 8 of 8

Thread: Pascal and XNA - Loading and displaying an Image

  1. #1
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Pascal and XNA - Loading and displaying an Image

    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.



    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

    Code:
    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.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  2. #2
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Pascal and XNA - Loading and displaying an Image

    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
    NecroSOFT - End of line -

  3. #3

    Pascal and XNA - Loading and displaying an Image

    Hi cairnswm,
    I tried following the first tutorial that shows how to load a 3D textured object , but could not get the code...
    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.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  4. #4

    Pascal and XNA - Loading and displaying an Image

    This may be a silly question but is there any reason you could not use Turbo Delphi for .Net for this?

  5. #5

    Pascal and XNA - Loading and displaying an Image

    Quote Originally Posted by bigsofty
    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.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  6. #6
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Pascal and XNA - Loading and displaying an Image

    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.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  7. #7

    Pascal and XNA - Loading and displaying an Image

    Quote Originally Posted by cairnswm
    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.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  8. #8
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Pascal and XNA - Loading and displaying an Image

    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.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •