Miserable file loader

Started by
13 comments, last by cowsarenotevil 21 years, 5 months ago
sorry for the multiple post, my computer freaked out on me
-~-The Cow of Darkness-~-
Advertisement
You''re creating a 50,000 size vertex array regardless of the size....
That''s not something you want to do. First of all most of your models won''t have that many vertices. and ti''s possible some will have more.
So, you want to call resize() with the real amount of vertices in the file. If you can''t figure that out beforehand, try reading everything into a memory buffer - that will definitely speed it up anyway.

-----------------------------
Gamedev for learning.
libGDN for putting it all together.
I do resize it... hence "vertex.resize(a+1);" I still will change it though so that it resizes before in case I load a huge model

thanks

-~-The Cow of Darkness-~-
-~-The Cow of Darkness-~-
as I said up above...find out how long the file is before you read it into the vector. The code for that is in my previous post, so check it out and make any other changes you need! Good luck getting this to work!
It's only after you've lost everything that you're free to do anything.
hi...

you must understand how iostream works before you use ist

this is a simple stdio fileloader doing exactly what you want, plus some easy (error)reporting


  #include <windows.h>#include <stdio.h>#define MY_VEC_SIZE 100struct YourVector{    float x,y,z;};int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow ){ 	YourVector v[MY_VEC_SIZE];        // Workaround the MSVC R6002 Bug    for(int c = 0; c < MY_VEC_SIZE; c++)    {        v[c].x = 0.0f;        v[c].y = 0.0f;        v[c].z = 0.0f;    }    // Open your DataFile     FILE* YourFile;    YourFile = fopen("datafiles/MyDataFile.txt","rt");    if(YourFile == NULL)    {        MessageBox(NULL,"fopen failed","Error",MB_OK);        return 0;    }        long int n = 0;    int nResult;    while(n < MY_VEC_SIZE)    {             nResult = fscanf(YourFile,"%f%f%f",&v[n].x,&v[n].y,&v[n].z);        if((nResult == EOF) || (nResult < 3))            break;        n++;    }        char tmp[150] = "empty\0";    if(nResult == EOF)    {        if(n < MY_VEC_SIZE)            sprintf(tmp,"%d of %d vectors read - datafile shorter than vector",n,MY_VEC_SIZE);    }    else     {           if(n == MY_VEC_SIZE)            sprintf(tmp,"%d of %d vectors read - datafile ok",n,MY_VEC_SIZE);        else if(nResult < 3)             sprintf(tmp,"%d of %d vectors read - datafile ist corrupt: ",n,MY_VEC_SIZE);    }         MessageBox(NULL,tmp,"Error",MB_OK);    fclose(YourFile);	return 0;}  


in your datafile you can have your vevtor elements:

each at a single line
1.938
1.8474
3.83733

each vector at one line
1.938 1.8474 3.83733

all vectors in one big line
1.938 1.8474 3.83733 4.9383 4.88 3.48484 ...

if you want some datafileformat like
vec(1.938,1.8474,3.83733) a.s.o

simply change the argumentstring in the line:

   nResult = fscanf(YourFile,"%f%f%f",&v[n].x,&v[n].y,&v[n].z);// "%f%f%f"  


into:

   "vec(%f,%f,%f)"  


if you absolutly want to use iostream i''ll post the iostream version of the code above, but i prefer this one..

regards














This topic is closed to new replies.

Advertisement