Jump to content
  • Advertisement
Sign in to follow this  
REspawn

Memory allocation

This topic is 4078 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

Hi, Im after running into a problem in c++ I have my model class, and inside this class there are structs, an example of one of these structs would be a vertex data struct, since i dont know until i read the model how many verts there will be i use a pointer and dynamic allocation. Everyhting in the model loader is working fine when i read a single file on its own off the desktop for example. But in the full system it reads the model files from inside one large file. But where i run into trouble is i get a std::bad_alloc error at random places in the file when declaring how big the dynamic arrays are. Is there something extra i need to do when i am going to be allocating memory like that? Cheers for any help, -Dave

Share this post


Link to post
Share on other sites
Advertisement
Debug and Check the allocation sizes before allocation.
You are likely attempting to allocate some huge number of bytes. This in turn is likely due to you reading the
wrong data from the file. (ie you lost your place in the file parsing)

Share this post


Link to post
Share on other sites
Im not, i tought that to at first, so i steped through the program step by step as it read the data to make sure it was coming in right and it is.

Im 100% sure that the data coming in is correct.

Share this post


Link to post
Share on other sites
100% right values coming in.

This is why it makes no sense to me, the file it messes up on the part just before it messes up is a char pointer dec.
string[0] = new char[20];

If i take that file out and just read it on its own from the desktop it dosent crash.

////

Edit: after the above test, i made the post and then i tought why not just loop the reading of the file i know works, if its a memory problem them it will mess up and it did.

I tried to read the file 20 times over, even with cleaning up the memory it used after each go it messed up.

////

Edit: Could using the same var to read each one cause problems, even if i delete it when done and use the new to recreate it for the next read?

Share this post


Link to post
Share on other sites
calling new char[20] shouldn't throw a bad_alloc, would really need to see your full loading function, important structure defs, and a sample file that makes your function go boom.

Share this post


Link to post
Share on other sites
Um.

Oh my god.

I see you've been a member here since 2003. I don't know what you think you've been learning/getting taught since then, but it sure as hell doesn't look like anything I'd normally call "C++". I also don't really know how it's possible to write this much code of this caliber without suspecting that something must be wrong with the general approach.

You might want to hang around For Beginners for a while and try to get a fresh start. Also, RTFF to hopefully get at least a few good ideas.

Here's my first pass at fixing things, along with comments on things that popped out while getting this far. I'd estimate I've, so far, done less than 10% of the work that's really required to bring things up to standard.

(EDIT: Please note that because I don't have any D3D stuff set up locally, I have taken out references to those headers and put in stubs for the relevant types, in order to get the code into a state where I can at least compile and test the basic functionality. The alleged problem isn't to do with D3D, after all.)

(EDIT 2: Manually linewrapped stuff in the first box, per request.)

BAD EVIL WRONG THINGS.txt

- Don't rely on #pragma once.

- 'Stream' is completely useless. This is C++; instead of putting your own
potentially buggy wrappers around ancient APIs, use perfectly good new ones -
in this case, <iostream>. (Worse, I can only assume you know about such a thing
and are deliberately avoiding it for what will surely turn out to be a
hilarious reason; my evidence is that you would otherwise not even think of the
term "stream" to describe your object.) Not to mention, there isn't even such a
thing as <stdio.h> any more. In C++, we spell that <cstdio>, on the
mind-bogglingly bizarre occasions that we might see fit to use it.

- Don't keep things as members unless they're logically part of the
representation of the object. Member functions can have parameters just like
free functions, you know.

- RAII concept #1: Don't make 'load' functions. The constructor is supposed to
do that work. If you pass a stream to a constructor, the expectation is that the
constructor will read the data from the stream. Don't use 'loaded' flags as
members, either. If you can't load the object, it shouldn't exist; throw an
exception from the constructor. Also, don't define a default constructor if
there's nothing it can meaningfully do.

- Re "where you should be": If something isn't what you expect, don't just set
it and assume things are OK. There's a *reason* things didn't work out right,
and you should probably *do something* about it; which in turn requires that you
*find out*. This is what assertions and/or exceptions are for.

- sprintf() is NOT a copying function. The following code is BROKEN:

clump.geometryList.geometry->materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.strings[0].string = new char[header.sectionSize];
tempString = new char[header.sectionSize];
stream->getBytes(tempString, header.sectionSize);

sprintf(clump.geometryList.geometry->materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.strings[0].string, tempString);
delete [] tempString;

