1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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 this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "bladerunner/game_flags.h"
24 
25 #include "bladerunner/savefile.h"
26 
27 #include "common/debug.h"
28 
29 namespace BladeRunner {
30 
GameFlags()31 GameFlags::GameFlags()
32 	: _flags(nullptr), _flagCount(0) {
33 }
34 
~GameFlags()35 GameFlags::~GameFlags() {
36 	delete[] _flags;
37 }
38 
clear()39 void GameFlags::clear() {
40 	for (int i = 0; i <= _flagCount; ++i) {
41 		reset(i);
42 	}
43 }
44 
setFlagCount(int count)45 void GameFlags::setFlagCount(int count) {
46 	assert(count > 0);
47 
48 	_flagCount = count;
49 	_flags = new uint32[count / 32 + 1]();
50 
51 	clear();
52 }
53 
set(int flag)54 void GameFlags::set(int flag) {
55 	assert(flag >= 0 && flag <= _flagCount);
56 
57 	_flags[flag / 32] |= (1 << (flag % 32));
58 }
59 
reset(int flag)60 void GameFlags::reset(int flag) {
61 	assert(flag >= 0 && flag <= _flagCount);
62 
63 	_flags[flag / 32] &= ~(1 << (flag % 32));
64 }
65 
query(int flag) const66 bool GameFlags::query(int flag) const {
67 	//debug("GameFlags::query(%d): %d", flag, !!(flags[flag / 32] & (1 << (flag % 32))));
68 	assert(flag >= 0 && flag <= _flagCount);
69 
70 	return !!(_flags[flag / 32] & (1 << (flag % 32)));
71 }
72 
save(SaveFileWriteStream & f)73 void GameFlags::save(SaveFileWriteStream &f) {
74 	for (int i = 0; i != _flagCount / 32 + 1; ++i) {
75 		f.writeUint32LE(_flags[i]);
76 	}
77 }
78 
load(SaveFileReadStream & f)79 void GameFlags::load(SaveFileReadStream &f) {
80 	for (int i = 0; i != _flagCount / 32 + 1; ++i) {
81 		_flags[i] = f.readUint32LE();
82 	}
83 }
84 
85 } // End of namespace BladeRunner
86