4
\$\begingroup\$

I was thinking about dividing my game engine into 2 threads: render thread and update thread (I can't come up on how to divide update thread from physic thread at the moment).

That said, I have to duplicate all Sprite informations, what do I really need to represents a 2D Sprite graphically?

Here are my ideas (I'll mark with ? things that I'm not sure):

Vector2 Position
float Rotation ?
Vector2 Pivot ?
Rectangle TextureRectangle
Texture2D Texture
Vector2 ImageOrigin ? (is it tracked somewhere else?)

If you have any suggestion about using different types for datas, it's appreciated

Last part of the question: isn't this a lot of data to copy in a buffer?what should I really copy in the buffer?I'm following this tutorial:

http://www.sgtconker.com/2009/11/article-multi-threading-your-xna/3/

Thanks

UPDATE 1:

Newer values at the moment:

Vector2 Position
float Rotation
Vector2 Pivot
Rectangle TextureRectangle
Texture2D Texture
Color Color
byte Facing (can be left or right, I'll do it with an enum)

I re-read the tutorial, what I was doing wrong is not that I need to pass all those values, I need to pass only changed values as messages.

UPDATE 2:

Vector2 Position
float Rotation
Vector2 Pivot
Rectangle TextureRectangle
Texture2D Texture
Color Color
bool Flip
uint DrawOrder
Vector2 Scale
bool Visible ?

Mhhh, should Visibile be included?

\$\endgroup\$
3
  • \$\begingroup\$ You should put any code in a code box so it is more readable. \$\endgroup\$ Dec 9, 2010 at 21:00
  • \$\begingroup\$ @AttackingHobo: Sorry, I was sure to have done it! \$\endgroup\$ Dec 9, 2010 at 21:04
  • \$\begingroup\$ I might consider doing some kind of state driven drawing for my next game. I kind of like the idea any how. \$\endgroup\$ Dec 9, 2010 at 22:33

5 Answers 5

4
\$\begingroup\$

If you want to use that method of multi-threading, for each object in the game you ideally just want to the information that it needs to render.

  • Location
  • Rotation
  • Scale
  • Reference of image to draw for Sprite
  • Draw order
  • Anything the draw thread needs to know to draw.

You do not want to be copying the actual graphics back and forth, just references to data, and any small data that is unique to each one.

\$\endgroup\$
2
  • \$\begingroup\$ You are right but in that tutorial it explains that if I have to read data, I have to sync threads losing all the advantages that I can obtain by separating data, that's why I wrote this, I'm not sure what should I do so \$\endgroup\$ Dec 9, 2010 at 21:16
  • \$\begingroup\$ What? You don't have to sync the assets, they should be stored somewhere static. \$\endgroup\$ Dec 10, 2010 at 0:20
2
\$\begingroup\$

My question is why do you wanna to do that? :)

Separate draw and update into two threads is really important for games performance. But XNA does helps you in this. There are

protected override void Update(GameTime gameTime)
protected override void Draw(GameTime gameTime)

callbacks. And XNA calls them properly. As you are making some 2D game i'm pretty sure that you dont need multithreading performance benefit. But if you wanna to try multithreading... ok.

To your question. Data that you want to share between threads seems to be right. But maybe imageOrigin==pivot. And also you are propably not going to change textures every frame, they may be saved in both threads and changed only when needed.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the suggestion but I would like to use the multithreading approach for learning purposes \$\endgroup\$ Dec 9, 2010 at 22:46
  • \$\begingroup\$ Ok. Very well, thats the reason i understand and appriciate. Thumbs up. \$\endgroup\$
    – Notabene
    Dec 9, 2010 at 23:13
2
\$\begingroup\$

Responding to Update #1

Pivot

Are you planning on rotating the sprites around some point other than their center? If not then this seems like a duplicate of position.

Facing

Not sure what this is for although with your enum of Left/Right I'm assuming it's for backface culling? If you are just drawing a quad at a 2D point in space then you don't need to worry about which side you are on.

Draw Order

Your update 1 is still missing a draw order index, which AttackingHobo also mentioned. You'll want that to force some sprites to the top as well as keep them consistent when crossing over one another.

\$\endgroup\$
1
  • \$\begingroup\$ Pivot: yes, think about a sword. Facing: I was thinking about (it's an action side scroller game engine) facing left or right, the char can go left or right but the image should be mirrored based on this. Draw Order: You are totally right \$\endgroup\$ Dec 10, 2010 at 13:15
1
\$\begingroup\$

I think notabene is write in that the Pivot does equal ImageOrigin. The XNA draw call takes an origin that is the pivot for rotation and the image origin, they are one in the same.

Anyway, I would suggest adding a Color field for tinting and make sure that you allow the TextureRectangle to have some null value. (I think adding a "?" at the end of Rectangle would do the trick, but might cause boxing due to Nullable???) Probably set the TextureRectangle to new Rectangle(-1, -1, -1, -1) as a way to communicate that it's a null source rectangle.

Also, you should consider doing some benchmarking if your not completely sure if you need to do this, don't optimize needlessly. You could throw in a SpriteEffects enum field in there, but I doubt you'll need it.

\$\endgroup\$
2
  • \$\begingroup\$ Damn I forgot color!!! Thank you very much. I don't see the point for null rectangle, what will you use it for?I think textures will be spritesheets \$\endgroup\$ Dec 9, 2010 at 22:43
  • \$\begingroup\$ They might not be always sprite sheets. Just in the off chance, but yeah your right, they probably won't be like 99% of the time for game entities. \$\endgroup\$ Dec 11, 2010 at 3:32
0
\$\begingroup\$

Basically if you want total control over spritebatch, you'd probably want to use variables for ALL of its overloads, (with the exception of spriteEffect and rectangle unless you're doing a side-scrolling platformer) like so:

public class Character()
{

     Texture2D texture; (optional variable)

     Vector2 position;
     Vector2 origin;
     float rotation = 0f;
     float scale = 1.0f;
     float depth = 0;
     Color color = Color.White;
}

i.e

   spriteBatch.Draw(character.texture, character.position, null, character.color, character.rotation, character.origin, character.scale, SpriteEffects.None, character.depth);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .