Difference between revisions of "Complete roguelike tutorial using C++ and libtcod - part 10.1: persistence"

From RogueBasin
Jump to navigation Jump to search
(pasted →‎top: , cat and sidebar)
 
(pasted →‎The plan)
Line 17: Line 17:


[http://roguecentral.org/doryen/data/libtcod/doc/1.5.2/html2/system_filesystem.html?c=false&cpp=true&cs=true&py=false&lua=true#5 TCODSystem::fileExists]
[http://roguecentral.org/doryen/data/libtcod/doc/1.5.2/html2/system_filesystem.html?c=false&cpp=true&cs=true&py=false&lua=true#5 TCODSystem::fileExists]
==The plan==
Since this part does not include a game menu, we're going to do a minimal savegame support :
*when the player closes the game window, the game state is saved to a file
*when the player starts the game, it resumes the last saved game
*when the player dies, the savegame is deleted (you wouldn't write a roguelike without permadeath, would you?)
While this simplifies the article, it leaves an annoying case : when the player manage to kill all monsters, he will be stuck. The only way to restart a game is to erase the savegame by hand. This will be fixed in the next article.




[[Category:Developing]]
[[Category:Developing]]

Revision as of 12:32, 16 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 part, we will implement saving and loading the game to be able to close it and resume it later. And this is not going to be pleasing. C++ has a few great feats, but it certainly sucks at type introspection and object serialization. Most modern languages have these features built in. For this reason, the article 10 of Jotaf's python tutorial will be split in two for the C++ version.

In fact, there's a way to serialize a C++ object and store it on the disk using only a couple of lines of code : using boost Serialization library. But we're not going to use it for several reasons :

  • this tutorial is not about using boost but rather how to code the features yourself using basic C++.
  • even if using boost would considerably reduce the size of the code to write, you still have to embed boost in your game. Using hand-made code will replace 8000 lines of boost code with less than 300 lines of specific code, resulting in a lighter executable and a simplified compilation process for your project.
  • using libtcod's zip toolkit means that the save file will be compressed. Of course, this means less space used on the disk, but also harder to hack savegame files. Of course, using boost doesn't keep you from compressing the file, but it just comes at no cost when using libtcod.

Anyway if you prefer to use boost, you can follow this article to reorganize the Map and Engine code, and just skip the whole Persistent stuff and replace Engine::load and Engine::save code with boost invocation.

libtcod features used in this article :

TCODZip

TCODSystem::deleteFile

TCODSystem::fileExists

The plan

Since this part does not include a game menu, we're going to do a minimal savegame support :

  • when the player closes the game window, the game state is saved to a file
  • when the player starts the game, it resumes the last saved game
  • when the player dies, the savegame is deleted (you wouldn't write a roguelike without permadeath, would you?)

While this simplifies the article, it leaves an annoying case : when the player manage to kill all monsters, he will be stuck. The only way to restart a game is to erase the savegame by hand. This will be fixed in the next article.