It will crash if the read-in 'tempString' happens to contain any format
specifiers, because no arguments are explicitly provided. This is just one
example of what's so incredibly evil about the old C-style I/O. But more to the
point, sprintf() was never documented as being any kind of string-copying
function; C provides strcpy() for that. But more to the point than that, there's
no need to copy here at all. The current process is:

- Allocate buffer 1.
- Allocate buffer 2.
- Read data into buffer 2.
- Copy contents of buffer 2 to buffer 1.
- Deallocate buffer 2.

Wouldn't it make more sense to just do this?

- Allocate buffer 1.
- Read data into buffer 1.

:P (note that you can read into the buffer just fine before you assign to that
data member).

As it turns out, I'm going to replace all of this completely later, but just
thought I'd point it out as an example of complete lack of understanding and/or
thought.

- On a related note: 0 should generally not be a special case. The following
code is silly:

if(header.sectionSize != 0)
stream->getbytes(&clump.geometryList.geometry->materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.extension.data, header.sectionSize);
}

If the size is indeed 0, then the call should correctly do nothing. Otherwise,
the problem is in the implementation of getbytes().

- Working with file offsets is usually wrong-minded. Normally, the things you
need to read are in sequence, so after reading one item, you don't need to know
where to see to in order to find the next - you are already there, by
definition. The conclusion is that the 'fileoffset' parameter to the DFFModel
constructor is likely to be useless in general. In fact, a quick inspection
reveals that the 'offset' member of the class is set and then never actually
used. So I removed it.

- To facilitate the use of iostream for reading, I made a simple wrapper
function around the iostream's .read() member function. This allows us to use
the magic of templates to avoid needing to worry about the sizeof() what we're
reading. This doesn't work quite right for pointers, of course (because it would
try to read in a value for the address, rather than reading stuff into the
pointed-at region), so I made another function to create and fill a char
allocation of a specified size. This is totally temporary and will be replaced
later when things are in good enough shape that I can show you how to avoid
doing all the memory management yourself. But it did lead at least to a little
extra cleanup, namely being able to remove an ugly 'char* tempString' variable.

- Don't artificially pause your programs at the end. Also, main() implicitly
returns 0 at the end, so you don't need to worry about that, either.



SampleProblem.cpp

#include <fstream>
#include <iostream>
#include "DFFModel.h"

int main()
{
std::cerr << "TOP OF MAIN\n";

for(int i=0; i<20; i++){
std::ifstream dffstream("sample_file.dff", std::ios::binary);
std::cerr << i << '\n';
DFFModel* model = new DFFModel("sample_file.dff", dffstream);
delete model;
std::cerr << '~' << i << '\n';
}

}






DFFModel.h

#ifndef DFF_MODEL_H
#define DFF_MODEL_H

#include <iosfwd>

typedef unsigned uint;
typedef unsigned short word;
typedef unsigned char byte;
typedef int DWORD;

#pragma region -- Structures --

// Vertex
struct Vertex{
float x, y, z;
DWORD color;
float u, v;

enum FVF{ FVF_Flags = 7 };
};

// Extension
struct RWExtension{
char* data;
};

// Atmoic
struct RWAtomic{
uint frameNumber;
uint geometryNumber;
uint unknown1;
uint unknown2;
};

struct D3DVECTOR { float x, y, z; };
typedef D3DVECTOR* LPD3DVECTOR;

// Frame list data
struct RWFrameListData{
D3DVECTOR rotmatrixone;
D3DVECTOR rotmatrixtwo;
D3DVECTOR rotmatrixthree;
D3DVECTOR cordsoffset;
uint parentframe;
uint unknown;
};

// Frame list
struct RWFrameList{
RWFrameListData* framesData;
RWExtension* extensions;
uint frameCount;
bool loaded;
};

// Bounding info
struct RWGeometryBoundingInfo{
float boundingSpherex;
float boundingSpherey;
float boundingSpherez;
float boundingSphereRadius;
uint hasPosition;
uint hasNormals;
};

// Geometry color info
struct RWGeometryColorInfo{
byte red;
byte green;
byte blue;
byte alpha;
};

// Geometry face info
struct RWGeometryFaceInfo{
word vertex2;
word vertex1;
word flags;
word vertex3;
};

// Grometry Mapping info
struct RWGeometryMappingInfo{
float u;
float v;
};

// Geometry Normal info
struct RWGeometryNormalInfo{
float x;
float y;
float z;
};

// Geometry Vertex Info
struct RWGeometryVertexInfo{
float x;
float y;
float z;
};

