freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BlockManager.h
Go to the documentation of this file.
1  /* freg, Free-Roaming Elementary Game with open and interactive world
2  * Copyright (C) 2012-2014 Alexander 'mmaulwurff' Kromm
3  * mmaulwurff@gmail.com
4  *
5  * This file is part of FREG.
6  *
7  * FREG is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * FREG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with FREG. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #ifndef BLOCKMANAGER_H
21 #define BLOCKMANAGER_H
22 
23 #include "header.h"
24 
25 class Block;
26 class QDataStream;
27 
28 /** \class BlockManager BlockManager.h
29  * \brief This class is used for creating and deleting blocks,
30  * also for loading them from file.
31  *
32  * Memory management, if any, should be implemented in this class.
33  * At the current moment no special memory management is used.
34  *
35  * Normal blocks: blocks that are not special, e.g. usual stone, air, soil
36  * are actually one block (for each substance).
37  * One can receive a pointer to such block with
38  * Block * Normal(int sub).
39  * Normal blocks are not needed to be deleted.
40  * Use Block * NewBlock(int kind, int sub) to receive a pointer to
41  * block that will be changed (damaged, inscribed, etc). */
42 
43 class BlockManager final {
44 public:
45  BlockManager();
46  ~BlockManager();
47 
48  /// Use this to receive a pointer to normal block.
49  Block * Normal(int sub) const;
50  /// Use this to receive a pointer to new not-normal block.
51  static Block * NewBlock(int kind, int sub);
52  /// Use this to load block from file.
53  static Block * BlockFromFile(QDataStream &, int kind, int sub);
54  /// Returns true if block is normal.
55  static bool KindSubFromFile(QDataStream &, quint8 * kind, quint8 * sub);
56  /// Does not actually delete normal blocks.
57  void DeleteBlock(Block *) const;
58  /// For memory economy.
59  /** Checks and replaces block with corresponding normal block.
60  * Can delete block, use carefully. */
61  Block * ReplaceWithNormal(Block * block) const;
62 
63  /// If kind is unknown, returns "unknown_kind".
64  static QString KindToString(int kind);
65  /// If substance is unknown, returns "unknown_sub".
66  static QString SubToString(int sub);
67  /// If string is not convertible to kind, returns LAST_KIND.
68  static int StringToKind(QString);
69  /// If string is not convertible to substance, returns LAST_SUB.
70  static int StringToSub(QString);
71 
72  constexpr static int MakeId(const int kind, const int sub) {
73  return (kind << 6) | sub;
74  }
75 
76  static int KindFromId(int id);
77  static int SubFromId(int id);
78 
79  static bool IsValid(int kind, int sub);
80 
81 private:
82  BlockManager(const BlockManager &) = delete;
83  BlockManager & operator=(const BlockManager &) = delete;
84 
86  static const QByteArray kinds[];
87  static const QByteArray subs[];
88 };
89 
91 
92 #endif // BLOCKMANAGER_H
static int StringToSub(QString)
If string is not convertible to substance, returns LAST_SUB.
void DeleteBlock(Block *) const
Does not actually delete normal blocks.
static int KindFromId(int id)
Block * normals[LAST_SUB]
Definition: BlockManager.h:85
Block * Normal(int sub) const
Use this to receive a pointer to normal block.
Nothing is made from LAST_SUB.
Definition: header.h:193
static QString SubToString(int sub)
If substance is unknown, returns "unknown_sub".
This class is used for creating and deleting blocks, also for loading them from file.
Definition: BlockManager.h:43
Block * ReplaceWithNormal(Block *block) const
For memory economy.
static int StringToKind(QString)
If string is not convertible to kind, returns LAST_KIND.
static int SubFromId(int id)
static bool IsValid(int kind, int sub)
BlockManager block_manager
kinds
Kinds of atom.
Definition: header.h:112
subs
Substance block is made from.
Definition: header.h:157
static Block * NewBlock(int kind, int sub)
Use this to receive a pointer to new not-normal block.
static QString KindToString(int kind)
If kind is unknown, returns "unknown_kind".
BlockManager & operator=(const BlockManager &)=delete
static Block * BlockFromFile(QDataStream &, int kind, int sub)
Use this to load block from file.
Block without special physics and attributes.
Definition: Block.h:89
static constexpr int MakeId(const int kind, const int sub)
Definition: BlockManager.h:72
static bool KindSubFromFile(QDataStream &, quint8 *kind, quint8 *sub)
Returns true if block is normal.