diff options
Diffstat (limited to 'MagicEx')
-rw-r--r-- | MagicEx/src/MagicLoad.cpp | 169 | ||||
-rw-r--r-- | MagicEx/src/convert.inl.h | 3 |
2 files changed, 78 insertions, 94 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 <windows.h> + +#include <cstring> +#include <fstream> +#include <ios> +#include <iostream> +#include <limits> +#include <vector> + #include <MagicLoad.h> #include <Bitmap.h> @@ -15,116 +24,88 @@ #include <Pcx.h> -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<std::streamsize>::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<char*>(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<char> 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 <memory> #include <string> +#include <Clock.h> #include <DataLoader.h> #include <Solid.h> @@ -21,6 +22,7 @@ template <typename Input, typename Output> int convert(const std::string& input, const std::string& output) { + Clock::Init(); DataLoader::Initialize(); auto solid = std::make_unique<Solid>(); if (!ImportInto<Input>(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; } |