// String
struct RWString{
char* string;
};

// Texture
struct RWTexture{
word textureFilter;
word unknown;
RWString strings[2];
RWExtension extension;
bool uninitalized;
};

// Material
struct RWMaterial{
RWTexture texture;
uint unknown;
uint red;
uint green;
uint blue;
uint alpha;
uint unknown2;
uint textureCount;
uint unknown3;
uint unknown4;
uint unknown5;
};

// Material List
struct RWMaterialList{
RWMaterial* materials;
uint materialCount;
uint materialPos;
uint* unknown;
};

// Material Split Info
struct RWMaterialSplitInfo{
uint faceIndex;
uint materialIndex;
uint* vertex;
};

// Material Split
struct RWMaterialSplit{
uint triangleStrip;
uint splitCount;
uint faceCount;
RWMaterialSplitInfo* spiltInfo;
};

//Subset map
struct SubsetMap{
uint textureIndex;
uint subsetId;
};

struct D3DXMESH {
void Release() {}
};

typedef D3DXMESH* LPD3DXMESH;

// Geometry
struct RWGeometry{
LPD3DXMESH mesh;
word flags;
word unknown;
uint triangleCount;
uint vertexCount;
uint morphTargetCount;
float ambient;
float diffuse;
float specular;
RWGeometryColorInfo* colorinfo;
RWGeometryMappingInfo* mappinginfo;
RWGeometryFaceInfo* faceinfo;
RWGeometryBoundingInfo boundingInfo;
RWGeometryVertexInfo* vertexInfo;
RWGeometryNormalInfo* normalInfo;
RWMaterialList materiallist;
RWMaterialSplit materialsplit;
SubsetMap* subsetMap;
bool hasColors;
bool hasMapping;
bool hasBaseColors;
bool hasNormals;
bool loaded;
bool meshCreated;
};

// Geometry List
struct RWGeometryList{
RWGeometry* geometry;
uint geometryCount;
};

// Clump
struct RWClump{
uint objectCount;
RWFrameList frameList;
RWGeometryList geometryList;
RWAtomic atomic;
RWExtension extension;
};

//Section Header
struct SectionHeader{
uint sectionID;
uint sectionSize;
uint version;
};

#pragma endregion

class DFFModel
{
public:
DFFModel(char* sourcefile, std::istream& is);
~DFFModel();

private:
void readHeader(std::istream& is);
void parseSection(std::istream& is);
void parseData(std::istream& is);

void parseClump(std::istream& is);
void parseClumpData(std::istream& is);
void parseFrameList(std::istream& is);
void parseFrameListData(std::istream& is);
void parseGeometryList(std::istream& is);
void parseGeometryListData(std::istream& is);
void parseGeometry(std::istream& is);
void parseGeometryData(std::istream& is);
void parseMaterialListData(std::istream& is);
void parseMaterialList(std::istream& is);
void parseMaterialSplit(std::istream& is);
void parseMaterial(std::istream& is);
void parseTexture(std::istream& is);
void parseTextureData(std::istream& is);
void parseAtomic(std::istream& is);
void parseAtomicData(std::istream& is);
void parseExtension(std::istream& is);
void parseUnused(std::istream& is);

//variables
char file[256];
SectionHeader header;
SectionHeader previousHeader;
RWClump clump;
uint geometryPos;
};

#endif






DFFModel.cpp

#include "DFFModel.h"

#include <iostream>
using std::istream;
std::ios_base::seekdir cur = std::ios_base::cur;

template <typename T>
void readBinary(istream& is, T& t) {
is.read(reinterpret_cast<char*>(&t), sizeof(t));
}

char* createBuffer(istream& is, int size) {
char* result = new char[size];
is.read(result, size);
return result;
}

template <typename T>
void readBinary(istream& is, T*& t) {
T::cannot_use_this_function_with_a_pointer;
}

DFFModel::DFFModel(char* sourcefile, istream& is)
{
strcpy(file, sourcefile);

geometryPos = (uint)-1;

readHeader(is);
}

