5 Replies - 4612 Views - Last Post: 23 December 2011 - 12:48 PM

#1 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

[Solved] How do i split a Texture2D into multiple Texture2D's

Posted 22 December 2011 - 10:15 PM

I am attempting to make a tile based game.

I have a tile set that contains 3 32x32 tiles i am attempting to load the tile set into a Texture2D and then i am trying to draw each tile onto another Texture2D after that i add the new Texture2D containing one tile to a array list.

When i draw back my image stored in my array list i get nothing on the screen.

I know I am doing this wrong could anyone shed some light on the problem?

here is my a code snippet

Texture2D tempTileSet = Content.Load<Texture2D>("testMegaman1");
Texture2D tempTex = new Texture2D(graphics.GraphicsDevice, 32, 32);
SpriteBatch sb = new SpriteBatch(tempTex.GraphicsDevice);
sb.Begin();
sb.Draw(tempTileSet, new Vector2(0, 0), new Rectangle(0, 0, TILE_SIZE_X, TILE_SIZE_Y), Color.White);
sb.End();
tempTex.GraphicsDevice.Clear(Color.Black);
tileSet.Add(tempTex);



I am aware that this is inefficient but it makes it easy for me to draw a map. I don't need to store the whole map image...

Wait a second... Would it be more efficient to have a raw image (png) of the map and then just draw the onscreen section?

It sounds more efficient basing that information what iv read online.

But what do i do about an animated map? do i have a separate map image for each frame of the background animation?

This post has been edited by giuseppe105: 23 December 2011 - 09:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [Solved] How do i split a Texture2D into multiple Texture2D's

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: [Solved] How do i split a Texture2D into multiple Texture2D's

Posted 23 December 2011 - 08:23 AM

In the end tile engines are more efficient. Rather than trying to separate the image why not create a collection of source rectangles for the tiles? That is the most common approach rather than trying to create separate Texture2Ds. You can look at this topic, about 6 or 7 replies down, for a pretty generic tile engine that uses that approach.
Was This Post Helpful? 1
  • +
  • -

#3 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: [Solved] How do i split a Texture2D into multiple Texture2D's

Posted 23 December 2011 - 08:35 AM

I made some code to generate a dictionary for all the tile locations


int numX = (tileSet.Width / TILE_SIZE_X);
int numY = (tileSet.Height / TILE_SIZE_Y);
int incrementer = 0;

for (int y = 0; y < numY; y++)
{
     for (int x = 0; x < numX; x++)
     {
          tileSetD.Add(incrementer, new Rectangle(x*TILE_SIZE_X, y*TILE_SIZE_Y, TILE_SIZE_X, TILE_SIZE_Y));
          incrementer++;
     }
}



Where do i place my map text files i cant put it in content because visual studios 2010 express complains about not knowing how to handle it.
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: [Solved] How do i split a Texture2D into multiple Texture2D's

Posted 23 December 2011 - 08:43 AM

You can put it into content. You can write custom content pipeline extensions for your maps or for your maps you can set the Build Action to None in the properties window for your maps. You'll also want to set the Copy to output directory to either copy always or copy if newer as well.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#5 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: [Solved] How do i split a Texture2D into multiple Texture2D's

Posted 23 December 2011 - 09:34 AM

k now that i know how to add different content I want to organize everything into folders

how do i load content from a folder inside content?

Edit:

I figured it out.

I changed my content load line from

tileSet = Content.load<Texture2D>("tileSet1");



to

tileSet = Content.load<Texture2D>("TileSets/tileSet1");


This post has been edited by giuseppe105: 23 December 2011 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: [Solved] How do i split a Texture2D into multiple Texture2D's

Posted 23 December 2011 - 12:48 PM

Below is the code for creating a PartImage of an existing image.
Personally I am doing this now for a TileEngine as I find using the advantages of Reference Objects in C# can save quite a considerable amount of memory.
This way I only need to load a Tileset data file which creates the class that holds all the Tile data including its individual "Sprite" which is then referenced from the layer class using a simple int array to refer to what tile to use.

Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1