Jump to content
  • Advertisement
Sign in to follow this  
web_scripter

[.net] Sprites

This topic is 4809 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

I have a sprite and it loads part of a texture. The texture has the object i'm trying to draw, so a gun or a bullet. Also, the texture has a black background. when i draw the sprite it draws the gun and the black background. I need to know how to remove the black background. (i'm using directx 9.0 (june update) and C#)

Share this post


Link to post
Share on other sites
Advertisement
Removing the background can be done in several ways.
1. Use colorkeying
2. Use alpha supporting bitmaps

Both ways need you to tell us more on how you are loading the textures.

The fact that it is loading a part of the texture make me think that you are using a way of loading the texture that requires the texture to have a width and height of a power of two.

Show us your code and we'll take a look.

Cheers

BTW: Don't be shy in giving information; it'll give you better answers (being polite will also help)

Share this post


Link to post
Share on other sites
Here is my code:



Sprite spr;
Texture tex;
Device device;
public bool InitializeGraphics()
{
try
{
PresentParameters presParams = new PresentParameters();
presParams.Windowed = true;
presParams.SwapEffect = SwapEffect.Discard;
device = new Device( 0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presParams );
device.DeviceReset += new EventHandler( device_DeviceReset );

spr = new Sprite(device);
tex = new Texture(device,new Bitmap(Application.StartupPath + "//IsoMap_Tiles.gif"), 0, Pool.Managed);
return true;
}
catch( DirectXException ) { return false; }
}

void device_DeviceReset( object sender, EventArgs e )
{
Device dev = (Device)sender;
dev.RenderState.CullMode = Cull.None;
dev.RenderState.Clipping = true;
dev.RenderState.Lighting = false;
}
public void Render()
{
if( device == null )
return;
if( pause )
return;
device.Clear( ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0 );
SetupMatrices();
device.BeginScene();
spr.Begin(SpriteFlags.None);
spr.Draw2D(tex,new Rectangle(t1*64,0,64,64),new Rectangle(0,0,64,64),new Point(400 - 30,303 + 20),Color.White.ToArgb());
spr.End();
device.EndScene();
device.Present();
this.Invalidate();
}
private void SetupMatrices()
{
device.Transform.World = Matrix.RotationYawPitchRoll( 0, 0, 0 );
device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f + CameraX, 0.0f + CameraY, -1.0f ), new Vector3( 0.0f + CameraX, 0.0f + CameraY, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );
device.Transform.Projection = Matrix.OrthoLH( 10.0f, 10.0f, 1.0f, 100.0f );
}





Hope that helps.

Also, I tried animating the sprite and it works when I start the program with debugging, but it does not show anything when I start without debugging.
Any ideas on why this is happening and how i can fix it.

Thanks for your help.

Share this post


Link to post
Share on other sites
I've figured out the background problem.

instead of

tex = new Texture(device,new Bitmap(Application.StartupPath + "//IsoMap_Tiles.gif"), 0, Pool.Managed);




i replaced that with

tex = TextureLoader.FromFile(device, "IsoMap_Tiles.bmp", 0, 0, 0, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, Color.FromArgb(255,255,0,0).ToArgb());



and

changed the render function to this

if( device == null )
return;
if( pause )
return;
device.Clear( ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0 );
SetupMatrices();
device.BeginScene();

device.RenderState.AlphaBlendEnable = true;
spr.Begin(SpriteFlags.AlphaBlend);
//spr.SetWorldViewLH(device.Transform.World,device.Transform.View);
spr.Draw2D(tex,new Rectangle(t1*64,0,64,64),new Rectangle(0,0,64,64),new Point(400-30,303+20),Color.White.ToArgb());
//spr.Draw(tex,new Rectangle(0,0,50,50),new Vector3(1,1,0),new Vector3(0,0,0),Color.White.ToArgb());
spr.End();
device.EndScene();
device.Present();
this.Invalidate();





So now it works perfectly, but i still have the problem of the sprite not rendering when i run the program with out debugging. I would appreciate any help to fix this problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!