|
-
Sep 10th, 2002, 10:29 PM
#1
Thread Starter
Addicted Member
Dynamically increase size of array??
Hi,
VB6 allows me to increase the size of an array as required to load data...
REDIM myarray(UBOUND +1)
And I can use PRESERVE to prevent array contents from being loss. In C++...
How do I detect upper boundary of an array?
How do I dynamically increase the size of the array?
How do I preserve the data in the array?
Regards,
ChuckB
-
Sep 10th, 2002, 10:45 PM
#2
Frenzied Member
STL vector. Use it, love it =).
Code:
#include <vector>
using std::vector;
int main()
{
int x = 10;
vector<int> v(10); // new vector, size 10;
vector<int>::iterator i;
for(i = v.begin(); i != v.end(); i++)
{
(*i) = x;
}
v.resize(20);
for(i = v.begin(); i != v.end(); i++)
(*i) = ++(*i);
x = 16;
v.push_back(x);
return v.size();
}
In the end you have a vector of size 21, each element 0..9 holding 11, 10..19 holding junk, and 20 holding 16.
MSDN it for more info =).
Z.
-
Sep 11th, 2002, 12:00 AM
#3
Thread Starter
Addicted Member
Hi Z,
Thanks for the tip on vectors. I was focused upon arrays. I have just obtained "The C++ Standard Library - A Tutorial and Reference". It has a lot about vectors.
I must read up on iterators for this class so the code will make better sense.
I need to read a text file full of X,Y and Z vertex values that make up my 3D model. I need to program a vector that can be increased as required to load any model I have in a file. Size will vary. I think this will do it.
Just a side note, your thoughts about game programming in C++ and others I have read motivated me to finally learn this thing. Thanks! My friends now think I am cool with my 3D programming...fog, light, camera....pretty cool.
Regards,
ChuckB
-
Sep 11th, 2002, 06:21 AM
#4
Frenzied Member
=). I try to avoid letting people know that I am a game programmer, because then they are like "Ohh, let me see your game". Unfortunatly, all you can do at the moment, is move the camera, types some commands into the console, and such, which isnt so impressive to anyone but me =). So, I wait, and when I am finished... well... =).
What you might want to do, instead of using a vector to hold your information, place a long value at the start of your verex data to give you the number of vertices to expect. Then, you can create a buffer of that size (with new), and lead your data in in one big chunk. That is how I do it =). The reason behind this is that when you access a vector, it is NOT a direct access to a pointer, so repreated access can slow you down a bit.
Z.
-
Sep 11th, 2002, 11:54 AM
#5
Monday Morning Lunatic
Originally posted by Zaei
=). I try to avoid letting people know that I am a game programmer, because then they are like "Ohh, let me see your game". Unfortunatly, all you can do at the moment, is move the camera, types some commands into the console, and such, which isnt so impressive to anyone but me =). So, I wait, and when I am finished... well... =).
What you might want to do, instead of using a vector to hold your information, place a long value at the start of your verex data to give you the number of vertices to expect. Then, you can create a buffer of that size (with new), and lead your data in in one big chunk. That is how I do it =). The reason behind this is that when you access a vector, it is NOT a direct access to a pointer, so repreated access can slow you down a bit.
Z.
Most implementations of vector have an iterator that resolves to a raw pointer, or that can be inlined to one if necessary. There's no range checking or anything like that.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 11th, 2002, 03:13 PM
#6
Frenzied Member
I was talking about the [] operator, but you are right =).
Z.
-
Sep 11th, 2002, 03:18 PM
#7
Monday Morning Lunatic
Originally posted by Zaei
I was talking about the [] operator, but you are right =).
Z.
I thought [] did the same thing, but that might just be me.
Doesn't .at() do range-checking?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 11th, 2002, 11:54 PM
#8
Thread Starter
Addicted Member
Hi,
I am writing a function to do with vectors what my function did with arrays.
I have a string read from a file that contains three numbers, separated by commas without any whitespace. I load that into 'string s'. The below code snippet shows the first number being extracted from the string, converted to a float and then loaded into an array.
Code:
snum=s.substr(0,comma1);
dat[j][1]=atof(snum.c_str());
My vector examples do not show a 2 dimensional example such as above. It is all 1 dimensional. Is it possible to create a vector such as
Code:
vector<float> num[12][3];
My compiler allows this creation, but does not like this section of code where num is a vector instead of a an array. It chokes.
Code:
snum=s.substr(0,comma1);
num[j][1]=atof(snum.c_str());
Any advice?
What you might want to do, instead of using a vector to hold your information, place a long value at the start of your verex data to give you the number of vertices to expect. Then, you can create a buffer of that size (with new), and lead your data in in one big chunk. That is how I do it =).
Z, can you provide a brief sample of reading a 'chunk'?
Regards,
ChuckB
-
Sep 12th, 2002, 06:14 AM
#9
Frenzied Member
Your code above would create a 2 dimensional array, each element holding a vector, so, a 3D array =).
Take the following snippet:
Code:
FILE* f = fopen(...);
if(!f) return;
unsigned long numVerts = 0;
fread(&numVerts, 1, 4, f);
VERTEX* v = new VERTEX[numVerts];
fread(v, numVerts, sizeof(VERTEX), f);
The above code opens a file, reads in one unsigned long value, creates a block of memory big enough to hold that many verices, and then loads ALL of the vertices in one shot.
If you are using a text file, you might want to use the fstream library. It will do the value conversions for you.
Z.
-
Sep 12th, 2002, 08:40 AM
#10
Thread Starter
Addicted Member
Hi,
Thanks for the code sample. As I understand it, the first line of the data file contains a number that indicates the number of data lines that follow in the data file. This number is loaded into numVerts.
Do you mean to use vector instead of vertex? Or is vertex a struct that contains 3 points(X,Y,Z)? Or does each line contain either an X, Y or Z value. So you must read three lines to get one vertex?
Code:
VERTEX* v = new VERTEX[numVerts];
Here's a funny one that stopped me. I got confused and used this include line...
#include <vertex>
I spent 10 minutes trying to find out why my ANSI C++ compiler did not have a 'vector' header....duh! :-)
Regards,
ChuckB
-
Sep 12th, 2002, 10:24 AM
#11
Frenzied Member
Yeah, it could be really easy to confuse that =). In my code sample, you will notice the line:
Code:
VERTEX* v = new VERTEX[numVerts];
This simply allocates a block of numVerts VERTEX structures (yes, 3 float values). In all of thise code, I am assuming a straight binary file. You COULD use a vector here, like this:
Code:
vector<VERTEX> v(numVerts);
fread(&v[0], numVerts, sizeof(VERTEX), f);
But you have to be careful when you do something like that, because, though &v[0] will usually return the pointer that the vector is holding, the standard doesnt actually specify HOW a vector holds its data, just that it must expose a certain functionality. It could easily be a linked list under that hood, which would give you a LOT of problems.
For reference, your file would look like this:
Code:
| 4 bytes, numVerts | numVerts * sizeof(VECTOR) bytes, vertex data |
It is binary, so you need a model converter to write out the data into your format. Using biary files saves you file space, as well as time (conversions from text to various number values can take a while).
Z.
-
Sep 12th, 2002, 12:08 PM
#12
Thread Starter
Addicted Member
Hi Z,
I follow what you are saying now. The binary format makes sense for your example. I did some reading at gamedev and created this modified portion of code that reads an ASCII file, with a number at top stating number of triangles as you suggested. You got me thinking about auto conversion....so I learned sscanf today.
Code:
//create pointer to store data
TRIANGLE *triangle; //creates pointer
triangle = new TRIANGLE[numTriangles];
//get data...note auto conversion to float
for (int triLoop =0;triLoop<numTriangles;triLoop++)
{
for (int vertLoop =0;vertLoop<3;vertLoop++)
{
ReadString(fp,oneLine);
sscanf(oneLine,"%f, %f, %f", &x,&y,&z);
triangle[triLoop].vertex[vertLoop].x=x;
triangle[triLoop].vertex[vertLoop].y=y;
triangle[triLoop].vertex[vertLoop].z=z;
}
}
I believe a floating point number uses 32 bits. Is that correct? If so, I then see in theory how a float can be converted to 4 bytes and placed into a binary file.
In the meantime I will use the ASCII file format so I can manually build models from graph paper with Notepad. I have a 3D model builder I programmed in QBASIC three years ago. It works pretty good so I might go back and write a function to output my particular file format. It's funny to think that I can be using QBASIC to build some neat C++ stuff. ;-)
Regards,
ChuckB
-
Sep 12th, 2002, 12:16 PM
#13
Frenzied Member
Like I said, if you want to use text files, you might want to use the fstream library:
Code:
#include <fstream>
using namespace std;
....
ifstream f("c:\myfile.txt");
...
float floats[10]
for(int i = 0; i < 10; ++i)
f >> floats[i];
f.close();
It will do all of the data conversions for you, based on the type.
Z.
-
Sep 12th, 2002, 12:24 PM
#14
Monday Morning Lunatic
If you want to use a binary file, there's nothing wrong with the streams either
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 13th, 2002, 05:39 AM
#15
Except that you then don't use the stream operators but rather fstream::read and fstream::write. Using << or >> will result in strange behaviour.
A binary file will basically be much faster as you don't need to do conversions. If you want to read the whole file in one command (not necessarily all data at once) but you still want to use a vector, you could do this:
Code:
#include <vector>
#include <algorithm>
#include <fstream>
#include <iterator>
using namespace std;
// read a file:
// you might want to prealloc memory in order to avoid multiple reallocations
vector<vertex> vec;
ifstream file;
copy(istream_iterator<vertex>(file), istream_iterator<vertex>(), vec.begin());
// or if vec already contains elements that you want to preserve:
copy(istream_iterator<vertex>(file), istream_iterator<vertex>(), back_insert_iterator<vector<vertex> >(vec));
I think that should do for binary files.
The more I play with iterators, the more I like them!
Last edited by CornedBee; Sep 13th, 2002 at 05:43 AM.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 13th, 2002, 06:11 AM
#16
Frenzied Member
Agree Mr Bee. They make things nice and easy =).
fstreams go NUTS when I use them with my overloaded new and delete operators, which is better then half the reason I dont use them =).
Z.
-
Sep 13th, 2002, 08:35 AM
#17
Thread Starter
Addicted Member
Alright Z and CornedBee, I am going to try the code sample above. I have been reading about iterators for over a week now and also on vectors so it is time I settle down and figure them out.
I read a book from cover to cover on C++ a few years ago. It was about the basics. I never realized until recently that this STL was the real meat behind C++. The previous author kept me in the dark. :-) So it has been definitely a mind-blowing experience.
I will be programming a VB map editor later today just to rest my brain from C++. Can't think of any more questions now...I need time coding.
Thanks for the info.
Regards,
ChuckB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|