\$25

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1nooblet  Members

Posted 02 May 2012 - 10:05 PM

Hello,

I'm currently working on a top-down shooter, and have reached a "hmm, I'm doing something terribly wrong" point. Basically, here's what I'm trying to accomplish:
• I'd like to allow the programmer to hard-code in the map (I don't understand how to make a tile editor yet, need to familiarze myself with WPF prior to beginning that project) so that it can be loaded into application for later use. This has been accomplished.
• After the map has been created, I alter the code so that I can load the map and draw the contents of it. Essentially, if there's a 0 in the text file, draw the grass tile. If there's a 1, draw the dirt tile, etc.
• I'd like to have mulitple worlds so that if the player goes to a certain location (for instance, they step over the left side of the screen), then the world will change entirely. Think of the old Zelda games.

The problem is that when I load the text file and draw the world to the screen, I receive extremely bad framerate lag. I've tried altering the source a bit, but now the world doesn't even draw. I've attached a zip that contains the project's code and assets. I'm looking for any type of suggestions with creating and loading tiled maps using C# and XNA. In case you don't wish to download the project, I went ahead and included three source code files which are likely the cause of the issue to this post.

WARNING: MY SOURCE CODE IS REALLY MESSY AT THE MOMENT!

WorldManager.cs
Spoiler

Grass.cs
Spoiler

Game1.cs
Spoiler

### #2slynk  Members

Posted 02 May 2012 - 10:22 PM

Grass.cs

public void Draw(SpriteBatch spriteBatch)
{
for (int row = 0; row < 19; row++)
{
for (int col = 0; col < 25; col++)
{
spriteBatch.Begin();
spriteBatch.Draw(GrassTexture, WorldSize[row, col], Color.White);
spriteBatch.End();
}
}
}


public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
for (int row = 0; row < 19; row++)
{
for (int col = 0; col < 25; col++)
{
spriteBatch.Draw(GrassTexture, WorldSize[row, col], Color.White);
}
}
spriteBatch.End();
}


Or better yet, I don't see where you call grass.Draw() but I'm going to assume it's in Draw() inside Game1.cs (though I only skimmed your code.) If this is the case, just strip the Begin() and End from Grass.Draw to begin with. I assume it works like OpenGL in that it batch's the draw code together so that it only has to draw once. It speeds things up.

Give it a try. ^^

EDIT: ah, I didn't read that your world wasn't drawing. That's because you never call grass.draw().

Edited by slynk, 02 May 2012 - 10:33 PM.

### #3nooblet  Members

Posted 03 May 2012 - 02:08 AM

Thanks for your input! If I try the method you recommended (placing spriteBatch.Begin() and .End() before and after the for loop, respectively), then I receive the error:
Begin cannot be called again until End has been successfully called.

If I call grass.Draw() in my Game1.cs after removing spriteBatch.Begin() and .End(), then I receive the following:
Begin must be called successfully before a Draw can be called.

Edited by nooblet, 03 May 2012 - 02:09 AM.

### #4slynk  Members

Posted 03 May 2012 - 06:51 AM

Did you remove Begin and End from grass or game1? (should remove from grass) and did you place grass.Draw AFTER Begin but BEFORE End just like player.Draw and crosshair.Draw?

### #5nooblet  Members

Posted 03 May 2012 - 09:26 AM

Removed them from Grass.cs, and I sure did place them after begin but before end. That's why I'm really confused, not sure why it's doing this.

### #6slynk  Members

Posted 03 May 2012 - 09:51 AM

It's been a while since I used C#, are objects automatcally passed by ref? You may need to implicitly pass it by ref... seems I remember a ref keyword *shrug*

### #7Alphablackwolf  Members

Posted 03 May 2012 - 05:07 PM

As far as I can tell, you will need to make the following changes:

