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 /*
24  * This code is based on original Sfinx source code
25  * Copyright (c) 1994-1997 Janusz B. Wisniewski and L.K. Avalon
26  */
27 
28 #include "cge2/spare.h"
29 
30 namespace CGE2 {
31 
sync(Common::Serializer & s)32 void Spare::sync(Common::Serializer &s) {
33 	int size = 0;
34 	if (s.isSaving()) {
35 		for (uint i = 0; i < _container.size(); i++)
36 			if (_container[i]->_ref >= 141)
37 				size++;
38 		s.syncAsSint16LE(size);
39 
40 		for (uint i = 0; i < _container.size(); i++) {
41 			if (_container[i]->_ref >= 141)
42 				_container[i]->sync(s);
43 		}
44 	} else {
45 		s.syncAsSint16LE(size);
46 
47 		for (int i = 0; i < size; i++) {
48 			Sprite *sprite = new Sprite(_vm);
49 			sprite->sync(s);
50 			update(sprite);
51 		}
52 	}
53 }
54 
clear()55 void Spare::clear() {
56 	for (uint i = 0; i < _container.size(); i++)
57 		delete _container[i];
58 
59 	_container.clear();
60 }
61 
locate(int ref)62 Sprite *Spare::locate(int ref) {
63 	for (uint i = 0; i < _container.size(); ++i) {
64 		if (_container[i]->_ref == ref)
65 			return _container[i];
66 	}
67 	return nullptr;
68 }
69 
take(int ref)70 Sprite *Spare::take(int ref) {
71 	Sprite *spr = nullptr;
72 	if ((spr = locate(ref)) != nullptr) {
73 		for (uint i = 0; i < _container.size(); ++i) {
74 			if (spr == _container[i]) {
75 				_container.remove_at(i);
76 				break;
77 			}
78 		}
79 	}
80 	return spr;
81 }
82 
takeScene(int cav)83 void Spare::takeScene(int cav) {
84 	int bakRef = cav << 8;
85 	Common::Array<Sprite*> tempCont = _container;
86 	for (uint i = 0; i < tempCont.size(); ++i) {
87 		Sprite *spr = tempCont[i];
88 		int c = spr->_scene;
89 		if ((c == _vm->_now || c == 0) && spr->_ref != bakRef) {
90 			spr = locate(spr->_ref);
91 			_vm->_vga->_showQ->insert(spr);
92 		}
93 	}
94 }
95 
store(Sprite * spr)96 void Spare::store(Sprite *spr) {
97 	_container.push_back(spr);
98 }
99 
update(Sprite * spr)100 void Spare::update(Sprite *spr) {
101 	Sprite *sp = locate(spr->_ref);
102 	if (sp == nullptr)
103 		store(spr);
104 	else {
105 		sp->contract();
106 		*sp = *spr;
107 	}
108 }
109 
dispose(Sprite * spr)110 void Spare::dispose(Sprite *spr) {
111 	if (spr) {
112 		_vm->_vga->_showQ->remove(spr);
113 		update(spr->contract());
114 	}
115 }
116 
dispose(int ref)117 void Spare::dispose(int ref) {
118 	dispose(_vm->_vga->_showQ->locate(ref));
119 }
120 
dispose()121 void Spare::dispose() {
122 	for (uint i = 0; i < _container.size(); ++i) {
123 		if (_container[i]->_ref > 255)
124 			dispose(_container[i]);
125 	}
126 }
127 
128 } // End of namespace CGE2
129