• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
nooblet

Loading a tiled map

11 posts in this topic

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:
[list]
[*]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.
[/list]

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.

[b]WARNING: [/b][b]MY SOURCE CODE IS REALLY MESSY AT THE MOMENT! :D[/b]

WorldManager.cs
[spoiler]
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace MyGame
{
public class WorldManager
{
private Vector2[,] worldSize;

public Vector2[,] WorldSize
{
get { return worldSize; }
set { worldSize = value; }
}

public void CreateWorld(string path, string[,] world)
{
using (StreamWriter sw = new StreamWriter(path))
{
for(int row = 0; row < 19; row++)
{
for(int col = 0; col < 25; col++)
{
sw.Write(world.GetValue(row, col));
}

sw.WriteLine();
}
}
}

public char[] LoadWorld(string path)
{
string data;
char[] columns;

using (StreamReader sr = new StreamReader(path))
{
data = sr.ReadToEnd();
columns = data.ToCharArray();
}

return columns;
}
}
}
[/source]
[/spoiler]

Grass.cs
[spoiler]
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace MyGame
{
class Grass : WorldManager
{
private string[,] world = new string[19, 25];
private int y = 0;

public Texture2D GrassTexture
{
get;
set;
}

public void Initialize()
{
WorldSize = new Vector2[19, 25];

for (int row = 0; row < 19; row++)
{
y = row * 32;

for (int col = 0; col < 25; col++)
{
WorldSize.SetValue(new Vector2(col * 32, y), row, col);
Console.WriteLine(WorldSize.GetValue(row, col));
}
}
}

public void LoadContent(ContentManager Content)
{
GrassTexture = Content.Load<Texture2D>("World/Tiles/Grass");
}

public void Create()
{
for (int row = 0; row < 19; row++)
{
for (int col = 0; col < 25; col++)
{
world.SetValue("0", row, col);
}

Console.WriteLine();
}

CreateWorld(@"C:\Games\MyGame\Worlds\GrassWorld.txt", world);
}

public void Load(SpriteBatch spriteBatch)
{
string path = @"C:\Games\MyGame\Worlds\GrassWorld.txt";

for (int i = 0; i < LoadWorld(path).Length; i++)
{
if(LoadWorld(path)[i] == '0')
{
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();
}
}
}
}
}
[/source]
[/spoiler]

Game1.cs
[spoiler]
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MyGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
public static GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Grass grass = new Grass();

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
this.Window.Title = "My Game";

graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ApplyChanges();

Player.Initialize();

grass.Initialize();

base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);

Player.LoadContent(Content);
Crosshair.LoadContent(Content);
grass.LoadContent(Content);
grass.Load(spriteBatch);
}

protected override void UnloadContent()
{
}

protected override void Update(GameTime gameTime)
{
InputHandler.Update();
Crosshair.Update();
Player.Update();

base.Update(gameTime);
}

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

spriteBatch.Begin();

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

spriteBatch.End();

base.Draw(gameTime);
}
}
}

[/source]
[/spoiler]
0

Share this post


Link to post
Share on other sites
[left][background=rgb(250, 251, 252)]Grass.cs[/background][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)][CODE]
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();
}
}
}
[/CODE][/background][/size][/font][/color][/left]


Try this instead:

[left][font=helvetica, arial, verdana, tahoma, sans-serif][size=2][color=#282828][CODE]
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();
}
[/CODE][/color][/size][/font][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]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().[/background][/size][/font][/color][/left] Edited by slynk
1

Share this post


Link to post
Share on other sites
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:
[b]Begin cannot be called again until End has been successfully called.[/b]

If I call grass.Draw() in my Game1.cs after removing spriteBatch.Begin() and .End(), then I receive the following:
[b]Begin must be called successfully before a Draw can be called.[/b] Edited by nooblet
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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*
0

Share this post


Link to post
Share on other sites
As far as I can tell, you will need to make the following changes:

1) Add a routine for drawing your tiles, and call that from your draw routine.
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.
1

Share this post


Link to post
Share on other sites
in Game.cs

[CODE]

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);
}
[/CODE]

In Grass.cs


[CODE]

public void Load(SpriteBatch spriteBatch)
{
string path = @"C:\Users\mainuser\Desktop\MyGame\MyGame\Worlds\GrassWorld.txt";
for (int i = 0; i < LoadWorld(path).Length; i++)
{
if(LoadWorld(path)[i] == '0')
{
// >>>> 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();
}
}
}
[/CODE]

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. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] Edited by androidedev
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0