DFFModel::~DFFModel()
{

int x;

for(int i=0; i<clump.geometryList.geometryCount; i++){
if(clump.geometryList.geometry.loaded){
if(clump.geometryList.geometry.meshCreated)
clump.geometryList.geometry.mesh->Release();

if(clump.geometryList.geometry.hasColors)
delete [] clump.geometryList.geometry.colorinfo;

if(clump.geometryList.geometry.hasMapping)
delete [] clump.geometryList.geometry.mappinginfo;

delete [] clump.geometryList.geometry.faceinfo;
delete [] clump.geometryList.geometry.vertexInfo;

if(clump.geometryList.geometry.hasNormals)
delete [] clump.geometryList.geometry.normalInfo;

delete [] clump.geometryList.geometry.subsetMap;

for(x=0; x<clump.geometryList.geometry.materialsplit.splitCount; x++)
delete [] clump.geometryList.geometry.materialsplit.spiltInfo[x].vertex;
delete [] clump.geometryList.geometry.materialsplit.spiltInfo;

for(x=0; x<clump.geometryList.geometry.materiallist.materialCount; x++){
if(clump.geometryList.geometry.materiallist.materials[x].texture.uninitalized == false){
delete [] clump.geometryList.geometry.materiallist.materials[x].texture.strings[0].string;
delete [] clump.geometryList.geometry.materiallist.materials[x].texture.strings[1].string;
}
}
delete [] clump.geometryList.geometry.materiallist.unknown;
}
}

delete [] clump.geometryList.geometry;

//Memory leak, fix this
/*if(clump.frameList.loaded){
for(x=0; x<clump.frameList.frameCount; x++)
delete [] clump.frameList.extensions[x].data;

delete [] clump.frameList.extensions;
delete [] clump.frameList.framesData;
}*/

}

//------------------------------------------------------------------------------
// All functions below this line related to reading the dff file, one section
// at a time
//------------------------------------------------------------------------------

void DFFModel::readHeader(istream& is)
{
previousHeader = header;

readBinary(is, header);

parseSection(is);
}

void DFFModel::parseSection(istream& is)
{
switch (header.sectionID){
case 1:
parseData(is);
break;
case 2:
parseUnused(is);
break;
case 3:
parseExtension(is);
break;
case 6:
parseTexture(is);
break;
case 7:
parseMaterial(is);
break;
case 8:
parseMaterialList(is);
break;
case 14:
parseFrameList(is);
break;
case 15:
parseGeometry(is);
break;
case 16:
parseClump(is);
break;
case 20:
parseAtomic(is);
break;
case 26:
parseGeometryList(is);
break;
case 1294:
parseMaterialSplit(is);
break;
case 39056126:
//parseFrame(is);
break;
default:
parseUnused(is);
break;
}
}

void DFFModel::parseData(istream& is)
{
switch (previousHeader.sectionID){
case 6:
parseTextureData(is);
break;
case 8:
parseMaterialListData(is);
break;
case 14:
parseFrameListData(is);
break;
case 15:
geometryPos++;
parseGeometryData(is);
break;
case 16:
parseClumpData(is);
break;
case 20:
parseAtomicData(is);
break;
case 26:
parseGeometryListData(is);
break;
}
}

void DFFModel::parseClump(istream& is)
{
readHeader(is);
}

void DFFModel::parseClumpData(istream& is) {
readBinary(is, clump.objectCount);

if (header.version == 268697599 ||
header.version == 402915327 ||
header.version == 469893130) {
is.ignore(8);
}

clump.frameList.loaded = false;

readHeader(is);
}

void DFFModel::parseFrameList(istream& is)
{
readHeader(is);
}

void DFFModel::parseFrameListData(istream& is)
{
int i, tempInt;

readBinary(is, clump.frameList.frameCount);
clump.frameList.framesData = new RWFrameListData[clump.frameList.frameCount];
clump.frameList.extensions = new RWExtension[clump.frameList.frameCount];

clump.frameList.loaded = true;

for(i=0; i<clump.frameList.frameCount; i++){
readBinary(is, clump.frameList.framesData.rotmatrixone);
readBinary(is, clump.frameList.framesData.rotmatrixtwo);
readBinary(is, clump.frameList.framesData.rotmatrixthree);
readBinary(is, clump.frameList.framesData.cordsoffset);
readBinary(is, clump.frameList.framesData.parentframe);
readBinary(is, clump.frameList.framesData.unknown);
}

for(i=0; i<clump.frameList.frameCount; i++){
readBinary(is, tempInt);

if (tempInt == 3){
is.seekg(-4, cur);
previousHeader = header;
readBinary(is, header);

clump.frameList.extensions.data = createBuffer(is, header.sectionSize);
}
else
is.seekg(-4, cur);
}

readHeader(is);
}

void DFFModel::parseGeometryList(istream& is)
{
readHeader(is);
}

