68 if (m && scale > 0 && strlen(
filename) > 0) {
76 ::MessageBox(0,
"File Open Failed:\nMagic could not open the requested file.",
"ERROR", MB_OK);
80 ZeroMemory(
pname, 64);
84 fread(file_id, 4, 1, fp);
88 if (!strcmp(file_id,
"MAG6")) {
91 else if (!strcmp(file_id,
"MAG5")) {
94 else if (!strcmp(file_id,
"MAG4")) {
98 ::MessageBox(0,
"File Open Failed:\nThe requested file uses an invalid format.",
"ERROR", MB_OK);
141 ::MessageBox(0,
"Save Failed:\nMagic could not open the file for writing.",
"ERROR", MB_OK);
145 fwrite(
"MAG6", 4, 1, fp);
158 if (bmp && !textures.
contains(bmp)) {
164 if (bmp && !textures.
contains(bmp)) {
170 if (bmp && !textures.
contains(bmp)) {
176 if (bmp && !textures.
contains(bmp)) {
191 fwrite(&ntex, 4, 1, fp);
192 fwrite(&nmtls, 4, 1, fp);
193 fwrite(&nsurfs, 4, 1, fp);
213 ZeroMemory(&m6,
sizeof(m6));
246 fwrite(&m6,
sizeof(m6), 1, fp);
256 BYTE namelen = strlen(s->
Name()) + 1;
258 fwrite(&nverts, 4, 1, fp);
259 fwrite(&npolys, 4, 1, fp);
260 fwrite(&namelen, 1, 1, fp);
261 fwrite(s->
Name(), 1, namelen, fp);
267 for (
int v = 0; v < nverts; v++) {
268 fwrite(&vset->loc[v],
sizeof(
float), 3, fp);
269 fwrite(&vset->nrm[v],
sizeof(
float), 3, fp);
270 fwrite(&vset->tu[v],
sizeof(
float), 1, fp);
271 fwrite(&vset->tv[v],
sizeof(
float), 1, fp);
275 for (
int n = 0; n < npolys; n++) {
276 Poly& poly = polys[n];
277 BYTE poly_nverts = (BYTE) poly.
nverts;
278 BYTE material_index = 0;
282 while (++m_iter && !material_index) {
284 material_index = m_iter.
index() + 1;
287 for (
int i = 0; i < poly_nverts; i++) {
288 poly_verts[i] = poly.
verts[i];
291 fwrite(&poly_nverts,
sizeof(BYTE), 1, fp);
292 fwrite(&material_index,
sizeof(BYTE), 1, fp);
293 fwrite(&poly_verts[0],
sizeof(WORD), poly_nverts, fp);
314 static void LoadPlane(
Plane& p, FILE* fp)
320 static void LoadFlags(LPDWORD flags, FILE* fp)
323 fread(&magic_flags,
sizeof(DWORD), 1, fp);
336 const DWORD magic_mask = 0x0fc3;
338 *flags = magic_flags & magic_mask;
343 static int mcomp(
const void* a,
const void* b)
365 fread(&ntex,
sizeof(ntex), 1, fp);
366 fread(&nsurfs,
sizeof(nsurfs), 1, fp);
383 strcpy_s(mtl->
name,
"(default)");
389 for (
int i = 0; i < ntex; i++) {
406 fread(tname, 32, 1, fp);
408 strcpy_s(mtl->
name, tname);
410 char* dot = strrchr(mtl->
name,
'.');
414 char* plus = strrchr(mtl->
name,
'+');
425 fread(&nverts, 4, 1, fp);
426 fread(&npolys, 4, 1, fp);
429 int mag_nverts = nverts;
430 int next_vert = nverts;
445 ZeroMemory(vset->
loc, nverts *
sizeof(
Vec3));
446 ZeroMemory(vset->
diffuse, nverts *
sizeof(DWORD));
447 ZeroMemory(vset->
specular, nverts *
sizeof(DWORD));
448 ZeroMemory(vset->
tu, nverts *
sizeof(
float));
449 ZeroMemory(vset->
tv, nverts *
sizeof(
float));
450 ZeroMemory(vset->
rw, nverts *
sizeof(
float));
454 for (v = 0; v < mag_nverts; v++) {
458 fread(&vert,
sizeof(
Vec3), 1, fp);
459 fread(&norm,
sizeof(
Vec3), 1, fp);
460 fread(&vstate,
sizeof(DWORD), 1, fp);
463 vert *= (float) scale;
484 int vert_index_buffer[32];
485 float texture_index_buffer[32];
487 for (
int n = 0; n < npolys; n++) {
488 Poly& poly = polys[n];
491 fread(&dummy_flags,
sizeof(DWORD), 1, fp);
492 fread(&dummy_center,
sizeof(
Vec3), 1, fp);
493 LoadPlane(poly.
plane, fp);
494 fread(&dummy_color,
sizeof(DWORD), 1, fp);
495 fread(&texture_num,
sizeof(
int), 1, fp);
497 if (texture_num >= 0 && texture_num < ntex) {
503 if (dummy_flags & 2) {
529 poly.
visible = (BYTE) (dummy_flags & 1);
531 fread(&poly_nverts,
sizeof(
int), 1, fp);
532 fread(vert_index_buffer,
sizeof(
int), poly_nverts, fp);
534 if (poly_nverts == 3)
537 else if (poly_nverts == 4)
540 poly.
nverts = poly_nverts;
541 for (
int vi = 0; vi < poly_nverts; vi++) {
542 int v = vert_index_buffer[vi];
544 if (vset->
rw[v] > 0) {
553 fread(texture_index_buffer,
sizeof(
float), poly_nverts, fp);
554 for (
int vi = 0; vi < poly_nverts; vi++) {
555 int v = poly.
verts[vi];
556 vset->
tu[v] = texture_index_buffer[vi];
559 fread(texture_index_buffer,
sizeof(
float), poly_nverts, fp);
560 for (
int vi = 0; vi < poly_nverts; vi++) {
561 int v = poly.
verts[vi];
562 vset->
tv[v] = texture_index_buffer[vi];
566 fread(unused, 16, 1, fp);
570 for (
int n = 0; n < npolys; n++) {
571 Poly& poly = polys[n];
579 int poly_nverts = poly.
nverts;
581 for (
int vi = 0; vi < poly_nverts; vi++) {
582 int v = poly.
verts[vi];
589 qsort((
void*) polys, npolys,
sizeof(
Poly), mcomp);
594 for (
int n = 0; n < npolys; n++) {
606 segment->
polys = &polys[n];
620 result = nverts && npolys;
639 fread(&ntex,
sizeof(ntex), 1, fp);
640 fread(&nmtls,
sizeof(nmtls), 1, fp);
641 fread(&nsurfs,
sizeof(nsurfs), 1, fp);
645 char* buffer =
new char[ntex];
649 fread(buffer, ntex, 1, fp);
651 while (p < buffer + ntex) {
661 for (i = 0; i < nmtls; i++) {
665 fread(&m6,
sizeof(m6), 1, fp);
708 for (i = 0; i < nsurfs; i++) {
714 fread(&nverts, 4, 1, fp);
715 fread(&npolys, 4, 1, fp);
716 fread(&namelen, 1, 1, fp);
717 fread(name, 1, namelen, fp);
727 ZeroMemory(polys,
sizeof(
Poly) * npolys);
730 for (
int v = 0; v < nverts; v++) {
731 fread(&vset->
loc[v],
sizeof(
float), 3, fp);
732 fread(&vset->
nrm[v],
sizeof(
float), 3, fp);
733 fread(&vset->
tu[v],
sizeof(
float), 1, fp);
734 fread(&vset->
tv[v],
sizeof(
float), 1, fp);
742 for (
int n = 0; n < npolys; n++) {
743 Poly& poly = polys[n];
744 BYTE poly_nverts = 0;
745 BYTE material_index = 0;
748 fread(&poly_nverts,
sizeof(BYTE), 1, fp);
749 fread(&material_index,
sizeof(BYTE), 1, fp);
750 fread(&poly_verts[0],
sizeof(WORD), poly_nverts, fp);
752 if (poly_nverts >= 3) {
753 poly.
nverts = poly_nverts;
755 for (
int i = 0; i < poly_nverts; i++) {
756 poly.
verts[i] = poly_verts[i];
763 if (material_index > 0) {
778 else if (poly.
nverts == 4)
788 qsort((
void*) polys, npolys,
sizeof(
Poly), mcomp);
793 for (
int n = 0; n < npolys; n++) {
805 segment->
polys = &polys[n];
820 result = nverts && npolys;