2) Call the grass draw event from that new routine for any tile object that needs grass drawn on it.
3) remove the spritebatch.begin/end calls in the grass draw routine.
4)remove your grass load procedure. Change it so that you load in tile information from your file and set an enum or property so that you know what tile object needs what type (grass, dirt, whatever you have) then you will 'load' in the type without drawing it. Unless I am much mistaken, you are not going to want to draw your grass when you load the content, only on the draw event.

Slynk is correct in pointing out the draw.begin/end inside of your grass draw loop is causing a lot of your lag.

You are also calling your grass draw routine inside of a loop that calls LoadWorld each time. you call and use loadworld once to find it's length and then again for every single loop in the if statement to see if it is the first time. So if the LoadWorld function returns a char array of length 30, you read the entire file 30 times. Load it once before your loop, and save it. call the saved value instead of the actual function over and over again.

### #8androidedev  Members

Posted 03 May 2012 - 06:37 PM

in Game.cs


protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

grass.Draw(spriteBatch);
Player.Draw(spriteBatch);
Crosshair.Draw(spriteBatch);

spriteBatch.End();

base.Draw(gameTime);
}


In Grass.cs


{
string path = @"C:\Users\mainuser\Desktop\MyGame\MyGame\Worlds\GrassWorld.txt";
for (int i = 0; i < LoadWorld(path).Length; i++)
{
{
// >>>>					Draw(spriteBatch);
}
}
}

public void Draw(SpriteBatch spriteBatch)
{
for (int row = 0; row < 19; row++)
{
for (int col = 0; col < 25; col++)
{
//					spriteBatch.Begin();
spriteBatch.Draw(GrassTexture, WorldSize[row, col], Color.White);
//					spriteBatch.End();
}
}
}


This don't fix your code, because the world loading is not well done, but it makes you can call draw in apropiate manner, basically i don't see why you call a Draw function inside one ("Load(SpriteBatch spriteBatch)") that is called at loading time, and don't call it in the loop Draw(...) of game.

i will recomend you that change the load code, in order it fills the world[,] array from .txt file, and then in grass.Draw call spritebatch.Draw with the proper texture depending of tile loaded in world[,].

PD: Sorry by my awfull english, i dont speak english at all.

Edited by androidedev, 03 May 2012 - 06:37 PM.

### #9nooblet  Members

Posted 04 May 2012 - 12:24 AM

Thanks for everybody's help and suggestions! I'll see if I can't fix it or find another way of loading a tiled world. Can't find many tutorials and it's very foreign to me. I called it within the load method as I thought the draw method was causing the lag issue (since it was looping the file check every Draw() call. Again, I'll scrap what I have and see if I can't work something different out.

### #10Alphablackwolf  Members

Posted 07 May 2012 - 12:45 PM

If you load the file once in the load process and save it into an object stored in memory it won't have to reach out to the filesystem to read the data every time. It's expensive to read the file each time, so if you store in the load, the draw process won't have to read the file to get the information.

### #11StratfordCanning  Members

Posted 09 May 2012 - 01:29 PM

I think it's worth working out what the common textures are and then storing them ready for later instead of loading each one before display, is that what alphablackwolf is saying? You have a 19x25 grid so if you've only got grass and dirt, maybe you only need to load two textures each time instead of 475.

Regarding the map loading, if you look at the Hexwar project I posted somewhere on this forum, I managed to get the map to load from a database that also stores the texture image file locations and so on. Feel free to use it, there's a .bak file in there somewhere for SQL Server Express. This surely is one of the easiest ways to store the board and it leaves you free to think about making an editor later on because you know the format will be something sensible.

### #12ndssia  Members

Posted 10 May 2012 - 11:03 AM

Be mindful of what you put into Update and Draw - do as little as you can in there, and as much as you can outside.

Any one-off, IO, or instantiation of objects, as a strong rule of thumb, should not be inside those loops.

You can find a decent map-editor in Tiled, which can be used together with Gravelyn's TiledLib for XNA.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.