Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Code_X

Need help with ID3DXSprite and C#

This topic is 5272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi first post here.. and i feel kinda sorry that it`s to ask a kinda dumb question but oh well.. thats what forums are for! i am trying to use directx9 and C# to make a game... i managed to initialize directx and even draw a 3d cube with some tutorials to teach me, but now, since i''m more into 2D stuff than 3D, i''d like to use the ID3DSprite everyone is talking about. Sounds like it is a lot faster than blitting and all.. the proble is i searched everywhere and couldnt find any tutorials examples etc to do it. i found some in c++ and even VB, but i just cant make it work on C# :/ if any of you could give me a small sample project to display D3DSprites i would be most thankful, a simple link to a tutorial or example would do... anything to let me learn, cuz i tried to figure it out for hours and i''m getting pretty frustrated. thanks a lot

Share this post


Link to post
Share on other sites
Advertisement
Not familiar with the C# implementation, but it goes something like this:

1.) Call the Sprite''s "Begin()" method.

2.) Call the sprite''s "Draw()" method, passing it an IDirect3DTexture9 (well, a pointer to one in c++...check the paramters in the sdk docs) for each sprite you want to draw.

3.) Call the sprite''s "End()" method. This renders the vertex buffer that it had been working with in the background to the screen.

Share this post


Link to post
Share on other sites
Well that''s the prob... I read the doc, but it''s all C++. I know all that, but i just cant figure out how to get an instance of the sprite in C#.

Share this post


Link to post
Share on other sites
quote:
Original post by Code_X
Well that''s the prob... I read the doc, but it''s all C++. I know all that, but i just cant figure out how to get an instance of the sprite in C#.


That''s not too hard...

Sprite mysprite = new Sprite(mydevice);

Best do this after creating your device. Remember that the Sprite object is just an interface for drawing sprites, not a sprite itself. You only need one.

Share this post


Link to post
Share on other sites
mmm thanks.. but since i want to use the ID3DXSprite, should i just create a new Sprite? or a D3DSprite? cuz according to the doc there is no relation between Sprite and ID3DXSprite :/ i`m confused >.<

Share this post


Link to post
Share on other sites
ID3DXSprite is the C++ name, Sprite is the managed directx (C# or vb.net) name. They are the same thing, an interface for drawing sprites.
There is a managed directx helpfile in the summer update SDK, it''s better to use that than the C++ helpfile.

Share this post


Link to post
Share on other sites
Are there any sources out there that have sample code for how to setup a sprite on the screen?

I too have been messing around with sprites today..

I have managed to create a sprite, load the texture too it..and call the draw function on it..but nothing shows on my screen. I assume it has something to do with my Transforms but im still learning and not exactly sure how to get it working.

Heres what i have so far..any advice would be appreciated..

According to the SDK when your giving it the Vector3's during the Draw that is the location of the sprite..im assuming this is based on Screen Pixels? ie 800x600 ..middle of the screen would be 400.0f, 300.0f..if this is the case..why do they use the Z axis in a 2d world?




using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace Sprites
{
/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : Form
{

Device device = null;
Sprite sprite = null;
Texture texture = null;
Rectangle rect;
PresentParameters pparam = new PresentParameters();

public Form1()
{
this.ClientSize = new System.Drawing.Size(800,600);

}
public void InitializeGraphics()
{
pparam.Windowed = true;
pparam.SwapEffect = SwapEffect.Discard;
device = new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,pparam);
sprite = new Sprite(device);
texture = TextureLoader.FromFile(device,"banana.bmp");
rect = new Rectangle(0,0,256,256);
}


public void Render()
{
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
sprite.Begin(SpriteFlags.Billboard);
sprite.Draw(texture,rect,new Vector3(0.0f,0.0f,0.0f),new Vector3(0.0f,0.0f,0.0f),System.Drawing.Color.White);
sprite.End();

}
protected override void OnPaint(PaintEventArgs e)
{
this.Render();
}

static void Main()
{

using(Form1 frm = new Form1())
{
frm.InitializeGraphics();
frm.Show();
frm.Render();
Application.Run(frm);
}
}
}
}


[edited by - Arsenic0 on June 10, 2004 2:39:59 AM]

[edited by - Arsenic0 on June 10, 2004 3:15:07 AM]

Share this post


Link to post
Share on other sites
Don''t know if your render loop is correct. If you override OnPaint you must invalidate the form''s canvas again after rendering.
Try the traditional while-true loop with an Application.DoEvents() instead of Application.Run().

Also, Billboards are 3D objects, a sprite in 3D space that always faces the camera. To properly use those you''d need to set a transformation matrix and projection matrix.
Try SpriteFlags.None to get started if you want 2D sprites on the screen.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!