Rise
The Vieneo Province
Loading...
Searching...
No Matches
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>
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.get();
18 game = prmGame;
19}
20
22{
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
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 = Rendering::ClientRenderer()->CreateVertexBuffer(static_cast<long>(polyobject.vertices) * sizeof(RiseVertexVertex),
53 Rendering::ResourcePool::Managed, &polyobject.VB, NULL)))
54 {
55 sprintf_s(msg, sizeof(msg), "LoadDAW() could not create memory for %i vertices! (%S)", polyobject.vertices, Rendering::ErrorString(hr));
57 _close(handle);
58 return polyobject;
59 }
60
62 V(polyobject.VB->Lock(0, 0, reinterpret_cast<void**>(&vtx), 0));
63
64 RiseMath::Vector3 tempnormal;
65 for (long s = 0; s < polyobject.vertices; s++)
66 {
67 _lseek(handle, 2L + s * 32L + 0L, SEEK_SET);
68 _read(handle, &tempnormal, 12);
69 vtx->x = tempnormal.x;
70 vtx->y = tempnormal.y;
71 vtx->z = tempnormal.z;
72 _lseek(handle, 2L + s * 32L + 12L, SEEK_SET);
73 _read(handle, &tempnormal, 12);
74 vtx->nx = tempnormal.x;
75 vtx->ny = tempnormal.y;
76 vtx->nz = tempnormal.z;
77 _lseek(handle, 2L + s * 32L + 24L, SEEK_SET);
78 _read(handle, &tempnormal.x, 4);
79 _read(handle, &tempnormal.y, 4);
80 vtx->tu = tempnormal.x;
81 vtx->tv = tempnormal.y;
82 vtx++;
83 }
84
85 V(polyobject.VB->Unlock());
86
87 _close(handle);
88
89 return polyobject;
90}
91
92void PolyObj::LoadDIX(const char* prmFilename)
93{
94 strcpy_s(filename, sizeof filename, prmFilename);
95
96 int handle = -1;
97 short i_temp;
98 char msg[99];
99
100 _sopen_s(&handle, filename, _O_RDONLY | _O_BINARY | _O_SEQUENTIAL, _SH_DENYWR, _S_IWRITE);
101 if (handle == -1)
102 {
103 sprintf_s(msg, sizeof(msg), "LoadDIX() could not open \"%s\"!", filename);
105 return;
106 }
107
108 _lseek(handle, 0, SEEK_SET);
109 _read(handle, &componentTotal, 2);
110
111 float version;
112 _read(handle, &version, 4);
113
114 sprintf_s(msg, sizeof msg, "PolyObj %s CA", filename);
115 if (componentarray)
116 {
119 }
121 if (componentarray == nullptr)
122 {
123 sprintf_s(msg, sizeof(msg), "LoadDIX() could not open \"%s\"!", filename);
125 _close(handle);
126 return;
127 }
128 game->AddTrackedResource(msg); // managed, it is just memory
129
130
131 sprintf_s(msg, sizeof(msg), "LoadDIX() initialized %i component(s)...", componentTotal);
132 logger->Log(msg);
133
134 for (short s = 0; s < componentTotal; s++)
135 {
136 _lseek(handle, (long)s * 100L + 99L, SEEK_SET); // To 0
137 _read(handle, &i_temp, 2); // 0
138 componentarray[s].startvertex = (UINT)i_temp;
139 _read(handle, &i_temp, 2); // 2
140 componentarray[s].primitives = (UINT)i_temp;
141 _read(handle, &componentarray[s].nominalxyz, 12); // 4
142 _read(handle, &componentarray[s].nominalypr, 12); // 16
143 _read(handle, &componentarray[s].boundsphere, 4); // 28
144// char msg[99];
145// sprintf_s(msg, sizeof(msg), "Type %i Component %i Bound %f", t, s, componentarray[s].boundsphere );
146// _write(logfile, msg, strlen(msg));
147 _read(handle, &componentarray[s].extendby, 12); // 32
148 _read(handle, &componentarray[s].texture, 2); // 44
149/* if (componentarray[s].texture>=shiptextureC)
150 {
151 char msg[99];
152 sprintf_s(msg, sizeof(msg), "Type %i Component %i Texture %i out-of-bounds", t, s, componentarray[s].texture );
153 _write(logfile, msg, strlen(msg));
154 }
155*/ _read(handle, &componentarray[s].nominalCG, 12); // 46
156 _read(handle, &componentarray[s].type, 1); // 58
157 _read(handle, &componentarray[s].speedExtend, 4); // 59
158 _read(handle, &componentarray[s].hingeaxis, 12); // 63
159 _read(handle, &componentarray[s].hingeCG, 12); // 75
160 _read(handle, &componentarray[s].hingeextent, 4); // 87
161 _read(handle, &componentarray[s].attachto, 2); // 91
162
163 if (version == 2.44f)
164 {
165 _read(handle, &componentarray[s].useLightMap, 1); // 93
166 _read(handle, &componentarray[s].speedRetract, 4); // 59
167 }
168 else
169 {
171 componentarray[s].useLightMap = true;
172 }
173
174 // 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);
175 // _write(logfile, msg, strlen(msg));
176
177 // Dynamic
178 componentarray[s].extended = 0.0f;
179 }
180 _close(handle);
181
182 sprintf_s(msg, sizeof(msg), "LoadDIX() finished component list");
183 logger->Log(msg);
184
185}
186
187unsigned short PolyObj::GetComponents() const
188{
189 return componentTotal;
190}
191
193{
194 if (i >= componentTotal || i < 0)
195 {
196 logger->Log("PolyObj::GetComponent out-of-bound array value!", Logger::Level::Fatal);
197 return s_mesh_component();
198 }
199
200 return componentarray[i];
201}
202
204{
205 if (componentarray)
206 {
207 char msg[99];
208 sprintf_s(msg, sizeof msg, "PolyObj %s CA", filename);
211 }
212}
213
#define SAFE_DELETE_ARRAY(value)
#define V(expression)
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
void AddTrackedResource(const char *name, Rendering::ResourcePool pool=Rendering::ResourcePool::Managed)
void UpdateTrackedResource(const char *name, int status)
std::unique_ptr< Logger > logger
Definition GameClass.h:109
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
@ Warn
Definition Logger.h:26
@ Fatal
Definition Logger.h:29
@ Error
Definition Logger.h:28
void ReleaseComponents()
Definition polyobj.cpp:203
PolyObj()
Definition polyobj.h:16
s_mesh_component GetComponent(short i) const
Definition polyobj.cpp:192
void LoadDIX(const char *prmFilename)
Definition polyobj.cpp:92
unsigned short GetComponents() const
Definition polyobj.cpp:187
s_mesh_component * componentarray
Definition polyobj.h:11
char filename[MAX_PATH]
Definition polyobj.h:12
s_polygon_object LoadDAW(const char *filename) const
Definition polyobj.cpp:21
GameClass * game
Definition polyobj.h:9
unsigned short componentTotal
Definition polyobj.h:13
Logger * logger
Definition polyobj.h:10
HRESULT Lock(UINT offset, UINT bytes, void **output, DWORD flags)
#define RISE_FVF_VERTEX
Definition globals.h:118
constexpr uint32_t WRITEONLY
const wchar_t * ErrorString(HRESULT code)
RenderDevice * ClientRenderer()
float speedExtend
Definition globals.h:409
float speedRetract
Definition globals.h:409
Rendering::RenderBuffer * VB
Definition globals.h:360