void DFFModel::parseGeometryListData(istream& is)
{
readBinary(is, clump.geometryList.geometryCount);

clump.geometryList.geometry = new RWGeometry[clump.geometryList.geometryCount];

for(int x=0; x<clump.geometryList.geometryCount; x++){
clump.geometryList.geometry[x].loaded = false;
clump.geometryList.geometry[x].hasColors = false;
clump.geometryList.geometry[x].hasMapping = false;
clump.geometryList.geometry[x].hasNormals = false;
clump.geometryList.geometry[x].meshCreated = false;
clump.geometryList.geometry[x].hasBaseColors = false;
}

readHeader(is);
}

void DFFModel::parseGeometry(istream& is)
{
readHeader(is);
}

void DFFModel::parseGeometryData(istream& is)
{

std::streampos whereYouShouldBe = is.tellg() + std::streamoff(header.sectionSize);
int i = 0;

readBinary(is, clump.geometryList.geometry[geometryPos].flags);
readBinary(is, clump.geometryList.geometry[geometryPos].unknown);
readBinary(is, clump.geometryList.geometry[geometryPos].triangleCount);
readBinary(is, clump.geometryList.geometry[geometryPos].vertexCount);
readBinary(is, clump.geometryList.geometry[geometryPos].morphTargetCount);

if (header.version == 134283263 || header.version == 784){
readBinary(is, clump.geometryList.geometry[geometryPos].ambient);
readBinary(is, clump.geometryList.geometry[geometryPos].diffuse);
readBinary(is, clump.geometryList.geometry[geometryPos].specular);
}

//Color info
if ((8 & clump.geometryList.geometry[geometryPos].flags) == 8){
clump.geometryList.geometry[geometryPos].hasColors = true;
clump.geometryList.geometry[geometryPos].colorinfo = new RWGeometryColorInfo[clump.geometryList.geometry[geometryPos].vertexCount];

for(i=0; i<clump.geometryList.geometry[geometryPos].vertexCount; i++)
readBinary(is, clump.geometryList.geometry[geometryPos].colorinfo);
}

//Mapping info
if((4 & clump.geometryList.geometry[geometryPos].flags) == 4){
clump.geometryList.geometry[geometryPos].hasMapping = true;
clump.geometryList.geometry[geometryPos].mappinginfo = new RWGeometryMappingInfo[clump.geometryList.geometry[geometryPos].vertexCount];

for(i=0; i<clump.geometryList.geometry[geometryPos].vertexCount; i++)
readBinary(is, clump.geometryList.geometry[geometryPos].mappinginfo);
}

//Face info
clump.geometryList.geometry[geometryPos].faceinfo = new RWGeometryFaceInfo[clump.geometryList.geometry[geometryPos].triangleCount];
for(i=0; i<clump.geometryList.geometry[geometryPos].triangleCount; i++)
readBinary(is, clump.geometryList.geometry[geometryPos].faceinfo);

//Bounding info
readBinary(is, clump.geometryList.geometry[geometryPos].boundingInfo);

//Vertex info
clump.geometryList.geometry[geometryPos].vertexInfo = new RWGeometryVertexInfo[clump.geometryList.geometry[geometryPos].vertexCount];
for(i=0; i<clump.geometryList.geometry->vertexCount; i++)
readBinary(is, clump.geometryList.geometry[geometryPos].vertexInfo);

//Normal info
if((16 & clump.geometryList.geometry[geometryPos].flags) == 16){
clump.geometryList.geometry[geometryPos].normalInfo = new RWGeometryNormalInfo[clump.geometryList.geometry[geometryPos].vertexCount];
for(i=0; i<clump.geometryList.geometry[geometryPos].vertexCount; i++)
readBinary(is, clump.geometryList.geometry[geometryPos].normalInfo);
}

//check position -- I R REDUNDANT NOW!
assert(is.tellg() == whereYouShouldBe);

clump.geometryList.geometry[geometryPos].loaded = true;

readHeader(is);
}

void DFFModel::parseMaterialList(istream& is)
{
readHeader(is);
}

void DFFModel::parseMaterialListData(istream& is)
{
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materialCount);

clump.geometryList.geometry[geometryPos].materiallist.materials = new RWMaterial[clump.geometryList.geometry[geometryPos].materiallist.materialCount];
clump.geometryList.geometry[geometryPos].materiallist.unknown = new uint[clump.geometryList.geometry[geometryPos].materiallist.materialCount];

clump.geometryList.geometry->materiallist.materialPos = (uint)-1;

for(int i=0; i<clump.geometryList.geometry[geometryPos].materiallist.materialCount; i++)
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.unknown);

