Sign in to follow this  

[C#][SDL.Net]Loading maps

This topic is 3149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there! I'm new to this forum and I've found it with google. =P Anyway, I've always wondered how to load maps... 2D maps with tiles... Anyway, here's my try at it with C# and SDL.Net : [CODE] using System; using System.IO; using System.Drawing; using SdlDotNet.Graphics; using SdlDotNet.Core; using SdlDotNet.Audio; using SdlDotNet.Graphics.Primitives; using SdlDotNet.Graphics.Sprites; using SdlDotNet.Input; using SdlDotNet.Particles; using SdlDotNet.Windows; namespace Contra2012 { class Map { private Rectangle m_source; private SpriteCollection m_spritecollection; private Sprite m_sprite; private StreamReader m_data; private int m_offsetx; private int m_offsety; private int m_value; private int m_tile; private int m_line; private string m_current; public void Initialize(string surface, string data) { m_data = File.OpenText(data); while (!m_data.EndOfStream) { m_current = m_data.ReadLine(); for(int i = 0;i < m_current.Length;i++) { for (int u = 0; u < m_current.Length; u += 1) { if (m_current[i + u] != '(' || m_current[i + u] != ',' || m_current[i + u] != ')' && i+u<m_current.Length) m_value += (int)m_current[i] + u * 10; else break; } if (m_current[i] == ',') m_offsetx = m_value * 16; if(m_current[i] == ')') { m_offsety = m_value * 16; m_sprite = new Sprite(new Surface(surface), new Point(m_tile * 16, m_line * 16), m_spritecollection); m_sprite.Surface.ClipRectangle = new Rectangle(m_offsetx,m_offsety,16,16); m_spritecollection.Add(m_sprite); } } m_line ++; } } } } [/CODE] The format of the text files : (#th of the tile in the spritesheet on the X axis,#th of the tile in the spritesheet on the Y axis) (1,0)(2,4) (2,3) But I can't seem to make it work. It said that my Index is going outside the limits... I don't mind rewriting all of the stuff, so if there is a better way to do so,say it to me. ;) Thanks. Ps.I hope there isn't too much code to look at. =P

Share this post


Link to post
Share on other sites
You got two nested loops walking through the same string, and you are adding those indexes together. Something fishy about that.

Instead of trying to identify the problem I wrote another parsing function that I think is clearer and less prone to errors. I don't have SDL installed for C# so I'm just gonna extract the coordinates and print them to the screen.


public void Initialize(string surface, string data)
{
m_data = File.OpenText(data);

char[] separators = { ',', '(', ')' }; // Adding all characters we consider meta-data

while (!m_data.EndOfStream)
{
m_current = m_data.ReadLine();

// Creating an array of strings, removing ( ) and ,
string[] pairs = m_current.Split(separators, StringSplitOptions.RemoveEmptyEntries);

// Making sure that there is at least two coordinates, and that the number of coordinates found is an even number
if (pairs.Length < 2 || (pairs.Length % 2) != 0)
{
Console.WriteLine("Something is wrong with the file format");
return;
}

// Walking through all the coordinate pairs
for (int i = 0; i < pairs.Length; i += 2)
{
// Extracting the x and y values, and removing white spaces
string sx = pairs[i].Trim();
string sy = pairs[i+1].Trim();

// Converting the values into 32 bit integers
int x = Convert.ToInt32(sx);
int y = Convert.ToInt32(sy);

// Do whatever you want to the coordinates here, I'm just gonna print them to screen for convenience
Console.WriteLine(x.ToString() + " " + y.ToString());
}
}
}

Share this post


Link to post
Share on other sites

This topic is 3149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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