Object.cpp 1.92 KB
Newer Older
1
#include "Object.h"
Anakin's avatar
Anakin committed
2
#include <iostream>
3
4
5
6
7
8



/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

Anakin's avatar
Anakin committed
9
Object::Object(const char* path)
10
{
Anakin's avatar
Anakin committed
11
	// open file
12
	fsMesh.open(path, std::ios::in | std::ios::binary);
13

Anakin's avatar
Anakin committed
14
15
	if (!fsMesh.is_open())
		throw std::invalid_argument(std::string("file not found: ") += path);
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	fsMesh.seekg(8);
	char tempChunkName[5] = { 0 };
	fsMesh.read(reinterpret_cast<char*>(&tempChunkName[0]), sizeof(tempChunkName) - 1);

	if (strcmp(tempChunkName, "MSH2"))
		throw std::invalid_argument(std::string("corrupted file MSH2 expected instead of ") += tempChunkName);

	fsMesh.seekg(4, std::ios_base::cur);

	loadChunks(lChunkMsh2, fsMesh.tellg(), "CL1L");

	for (std::list<chunkHeader*>::iterator it = lChunkMsh2.begin(); it != lChunkMsh2.end(); it++)
	{
		if (!strcmp("MODL", (*it)->name))
		{
			loadChunks(lChunkModl, (*it)->position, "CL1L");
		}
	}
35

36
37
38
39
40
41
42
43
	// close file
	fsMesh.close();
}

Object::~Object()
{
	//delete Chunk list;	
}
44
45


46

47
48
/////////////////////////////////////////////////////////////////////////
// private functions
49

50
void Object::loadChunks(std::list<chunkHeader*>& destination, std::streampos start, const char end[5])
51
52
{
	// Jump into Mesh2
53
	fsMesh.seekg(start);
54

Anakin's avatar
Anakin committed
55
56
	do
	{
57
		chunkHeader* tempHeader = new chunkHeader();
58

59
60
61
		fsMesh.read(reinterpret_cast<char*>(&tempHeader->name[0]), sizeof(tempHeader->name) - 1);
		fsMesh.read(reinterpret_cast<char*>(&tempHeader->size), sizeof(tempHeader->size));
		tempHeader->position = fsMesh.tellg();
62

63
		destination.push_back(tempHeader);
64

65
		fsMesh.seekg(tempHeader->size, std::ios_base::cur);
66

67
		if (!std::strcmp(tempHeader->name, end))
Anakin's avatar
Anakin committed
68
			break;
69
70
71
	} while (fsMesh.good());

	std::cout << "got all chunks, totaly found: " << destination.size() << std::endl;
Anakin's avatar
Anakin committed
72

73
74
75
}


Anakin's avatar
Anakin committed
76
77
/////////////////////////////////////////////////////////////////////////
// public getter
78
79
80
81
82
83



/////////////////////////////////////////////////////////////////////////
// public functions