Rise
The Vieneo Province
polyobj.cpp
Go to the documentation of this file.
1 #include "polyobj.h"
2 
3 #include <WinSock2.h>
4 #include <WS2tcpip.h>
5 #include <Windows.h>
6 #include "DXUT.h"
7 
8 #include <fcntl.h> // For _O_RDWR, _O_BINARY, _O_RDONLY, O_TEXT
9 #include <io.h> // For _open, _read, _close, _lseek, eof, _write
10 #include <sys/stat.h> // For _S_IWRITE, _S_IREAD
11 
12 #include "GameClass.h"
13 #include "Viewscreen/globals.h"
14 
16 {
17  logger = prmGame->logger;
18  game = prmGame;
19 }
20 
21 s_polygon_object PolyObj::LoadDAW(const char* filename) const
22 {
23  s_polygon_object polyobject = s_polygon_object();
24 
25  char msg[99];
26  HRESULT hr;
27 
28  int handle = -1;
29  _sopen_s(&handle, filename, _O_RDONLY | _O_BINARY | _O_SEQUENTIAL, _SH_DENYWR, _S_IWRITE);
30  if (handle == -1)
31  {
32  char strerr[99];
33  strerror_s(strerr, 99, errno);
34  sprintf_s(msg, sizeof(msg), "LoadDAW() could not load file \"%s\": %s", filename, strerr);
35 
36  logger->Log(msg, Logger::Level::Fatal);
37  return polyobject;
38  }
39 
40  // Vertices -----------------------------------------------------------------------------
41  _lseek(handle, 0L, SEEK_SET);
42  _read(handle, &polyobject.vertices, 2);
43  sprintf_s(msg, sizeof(msg), "LoadDAW() loading \"%s\"...", filename);
44  logger->Log(msg);
45  polyobject.polygons = polyobject.vertices / 3;
46 
47  // Create the vertex buffer
48  sprintf_s(msg, sizeof(msg), "LoadDAW() creating memory for %i vertices...", polyobject.vertices);
49  logger->Log(msg);
50 
51  if (FAILED(hr = DXUTGetD3D9Device()->CreateVertexBuffer(static_cast<long>(polyobject.vertices) * sizeof(D3DVERTEX),
52  D3DUSAGE_WRITEONLY, D3DFVF_VERTEX,
53  D3DPOOL_MANAGED, &polyobject.VB, NULL)))
54  {
55  sprintf_s(msg, sizeof(msg), "LoadDAW() could not create memory for %i vertices! %x", polyobject.vertices, hr);
56  logger->Log(msg, Logger::Level::Warn);
57  return polyobject;
58  }
59 
60  D3DVERTEX* vtx;
61  V(polyobject.VB->Lock(0, 0, reinterpret_cast<void**>(&vtx), 0));
62 
63  D3DXVECTOR3 tempnormal;
64  for (long s = 0; s < polyobject.vertices; s++)
65  {
66  _lseek(handle, 2L + s * 32L + 0L, SEEK_SET);
67  _read(handle, &tempnormal, 12);
68  (*vtx).x = tempnormal.x;
69  (*vtx).y = tempnormal.y;
70  (*vtx).z = tempnormal.z;
71  _lseek(handle, 2L + s * 32L + 12L, SEEK_SET);
72  _read(handle, &tempnormal, 12);
73  (*vtx).nx = tempnormal.x;
74  (*vtx).ny = tempnormal.y;
75  (*vtx).nz = tempnormal.z;
76  _lseek(handle, 2L + s * 32L + 24L, SEEK_SET);
77  _read(handle, &tempnormal.x, 4);
78  _read(handle, &tempnormal.y, 4);
79  (*vtx).tu = tempnormal.x;
80  (*vtx).tv = tempnormal.y;
81  vtx++;
82  }
83 
84  V(polyobject.VB->Unlock());
85 
86  _close(handle);
87 
88  return polyobject;
89 }
90 
91 void PolyObj::LoadDIX(const char* prmFilename)
92 {
93  strcpy_s(filename, sizeof filename, prmFilename);
94 
95  int handle = -1;
96  short i_temp;
97  char msg[99];
98 
99  _sopen_s(&handle, filename, _O_RDONLY | _O_BINARY | _O_SEQUENTIAL, _SH_DENYWR, _S_IWRITE);
100  if (handle == -1)
101  {
102  sprintf_s(msg, sizeof(msg), "LoadDIX() could not open \"%s\"!", filename);
103  logger->Log(msg, Logger::Level::Error);
104  }
105 
106  _lseek(handle, 0, SEEK_SET);
107  _read(handle, &componentTotal, 2);
108 
109  float version;
110  _read(handle, &version, 4);
111 
112  sprintf_s(msg, sizeof msg, "PolyObj %s CA", filename);
113  if (componentarray)
114  {
115  SAFE_DELETE(componentarray);
116  game->UpdateTrackedResource(msg, 3);
117  }
119  if (componentarray == nullptr)
120  {
121  sprintf_s(msg, sizeof(msg), "LoadDIX() could not open \"%s\"!", filename);
122  logger->Log(msg, Logger::Level::Error);
123  return;
124  }
125  game->AddTrackedResource(msg); // managed, it is just memory
126 
127 
128  sprintf_s(msg, sizeof(msg), "LoadDIX() initialized %i component(s)...", componentTotal);
129  logger->Log(msg);
130 
131  for (short s = 0; s < componentTotal; s++)
132  {
133  _lseek(handle, (long)s * 100L + 99L, SEEK_SET); // To 0
134  _read(handle, &i_temp, 2); // 0
135  componentarray[s].startvertex = (UINT)i_temp;
136  _read(handle, &i_temp, 2); // 2
137  componentarray[s].primitives = (UINT)i_temp;
138  _read(handle, &componentarray[s].nominalxyz, 12); // 4
139  _read(handle, &componentarray[s].nominalypr, 12); // 16
140  _read(handle, &componentarray[s].boundsphere, 4); // 28
141 // char msg[99];
142 // sprintf_s(msg, sizeof(msg), "Type %i Component %i Bound %f", t, s, componentarray[s].boundsphere );
143 // _write(logfile, msg, strlen(msg));
144  _read(handle, &componentarray[s].extendby, 12); // 32
145  _read(handle, &componentarray[s].texture, 2); // 44
146 /* if (componentarray[s].texture>=shiptextureC)
147  {
148  char msg[99];
149  sprintf_s(msg, sizeof(msg), "Type %i Component %i Texture %i out-of-bounds", t, s, componentarray[s].texture );
150  _write(logfile, msg, strlen(msg));
151  }
152 */ _read(handle, &componentarray[s].nominalCG, 12); // 46
153  _read(handle, &componentarray[s].type, 1); // 58
154  _read(handle, &componentarray[s].speedExtend, 4); // 59
155  _read(handle, &componentarray[s].hingeaxis, 12); // 63
156  _read(handle, &componentarray[s].hingeCG, 12); // 75
157  _read(handle, &componentarray[s].hingeextent, 4); // 87
158  _read(handle, &componentarray[s].attachto, 2); // 91
159 
160  if (version == 2.44f)
161  {
162  _read(handle, &componentarray[s].useLightMap, 1); // 93
163  _read(handle, &componentarray[s].speedRetract, 4); // 59
164  }
165  else
166  {
168  componentarray[s].useLightMap = true;
169  }
170 
171  // sprintf_s(msg, sizeof(msg), "YPR for %i is %f %f %f", s, componentarray[s].nominalypr.x,componentarray[s].nominalypr.y,componentarray[s].nominalypr.z);
172  // _write(logfile, msg, strlen(msg));
173 
174  // Dynamic
175  componentarray[s].extended = 0.0f;
176  }
177  _close(handle);
178 
179  sprintf_s(msg, sizeof(msg), "LoadDIX() finished component list");
180  logger->Log(msg);
181 
182 }
183 
184 unsigned short PolyObj::GetComponents() const
185 {
186  return componentTotal;
187 }
188 
190 {
191  if (i >= componentTotal || i < 0)
192  logger->Log("PolyObj::GetComponent out-of-bound array value!", Logger::Level::Fatal);
193 
194  return componentarray[i];
195 }
196 
198 {
199  if (componentarray)
200  {
201  char msg[99];
202  sprintf_s(msg, sizeof msg, "PolyObj %s CA", filename);
203  SAFE_DELETE(componentarray);
204  game->UpdateTrackedResource(msg, 3);
205  }
206 }
207 
209 {
211 }
void LoadDIX(const char *prmFilename)
Definition: polyobj.cpp:91
unsigned short componentTotal
Definition: polyobj.h:13
char filename[MAX_PATH]
Definition: polyobj.h:12
void ReleaseComponents()
Definition: polyobj.cpp:197
float speedRetract
Definition: globals.h:412
~PolyObj()
Definition: polyobj.cpp:208
float extended
Definition: globals.h:427
LPDIRECT3DVERTEXBUFFER9 VB
Definition: globals.h:366
Logger * logger
Definition: GameClass.h:113
s_mesh_component GetComponent(short i) const
Definition: polyobj.cpp:189
bool useLightMap
Definition: globals.h:411
short polygons
Definition: globals.h:365
s_mesh_component * componentarray
Definition: polyobj.h:11
Logger * logger
Definition: polyobj.h:10
float speedExtend
Definition: globals.h:412
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition: Logger.cpp:11
short vertices
Definition: globals.h:365
UINT startvertex
Definition: globals.h:408
void AddTrackedResource(const char *name, _D3DPOOL pool=D3DPOOL_MANAGED)
Definition: GameClass.cpp:2702
#define D3DFVF_VERTEX
Definition: globals.h:101
s_polygon_object LoadDAW(const char *filename) const
Definition: polyobj.cpp:21
void UpdateTrackedResource(const char *name, int status)
Definition: GameClass.cpp:2725
unsigned short GetComponents() const
Definition: polyobj.cpp:184
GameClass * game
Definition: polyobj.h:9
PolyObj()
Definition: polyobj.h:16