Difference between revisions of "Complete roguelike tutorial using C++ and libtcod - part 10.2: game menu"

From RogueBasin
Jump to navigation Jump to search
(pasted →‎top: , cat and sidebar)
 
Line 11: Line 11:
[http://codeumbra.eu/roguecentral.org/doryen/data/libtcod/doc/1.5.2/html2/image_blit.html?c=false&cpp=true&cs=true&py=false&lua=true#3 TCODImage::blit2x]
[http://codeumbra.eu/roguecentral.org/doryen/data/libtcod/doc/1.5.2/html2/image_blit.html?c=false&cpp=true&cs=true&py=false&lua=true#3 TCODImage::blit2x]


==Engine refactoring==


We now have to be able to restart a new game in the middle of a game. For this, we need some function to clean the engine without having to call the destructor :
Engine.hpp :
    void init();
    <span style="color:green">void term();</span>
};
The term function must clean the map, the actors and the message log so that the engine is ready to either restart a new game or load a saved one.
Engine::~Engine() {
    <term();
    delete gui;
}
void Engine::term() {
    actors.clearAndDelete();
    if ( map ) delete map;
    gui->clear();
}
The Gui::clear function clears the log :
Gui::~Gui() {
    delete con;
    clear();
}
void Gui::clear() {
    log.clearAndDelete();
}
Now a small subtlety, this the engine can now be initialized after a game was started, so we need to reset the game status to STARTUP to force the recomputation of the field of view, else the fov would be empty after we load a game from the pause menu. In Engine::init :
    gui->message(TCODColor::red,
        "Welcome stranger!\nPrepare to perish in the Tombs of the Ancient Kings.");
    <span style="color:green">gameStatus=STARTUP;</span>
}
[[Category:Developing]]
[[Category:Developing]]

Revision as of 15:32, 17 October 2015

Complete roguelike tutorial using C++ and libtcod
-originally written by Jice
Text in this tutorial was released under the Creative Commons Attribution-ShareAlike 3.0 Unported and the GNU Free Documentation License (unversioned, with no invariant sections, front-cover texts, or back-cover texts) on 2015-09-21.


In this article, we're going to implement the game menu. This will bring several improvements to the game :

possibility to start a new game even if there is a saved game. possibility to restart a new game once the player dies or wins, or even in the middle of a game. libtcod function used in this article :

TCODImage::TCODImage

TCODImage::blit2x

Engine refactoring

We now have to be able to restart a new game in the middle of a game. For this, we need some function to clean the engine without having to call the destructor :

Engine.hpp :

   void init();
   void term();
};

The term function must clean the map, the actors and the message log so that the engine is ready to either restart a new game or load a saved one.

Engine::~Engine() {
    <term();
   delete gui;
}

void Engine::term() {
   actors.clearAndDelete();
   if ( map ) delete map;
   gui->clear();
}

The Gui::clear function clears the log :

Gui::~Gui() {
   delete con;
   clear();
}

void Gui::clear() {
   log.clearAndDelete();
}

Now a small subtlety, this the engine can now be initialized after a game was started, so we need to reset the game status to STARTUP to force the recomputation of the field of view, else the fov would be empty after we load a game from the pause menu. In Engine::init :

   gui->message(TCODColor::red, 
       "Welcome stranger!\nPrepare to perish in the Tombs of the Ancient Kings.");
    gameStatus=STARTUP;
}