Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh committed Oct 16, 2023
1 parent 47d8f4f commit 4e13725
Show file tree
Hide file tree
Showing 59 changed files with 447 additions and 476 deletions.
21 changes: 9 additions & 12 deletions source/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ Change::Change(Tile* tile) :
data = tile;
}

Change* Change::Create(House* house, const Position &position) {
Change* change = new Change();
std::shared_ptr<Change> Change::Create(House* house, const Position &position) {
std::shared_ptr<Change> change = std::make_shared<Change>();
change->type = CHANGE_MOVE_HOUSE_EXIT;
change->data = new HouseData { house->id, position };
return change;
}

Change* Change::Create(Waypoint* waypoint, const Position &position) {
Change* change = new Change();
std::shared_ptr<Change> Change::Create(Waypoint* waypoint, const Position &position) {
auto change = std::make_shared<Change>();
change->type = CHANGE_MOVE_WAYPOINT;
change->data = new WaypointData { waypoint->name, position };
return change;
Expand Down Expand Up @@ -96,9 +96,6 @@ Action::Action(Editor &editor, ActionIdentifier ident) :
}

Action::~Action() {
for (Change* change : changes) {
delete change;
}
changes.clear();
}

Expand All @@ -110,9 +107,9 @@ size_t Action::approx_memsize() const {

size_t Action::memsize() const {
uint32_t mem = sizeof(*this);
mem += sizeof(Change*) * 3 * changes.size();
mem += sizeof(const std::shared_ptr<Change>&) * 3 * changes.size();

for (const Change* change : changes) {
for (const auto& change : changes) {
if (change && change->getType() == CHANGE_TILE) {
mem += reinterpret_cast<Tile*>(change->getData())->memsize();
}
Expand All @@ -126,7 +123,7 @@ void Action::commit(DirtyList* dirty_list) {
Selection &selection = editor.getSelection();
selection.start(Selection::INTERNAL);

for (Change* change : changes) {
for (const auto& change : changes) {
switch (change->getType()) {
case CHANGE_TILE: {
void** data = &change->data;
Expand Down Expand Up @@ -283,7 +280,7 @@ void Action::undo(DirtyList* dirty_list) {
Selection &selection = editor.getSelection();
selection.start(Selection::INTERNAL);

for (Change* change : changes) {
for (const auto& change : changes) {
switch (change->getType()) {
case CHANGE_TILE: {
void** data = &change->data;
Expand Down Expand Up @@ -724,7 +721,7 @@ void DirtyList::AddPosition(int x, int y, int z) {
}
}

void DirtyList::AddChange(Change* c) {
void DirtyList::AddChange(const std::shared_ptr<Change> &c) {
ichanges.push_back(c);
}

Expand Down
15 changes: 8 additions & 7 deletions source/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ struct WaypointData {

class Change {
public:
Change(Tile* tile);
explicit Change(Tile* tile);
~Change();

static Change* Create(House* house, const Position &position);
static Change* Create(Waypoint* waypoint, const Position &position);
static std::shared_ptr<Change> Create(House* house, const Position &position);
static std::shared_ptr<Change> Create(Waypoint* waypoint, const Position &position);

void clear();

Expand All @@ -83,15 +83,16 @@ class Change {

uint32_t memsize() const;

Change();

private:
Change();
ChangeType type;
void* data;

friend class Action;
};

typedef std::vector<Change*> ChangeList;
typedef std::vector<std::shared_ptr<Change>> ChangeList;

// A dirty list represents a list of all tiles that was changed in an action
class DirtyList {
Expand All @@ -114,7 +115,7 @@ class DirtyList {
typedef std::set<ValueType, Comparator> SetType;

void AddPosition(int x, int y, int z);
void AddChange(Change* c);
void AddChange(const std::shared_ptr<Change> &c);
bool Empty() const {
return iset.empty() && ichanges.empty();
}
Expand All @@ -130,7 +131,7 @@ class Action {
public:
virtual ~Action();

void addChange(Change* t) {
void addChange(const std::shared_ptr<Change> &t) {
changes.push_back(t);
}

Expand Down
37 changes: 37 additions & 0 deletions source/beats.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <memory>

class SharedObject;
using SharedObjectPtr = std::shared_ptr<SharedObject>;

class SharedObject : public std::enable_shared_from_this<SharedObject> {
public:
virtual ~SharedObject() = default;

SharedObject &operator=(const SharedObject &) = delete;

SharedObjectPtr asSharedObject() {
return shared_from_this();
}

template <typename T>
std::shared_ptr<T> static_self_cast() {
return std::static_pointer_cast<T>(shared_from_this());
}

template <typename T>
std::shared_ptr<T> dynamic_self_cast() {
return std::dynamic_pointer_cast<T>(shared_from_this());
}

template <typename TargetType, typename SourceType>
std::shared_ptr<TargetType> static_self_cast(std::shared_ptr<SourceType> source) {
return std::static_pointer_cast<TargetType>(source);
}

template <typename TargetType, typename SourceType>
std::shared_ptr<TargetType> dynamic_self_cast(std::shared_ptr<SourceType> source) {
return std::dynamic_pointer_cast<TargetType>(source);
}
};
16 changes: 5 additions & 11 deletions source/browse_tile_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class BrowseTileListBox : public wxVListBox {

void OnDrawItem(wxDC &dc, const wxRect &rect, size_t index) const;
wxCoord OnMeasureItem(size_t index) const;
Item* GetSelectedItem();
std::shared_ptr<Item> GetSelectedItem();
void RemoveSelected();

protected:
void UpdateItems();

typedef std::map<int, Item*> ItemsMap;
typedef std::map<int, std::shared_ptr<Item>> ItemsMap;
ItemsMap items;
Tile* edit_tile;
};
Expand All @@ -57,7 +57,7 @@ BrowseTileListBox::~BrowseTileListBox() {

void BrowseTileListBox::OnDrawItem(wxDC &dc, const wxRect &rect, size_t n) const {
ItemsMap::const_iterator item_iterator = items.find(int(n));
Item* item = item_iterator->second;
const auto& item = item_iterator->second;

Sprite* sprite = g_gui.gfx.getSprite(item->getClientID());
if (sprite) {
Expand Down Expand Up @@ -85,7 +85,7 @@ wxCoord BrowseTileListBox::OnMeasureItem(size_t n) const {
return 32;
}

Item* BrowseTileListBox::GetSelectedItem() {
std::shared_ptr<Item> BrowseTileListBox::GetSelectedItem() {
if (GetItemCount() == 0 || GetSelectedCount() == 0) {
return nullptr;
}
Expand All @@ -101,12 +101,6 @@ void BrowseTileListBox::RemoveSelected() {
Clear();
items.clear();

// Delete the items from the tile
ItemVector tile_selection = edit_tile->popSelectedItems(true);
for (ItemVector::iterator iit = tile_selection.begin(); iit != tile_selection.end(); ++iit) {
delete *iit;
}

UpdateItems();
Refresh();
}
Expand Down Expand Up @@ -195,7 +189,7 @@ void BrowseTileWindow::OnClickDelete(wxCommandEvent &WXUNUSED(event)) {
}

void BrowseTileWindow::OnClickSelectRaw(wxCommandEvent &WXUNUSED(event)) {
Item* item = item_list->GetSelectedItem();
const auto& item = item_list->GetSelectedItem();
if (item && item->getRAWBrush()) {
g_gui.SelectBrush(item->getRAWBrush(), TILESET_RAW);
}
Expand Down
10 changes: 5 additions & 5 deletions source/brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ int DoorBrush::getLookID() const {
}
}

void DoorBrush::switchDoor(Item* item) {
void DoorBrush::switchDoor(std::shared_ptr<Item> item) {
ASSERT(item);
ASSERT(item->isBrushDoor());

Expand Down Expand Up @@ -387,7 +387,7 @@ bool DoorBrush::canDraw(BaseMap* map, const Position &position) const {
return false;
}

Item* item = tile->getWall();
const auto& item = tile->getWall();
if (!item) {
return false;
}
Expand Down Expand Up @@ -440,7 +440,7 @@ bool DoorBrush::canDraw(BaseMap* map, const Position &position) const {

void DoorBrush::undraw(BaseMap* map, Tile* tile) {
for (ItemVector::iterator it = tile->items.begin(); it != tile->items.end(); ++it) {
Item* item = *it;
const auto& item = *it;
if (item->isBrushDoor()) {
item->getWallBrush()->draw(map, tile, nullptr);
if (g_settings.getInteger(Config::USE_AUTOMAGIC)) {
Expand All @@ -453,7 +453,7 @@ void DoorBrush::undraw(BaseMap* map, Tile* tile) {

void DoorBrush::draw(BaseMap* map, Tile* tile, void* parameter) {
for (ItemVector::iterator item_iter = tile->items.begin(); item_iter != tile->items.end();) {
Item* item = *item_iter;
auto item = *item_iter;
if (!item->isWall()) {
++item_iter;
continue;
Expand Down Expand Up @@ -516,7 +516,7 @@ void DoorBrush::draw(BaseMap* map, Tile* tile, void* parameter) {

if (g_settings.getInteger(Config::AUTO_ASSIGN_DOORID) && tile->isHouseTile()) {
Map* mmap = dynamic_cast<Map*>(map);
Door* door = dynamic_cast<Door*>(item);
const std::shared_ptr<Door> &door = static_self_cast<Door>(item);
if (mmap && door) {
House* house = mmap->houses.getHouse(tile->getHouseID());
ASSERT(house);
Expand Down
8 changes: 4 additions & 4 deletions source/brush.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ class FlagBrush : public Brush {
//=============================================================================
// Doorbrush, add doors, windows etc.

class DoorBrush : public Brush {
class DoorBrush : public Brush, public SharedObject{
public:
DoorBrush(DoorType _doortype);
virtual ~DoorBrush();
explicit DoorBrush(DoorType _doortype);
~DoorBrush() override;

bool isDoor() const {
return true;
Expand All @@ -359,7 +359,7 @@ class DoorBrush : public Brush {
return static_cast<DoorBrush*>(this);
}

static void switchDoor(Item* door);
static void switchDoor(std::shared_ptr<Item> door);

virtual bool canDraw(BaseMap* map, const Position &position) const;
virtual void draw(BaseMap* map, Tile* tile, void* parameter);
Expand Down
7 changes: 3 additions & 4 deletions source/carpet_brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,10 @@ void CarpetBrush::draw(BaseMap* map, Tile* tile, void* parameter) {
void CarpetBrush::undraw(BaseMap* map, Tile* tile) {
auto &items = tile->items;
for (auto it = items.begin(); it != items.end();) {
Item* item = *it;
const auto& item = *it;
if (item->isCarpet()) {
CarpetBrush* carpetBrush = item->getCarpetBrush();
if (carpetBrush) {
delete item;
it = items.erase(it);
} else {
++it;
Expand All @@ -176,7 +175,7 @@ void CarpetBrush::doCarpets(BaseMap* map, Tile* tile) {
return false;
}

for (Item* item : tile->items) {
for (const auto& item : tile->items) {
if (item->getCarpetBrush() == carpetBrush) {
return true;
}
Expand All @@ -203,7 +202,7 @@ void CarpetBrush::doCarpets(BaseMap* map, Tile* tile) {
//
}
*/
for (Item* item : tile->items) {
for (const auto& item : tile->items) {
ASSERT(item);

CarpetBrush* carpetBrush = item->getCarpetBrush();
Expand Down
12 changes: 6 additions & 6 deletions source/common_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ void SortableListBox::DoSort() {
// ============================================================================
// Object properties base

ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, Item* item, wxPoint position /* = wxDefaultPosition */) :
ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, Map* map, Tile* tile, std::shared_ptr<Item> item, wxPoint position /* = wxDefaultPosition */) :
wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER),
edit_map(map),
edit_tile(tile),
Expand All @@ -1112,7 +1112,7 @@ ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxStrin
////
}

ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, Monster* monster, wxPoint position /* = wxDefaultPosition */) :
ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, Map* map, Tile* tile, Monster* monster, wxPoint position /* = wxDefaultPosition */) :
wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER),
edit_map(map),
edit_tile(tile),
Expand All @@ -1124,7 +1124,7 @@ ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxStrin
////
}

ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, SpawnMonster* spawnMonster, wxPoint position /* = wxDefaultPosition */) :
ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, Map* map, Tile* tile, SpawnMonster* spawnMonster, wxPoint position /* = wxDefaultPosition */) :
wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER),
edit_map(map),
edit_tile(tile),
Expand All @@ -1136,7 +1136,7 @@ ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxStrin
////
}

ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, Npc* npc, wxPoint position /* = wxDefaultPosition */) :
ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, Map* map, Tile* tile, Npc* npc, wxPoint position /* = wxDefaultPosition */) :
wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER),
edit_map(map),
edit_tile(tile),
Expand All @@ -1148,7 +1148,7 @@ ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxStrin
////
}

ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, SpawnNpc* spawnNpc, wxPoint position /* = wxDefaultPosition */) :
ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, Map* map, Tile* tile, SpawnNpc* spawnNpc, wxPoint position /* = wxDefaultPosition */) :
wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER),
edit_map(map),
edit_tile(tile),
Expand All @@ -1160,7 +1160,7 @@ ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxStrin
////
}

Item* ObjectPropertiesWindowBase::getItemBeingEdited() {
std::shared_ptr<Item> ObjectPropertiesWindowBase::getItemBeingEdited() {
return edit_item;
}

Expand Down
Loading

0 comments on commit 4e13725

Please sign in to comment.