Loading coordinates from a text file

Hello all! I’ve been trying to figure this out and I need help! I have one
image with 14 different sprites on it to animate a guy running. I also have
a text file that houses the coordinates (x1, y1, x2, y2 - per line) for each
sprite. I want to put these into an array (or vector) so I can easily go
through the array to animate the hero.

The best example I’ve found, has been opening a file with ifstream, using
the getline function, putting the line string into a stringstream and
putting that into a vector of strings.

This is good because it takes each individual number and put it into its own
spot in the vector. But in the end I’m going to need those to be ints, not
strings. The text file is all integers and I’m really having a hard time
converting a string to an int.

This whole thing leads me to a few questions:

  1. is loading a text file into an array the best way to do it? or should I
    just have a big array hardcoded in the class? i would think it would be
    better to load in the coordinates and not bog down the class
  2. is this the correct way to animate sprites (have one sheet of different
    pics and load their different coordinates based on keyboard input)? or is
    there an easier way through SDL to accomplish this?
  3. how do i convert a string to an int?

Thanks so much for any help!

Mike

Hi Mike,

You may want to try SuperTux Lisp parser. Lisp allows for some very flexible
level and other files. SuperTux Lisp parser at SVN was done from scratch,
while the 0.1.x builds is a C++ port from another guy parser – if you like
the suggestion, you may want to compare both to see what you prefer.
I can’t give you direct links because berlios.de seems to be down.

If you prefer a more simple solution, you may want INI-like syntax. Here’s a
parser for it:
http://microracers.cvs.sourceforge.net/microracers/microracers/src/lib/ini_parser.h?view=markup
http://microracers.cvs.sourceforge.net/microracers/microracers/src/lib/ini_parser.cpp?view=markup

Cheers,
RicardoEm Domingo, 21 de Maio de 2006 16:38, o Michael Balogh escreveu:

Hello all! I’ve been trying to figure this out and I need help! I have
one image with 14 different sprites on it to animate a guy running. I also
have a text file that houses the coordinates (x1, y1, x2, y2 - per line)
for each sprite. I want to put these into an array (or vector) so I can
easily go through the array to animate the hero.

The best example I’ve found, has been opening a file with ifstream, using
the getline function, putting the line string into a stringstream and
putting that into a vector of strings.

This is good because it takes each individual number and put it into its
own spot in the vector. But in the end I’m going to need those to be ints,
not strings. The text file is all integers and I’m really having a hard
time converting a string to an int.

This whole thing leads me to a few questions:

  1. is loading a text file into an array the best way to do it? or should I
    just have a big array hardcoded in the class? i would think it would be
    better to load in the coordinates and not bog down the class
  2. is this the correct way to animate sprites (have one sheet of different
    pics and load their different coordinates based on keyboard input)? or is
    there an easier way through SDL to accomplish this?
  3. how do i convert a string to an int?

Thanks so much for any help!

Mike


Any philosophy that can be put in a nutshell belongs there.
– Sydney J. Harris

  1. is loading a text file into an array the best way to do it? or
    should I just have a big array hardcoded in the class? i would think
    it would be better to load in the coordinates and not bog down the
    class

If these numbers aren’t going to change, you’re probably better off to
just include them in the code as constants.

  1. how do i convert a string to an int?

Assuming you’re talking about C++, just input them directly into
integers. Pseudocode:

ifstream datafile("input_filename");
Check_For_Error_In(datafile);
while(<not done> && <not error>) {
	int temp_int;
	datafile >> temp_int;
	my_vector.push_back(temp_int);
}
datafile.close();On Sun, May 21, 2006 at 10:38:02AM -0500, Michael Balogh wrote:


Steaphan Greene
Lecturer, Computer Science, Binghamton University
GPG public key: http://www.cs.binghamton.edu/~sgreene/gpg.key.txt
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060521/6e5e65aa/attachment.pgp