Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: C#: Possible To Use a Sort of Sprite Index???  (Read 1820 times)

0 Members and 1 Guest are viewing this topic.
C#: Possible To Use a Sort of Sprite Index???
« on: October 15, 2006, 11:14:37 am »
  • Flash Software Studios
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 71
I need help with sprites. I was wondering whether it was possible to just add the gif of link running down say, and just using index's like the sprite index in GM to get the frame i want other than initialising 600 different parts of the animation as i can see this becoming rather laggy... Thanks to anyone who can point me in the right direction!

-flashGX
Logged
 
  • http://www.myspace.com/kristruman
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #1 on: October 15, 2006, 11:29:33 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
What API are you using to render GDI? DirectX? OpenGL? SDL? Its pretty hard to help without knowing.
Logged
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #2 on: October 15, 2006, 11:33:43 am »
  • Flash Software Studios
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 71
DirectX 9.0c
Logged
 
  • http://www.myspace.com/kristruman
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #3 on: October 15, 2006, 11:45:22 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Right the way I would do it (although there are other ways, I choose this as its easier when creating a abstracted rendering layer).

If your using a 'sprite strip' as I think you call it in GM, you would do something similar to this.

1) Load the entire pixel data into a 2 dimensions array.
2) Create an array with a size equaling the amount of frames in your image.
3) Split your image up into seperate 2 dimensional arrays (look at example 1), one for each frame, then create a DX9 texture from that data and place it into your frame array (created in step 2).
4) Thats it really, to access the texture of a specific frame you would just index the frame array (created in step 2) with the frame you want to access.

If your using a gif file its more or less the same as before except the image has already been split up for you so you can skip step 1.

Example 1:
Code: [Select]
int framesH = (imageWidth / (frameWidth + frameHSeperation));
int framesV = (imageHeight / (frameHeight + frameVSeperation));
int frameCount = framesH * framesV;
for (int i = 0; i < frameCount; i++)
{
int frameX = (i % framesH) * frameWidth;
int frameY = (i / framesH) * frameHeight;
//
// Just grab the data at frameX,frameY (of frameWidth x frameHeight)
// of your image and place it into a new dx9 texture representing your frame.
           // Then place that texture into the frame array.
//
}

If your using the TextureLoader class for loading in your image you will have to extract the data from your texture first, I'm pretty sure there is a method in the Texture class for this, just check.

Thats the way I would do it at any rate there are a few other easier ones, such as having each frame as a seperate image and loading them into a frame array, or using the DX9Sprite class which I am pretty sure has a method to only render a section of an image (you can get the position of the section with the code in example 1).
« Last Edit: October 15, 2006, 11:49:53 am by Helios »
Logged
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #4 on: October 15, 2006, 05:58:32 pm »
  • The Broken King
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1259
In Direct X, there's an interface to the sprite class, if you're looking for a quick and easy solution, than you can use that.

Just line up your images like a sprite strip, as Helios said. Then create a class for a sprite. That class should have:

->A pointer to a texture
->A pointer to a sprite interface (if you choose to use the sprite interface)
->A number representing the amount of frames
->A number representing the width of each image
->A number representing the height of each image
->A bool indication if the sprite strip is arranged horizontally or vertically
->Two numbers indicating the top left corner of the sprite
That would hold all the information regarding your sprite, but so that you don't have to have redundant data when you have multiple objects using the same sprite, you should have a seperate 'object' class that also has
->A pointer to a sprite
->A pointer to a directx device (or use some sort of graphics manager to handle the rendering)
->A number representing what frame of the sprite you're ine
->A number representing the image speed
->Numbers to represent the x and y position

Then, when you want to render your sprite, render starting from the top left corner, then if it's horizontal, translate the start point by (width * curFrame (or curFrame - 1 if you don't start at 0), 0), or if it's vertical, translate the start point by (0, height * curFrame (or curFrame - 1 if you don't start at 0)). Then make it render the portion of the texture between the start point and (startx + width, starty + height).
Logged
  • Broken Kings [Temp Site]
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #5 on: October 15, 2006, 06:04:27 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Then, when you want to render your sprite, render starting from the top left corner, then if it's horizontal, translate the start point by (width * curFrame (or curFrame - 1 if you don't start at 0), 0), or if it's vertical, translate the start point by (0, height * curFrame (or curFrame - 1 if you don't start at 0)). Then make it render the portion of the texture between the start point and (startx + width, starty + height).
Or if you want it to work regardless of which direction it is aligned in, you can use the following formular to work out the offset to start rendering from.

Code: [Select]
int frameX = (frame % framesHorizontal) * frameWidth;
int frameY = (frame / framesHorizontal) * frameHeight;

Where frame is the frame you wish to render, framesHorizontal is the amount of frames that can be fitted in the image horizontally (you can work this out as imageWidth / frameWidth), and frameWidth / frameHeight are the dimensions of each frame.
Logged
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #6 on: October 16, 2006, 09:18:16 am »
  • Flash Software Studios
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 71
Ok by sprite strip u mean being able to see all the graphics in a line next to each other i think, whereas im using a gif animation of it. Does this work the same??
Logged
 
  • http://www.myspace.com/kristruman
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #7 on: October 16, 2006, 10:51:39 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Ok by sprite strip u mean being able to see all the graphics in a line next to each other i think, whereas im using a gif animation of it. Does this work the same??

Well as far as I know .gif formats are not nativly supported by the DX9 texture loader, so you will have to write your own, but its generally the same theory with what ever format you use.
But its a good idea to stear clear of the .gif format anyway as its more or less the spawn of satan when it comes to game development (just because GM uses it, dosen't make it a good format), .use png / .tga sprite strips instead.

Oh and by sprite strip I mean [look at attachment]
Logged
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #8 on: October 16, 2006, 10:22:36 pm »
  • Flash Software Studios
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 71
Ok i have all my sprite strips. Now for link the image sizes are different depending on the direction he is running. Does that matter or do i just make a different class for each direction??
Logged
 
  • http://www.myspace.com/kristruman
Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #9 on: October 16, 2006, 10:32:33 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Ok i have all my sprite strips. Now for link the image sizes are different depending on the direction he is running. Does that matter or do i just make a different class for each direction??

Well if you've been following my advice you shouldn't have any problem so long as each direction is in its own sprite strip.
Logged

gm112

Re: C#: Possible To Use a Sort of Sprite Index??...
« Reply #10 on: October 16, 2006, 10:46:37 pm »
Helios beat me to a post I was going to help out on... oh well! =P nice to know theres other C# programmers out there.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.233 seconds with 60 queries.

anything