Corrected major memory leak while loading maps and entities

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent b85db76ee5
commit b53ceaab97
  1. BIN
      C++ProjectTemplate
  2. 118
      main.cpp
  3. 11
      map.cpp
  4. 17
      map.h
  5. 4
      states.h
  6. 7
      test/test.cpp
  7. 4
      trigger.cpp

Binary file not shown.

@ -54,7 +54,7 @@ const float TARGET_RATE = 1/60.0;
int MAP_WIDTH=-1;
int MAP_HEIGHT=-1;
Map*CURRENT_MAP=nullptr;
Map*MAP_ONETT=nullptr;
extern std::map<MapName::Map,Map*> MAPS;
int GAME_STATE = GameState::EDITOR;
vi2d SELECTED_TILE={0,0};
vi2d HIGHLIGHTED_TILE={0,0};
@ -158,6 +158,8 @@ int SHOP_MENU_CURSOR=0;
Object*SHOPKEEPER_INTERACTING_WITH=nullptr;
extern std::map<TriggerName::Trigger,std::string> TRIGGERS;
TriggerName::Trigger SELECTED_TRIGGER;
int TRIGGER_OFFSET=0;
int WORLD_DISPLAY_OFFSET=0;
/*
[Choice1,Choice2,Choice3]
@ -246,11 +248,9 @@ bool SeasonI::OnUserCreate(){
additionalChars[Σ]={24,0};
additionalChars[γ]={32,0};
MAP_ONETT=new Map("map0","map0_2","map0_3","map0_4","map0_5",SPRITES["terrainmap.png"]);
CURRENT_MAP=MAP_ONETT;
CURRENT_MAP=MAPS[MapName::ONETT];
//OBJ_INFO["PLAYER"]=PLAYER_ANIMATION;
LoadMap(MAP_ONETT);
LoadMap(CURRENT_MAP);
TestCutscene=new Cutscene({
Fade(),
@ -762,33 +762,50 @@ void SeasonI::LoadMap(Map*map) {
}
MAP[i].clear();
}
for (int i=0;i<MAP.size();i++) {
for (int j=0;j<MAP[i].size();j++) {
for (int i=0;i<MAP2.size();i++) {
for (int j=0;j<MAP2[i].size();j++) {
delete MAP2[i][j];
}
MAP2[i].clear();
}
for (int i=0;i<MAP.size();i++) {
for (int j=0;j<MAP[i].size();j++) {
for (int i=0;i<MAP3.size();i++) {
for (int j=0;j<MAP3[i].size();j++) {
delete MAP3[i][j];
}
MAP3[i].clear();
}
for (int i=0;i<MAP.size();i++) {
for (int j=0;j<MAP[i].size();j++) {
for (int i=0;i<MAP4.size();i++) {
for (int j=0;j<MAP4[i].size();j++) {
delete MAP4[i][j];
}
MAP4[i].clear();
}
for (int i=0;i<MAP.size();i++) {
for (int j=0;j<MAP[i].size();j++) {
for (int i=0;i<MAP5.size();i++) {
for (int j=0;j<MAP5[i].size();j++) {
delete MAP5[i][j];
}
MAP5[i].clear();
}
for (int i=0;i<map->encounters.size();i++) {
for (int j=0;j<map->encounters[i]->objs.size();j++) {
delete map->encounters[i]->objs[j];
}
map->encounters[i]->objs.clear();
delete map->encounters[i];
}
map->encounters.clear();
for (int i=0;i<map->triggers.size();i++) {
delete map->triggers[i];
}
map->triggers.clear();
MAP_WIDTH=-1;
MAP_HEIGHT=-1;
MAP.clear();
MAP2.clear();
MAP3.clear();
MAP4.clear();
MAP5.clear();
for (int i=0;i<OBJECTS.size();i++) {
delete OBJECTS[i];
}
@ -797,6 +814,7 @@ void SeasonI::LoadMap(Map*map) {
PARTY_MEMBER_OBJ[i]=nullptr;
PARTY_MEMBER_ID[i]=0;
}
PARTY_MEMBER_COUNT=0;
std::string data;
while (f.good()) {
f>>data;
@ -876,6 +894,7 @@ void SeasonI::LoadMap(Map*map) {
MAP_HEIGHT++;
}
}
while (f2.good()) {
f2>>data;
std::vector<TILE*> tiles;
@ -2261,6 +2280,9 @@ void SeasonI::keyUpdates() {
EDITING_LAYER=layer::BACKGROUND;
EnableLayer(layer::COLLISION,false);
}
if (GetKey(HOME).bPressed) {
GAME_STATE=GameState::MAP_SELECT;
}
if (GetKey(ESCAPE).bPressed) {
GAME_STATE=GameState::OBJ_SELECT;
}
@ -2307,6 +2329,17 @@ void SeasonI::keyUpdates() {
}
}
}break;
case GameState::MAP_SELECT:{
if (GetKey(ESCAPE).bPressed) {
GAME_STATE=GameState::EDITOR;
}
if ((GetMouseWheel()<0||GetKey(PGDN).bHeld)&&WORLD_DISPLAY_OFFSET+(WIDTH/16)<MAPS.size()) {
WORLD_DISPLAY_OFFSET+=WIDTH/16;
} else
if ((GetMouseWheel()>0||GetKey(PGUP).bHeld)&&WORLD_DISPLAY_OFFSET-(WIDTH/16)>=0) {
WORLD_DISPLAY_OFFSET-=WIDTH/16;
}
}break;
case GameState::OBJ_SELECT:{
if (GetKey(ESCAPE).bPressed) {
GAME_STATE=GameState::EDITOR;
@ -2318,6 +2351,28 @@ void SeasonI::keyUpdates() {
OBJ_DISPLAY_OFFSET-=WIDTH/16;
}
}break;
case GameState::ENCOUNTER_SELECT:{
if (GetKey(ESCAPE).bPressed) {
GAME_STATE=GameState::EDITOR;
}
if ((GetMouseWheel()<0||GetKey(PGDN).bHeld)&&ENCOUNTER_OFFSET+(WIDTH/16)<OBJ_INFO.size()) {
ENCOUNTER_OFFSET+=WIDTH/16;
} else
if ((GetMouseWheel()>0||GetKey(PGUP).bHeld)&&ENCOUNTER_OFFSET-(WIDTH/16)>=0) {
ENCOUNTER_OFFSET-=WIDTH/16;
}
}break;
case GameState::TRIGGER_SELECT:{
if (GetKey(ESCAPE).bPressed) {
GAME_STATE=GameState::EDITOR;
}
if ((GetMouseWheel()<0||GetKey(PGDN).bHeld)&&TRIGGER_OFFSET+(WIDTH/16)<OBJ_INFO.size()) {
TRIGGER_OFFSET+=WIDTH/16;
} else
if ((GetMouseWheel()>0||GetKey(PGUP).bHeld)&&TRIGGER_OFFSET-(WIDTH/16)>=0) {
TRIGGER_OFFSET-=WIDTH/16;
}
}break;
case GameState::OVERWORLD_EQUIP_PLAYER_MENU:{
if (LeftPressed()) {
OVERWORLD_POWER_SELECTION_MEMBER--;
@ -2860,7 +2915,7 @@ void SeasonI::drawGame(){
vd2d drawpos={0,0};
int counter=0;
for (std::map<TriggerName::Trigger,std::string>::const_iterator it = TRIGGERS.cbegin();it!=TRIGGERS.cend();++it){
if (counter<OBJ_DISPLAY_OFFSET) {
if (counter<TRIGGER_OFFSET) {
counter++;
continue;
}
@ -2890,6 +2945,41 @@ void SeasonI::drawGame(){
counter++;
}
}break;
case GameState::MAP_SELECT:{
vd2d drawpos={0,0};
int counter=0;
for (std::map<MapName::Map,Map*>::const_iterator it = MAPS.cbegin();it!=MAPS.cend();++it){
if (counter<WORLD_DISPLAY_OFFSET) {
counter++;
continue;
}
if (drawpos.y>HEIGHT) {
break;
}
Map*map = it->second;
if (GetMouse(0).bHeld&&
GetMousePos().x>=drawpos.x&&
GetMousePos().x<drawpos.x+16&&
GetMousePos().y>=drawpos.y&&
GetMousePos().y<drawpos.y+24) {
CURRENT_MAP=map;
LoadMap(CURRENT_MAP);
EDITING_LAYER=layer::COLLISION;
EnableLayer(layer::COLLISION,false);
}
FillRectDecal(drawpos,{16,24},VERY_DARK_GREY);
DrawStringDecal({(float)(drawpos.x+2),(float)(drawpos.y)},it->second->mapname,WHITE,{(float)(12.0/GetTextSize(it->second->mapname).x),1.0});
if (CURRENT_MAP==it->second) {
DrawRectDecal(drawpos,{16,24},YELLOW);
}
drawpos.x+=16;
if (drawpos.x>=WIDTH) {
drawpos.x=0;
drawpos.y+=24;
}
counter++;
}
}break;
}
if (BATTLE_ENCOUNTER!=nullptr&&BATTLE_STATE==BattleState::TARGET_SELECT||GAME_STATE==GameState::OVERWORLD_TARGET_MENU) {
SetDrawTarget(layer::GROUND);

@ -0,0 +1,11 @@
#include "pixelGameEngine.h"
#include "map.h"
using namespace olc;
extern std::map<std::string,Decal*> SPRITES;
int MAP_ENUM_COUNT=0;
std::map<MapName::Map,Map*> MAPS={
{(MapName::Map)MAP_ENUM_COUNT++,new Map("map0","map0_2","map0_3","map0_4","map0_5",SPRITES["terrainmap.png"],"Onett")},
};

17
map.h

@ -6,6 +6,12 @@
using namespace olc;
namespace MapName{
enum Map{
ONETT,
};
}
class Map{
public:
std::string filename;
@ -13,16 +19,11 @@ class Map{
std::string l3filename;
std::string l4filename;
std::string l5filename;
std::string mapname;
Decal*tileset;
std::vector<Encounter*> encounters;
std::vector<Trigger*> triggers;
Map(std::string fname,std::string layer2_fname,std::string layer3_fname,std::string layer4_fname,std::string layer5_fname,Decal*tileset) {
this->filename=fname;
this->l2filename=layer2_fname;
this->l3filename=layer3_fname;
this->l4filename=layer4_fname;
this->l5filename=layer5_fname;
this->tileset=tileset;
}
Map(std::string fname,std::string layer2_fname,std::string layer3_fname,std::string layer4_fname,std::string layer5_fname,Decal*tileset,std::string mapname)
:filename(fname),l2filename(layer2_fname),l3filename(layer3_fname),l4filename(layer4_fname),l5filename(layer5_fname),mapname(mapname),tileset(tileset) {}
};
#endif

@ -21,6 +21,10 @@ namespace GameState{
SHOPKEEPER_SELL_MENU,
SHOPKEEPER_CONFIRM_SELL_MENU,
TRIGGER_SELECT,
MAP_SELECT,
NEW_MAP_INPUT_NAME,
NEW_MAP_INPUT_WIDTH,
NEW_MAP_INPUT_HEIGHT,
};
}

@ -21,6 +21,7 @@
#include "../battleproperty.h"
#include "test.h"
#include "../SeasonI.h"
#include "../map.h"
extern std::array<Entity*,7> PARTY_MEMBER_STATS;
extern Entity::pstats_t partyMemberDefaultStats;
@ -31,6 +32,7 @@ extern std::map<int,Object*> OBJ_INFO;
extern std::map<BattleMoveName,Battle::Move*>MOVELIST;
extern std::vector<Encounter*>ENCOUNTER_LIST;
extern std::map<Property,BattleProperty*> BATTLE_PROPERTIES;
extern std::map<MapName::Map,Map*> MAPS;
int testCount=0;
@ -202,7 +204,10 @@ bool SeasonI::OnUserCreate(){
Test("Freeze Packet description is set properly",
ITEMLIST[ItemName::FREEZE_PACKET]->description=="Lets out some blistering cold weather.");
Test("Freeze Packet drop chance is set properly",
ITEMLIST[ItemName::FREEZE_PACKET]->dropChance==2);
ITEMLIST[ItemName::FREEZE_PACKET]->dropChance==256);
for (int i=0;i<10000;i++) {
LoadMap(MAPS[MapName::ONETT]);
}
return true;
}

@ -1,8 +1,8 @@
#include "pixelGameEngine.h"
#include "trigger.h"
int i=1;
int TRIGGER_ENUM_COUNT=1;
std::map<TriggerName::Trigger,std::string> TRIGGERS={
{(TriggerName::Trigger)i++,"START_CUTSCENE_1"},
{(TriggerName::Trigger)TRIGGER_ENUM_COUNT++,"START_CUTSCENE_1"},
};
Loading…
Cancel
Save