readHeader(is);
}

void DFFModel::parseMaterialSplit(istream& is)
{
int x;

readBinary(is, clump.geometryList.geometry[geometryPos].materialsplit.triangleStrip);
readBinary(is, clump.geometryList.geometry[geometryPos].materialsplit.splitCount);
readBinary(is, clump.geometryList.geometry[geometryPos].materialsplit.faceCount);

clump.geometryList.geometry[geometryPos].materialsplit.spiltInfo = new RWMaterialSplitInfo[clump.geometryList.geometry[geometryPos].materialsplit.splitCount];
clump.geometryList.geometry[geometryPos].subsetMap = new SubsetMap[clump.geometryList.geometry[geometryPos].materialsplit.splitCount];

for(int i=0; i<clump.geometryList.geometry[geometryPos].materialsplit.splitCount; i++){
readBinary(is, clump.geometryList.geometry[geometryPos].materialsplit.spiltInfo.faceIndex);
readBinary(is, clump.geometryList.geometry[geometryPos].materialsplit.spiltInfo.materialIndex);

clump.geometryList.geometry[geometryPos].materialsplit.spiltInfo.vertex = new uint[clump.geometryList.geometry[geometryPos].materialsplit.spiltInfo.faceIndex];
for(x=0; x<clump.geometryList.geometry[geometryPos].materialsplit.spiltInfo.faceIndex; x++)
readBinary(is, clump.geometryList.geometry[geometryPos].materialsplit.spiltInfo.vertex[x]);
}

readHeader(is);
}

void DFFModel::parseMaterial(istream& is)
{
clump.geometryList.geometry[geometryPos].materiallist.materialPos++;

readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].unknown);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].red);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].green);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].blue);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].alpha);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].unknown2);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].textureCount);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].unknown3);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].unknown4);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].unknown5);

readHeader(is);
}

void DFFModel::parseTexture(istream& is)
{
readHeader(is);
}

void DFFModel::parseTextureData(istream& is)
{
int tempInt;

clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.uninitalized = false; //extra check for when reclaiming memory
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.textureFilter);
readBinary(is, clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.unknown);

readBinary(is, tempInt);
if (tempInt == 2) {
is.seekg(-4, cur);
previousHeader = header;
readBinary(is, header);

clump.geometryList.geometry->materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.strings[0].string = createBuffer(is, header.sectionSize);

readBinary(is, tempInt);
if(tempInt == 2){
is.seekg(-4, cur);
previousHeader = header;
readBinary(is, header);

clump.geometryList.geometry[geometryPos].materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.strings[1].string = createBuffer(is, header.sectionSize);

readBinary(is, tempInt);
if(tempInt == 3){
is.seekg(-4, cur);
previousHeader = header;
readBinary(is, header);

clump.geometryList.geometry->materiallist.materials[clump.geometryList.geometry[geometryPos].materiallist.materialPos].texture.extension.data = createBuffer(is, header.sectionSize);
}
else
is.seekg(-4, cur);
}
else
is.seekg(-4, cur);
}
else
is.seekg(-4, cur);

readHeader(is);
}

void DFFModel::parseAtomic(istream& is)
{
readHeader(is);
}

void DFFModel::parseAtomicData(istream& is)
{
readBinary(is, clump.atomic);
}

void DFFModel::parseExtension(istream& is)
{
readHeader(is);
}

void DFFModel::parseUnused(istream& is)
{
is.ignore(header.sectionSize);

readHeader(is);
}






TBH, I didn't even really know where to start, because there's so much redundancy, disorganization, and general ignorance of the standard library here. I decided to bite a big bullet and temporarily ignore the redundancy and disorganization, and start by attacking the specific bits of ignorance of the standard library that seem almost *willful* - in particular, the reinvention of a "stream". And I'm not even done with that; in pass 2, in addition to putting in std::strings where appropriate, I'll be putting in std::vectors to replace manually managed (count, array) pairs. I'll note that your original comments for SampleProblem.cpp make reference to 'a vector', so you seem to know this thing exists. Why not USE IT?

[Edited by - Zahlman on April 21, 2007 3:53:40 PM]

Share this post


Link to post
Share on other sites
Cheers Zahlman.

I have only been doing c++ for about 2/3 weeks now, i was using c# and mdx until then and i got sick of the speed so i moved to c++.

Sent you a pm.

Share this post


Link to post
Share on other sites
Applause to Zahlman for putting so much time and effort into that post![cool]

(and for making it fit better across the screen width)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!