summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-04-10 00:53:34 +0200
committerAki <please@ignore.pl>2024-04-10 00:53:34 +0200
commit4409303f4f2999ae7c4e65ff7553480e6968740f (patch)
treed111aac66cbaf312b59c082e8679c914946114cb
parent3cc6b923edb69907794c3dc6daa61a445b3a56c0 (diff)
downloadstarshatter-4409303f4f2999ae7c4e65ff7553480e6968740f.zip
starshatter-4409303f4f2999ae7c4e65ff7553480e6968740f.tar.gz
starshatter-4409303f4f2999ae7c4e65ff7553480e6968740f.tar.bz2
Fixed converters crashing on loading textures when they were available
-rw-r--r--MagicEx/src/MagicLoad.cpp169
-rw-r--r--MagicEx/src/convert.inl.h3
-rw-r--r--StarsEx/D3DXImage.cpp4
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 <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;
}
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,