Then refer to my first post.So I gotta set my std::string in my ms3d structure = to that of the character array in the file def.
And I'm having issues doing that...
Then refer to my first post.So I gotta set my std::string in my ms3d structure = to that of the character array in the file def.
And I'm having issues doing that...
There is a difference between tedious and difficult.
Ok, let's break your requirements down:So I gotta set my std::string in my ms3d structure = to that of the character array in the file def.
And I'm having issues doing that...
1)So I gotta set my std::string in my ms3d structure:2)Code:string strType;= to3)Code:string strType; strType = ;to that of the character arrayCode:char charArr[] = "hello"; string strType; strType = charArr;
Last edited by 7stud; 04-02-2006 at 01:36 PM.
I think I've said this already, maybe I havn't?
Every time I do that I get an error: "Cannot convert from const char * to const std::string *
This sets the std::string TextureFilenameCode:Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
equal to the char array m_textureCode:struct Material { float Ambient[4], Diffuse[4], Specular[4], Emissive[4]; float Shininess; GLuint Texture; std::string * TextureFilename; }; int NumMaterials; Material *Materials;
Now, what am I doing wrong now?Code:struct MS3DMaterial { char m_name[32]; float m_ambient[4]; float m_diffuse[4]; float m_specular[4]; float m_emissive[4]; float m_shininess; // 0.0f - 128.0f float m_transparency; // 0.0f - 1.0f byte m_mode; // 0, 1, 2 is unused now char m_texture[128]; char m_alphamap[128]; } PACK_STRUCT;
That happens either way, whether or not I take the new char strlen part out or not...
Bleh..
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Apparently switching it from a std::string * to just a plain old std::string fixed it..
Strange..
Sometimes I forget what I am doing when I enter a room, actually, quite often.
>> Strange..
It's not strange. Do you understand what an std::string is? Do you understand what a character array is? Try and understand what each of those two things are to understand why that worked. BTW, you'll almost never need a std::string * in your code.
Yeah, that is weird. string type, pointer to string type, what's the diff?Apparently switching it from a std::string * to just a plain old std::string fixed it..
Strange..
By the way, if all you did was change the type of:
Materials[i].TextureFilename
then your code is still completely wrong--even though it will compile. Can you see how ridiculous this code is:
Also, note the memory leak.Code:string str; str = new char[5]; str = "Something much longer than 5 characters. What the..?!"; cout<<str<<endl; str="hi"; cout<<str<<endl;
Last edited by 7stud; 04-02-2006 at 09:09 PM.
what in the world are you talking about?
:0
I just wanna switch it from using a char array to a string object, because I can do all sorts of nifty functions with string objects, such as finding the last of certain characters, erasing parts of the string.
If the code is blatantly wrong, consult with NeHe over that matter, I'm just working with what I've got.
Thanks though you've all been alot of help.
Sometimes I forget what I am doing when I enter a room, actually, quite often.
lol. Heck if I know.what in the world are you talking about?
You've posted so much contradictory stuff in this thread that we're all flying on instruments in a heavy fog.
However, one thing I am certain of is, if you have this statement:
and materials[i].TextureFilename is a string type, then you have created an instant memory leak: you both dynamically allocated memory and lost a pointer to the memory in the same line--and I didn't even know that was possible. Not to mention the fact that that statement demonstrates a complete ignorance of the string type.Code:Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
Last edited by 7stud; 04-03-2006 at 09:10 PM.
Well, I changed it back to a char array, and did this, this completes my origional goal, deleting everything out of the char array except for the final file name:
I changed materials.texturefilename back to a char *Code:void MS3DModel::ReloadTextures() { for ( int i = 0; i < NumMaterials; i++ ) if ( strlen(Materials[i].TextureFilename) > 0 ) { std::string TextureFilename = Materials[i].TextureFilename; int cutoff = TextureFilename.find_last_of('//'); TextureFilename = TextureFilename.substr(cutoff + 1); Materials[i].Texture = LoadGLTexture( TextureFilename.c_str() ); } else Materials[i].Texture = 0; }
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Just when I thought this whole thread couldn't get any stranger...
Throughout this whole thread, you've been trying to assign something to Materials[i].TextureFilename, e.g.:
andCode:strcpy( Materials[i].TextureFilename, pMaterial->m_texture );
but now you post code that apparently does what you want, and nowhere is Materials[i].TextureFilename assigned any value. What the??Code:Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
Uhg, you are slow, it isn't me!
Code:bool MS3DModel::Load(const std::string & name) { const char *ptr1 = 0; ptr1= name.data ( ); ifstream inputFile( ptr1, ios::in | ios::binary); if ( inputFile.fail()) { MessageBox(NULL,"Model file not found.","Model Error",MB_OK); return false; // "Couldn't open the model file." } inputFile.seekg( 0, ios::end ); long fileSize = inputFile.tellg(); inputFile.seekg( 0, ios::beg ); byte *pBuffer = new byte[fileSize]; inputFile.read((char *)pBuffer, fileSize ); inputFile.close(); const byte *pPtr = pBuffer; MS3DHeader *pHeader = ( MS3DHeader* )pPtr; pPtr += sizeof( MS3DHeader ); if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 ) { MessageBox(NULL,"Not a valid Milkshape3D model file.", "Model Error",MB_OK); return false; // "Not a valid Milkshape3D model file." } if ( pHeader->m_version < 3 || pHeader->m_version > 7 ) { MessageBox(NULL,"Not a valid Milkshape3D file version.","Model Error",MB_OK); return false; // "Unhandled file version. Milkshape3D Version 1.3 through 1.7 is supported." :) } int nVertices = *( word* )pPtr; NumVertices = nVertices; Vertices = new Vertex[nVertices]; pPtr += sizeof( word ); int i; for ( i = 0; i < nVertices; i++ ) { MS3DVertex *pVertex = ( MS3DVertex* )pPtr; Vertices[i].BoneID = pVertex->m_boneID; memcpy( Vertices[i].Location, pVertex->m_vertex, sizeof( float )*3 ); pPtr += sizeof( MS3DVertex ); } int nTriangles = *( word* )pPtr; NumTriangles = nTriangles; Triangles = new Triangle[nTriangles]; pPtr += sizeof( word ); for ( i = 0; i < nTriangles; i++ ) { MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr; int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] }; float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] }; memcpy( Triangles[i].VertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 ); memcpy( Triangles[i].Textures1, pTriangle->m_s, sizeof( float )*3 ); memcpy( Triangles[i].Textures2, t, sizeof( float )*3 ); memcpy( Triangles[i].VertexIndices, vertexIndices, sizeof( int )*3 ); pPtr += sizeof( MS3DTriangle ); } int nGroups = *( word* )pPtr; NumMeshes = nGroups; Meshes = new Mesh[nGroups]; pPtr += sizeof( word ); for ( i = 0; i < nGroups; i++ ) { pPtr += sizeof( byte ); // flags pPtr += 32; // name word nTriangles = *( word* )pPtr; pPtr += sizeof( word ); int *pTriangleIndices = new int[nTriangles]; for ( int j = 0; j < nTriangles; j++ ) { pTriangleIndices[j] = *( word* )pPtr; pPtr += sizeof( word ); } char materialIndex = *( char* )pPtr; pPtr += sizeof( char ); Meshes[i].MaterialIndex = materialIndex; Meshes[i].NumTriangles = nTriangles; Meshes[i].TriangleIndices = pTriangleIndices; } int nMaterials = *( word* )pPtr; NumMaterials = nMaterials; Materials = new Material[nMaterials]; pPtr += sizeof( word ); for ( i = 0; i < nMaterials; i++ ) { MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr; memcpy( Materials[i].Ambient, pMaterial->m_ambient, sizeof( float )*4 ); memcpy( Materials[i].Diffuse, pMaterial->m_diffuse, sizeof( float )*4 ); memcpy( Materials[i].Specular, pMaterial->m_specular, sizeof( float )*4 ); memcpy( Materials[i].Emissive, pMaterial->m_emissive, sizeof( float )*4 ); Materials[i].Shininess = pMaterial->m_shininess; Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1]; strcpy(Materials[i].TextureFilename, pMaterial->m_texture); pPtr += sizeof( MS3DMaterial ); } ReloadTextures(); delete[] pBuffer; return true; } void MS3DModel::ReloadTextures() { for ( int i = 0; i < NumMaterials; i++ ) if ( strlen(Materials[i].TextureFilename) > 0 ) { std::string TextureFilename = Materials[i].TextureFilename; int cutoff = TextureFilename.find_last_of('//'); TextureFilename = TextureFilename.substr(cutoff + 1); Materials[i].Texture = LoadGLTexture( TextureFilename.c_str() ); } else Materials[i].Texture = 0; }
Sometimes I forget what I am doing when I enter a room, actually, quite often.
If Materials[i].TextureFilename is a std::string, that's all you need. As I said before, I'm not sure why you thought you needed an std::string*, but as long as you make it a plain std::string, that is all you needed. You can do what feels comfortable for you, but using the string is easier than your current code.Code:Materials[i].TextureFilename = pMaterial->m_texture;