"Wow, guest1 , you are really experianced C++ programmer!"
Thanks. I'm actually not as experienced as you think. I only have about 3 months of C++ experience...
Data.h
Code: Select all
#ifndef __DATA_H__
#define __DATA_H__
extern IrrlichtDevice* IrrDevice;
struct ShipData;
struct Data
{
void ParseData(c8 *file);
ShipData* GetShip(u32 id);
ShipData* ParseShip(IXMLReader* xml);
array<ShipData*> ship_data;
};
struct ShipData
{
wchar_t name[64];
s32 id;
c8 mesh_path[256];
IAnimatedMesh* mesh;
bool mesh_loaded;
c8 tex0_path[256];
ITexture* tex0;
bool tex0_loaded;
f32 armour;
f32 mass;
f32 pstore;
f32 pdrain;
f32 heat;
vector3df position;
vector3df rotation;
vector3df scale;
};
#endif
Data.cpp
Code: Select all
#include "Data.h"
ShipData* Data::GetShip(u32 id)
{
s32 i;
ShipData* data;
for(i=ship_data.size()-1; i>=0; i--)
{
data = ship_data[i];
if(data->id == id) return data;
}
return 0;
}
void Data::ParseData(c8 *file)
{
IXMLReader* xml = IrrDevice->getFileSystem()->createXMLReader(file);
if(xml)
{
while(xml->read())
{
switch(xml->getNodeType())
{
case EXN_ELEMENT:
{
if(stringw("ship") == xml->getNodeName())
{
ShipData* ship = ParseShip(xml);
if(ship > 0) ship_data.push_back(ship);
}
/*
else if(stringw("item") == xml->getNodeName())
{
SShipItemData* item = parseShipItem(xml);
if(item > 0) ShipItemData.push_back(item);
}
*/
}
nextxml: break;
}
}
xml->drop();
}
}
ShipData* Data::ParseShip(IXMLReader* xml)
{
printf("Parsing Ship...");
ShipData *ship = new ShipData;
ship->id = xml->getAttributeValueAsInt(L"id");
//these should be installed as default or something
ship->mesh_path[0] = 0;
ship->position = vector3df(0,0,0);
ship->rotation = vector3df(0,0,0);
ship->scale = vector3df(1.0f,1.0f,1.0f);
ship->mesh_loaded = 0;
ship->armour = 0;
ship->heat = 0;
ship->mass = 0;
ship->pdrain = 0;
ship->pstore = 0;
ship->mesh_loaded = false;
while(xml->read())
{
switch(xml->getNodeType())
{
case EXN_ELEMENT:
{
if(stringw("position") == xml->getNodeName())
{
f32 x,y,z;
x = xml->getAttributeValueAsFloat(L"x");
y = xml->getAttributeValueAsFloat(L"y");
z = xml->getAttributeValueAsFloat(L"z");
//printf("Position: %f %f %f\n", x,y,z);
ship->position = vector3df(x,y,z);
}
else if(stringw("rotation") == xml->getNodeName())
{
f32 x,y,z;
x = xml->getAttributeValueAsFloat(L"x");
y = xml->getAttributeValueAsFloat(L"y");
z = xml->getAttributeValueAsFloat(L"z");
//printf("Rotation: %f %f %f\n", x,y,z);
ship->rotation = vector3df(x,y,z);
}
else if(stringw("scale") == xml->getNodeName())
{
f32 x,y,z;
x = xml->getAttributeValueAsFloat(L"x");
y = xml->getAttributeValueAsFloat(L"y");
z = xml->getAttributeValueAsFloat(L"z");
//printf("Scale: %f %f %f\n", x,y,z);
ship->scale = vector3df(x,y,z);
}
else if(stringw("attributes") == xml->getNodeName())
{
ship->armour = xml->getAttributeValueAsFloat(L"armour");
ship->mass = xml->getAttributeValueAsFloat(L"mass");
ship->pstore = xml->getAttributeValueAsFloat(L"pstore");
ship->pdrain = xml->getAttributeValueAsFloat(L"pdrain") / 1000.0;
ship->heat = xml->getAttributeValueAsFloat(L"heat") / 1000.0;
}
else if(stringw("model") == xml->getNodeName())
{
wcstombs(&ship->mesh_path[0], xml->getAttributeValue(L"file"), 2048);
}
else if(stringw("tex0") == xml->getNodeName())
{
wcstombs(&ship->tex0_path[0], xml->getAttributeValue(L"file"), 2048);
}
} break;
case EXN_ELEMENT_END:
printf("done\n");
if(ship->id > 0)
return ship;
else
return 0;
}
}
return 0;
}
Then, in my main.cpp, I have "Data DataStore;" and in my globals.h, I have "extern Data DataStore" so I can access that data anywhere in the program.
--------------------------
For your code, try changing: "irr::core::array<Wall>* walls;" to "irr::core::array<Wall*> walls;"
You have two choices on this one... your array can hold a list of pointers to Wall structures created using "new Wall" or you can hold the wall structure itself directly inside of the array. To do pointer, you'll want to put your code like the above. For direct access (that is, using the "." instead of the "->") I don't know how, so I'll have to read up how to do this...
I also don't know what a typedef struct is, but I'll assume it's the same thing as a normal struct. hehe