Strange display with objects (level manager)

chris-scientist

5 years ago

Hello,

I want to create a level manager, but i have an error.

In ZlatkoLevelManager.cpp :

#include "ZlatkoLevelManager.h"

ZlatkoLevelManager::ZlatkoLevelManager() :
  listOfLevel(new ZlatkoLinkedList<ZlatkoLevel *>())
{

}

void ZlatkoLevelManager::addLevel(ZlatkoLevel * aLevel) {
  listOfLevel->addBottom(aLevel);
}

ZlatkoLinkedList<ZlatkoLevel *> * ZlatkoLevelManager::getLevels() const {
  return listOfLevel;
}

I create the levels in MapBuilder.cpp :


#include "MapBuilder.h"

void MapBuilder::build(ZlatkoLevelManager * levelManager) {
  // Niveau 1
  const int heightMap1 = 20;
  const int widthMap1 = 30;
  const char map1[heightMap1][widthMap1] = {
    /* ... */
  };
  ZlatkoMatrix<char> * matrix1 = new ZlatkoMatrix<char>(widthMap1, heightMap1);
  for(int y = 0 ; y < heightMap1 ; y++) {
    for(int x = 0 ; x < widthMap1 ; x++) {
      matrix1->setCell(x, y, map1[y][x]);
    }
  }
  ZlatkoLevel * lvl1 = new ZlatkoLevel(LVL_1, "Niveau 1", matrix1, false);
  levelManager->addLevel(lvl1);

  // Niveau 2
  const int heightMap2 = 40;
  const int widthMap2 = 60;
  const char map2[heightMap2][widthMap2] = {
    /* ... */
  };
  ZlatkoMatrix<char> * matrix2 = new ZlatkoMatrix<char>(widthMap2, heightMap2);
  for(int y = 0 ; y < heightMap2 ; y++) {
    for(int x = 0 ; x < widthMap2 ; x++) {
      matrix2->setCell(x, y, map2[y][x]);
    }
  }
  ZlatkoLevel * lvl2 = new ZlatkoLevel(LVL_2, "Niveau 2", matrix2, true);
  levelManager->addLevel(lvl2);
}

I show UI of level manager in ZlatkoLevelsView.cpp :


#include "ZlatkoLevelsView.h"

ZlatkoLevelsView::ZlatkoLevelsView(ZlatkoLevelManager * aLevelManager) :
  levelManager(aLevelManager)
{
  if(!levelManager->getLevels()->isEmpty()) {
    currentLevel = levelManager->getLevels()->getFirstItem();
  }
}

void ZlatkoLevelsView::paint() const {
  gb.display.setColor(WHITE);
  if(levelManager->getLevels()->isEmpty()) {
    gb.display.print("Aucun niveau");
  } else {
    if(currentLevel->havePrevious()) {
      gb.display.print("< ");
    } else {
      gb.display.print("  ");
    }
    if(currentLevel->haveNext()) {
      gb.display.print(" >");
    }
    gb.display.println("");

    char * labelCurrentLevel = currentLevel->getValue()->getLabel();
    /*gb.display.print("[");
    gb.display.printf("%s", "?");
    gb.display.print("]");*/
    //gb.display.printf("%s", labelCurrentLevel);
    gb.display.print(labelCurrentLevel);
    //gb.display.print(currentLevel->getValue()->getlabel());
    ZlatkoMatrix<char> * mapModel = currentLevel->getValue()->getMap();
    gb.display.println("");
    //gb.display.print(mapModel->getWidth());
    //gb.display.printf("%d x %d", mapModel->getWidth(), mapModel->getHeight());

    /*char * test = "Hello";
    gb.display.println("");
    gb.display.print(test);*/

    gb.display.println("");
    gb.display.print(levelManager->getLevels()->getSize());
  }
}

void ZlatkoLevelsView::manageCommands() {
  if(!levelManager->getLevels()->isEmpty()) {
    if(currentLevel->havePrevious() && gb.buttons.pressed(BUTTON_LEFT)) {
      currentLevel = currentLevel->getPrevious();
    }
    if(currentLevel->haveNext() && gb.buttons.pressed(BUTTON_RIGHT)) {
      currentLevel = currentLevel->getNext();
    }
  }
}

I intialize the all objects in setup function (in ZlatkoEngine::initialize), and show the level manager in the loop function.

When i show the information for the first level :

- to show the name : then the show strange name ;

- to show the size of map : then the game crash ;

- i can't make it : to show the arrow who indicate the previous/next level... but i know that there are two levels becausse i show the number of levels.

Have you an idea, why i have the previous errors ?

All the source code is on GitHub here.

makerSquirrel

NEW 5 years ago

puh, that's a big chunk of code you are having on github. At the moment the most dangerous thing I see is that you do not check the pointers for validity. That your code crashes sounds like something was not initialized correctly and you are shoveling empty pointers around.

So when adding a level, check for nullptr and when calling for a pointer, only access it when you are sure the pointer is valid (so again, check for validity.)

chris-scientist

NEW 5 years ago

Thank you, makerSquirrel ! I'm going to look at my project.