From 4409303f4f2999ae7c4e65ff7553480e6968740f Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 10 Apr 2024 00:53:34 +0200 Subject: Fixed converters crashing on loading textures when they were available --- MagicEx/src/MagicLoad.cpp | 169 ++++++++++++++++++++-------------------------- MagicEx/src/convert.inl.h | 3 + StarsEx/D3DXImage.cpp | 4 +- 3 files changed, 80 insertions(+), 96 deletions(-) diff --git a/MagicEx/src/MagicLoad.cpp b/MagicEx/src/MagicLoad.cpp index 6687f8b..f398ab3 100644 --- a/MagicEx/src/MagicLoad.cpp +++ b/MagicEx/src/MagicLoad.cpp @@ -6,6 +6,15 @@ AUTHOR: John DiCamillo */ +#include + +#include +#include +#include +#include +#include +#include + #include #include @@ -15,116 +24,88 @@ #include -int LoadBuffer(const char* filename, BYTE*& buf, bool null_terminate) +int +LoadBuffer(const char* filename, BYTE*& buf, bool null_terminate) { - buf = 0; - - FILE* f = ::fopen(filename, "rb"); - - if (f) { - ::fseek(f, 0, SEEK_END); - int len = ftell(f); - ::fseek(f, 0, SEEK_SET); - - if (null_terminate) { - buf = new BYTE[len+1]; - if (buf) - buf[len] = 0; - } - - else { - buf = new BYTE[len]; - } - + buf = nullptr; + std::fstream file {filename, file.binary | file.in}; + if (!file.is_open()) + return 0; + file.ignore(std::numeric_limits::max()); + const auto size = file.gcount(); + file.clear(); + file.seekg(0); + if (null_terminate) { + buf = new BYTE[size + 1]; if (buf) - ::fread(buf, len, 1, f); - - ::fclose(f); - - return len; + buf[size] = 0; + } + else { + buf = new BYTE[size]; + } + if (buf) { + file.read(reinterpret_cast(buf), size); + return file.gcount(); } - return 0; } -int LoadTexture(const char* fname, Bitmap*& bitmap, int type) +int +LoadTexture(const char* fname, Bitmap*& bitmap, int type) { - int result = 0; - - if (!fname || !*fname) - return result; - + if (fname == nullptr || fname[0] == 0) + return 0; bitmap = Bitmap::CheckCache(fname); - - if (!bitmap) { - bool pcx_file = strstr(fname, ".pcx") || strstr(fname, ".PCX"); - - // handle PCX formats: - if (pcx_file) { - PcxImage pcx; - - if (pcx.Load((char*) fname) == PCX_OK) { - bitmap = new Bitmap; - - // 32-bit image - if (pcx.himap) { - bitmap->CopyHighColorImage(pcx.width, pcx.height, pcx.himap); - } - - // 8-bit image, check for 32-bit image as well - else if (pcx.bitmap) { - bitmap->CopyImage(pcx.width, pcx.height, pcx.bitmap); - - char tmp[256]; - int len = strlen(fname); - bool found = false; - - ZeroMemory(tmp, sizeof(tmp)); - - for (int i = 0; i < len && !found; i++) { - if (strstr(fname + i, ".pcx") == (fname+i)) { - found = true; - } - else { - tmp[i] = fname[i]; - } - } - - if (found) { - strcat_s(tmp, "+.pcx"); - if (pcx.Load(tmp) == PCX_OK && pcx.himap != 0) { - bitmap->CopyHighColorImage(pcx.width, pcx.height, pcx.himap); - } - } - } - } + if (bitmap) + return 0; // legacy behaviour + const bool is_pcx = std::strstr(fname, ".pcx") || std::strstr(fname, ".PCX"); + if (is_pcx) { + PcxImage pcx; + if (pcx.Load((char*) fname) != PCX_OK) // Casting away const! + return 0; + bitmap = new Bitmap; + if (bitmap == nullptr) + return 0; + if (pcx.himap) { + bitmap->CopyHighColorImage(pcx.width, pcx.height, pcx.himap); + } + else if (pcx.bitmap) { + bitmap->CopyImage(pcx.width, pcx.height, pcx.bitmap); + std::vector buf(std::strlen(fname) + 2); + std::strcpy(buf.data(), fname); + if (auto* ext = std::strstr(buf.data(), ".pcx")) + *ext = 0; + std::strcat(buf.data(), "+.pcx"); + if (pcx.Load(buf.data()) == PCX_OK && pcx.himap != 0) + bitmap->CopyHighColorImage(pcx.width, pcx.height, pcx.himap); } - - // for all other formats, use D3DX: else { - D3DXImage d3dx; - if (d3dx.Load((char*) fname)) { - bitmap = new Bitmap; - bitmap->CopyHighColorImage(d3dx.width, d3dx.height, d3dx.image); - } + // legacy no-op } - - if (bitmap) { - LoadAlpha(fname, *bitmap, type); - - bitmap->SetFilename(fname); - bitmap->SetType(type); - bitmap->MakeTexture(); - - Bitmap::AddToCache(bitmap); + } + else { + D3DXImage d3dx; + bitmap = new Bitmap; + if (d3dx.Load((char*) fname)) { + std::cout << "d3dx done" << std::endl; + bitmap = new Bitmap; + bitmap->CopyHighColorImage(d3dx.width, d3dx.height, d3dx.image); } } - - return result; + if (bitmap) { + LoadAlpha(fname, *bitmap, type); + bitmap->SetFilename(fname); + bitmap->SetType(type); + bitmap->MakeTexture(); + Bitmap::AddToCache(bitmap); + } + return 0; } -int LoadAlpha(const char* name, Bitmap& bitmap, int type) + +int +LoadAlpha(const char* name, Bitmap& bitmap, int type) { PcxImage pcx; D3DXImage d3dx; diff --git a/MagicEx/src/convert.inl.h b/MagicEx/src/convert.inl.h index 7bf29ee..4dbe771 100644 --- a/MagicEx/src/convert.inl.h +++ b/MagicEx/src/convert.inl.h @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -21,6 +22,7 @@ template int convert(const std::string& input, const std::string& output) { + Clock::Init(); DataLoader::Initialize(); auto solid = std::make_unique(); if (!ImportInto(input.c_str(), solid.get())) @@ -32,6 +34,7 @@ convert(const std::string& input, const std::string& output) if (!exporter.Save(model)) return 1; DataLoader::Close(); + Clock::Close(); return 0; } diff --git a/StarsEx/D3DXImage.cpp b/StarsEx/D3DXImage.cpp index 21b1fae..5145ac5 100644 --- a/StarsEx/D3DXImage.cpp +++ b/StarsEx/D3DXImage.cpp @@ -11,7 +11,6 @@ D3DX image file loader */ - #include "D3DXImage.h" #include "VideoDX9.h" @@ -66,7 +65,6 @@ bool D3DXImage::Load(char *filename) if (buf) { fread(buf, len, 1, f); fclose(f); - success = LoadBuffer(buf, len); } @@ -100,6 +98,8 @@ bool D3DXImage::LoadBuffer(BYTE* buf, int len) IDirect3DSurface9* surf = 0; IDirect3DDevice9* dev = VideoDX9::GetD3DDevice9(); + if (dev == nullptr) + return success; hr = dev->CreateOffscreenPlainSurface( width, -- cgit v1.1