diff options
author | Aki <please@ignore.pl> | 2024-04-04 02:16:26 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2024-04-04 02:16:26 +0200 |
commit | 56f76f4d524ca12bef5c775da6e7282e8322a951 (patch) | |
tree | fce3656dd3048df2c2eb61236a53b0972e9abc17 | |
parent | e0f9f411056c235947b809cb21c477aa5acca1b4 (diff) | |
download | starshatter-56f76f4d524ca12bef5c775da6e7282e8322a951.zip starshatter-56f76f4d524ca12bef5c775da6e7282e8322a951.tar.gz starshatter-56f76f4d524ca12bef5c775da6e7282e8322a951.tar.bz2 |
Extracted ModelFile related functionality out of Magic2 to a library
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | Magic2/CMakeLists.txt | 15 | ||||
-rw-r--r-- | Magic2/Grid.cpp | 1 | ||||
-rw-r--r-- | Magic2/MagicDoc.cpp | 166 | ||||
-rw-r--r-- | Magic2/MagicDoc.h | 6 | ||||
-rw-r--r-- | Magic2/MaterialDialog.cpp | 1 | ||||
-rw-r--r-- | MagicEx/CMakeLists.txt | 10 | ||||
-rw-r--r-- | MagicEx/MagicLoad.cpp | 178 | ||||
-rw-r--r-- | MagicEx/MagicLoad.h | 18 | ||||
-rw-r--r-- | MagicEx/ModelFile3DS.cpp (renamed from Magic2/ModelFile3DS.cpp) | 13 | ||||
-rw-r--r-- | MagicEx/ModelFile3DS.h (renamed from Magic2/ModelFile3DS.h) | 13 | ||||
-rw-r--r-- | MagicEx/ModelFileMAG.cpp (renamed from Magic2/ModelFileMAG.cpp) | 11 | ||||
-rw-r--r-- | MagicEx/ModelFileMAG.h (renamed from Magic2/ModelFileMAG.h) | 11 | ||||
-rw-r--r-- | MagicEx/ModelFileOBJ.cpp (renamed from Magic2/ModelFileOBJ.cpp) | 13 | ||||
-rw-r--r-- | MagicEx/ModelFileOBJ.h (renamed from Magic2/ModelFileOBJ.h) | 13 | ||||
-rw-r--r-- | MagicEx/l3ds.cpp (renamed from Magic2/l3ds.cpp) | 3 | ||||
-rw-r--r-- | MagicEx/l3ds.h (renamed from Magic2/l3ds.h) | 0 |
17 files changed, 234 insertions, 239 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ccd32d..be5bb74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ add_subdirectory(data) add_subdirectory(DefinitionEx) add_subdirectory(FoundationEx) add_subdirectory(InfoEx) +add_subdirectory(MagicEx) if(MSVC) add_subdirectory(Magic2) endif() diff --git a/Magic2/CMakeLists.txt b/Magic2/CMakeLists.txt index 3ccd588..186afdf 100644 --- a/Magic2/CMakeLists.txt +++ b/Magic2/CMakeLists.txt @@ -8,16 +8,12 @@ add_executable( Editor.cpp Grid.cpp GridProps.cpp - l3ds.cpp Magic.cpp Magic.rc MagicDoc.cpp MagicView.cpp MainFrm.cpp MaterialDialog.cpp - ModelFile3DS.cpp - ModelFileMAG.cpp - ModelFileOBJ.cpp ModelView.cpp Selection.cpp Selector.cpp @@ -41,12 +37,5 @@ target_precompile_headers( Magic2 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/StdAfx.h ) -target_link_libraries( - Magic2 - PUBLIC FoundationEx - PUBLIC StarsEx - ) -target_compile_definitions( - Magic2 - PRIVATE _AFXDLL - ) +target_link_libraries(Magic2 PUBLIC FoundationEx MagicEx StarsEx) +target_compile_definitions(Magic2 PRIVATE _AFXDLL) diff --git a/Magic2/Grid.cpp b/Magic2/Grid.cpp index 0f617e6..e988118 100644 --- a/Magic2/Grid.cpp +++ b/Magic2/Grid.cpp @@ -15,6 +15,7 @@ #include "Magic.h" #include "MagicDoc.h" +#include "MagicLoad.h" #include "Grid.h" #include "ActiveWindow.h" diff --git a/Magic2/MagicDoc.cpp b/Magic2/MagicDoc.cpp index 6bd327c..55ecd8d 100644 --- a/Magic2/MagicDoc.cpp +++ b/Magic2/MagicDoc.cpp @@ -405,40 +405,6 @@ MagicDoc::ExportFile(LPCTSTR path_name) // +--------------------------------------------------------------------+ -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]; - } - - if (buf) - ::fread(buf, len, 1, f); - - ::fclose(f); - - return len; - } - - return 0; -} - -// +--------------------------------------------------------------------+ - void MagicDoc::DeleteContents() { CDocument::DeleteContents(); @@ -453,138 +419,6 @@ void MagicDoc::DeleteContents() selection->Clear(); } -// +--------------------------------------------------------------------+ - -int LoadTexture(const char* fname, Bitmap*& bitmap, int type) -{ - int result = 0; - - if (!fname || !*fname) - return result; - - 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); - } - } - } - } - } - - // 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); - } - } - - if (bitmap) { - LoadAlpha(fname, *bitmap, type); - - bitmap->SetFilename(fname); - bitmap->SetType(type); - bitmap->MakeTexture(); - - Bitmap::AddToCache(bitmap); - } - } - - return result; -} - -int LoadAlpha(const char* name, Bitmap& bitmap, int type) -{ - PcxImage pcx; - D3DXImage d3dx; - bool pcx_file = strstr(name, ".pcx") || strstr(name, ".PCX"); - bool bmp_file = strstr(name, ".bmp") || strstr(name, ".BMP"); - bool jpg_file = strstr(name, ".jpg") || strstr(name, ".JPG"); - bool png_file = strstr(name, ".png") || strstr(name, ".PNG"); - bool tga_file = strstr(name, ".tga") || strstr(name, ".TGA"); - - // check for an associated alpha-only (grayscale) bitmap: - char filename[256]; - strcpy_s(filename, name); - - char* dot = strrchr(filename, '.'); - if (dot && pcx_file) - strcpy(dot, "@.pcx"); - else if (dot && bmp_file) - strcpy(dot, "@.bmp"); - else if (dot && jpg_file) - strcpy(dot, "@.jpg"); - else if (dot && png_file) - strcpy(dot, "@.png"); - else if (dot && tga_file) - strcpy(dot, "@.tga"); - else - return 0; - - // first try to load from current directory: - bool loaded = false; - - if (pcx_file) - loaded = pcx.Load(filename) == PCX_OK; - - else - loaded = d3dx.Load(filename); - - // now copy the alpha values into the bitmap: - if (loaded) { - if (pcx_file && pcx.bitmap) { - bitmap.CopyAlphaImage(pcx.width, pcx.height, pcx.bitmap); - } - else if (pcx_file && pcx.himap) { - bitmap.CopyAlphaRedChannel(pcx.width, pcx.height, pcx.himap); - } - else if (d3dx.image) { - bitmap.CopyAlphaRedChannel(d3dx.width, d3dx.height, d3dx.image); - } - } - - return 0; -} - - void MagicDoc::OnSurfaceOptimize() { if (solid && solid->GetModel()) { diff --git a/Magic2/MagicDoc.h b/Magic2/MagicDoc.h index 6242842..f9641f2 100644 --- a/Magic2/MagicDoc.h +++ b/Magic2/MagicDoc.h @@ -107,12 +107,6 @@ protected: // +--------------------------------------------------------------------+ -int LoadBuffer(const char* filename, BYTE*& buf, bool null_terminate=false); -int LoadTexture(const char* name, Bitmap*& bmp, int type=0); -int LoadAlpha(const char* name, Bitmap& bitmap, int type=0); - -// +--------------------------------------------------------------------+ - //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. diff --git a/Magic2/MaterialDialog.cpp b/Magic2/MaterialDialog.cpp index 4021dab..8359eef 100644 --- a/Magic2/MaterialDialog.cpp +++ b/Magic2/MaterialDialog.cpp @@ -15,6 +15,7 @@ #include "Magic.h" #include "MagicDoc.h" #include "MagicView.h" +#include "MagicLoad.h" #include "MaterialDialog.h" #include "Selection.h" #include "Selector.h" diff --git a/MagicEx/CMakeLists.txt b/MagicEx/CMakeLists.txt new file mode 100644 index 0000000..965312c --- /dev/null +++ b/MagicEx/CMakeLists.txt @@ -0,0 +1,10 @@ +project(MagicEx) +add_library( + ${PROJECT_NAME} STATIC + l3ds.cpp + MagicLoad.cpp + ModelFile3DS.cpp + ModelFileMAG.cpp + ModelFileOBJ.cpp) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(${PROJECT_NAME} PUBLIC StarsEx) diff --git a/MagicEx/MagicLoad.cpp b/MagicEx/MagicLoad.cpp new file mode 100644 index 0000000..2b13a93 --- /dev/null +++ b/MagicEx/MagicLoad.cpp @@ -0,0 +1,178 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. + + AUTHOR: John DiCamillo +*/ + +#include "MagicLoad.h" + +#include "Bitmap.h" +#include "Color.h" +#include "D3DXImage.h" +#include "Geometry.h" +#include "Pcx.h" + + +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]; + } + + if (buf) + ::fread(buf, len, 1, f); + + ::fclose(f); + + return len; + } + + return 0; +} + + +int LoadTexture(const char* fname, Bitmap*& bitmap, int type) +{ + int result = 0; + + if (!fname || !*fname) + return result; + + 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); + } + } + } + } + } + + // 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); + } + } + + if (bitmap) { + LoadAlpha(fname, *bitmap, type); + + bitmap->SetFilename(fname); + bitmap->SetType(type); + bitmap->MakeTexture(); + + Bitmap::AddToCache(bitmap); + } + } + + return result; +} + +int LoadAlpha(const char* name, Bitmap& bitmap, int type) +{ + PcxImage pcx; + D3DXImage d3dx; + bool pcx_file = strstr(name, ".pcx") || strstr(name, ".PCX"); + bool bmp_file = strstr(name, ".bmp") || strstr(name, ".BMP"); + bool jpg_file = strstr(name, ".jpg") || strstr(name, ".JPG"); + bool png_file = strstr(name, ".png") || strstr(name, ".PNG"); + bool tga_file = strstr(name, ".tga") || strstr(name, ".TGA"); + + // check for an associated alpha-only (grayscale) bitmap: + char filename[256]; + strcpy_s(filename, name); + + char* dot = strrchr(filename, '.'); + if (dot && pcx_file) + strcpy(dot, "@.pcx"); + else if (dot && bmp_file) + strcpy(dot, "@.bmp"); + else if (dot && jpg_file) + strcpy(dot, "@.jpg"); + else if (dot && png_file) + strcpy(dot, "@.png"); + else if (dot && tga_file) + strcpy(dot, "@.tga"); + else + return 0; + + // first try to load from current directory: + bool loaded = false; + + if (pcx_file) + loaded = pcx.Load(filename) == PCX_OK; + + else + loaded = d3dx.Load(filename); + + // now copy the alpha values into the bitmap: + if (loaded) { + if (pcx_file && pcx.bitmap) { + bitmap.CopyAlphaImage(pcx.width, pcx.height, pcx.bitmap); + } + else if (pcx_file && pcx.himap) { + bitmap.CopyAlphaRedChannel(pcx.width, pcx.height, pcx.himap); + } + else if (d3dx.image) { + bitmap.CopyAlphaRedChannel(d3dx.width, d3dx.height, d3dx.image); + } + } + + return 0; +} diff --git a/MagicEx/MagicLoad.h b/MagicEx/MagicLoad.h new file mode 100644 index 0000000..27b6380 --- /dev/null +++ b/MagicEx/MagicLoad.h @@ -0,0 +1,18 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. + + AUTHOR: John DiCamillo +*/ + +#pragma once + +#include <windows.h> + +#include <Bitmap.h> + + +int LoadBuffer(const char* filename, BYTE*& buf, bool null_terminate=false); +int LoadTexture(const char* name, Bitmap*& bmp, int type=0); +int LoadAlpha(const char* name, Bitmap& bitmap, int type=0); diff --git a/Magic2/ModelFile3DS.cpp b/MagicEx/ModelFile3DS.cpp index fb07a82..4c4a157 100644 --- a/Magic2/ModelFile3DS.cpp +++ b/MagicEx/ModelFile3DS.cpp @@ -11,15 +11,14 @@ File loader for 3DStudio MAX 3DS format models */ -#include "StdAfx.h" -#include "Magic.h" -#include "MagicDoc.h" #include "ModelFile3DS.h" -#include "Bitmap.h" -#include "Polygon.h" -#include "Text.h" -#include "List.h" +#include <Bitmap.h> +#include <List.h> +#include <Polygon.h> +#include <Text.h> + +#include "MagicLoad.h" #include "l3ds.h" // +--------------------------------------------------------------------+ diff --git a/Magic2/ModelFile3DS.h b/MagicEx/ModelFile3DS.h index 5424e6c..3cac828 100644 --- a/Magic2/ModelFile3DS.h +++ b/MagicEx/ModelFile3DS.h @@ -11,12 +11,10 @@ File loader for 3DStudio MAX 3DS format models */ -#ifndef ModelFile3DS_h -#define ModelFile3DS_h +#pragma once -#include "Solid.h" +#include <Solid.h> -// +--------------------------------------------------------------------+ class ModelFile3DS : public ModelFile { @@ -26,11 +24,4 @@ public: virtual bool Load(Model* m, double scale=1.0); virtual bool Save(Model* m); - -protected: }; - -// +--------------------------------------------------------------------+ - -#endif ModelFile3DS_h - diff --git a/Magic2/ModelFileMAG.cpp b/MagicEx/ModelFileMAG.cpp index 70c7b43..fe076ed 100644 --- a/Magic2/ModelFileMAG.cpp +++ b/MagicEx/ModelFileMAG.cpp @@ -11,14 +11,13 @@ File loader for MAG format models */ -#include "StdAfx.h" -#include "Magic.h" -#include "MagicDoc.h" #include "ModelFileMAG.h" -#include "Bitmap.h" -#include "Polygon.h" -#include "List.h" +#include <Bitmap.h> +#include <List.h> +#include <Polygon.h> + +#include "MagicLoad.h" // +--------------------------------------------------------------------+ diff --git a/Magic2/ModelFileMAG.h b/MagicEx/ModelFileMAG.h index a005691..27466ae 100644 --- a/Magic2/ModelFileMAG.h +++ b/MagicEx/ModelFileMAG.h @@ -11,12 +11,10 @@ File loader for MAG format models */ -#ifndef ModelFileMAG_h -#define ModelFileMAG_h +#pragma once -#include "Solid.h" +#include <Solid.h> -// +--------------------------------------------------------------------+ class ModelFileMAG : public ModelFile { @@ -31,8 +29,3 @@ protected: virtual bool LoadMag5(FILE* fp, Model* m, double scale); virtual bool LoadMag6(FILE* fp, Model* m, double scale); }; - -// +--------------------------------------------------------------------+ - -#endif ModelFileMAG_h - diff --git a/Magic2/ModelFileOBJ.cpp b/MagicEx/ModelFileOBJ.cpp index 33591bd..c682bff 100644 --- a/Magic2/ModelFileOBJ.cpp +++ b/MagicEx/ModelFileOBJ.cpp @@ -11,15 +11,14 @@ File loader for Wavefront/OBJ format models */ -#include "StdAfx.h" -#include "Magic.h" -#include "MagicDoc.h" #include "ModelFileOBJ.h" -#include "Bitmap.h" -#include "Polygon.h" -#include "List.h" -#include "Text.h" +#include <Bitmap.h> +#include <Polygon.h> +#include <List.h> +#include <Text.h> + +#include "MagicLoad.h" // +--------------------------------------------------------------------+ diff --git a/Magic2/ModelFileOBJ.h b/MagicEx/ModelFileOBJ.h index 5d0f9a9..dcffc7d 100644 --- a/Magic2/ModelFileOBJ.h +++ b/MagicEx/ModelFileOBJ.h @@ -11,12 +11,10 @@ File loader for Wavefront/OBJ format models */ -#ifndef ModelFileOBJ_h -#define ModelFileOBJ_h +#pragma once -#include "Solid.h" +#include <Solid.h> -// +--------------------------------------------------------------------+ class ModelFileOBJ : public ModelFile { @@ -26,11 +24,4 @@ public: virtual bool Load(Model* m, double scale=1.0); virtual bool Save(Model* m); - -protected: }; - -// +--------------------------------------------------------------------+ - -#endif ModelFileOBJ_h - diff --git a/Magic2/l3ds.cpp b/MagicEx/l3ds.cpp index c23075c..4c34ab5 100644 --- a/Magic2/l3ds.cpp +++ b/MagicEx/l3ds.cpp @@ -1,8 +1,5 @@ // copyright (c) 2001 Lev Povalahev - -#include "StdAfx.h" - #include "l3ds.h" #include <stdio.h> diff --git a/Magic2/l3ds.h b/MagicEx/l3ds.h index ea0a032..ea0a032 100644 --- a/Magic2/l3ds.h +++ b/MagicEx/l3ds.h |