From a1ea2f1ff1ecfd7a955632a01a9c6f5aa5c611e9 Mon Sep 17 00:00:00 2001 From: marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Mon, 28 Aug 2023 17:47:19 +0200 Subject: [PATCH 1/7] Split up io.cpp --- CMakeLists.txt | 2 +- src/game/editor/editor.cpp | 93 ++++++++++++++ src/game/editor/mapitems/map.cpp | 15 +++ .../editor/{io.cpp => mapitems/map_io.cpp} | 113 +----------------- 4 files changed, 111 insertions(+), 112 deletions(-) rename src/game/editor/{io.cpp => mapitems/map_io.cpp} (92%) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd51b267c..8f364d3b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2311,7 +2311,6 @@ if(CLIENT) editor.cpp editor.h explanations.cpp - io.cpp map_grid.cpp map_grid.h map_view.cpp @@ -2328,6 +2327,7 @@ if(CLIENT) mapitems/layer_tiles.cpp mapitems/layer_tune.cpp mapitems/map.cpp + mapitems/map_io.cpp mapitems/sound.cpp popups.cpp proof_mode.cpp diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 05d53c0cb..e54f79659 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -7824,4 +7824,97 @@ void CEditor::LoadCurrentMap() MapView()->SetWorldOffset(Center); } +bool CEditor::Save(const char *pFilename) +{ + // Check if file with this name is already being saved at the moment + if(std::any_of(std::begin(m_WriterFinishJobs), std::end(m_WriterFinishJobs), [pFilename](const std::shared_ptr &Job) { return str_comp(pFilename, Job->GetRealFileName()) == 0; })) + return false; + + return m_Map.Save(pFilename); +} + +bool CEditor::HandleMapDrop(const char *pFileName, int StorageType) +{ + return m_Map.HandleMapDrop(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE); +} + +bool CEditor::Load(const char *pFileName, int StorageType) +{ + const auto &&ErrorHandler = [this](const char *pErrorMessage) { + ShowFileDialogError("%s", pErrorMessage); + Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor/load", pErrorMessage); + }; + + Reset(); + bool Result = m_Map.Load(pFileName, StorageType, std::move(ErrorHandler)); + if(Result) + { + str_copy(m_aFileName, pFileName); + SortImages(); + SelectGameLayer(); + MapView()->OnMapLoad(); + } + else + { + m_aFileName[0] = 0; + Reset(); + } + return Result; +} + +bool CEditor::Append(const char *pFileName, int StorageType) +{ + CEditorMap NewMap; + NewMap.m_pEditor = this; + + const auto &&ErrorHandler = [this](const char *pErrorMessage) { + ShowFileDialogError("%s", pErrorMessage); + Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor/append", pErrorMessage); + }; + if(!NewMap.Load(pFileName, StorageType, std::move(ErrorHandler))) + return false; + + // modify indices + static const auto &&s_ModifyAddIndex = [](int AddAmount) { + return [AddAmount](int *pIndex) { + if(*pIndex >= 0) + *pIndex += AddAmount; + }; + }; + NewMap.ModifyImageIndex(s_ModifyAddIndex(m_Map.m_vpImages.size())); + NewMap.ModifySoundIndex(s_ModifyAddIndex(m_Map.m_vpSounds.size())); + NewMap.ModifyEnvelopeIndex(s_ModifyAddIndex(m_Map.m_vpEnvelopes.size())); + + // transfer images + for(const auto &pImage : NewMap.m_vpImages) + m_Map.m_vpImages.push_back(pImage); + NewMap.m_vpImages.clear(); + + // transfer sounds + for(const auto &pSound : NewMap.m_vpSounds) + m_Map.m_vpSounds.push_back(pSound); + NewMap.m_vpSounds.clear(); + + // transfer envelopes + for(const auto &pEnvelope : NewMap.m_vpEnvelopes) + m_Map.m_vpEnvelopes.push_back(pEnvelope); + NewMap.m_vpEnvelopes.clear(); + + // transfer groups + for(const auto &pGroup : NewMap.m_vpGroups) + { + if(pGroup != NewMap.m_pGameGroup) + { + pGroup->m_pMap = &m_Map; + m_Map.m_vpGroups.push_back(pGroup); + } + } + NewMap.m_vpGroups.clear(); + + SortImages(); + + // all done \o/ + return true; +} + IEditor *CreateEditor() { return new CEditor; } diff --git a/src/game/editor/mapitems/map.cpp b/src/game/editor/mapitems/map.cpp index 29e683223..5bc5338bc 100644 --- a/src/game/editor/mapitems/map.cpp +++ b/src/game/editor/mapitems/map.cpp @@ -177,3 +177,18 @@ void CEditorMap::MakeTuneLayer(const std::shared_ptr &pLayer) m_pTuneLayer = std::static_pointer_cast(pLayer); m_pTuneLayer->m_pEditor = m_pEditor; } + +bool CEditorMap::HandleMapDrop(const char *pFileName, int StorageType) +{ + if(m_pEditor->HasUnsavedData()) + { + str_copy(m_pEditor->m_aFileNamePending, pFileName); + m_pEditor->m_PopupEventType = CEditor::POPEVENT_LOADDROP; + m_pEditor->m_PopupEventActivated = true; + return true; + } + else + { + return m_pEditor->Load(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE); + } +} diff --git a/src/game/editor/io.cpp b/src/game/editor/mapitems/map_io.cpp similarity index 92% rename from src/game/editor/io.cpp rename to src/game/editor/mapitems/map_io.cpp index 503c97a48..f0962f913 100644 --- a/src/game/editor/io.cpp +++ b/src/game/editor/mapitems/map_io.cpp @@ -1,6 +1,5 @@ -/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ -/* If you are missing that file, acquire a complete release at teeworlds.com. */ -#include "editor.h" +#include + #include #include #include @@ -31,15 +30,6 @@ struct CSoundSource_DEPRECATED int m_SoundEnvOffset; }; -bool CEditor::Save(const char *pFilename) -{ - // Check if file with this name is already being saved at the moment - if(std::any_of(std::begin(m_WriterFinishJobs), std::end(m_WriterFinishJobs), [pFilename](const std::shared_ptr &Job) { return str_comp(pFilename, Job->GetRealFileName()) == 0; })) - return false; - - return m_Map.Save(pFilename); -} - bool CEditorMap::Save(const char *pFileName) { char aFileNameTmp[IO_MAX_PATH_LENGTH]; @@ -426,50 +416,6 @@ bool CEditorMap::Save(const char *pFileName) return true; } -bool CEditor::HandleMapDrop(const char *pFileName, int StorageType) -{ - return m_Map.HandleMapDrop(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE); -} - -bool CEditorMap::HandleMapDrop(const char *pFileName, int StorageType) -{ - if(m_pEditor->HasUnsavedData()) - { - str_copy(m_pEditor->m_aFileNamePending, pFileName); - m_pEditor->m_PopupEventType = CEditor::POPEVENT_LOADDROP; - m_pEditor->m_PopupEventActivated = true; - return true; - } - else - { - return m_pEditor->Load(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE); - } -} - -bool CEditor::Load(const char *pFileName, int StorageType) -{ - const auto &&ErrorHandler = [this](const char *pErrorMessage) { - ShowFileDialogError("%s", pErrorMessage); - Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "editor/load", pErrorMessage); - }; - - Reset(); - bool Result = m_Map.Load(pFileName, StorageType, std::move(ErrorHandler)); - if(Result) - { - str_copy(m_aFileName, pFileName); - SortImages(); - SelectGameLayer(); - MapView()->OnMapLoad(); - } - else - { - m_aFileName[0] = 0; - Reset(); - } - return Result; -} - bool CEditorMap::Load(const char *pFileName, int StorageType, const std::function &ErrorHandler) { CDataFileReader DataFile; @@ -1064,58 +1010,3 @@ void CEditorMap::PerformSanityChecks(const std::functionPrint(IConsole::OUTPUT_LEVEL_STANDARD, "editor/append", pErrorMessage); - }; - if(!NewMap.Load(pFileName, StorageType, std::move(ErrorHandler))) - return false; - - // modify indices - static const auto &&s_ModifyAddIndex = [](int AddAmount) { - return [AddAmount](int *pIndex) { - if(*pIndex >= 0) - *pIndex += AddAmount; - }; - }; - NewMap.ModifyImageIndex(s_ModifyAddIndex(m_Map.m_vpImages.size())); - NewMap.ModifySoundIndex(s_ModifyAddIndex(m_Map.m_vpSounds.size())); - NewMap.ModifyEnvelopeIndex(s_ModifyAddIndex(m_Map.m_vpEnvelopes.size())); - - // transfer images - for(const auto &pImage : NewMap.m_vpImages) - m_Map.m_vpImages.push_back(pImage); - NewMap.m_vpImages.clear(); - - // transfer sounds - for(const auto &pSound : NewMap.m_vpSounds) - m_Map.m_vpSounds.push_back(pSound); - NewMap.m_vpSounds.clear(); - - // transfer envelopes - for(const auto &pEnvelope : NewMap.m_vpEnvelopes) - m_Map.m_vpEnvelopes.push_back(pEnvelope); - NewMap.m_vpEnvelopes.clear(); - - // transfer groups - for(const auto &pGroup : NewMap.m_vpGroups) - { - if(pGroup != NewMap.m_pGameGroup) - { - pGroup->m_pMap = &m_Map; - m_Map.m_vpGroups.push_back(pGroup); - } - } - NewMap.m_vpGroups.clear(); - - SortImages(); - - // all done \o/ - return true; -} From 5f60d68e8bed35284e2d7f1ad259b0eecf137372 Mon Sep 17 00:00:00 2001 From: marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Mon, 28 Aug 2023 17:55:29 +0200 Subject: [PATCH 2/7] extract `CEditorImage` into separate header --- CMakeLists.txt | 1 + src/game/editor/editor.cpp | 2 ++ src/game/editor/editor.h | 29 +----------------- src/game/editor/mapitems/image.cpp | 2 ++ src/game/editor/mapitems/image.h | 39 ++++++++++++++++++++++++ src/game/editor/mapitems/layer_quads.cpp | 2 ++ src/game/editor/mapitems/layer_tiles.cpp | 2 ++ src/game/editor/mapitems/map.cpp | 2 ++ src/game/editor/mapitems/map_io.cpp | 2 ++ src/game/editor/popups.cpp | 1 + src/game/editor/tileart.cpp | 2 ++ 11 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 src/game/editor/mapitems/image.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f364d3b5..bf3edad33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2316,6 +2316,7 @@ if(CLIENT) map_view.cpp map_view.h mapitems/image.cpp + mapitems/image.h mapitems/layer_front.cpp mapitems/layer_game.cpp mapitems/layer_group.cpp diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index e54f79659..6a5dd13c2 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -31,6 +31,8 @@ #include #include +#include + #include "auto_map.h" #include "editor.h" diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index f1b382f90..a43b4957a 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -329,34 +329,7 @@ public: } }; -class CEditorImage : public CImageInfo -{ -public: - CEditor *m_pEditor; - - CEditorImage(CEditor *pEditor) : - m_AutoMapper(pEditor) - { - m_pEditor = pEditor; - m_aName[0] = 0; - m_Texture.Invalidate(); - m_External = 0; - m_Width = 0; - m_Height = 0; - m_pData = nullptr; - m_Format = 0; - } - - ~CEditorImage(); - - void AnalyseTileFlags(); - - IGraphics::CTextureHandle m_Texture; - int m_External; - char m_aName[IO_MAX_PATH_LENGTH]; - unsigned char m_aTileFlags[256]; - class CAutoMapper m_AutoMapper; -}; +class CEditorImage; class CEditorSound { diff --git a/src/game/editor/mapitems/image.cpp b/src/game/editor/mapitems/image.cpp index a29e5371d..365c6687d 100644 --- a/src/game/editor/mapitems/image.cpp +++ b/src/game/editor/mapitems/image.cpp @@ -1,3 +1,5 @@ +#include "image.h" + #include CEditorImage::~CEditorImage() diff --git a/src/game/editor/mapitems/image.h b/src/game/editor/mapitems/image.h new file mode 100644 index 000000000..77a845a87 --- /dev/null +++ b/src/game/editor/mapitems/image.h @@ -0,0 +1,39 @@ +#ifndef GAME_EDITOR_MAPITEMS_IMAGE_H +#define GAME_EDITOR_MAPITEMS_IMAGE_H + +#include + +#include + +class CEditor; + +class CEditorImage : public CImageInfo +{ +public: + CEditor *m_pEditor; + + CEditorImage(CEditor *pEditor) : + m_AutoMapper(pEditor) + { + m_pEditor = pEditor; + m_aName[0] = 0; + m_Texture.Invalidate(); + m_External = 0; + m_Width = 0; + m_Height = 0; + m_pData = nullptr; + m_Format = 0; + } + + ~CEditorImage(); + + void AnalyseTileFlags(); + + IGraphics::CTextureHandle m_Texture; + int m_External; + char m_aName[IO_MAX_PATH_LENGTH]; + unsigned char m_aTileFlags[256]; + class CAutoMapper m_AutoMapper; +}; + +#endif diff --git a/src/game/editor/mapitems/layer_quads.cpp b/src/game/editor/mapitems/layer_quads.cpp index 485ba9327..b16eeba4a 100644 --- a/src/game/editor/mapitems/layer_quads.cpp +++ b/src/game/editor/mapitems/layer_quads.cpp @@ -2,6 +2,8 @@ /* If you are missing that file, acquire a complete release at teeworlds.com. */ #include +#include "image.h" + CLayerQuads::CLayerQuads() { m_Type = LAYERTYPE_QUADS; diff --git a/src/game/editor/mapitems/layer_tiles.cpp b/src/game/editor/mapitems/layer_tiles.cpp index bcd8bc0b5..bf722c109 100644 --- a/src/game/editor/mapitems/layer_tiles.cpp +++ b/src/game/editor/mapitems/layer_tiles.cpp @@ -5,6 +5,8 @@ #include #include +#include "image.h" + CLayerTiles::CLayerTiles(int w, int h) { m_Type = LAYERTYPE_TILES; diff --git a/src/game/editor/mapitems/map.cpp b/src/game/editor/mapitems/map.cpp index 5bc5338bc..996d081e2 100644 --- a/src/game/editor/mapitems/map.cpp +++ b/src/game/editor/mapitems/map.cpp @@ -1,5 +1,7 @@ #include +#include "image.h" + void CEditorMap::OnModify() { m_Modified = true; diff --git a/src/game/editor/mapitems/map_io.cpp b/src/game/editor/mapitems/map_io.cpp index f0962f913..d2dce83f9 100644 --- a/src/game/editor/mapitems/map_io.cpp +++ b/src/game/editor/mapitems/map_io.cpp @@ -11,6 +11,8 @@ #include #include +#include "image.h" + template static int MakeVersion(int i, const T &v) { diff --git a/src/game/editor/popups.cpp b/src/game/editor/popups.cpp index 9d60c65ca..d5fad78fe 100644 --- a/src/game/editor/popups.cpp +++ b/src/game/editor/popups.cpp @@ -12,6 +12,7 @@ #include #include +#include #include "editor.h" diff --git a/src/game/editor/tileart.cpp b/src/game/editor/tileart.cpp index 0f33c8f50..85a37a7bb 100644 --- a/src/game/editor/tileart.cpp +++ b/src/game/editor/tileart.cpp @@ -1,5 +1,7 @@ #include "editor.h" +#include + #include bool operator<(const ColorRGBA &Left, const ColorRGBA &Right) From d68029a25275f504542a08b353b6965d6b9f9ae9 Mon Sep 17 00:00:00 2001 From: marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Mon, 28 Aug 2023 18:02:09 +0200 Subject: [PATCH 3/7] extract `CEditorSound` into separate header file --- CMakeLists.txt | 1 + src/game/editor/editor.cpp | 1 + src/game/editor/editor.h | 25 +--------------------- src/game/editor/mapitems/map_io.cpp | 1 + src/game/editor/mapitems/sound.cpp | 4 +++- src/game/editor/mapitems/sound.h | 32 +++++++++++++++++++++++++++++ src/game/editor/popups.cpp | 1 + 7 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 src/game/editor/mapitems/sound.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bf3edad33..c38e2b8f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2330,6 +2330,7 @@ if(CLIENT) mapitems/map.cpp mapitems/map_io.cpp mapitems/sound.cpp + mapitems/sound.h popups.cpp proof_mode.cpp proof_mode.h diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 6a5dd13c2..db6c97443 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -32,6 +32,7 @@ #include #include +#include #include "auto_map.h" #include "editor.h" diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index a43b4957a..ccd36e57f 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -330,30 +330,7 @@ public: }; class CEditorImage; - -class CEditorSound -{ -public: - CEditor *m_pEditor; - - CEditorSound(CEditor *pEditor) - { - m_pEditor = pEditor; - m_aName[0] = 0; - m_SoundID = 0; - - m_pData = nullptr; - m_DataSize = 0; - } - - ~CEditorSound(); - - int m_SoundID; - char m_aName[IO_MAX_PATH_LENGTH]; - - void *m_pData; - unsigned m_DataSize; -}; +class CEditorSound; class CEditorMap { diff --git a/src/game/editor/mapitems/map_io.cpp b/src/game/editor/mapitems/map_io.cpp index d2dce83f9..092431d2b 100644 --- a/src/game/editor/mapitems/map_io.cpp +++ b/src/game/editor/mapitems/map_io.cpp @@ -12,6 +12,7 @@ #include #include "image.h" +#include "sound.h" template static int MakeVersion(int i, const T &v) diff --git a/src/game/editor/mapitems/sound.cpp b/src/game/editor/mapitems/sound.cpp index 2ee5bb36e..cdf7e025e 100644 --- a/src/game/editor/mapitems/sound.cpp +++ b/src/game/editor/mapitems/sound.cpp @@ -1,7 +1,9 @@ -#include +#include "sound.h" #include +#include + CEditorSound::~CEditorSound() { m_pEditor->Sound()->UnloadSample(m_SoundID); diff --git a/src/game/editor/mapitems/sound.h b/src/game/editor/mapitems/sound.h new file mode 100644 index 000000000..6d3b00ed3 --- /dev/null +++ b/src/game/editor/mapitems/sound.h @@ -0,0 +1,32 @@ +#ifndef GAME_EDITOR_MAPITEMS_SOUND_H +#define GAME_EDITOR_MAPITEMS_SOUND_H + +#include + +class CEditor; + +class CEditorSound +{ +public: + CEditor *m_pEditor; + + CEditorSound(CEditor *pEditor) + { + m_pEditor = pEditor; + m_aName[0] = 0; + m_SoundID = 0; + + m_pData = nullptr; + m_DataSize = 0; + } + + ~CEditorSound(); + + int m_SoundID; + char m_aName[IO_MAX_PATH_LENGTH]; + + void *m_pData; + unsigned m_DataSize; +}; + +#endif diff --git a/src/game/editor/popups.cpp b/src/game/editor/popups.cpp index d5fad78fe..ca7a7f300 100644 --- a/src/game/editor/popups.cpp +++ b/src/game/editor/popups.cpp @@ -13,6 +13,7 @@ #include #include +#include #include "editor.h" From c3a07dd9774c80ececcffb16dd2c5476c15675f5 Mon Sep 17 00:00:00 2001 From: marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Mon, 28 Aug 2023 18:07:28 +0200 Subject: [PATCH 4/7] let `CEditorSound` inherit `CEditorComponent` --- src/game/editor/mapitems/map_io.cpp | 1 - src/game/editor/mapitems/sound.cpp | 7 +++++-- src/game/editor/mapitems/sound.h | 25 +++++++------------------ 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/game/editor/mapitems/map_io.cpp b/src/game/editor/mapitems/map_io.cpp index 092431d2b..31c95e0c7 100644 --- a/src/game/editor/mapitems/map_io.cpp +++ b/src/game/editor/mapitems/map_io.cpp @@ -549,7 +549,6 @@ bool CEditorMap::Load(const char *pFileName, int StorageType, const std::functio // copy base info std::shared_ptr pSound = std::make_shared(m_pEditor); - if(pItem->m_External) { char aBuf[IO_MAX_PATH_LENGTH]; diff --git a/src/game/editor/mapitems/sound.cpp b/src/game/editor/mapitems/sound.cpp index cdf7e025e..6465f915b 100644 --- a/src/game/editor/mapitems/sound.cpp +++ b/src/game/editor/mapitems/sound.cpp @@ -2,11 +2,14 @@ #include -#include +CEditorSound::CEditorSound(CEditor *pEditor) +{ + Init(pEditor); +} CEditorSound::~CEditorSound() { - m_pEditor->Sound()->UnloadSample(m_SoundID); + Sound()->UnloadSample(m_SoundID); free(m_pData); m_pData = nullptr; } diff --git a/src/game/editor/mapitems/sound.h b/src/game/editor/mapitems/sound.h index 6d3b00ed3..681f9ad30 100644 --- a/src/game/editor/mapitems/sound.h +++ b/src/game/editor/mapitems/sound.h @@ -3,30 +3,19 @@ #include -class CEditor; +#include -class CEditorSound +class CEditorSound : public CEditorComponent { public: - CEditor *m_pEditor; - - CEditorSound(CEditor *pEditor) - { - m_pEditor = pEditor; - m_aName[0] = 0; - m_SoundID = 0; - - m_pData = nullptr; - m_DataSize = 0; - } - + explicit CEditorSound(CEditor *pEditor); ~CEditorSound(); - int m_SoundID; - char m_aName[IO_MAX_PATH_LENGTH]; + int m_SoundID = 0; + char m_aName[IO_MAX_PATH_LENGTH] = ""; - void *m_pData; - unsigned m_DataSize; + void *m_pData = nullptr; + unsigned m_DataSize = 0; }; #endif From 07fd8e6712c74ea5dd3ff7d3dc7ec4045a4abce3 Mon Sep 17 00:00:00 2001 From: marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Mon, 28 Aug 2023 18:22:01 +0200 Subject: [PATCH 5/7] let `CAutoMapper` inherit `CEditorComponent` --- src/game/editor/auto_map.cpp | 13 +++++++------ src/game/editor/auto_map.h | 11 ++++++----- src/game/editor/mapitems/image.h | 3 +-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/game/editor/auto_map.cpp b/src/game/editor/auto_map.cpp index d339a2601..bca98c498 100644 --- a/src/game/editor/auto_map.cpp +++ b/src/game/editor/auto_map.cpp @@ -5,8 +5,10 @@ #include #include +#include + #include "auto_map.h" -#include "editor.h" +#include "editor.h" // TODO: only needs CLayerTiles // Based on triple32inc from https://github.com/skeeto/hash-prospector/tree/79a6074062a84907df6e45b756134b74e2956760 static uint32_t HashUInt32(uint32_t Num) @@ -39,15 +41,14 @@ static int HashLocation(uint32_t Seed, uint32_t Run, uint32_t Rule, uint32_t X, CAutoMapper::CAutoMapper(CEditor *pEditor) { - m_pEditor = pEditor; - m_FileLoaded = false; + Init(pEditor); } void CAutoMapper::Load(const char *pTileName) { char aPath[IO_MAX_PATH_LENGTH]; str_format(aPath, sizeof(aPath), "editor/%s.rules", pTileName); - IOHANDLE RulesFile = m_pEditor->Storage()->OpenFile(aPath, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL); + IOHANDLE RulesFile = Storage()->OpenFile(aPath, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL); if(!RulesFile) return; @@ -362,7 +363,7 @@ void CAutoMapper::Load(const char *pTileName) char aBuf[IO_MAX_PATH_LENGTH + 16]; str_format(aBuf, sizeof(aBuf), "loaded %s", aPath); - m_pEditor->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "editor/automap", aBuf); + Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "editor/automap", aBuf); m_FileLoaded = true; } @@ -470,7 +471,7 @@ void CAutoMapper::Proceed(CLayerTiles *pLayer, int ConfigID, int Seed, int SeedO for(int x = 0; x < pLayer->m_Width; x++) { CTile *pTile = &(pLayer->m_pTiles[y * pLayer->m_Width + x]); - m_pEditor->m_Map.OnModify(); + Editor()->m_Map.OnModify(); for(size_t i = 0; i < pRun->m_vIndexRules.size(); ++i) { diff --git a/src/game/editor/auto_map.h b/src/game/editor/auto_map.h index b5fd6cc4d..4e998af72 100644 --- a/src/game/editor/auto_map.h +++ b/src/game/editor/auto_map.h @@ -3,7 +3,9 @@ #include -class CAutoMapper +#include "component.h" + +class CAutoMapper : public CEditorComponent { struct CIndexInfo { @@ -55,7 +57,7 @@ class CAutoMapper }; public: - CAutoMapper(class CEditor *pEditor); + explicit CAutoMapper(CEditor *pEditor); void Load(const char *pTileName); void ProceedLocalized(class CLayerTiles *pLayer, int ConfigID, int Seed = 0, int X = 0, int Y = 0, int Width = -1, int Height = -1); @@ -67,9 +69,8 @@ public: bool IsLoaded() const { return m_FileLoaded; } private: - std::vector m_vConfigs; - class CEditor *m_pEditor; - bool m_FileLoaded; + std::vector m_vConfigs = {}; + bool m_FileLoaded = false; }; #endif diff --git a/src/game/editor/mapitems/image.h b/src/game/editor/mapitems/image.h index 77a845a87..b34edba79 100644 --- a/src/game/editor/mapitems/image.h +++ b/src/game/editor/mapitems/image.h @@ -12,8 +12,7 @@ class CEditorImage : public CImageInfo public: CEditor *m_pEditor; - CEditorImage(CEditor *pEditor) : - m_AutoMapper(pEditor) + CEditorImage(CEditor *pEditor): m_AutoMapper(pEditor) { m_pEditor = pEditor; m_aName[0] = 0; From 12d0608dfd2f852574141c87cef9b23122b46bca Mon Sep 17 00:00:00 2001 From: marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Mon, 28 Aug 2023 18:36:37 +0200 Subject: [PATCH 6/7] let `CEditorImage` inherit `CEditorComponent` --- src/engine/graphics.h | 8 ++++---- src/game/editor/mapitems/image.cpp | 18 ++++++++++++++++-- src/game/editor/mapitems/image.h | 27 +++++++-------------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 77a9da40b..5ed0fcab2 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -76,19 +76,19 @@ public: /* Variable: width Contains the width of the image */ - int m_Width; + int m_Width = 0; /* Variable: height Contains the height of the image */ - int m_Height; + int m_Height = 0; /* Variable: format Contains the format of the image. See for more information. */ - int m_Format; + int m_Format = FORMAT_RGB; /* Variable: data Pointer to the image data. */ - void *m_pData; + void *m_pData = nullptr; }; /* diff --git a/src/game/editor/mapitems/image.cpp b/src/game/editor/mapitems/image.cpp index 365c6687d..514a1cadf 100644 --- a/src/game/editor/mapitems/image.cpp +++ b/src/game/editor/mapitems/image.cpp @@ -1,14 +1,28 @@ #include "image.h" -#include +#include + +CEditorImage::CEditorImage(CEditor *pEditor) : + m_AutoMapper(pEditor) +{ + Init(pEditor); + m_Texture.Invalidate(); +} CEditorImage::~CEditorImage() { - m_pEditor->Graphics()->UnloadTexture(&m_Texture); + Graphics()->UnloadTexture(&m_Texture); free(m_pData); m_pData = nullptr; } +void CEditorImage::Init(CEditor *pEditor) +{ + CEditorComponent::Init(pEditor); + RegisterSubComponent(m_AutoMapper); + InitSubComponents(); +} + void CEditorImage::AnalyseTileFlags() { mem_zero(m_aTileFlags, sizeof(m_aTileFlags)); diff --git a/src/game/editor/mapitems/image.h b/src/game/editor/mapitems/image.h index b34edba79..cffbd6c7e 100644 --- a/src/game/editor/mapitems/image.h +++ b/src/game/editor/mapitems/image.h @@ -4,35 +4,22 @@ #include #include +#include -class CEditor; - -class CEditorImage : public CImageInfo +class CEditorImage : public CImageInfo, public CEditorComponent { public: - CEditor *m_pEditor; - - CEditorImage(CEditor *pEditor): m_AutoMapper(pEditor) - { - m_pEditor = pEditor; - m_aName[0] = 0; - m_Texture.Invalidate(); - m_External = 0; - m_Width = 0; - m_Height = 0; - m_pData = nullptr; - m_Format = 0; - } - + explicit CEditorImage(CEditor *pEditor); ~CEditorImage(); + void Init(CEditor *pEditor) override; void AnalyseTileFlags(); IGraphics::CTextureHandle m_Texture; - int m_External; - char m_aName[IO_MAX_PATH_LENGTH]; + int m_External = 0; + char m_aName[IO_MAX_PATH_LENGTH] = ""; unsigned char m_aTileFlags[256]; - class CAutoMapper m_AutoMapper; + CAutoMapper m_AutoMapper; }; #endif From 59df1d86d4430b47ae0db8c1d9a45a546b1abc43 Mon Sep 17 00:00:00 2001 From: marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Tue, 29 Aug 2023 22:03:11 +0200 Subject: [PATCH 7/7] move `CEditorMap::HandleMapDrop` to `CEditor` --- src/game/editor/editor.cpp | 12 +++++++++++- src/game/editor/editor.h | 1 - src/game/editor/mapitems/map.cpp | 15 --------------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index db6c97443..f8320c5d1 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -7838,7 +7838,17 @@ bool CEditor::Save(const char *pFilename) bool CEditor::HandleMapDrop(const char *pFileName, int StorageType) { - return m_Map.HandleMapDrop(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE); + if(HasUnsavedData()) + { + str_copy(m_aFileNamePending, pFileName); + m_PopupEventType = CEditor::POPEVENT_LOADDROP; + m_PopupEventActivated = true; + return true; + } + else + { + return Load(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE); + } } bool CEditor::Load(const char *pFileName, int StorageType) diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index ccd36e57f..851f62f0b 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -473,7 +473,6 @@ public: // io bool Save(const char *pFilename); bool Load(const char *pFilename, int StorageType, const std::function &ErrorHandler); - bool HandleMapDrop(const char *pFilename, int StorageType); void PerformSanityChecks(const std::function &ErrorHandler); // DDRace diff --git a/src/game/editor/mapitems/map.cpp b/src/game/editor/mapitems/map.cpp index 996d081e2..48c58acd1 100644 --- a/src/game/editor/mapitems/map.cpp +++ b/src/game/editor/mapitems/map.cpp @@ -179,18 +179,3 @@ void CEditorMap::MakeTuneLayer(const std::shared_ptr &pLayer) m_pTuneLayer = std::static_pointer_cast(pLayer); m_pTuneLayer->m_pEditor = m_pEditor; } - -bool CEditorMap::HandleMapDrop(const char *pFileName, int StorageType) -{ - if(m_pEditor->HasUnsavedData()) - { - str_copy(m_pEditor->m_aFileNamePending, pFileName); - m_pEditor->m_PopupEventType = CEditor::POPEVENT_LOADDROP; - m_pEditor->m_PopupEventActivated = true; - return true; - } - else - { - return m_pEditor->Load(pFileName, IStorage::TYPE_ALL_OR_ABSOLUTE); - } -}