PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/engines/wintermute/ad/ad_region.cpp

http://github.com/scummvm/scummvm
C++ | 415 lines | 257 code | 74 blank | 84 comment | 48 complexity | 0df7cf205c8d45576945a820a95120d5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  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. * This file is based on WME Lite.
  24. * http://dead-code.org/redir.php?target=wmelite
  25. * Copyright (c) 2011 Jan Nedoma
  26. */
  27. #include "engines/wintermute/ad/ad_region.h"
  28. #include "engines/wintermute/base/base_dynamic_buffer.h"
  29. #include "engines/wintermute/base/base_game.h"
  30. #include "engines/wintermute/base/base_file_manager.h"
  31. #include "engines/wintermute/base/base_parser.h"
  32. #include "engines/wintermute/base/scriptables/script_value.h"
  33. #include "engines/wintermute/base/scriptables/script.h"
  34. namespace Wintermute {
  35. IMPLEMENT_PERSISTENT(AdRegion, false)
  36. //////////////////////////////////////////////////////////////////////////
  37. AdRegion::AdRegion(BaseGame *inGame) : BaseRegion(inGame) {
  38. _blocked = false;
  39. _decoration = false;
  40. _zoom = 0;
  41. _alpha = 0xFFFFFFFF;
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. AdRegion::~AdRegion() {
  45. }
  46. uint32 AdRegion::getAlpha() const {
  47. return _alpha;
  48. }
  49. float AdRegion::getZoom() const {
  50. return _zoom;
  51. }
  52. bool AdRegion::isBlocked() const {
  53. return _blocked;
  54. }
  55. bool AdRegion::hasDecoration() const {
  56. return _decoration;
  57. }
  58. //////////////////////////////////////////////////////////////////////////
  59. bool AdRegion::loadFile(const char *filename) {
  60. char *buffer = (char *)BaseFileManager::getEngineInstance()->readWholeFile(filename);
  61. if (buffer == nullptr) {
  62. _gameRef->LOG(0, "AdRegion::LoadFile failed for file '%s'", filename);
  63. return STATUS_FAILED;
  64. }
  65. bool ret;
  66. setFilename(filename);
  67. if (DID_FAIL(ret = loadBuffer(buffer, true))) {
  68. _gameRef->LOG(0, "Error parsing REGION file '%s'", filename);
  69. }
  70. delete[] buffer;
  71. return ret;
  72. }
  73. TOKEN_DEF_START
  74. TOKEN_DEF(REGION)
  75. TOKEN_DEF(TEMPLATE)
  76. TOKEN_DEF(NAME)
  77. TOKEN_DEF(ACTIVE)
  78. TOKEN_DEF(ZOOM)
  79. TOKEN_DEF(SCALE)
  80. TOKEN_DEF(BLOCKED)
  81. TOKEN_DEF(DECORATION)
  82. TOKEN_DEF(POINT)
  83. TOKEN_DEF(ALPHA_COLOR)
  84. TOKEN_DEF(ALPHA)
  85. TOKEN_DEF(EDITOR_SELECTED_POINT)
  86. TOKEN_DEF(EDITOR_SELECTED)
  87. TOKEN_DEF(SCRIPT)
  88. TOKEN_DEF(CAPTION)
  89. TOKEN_DEF(PROPERTY)
  90. TOKEN_DEF(EDITOR_PROPERTY)
  91. TOKEN_DEF_END
  92. //////////////////////////////////////////////////////////////////////////
  93. bool AdRegion::loadBuffer(char *buffer, bool complete) {
  94. TOKEN_TABLE_START(commands)
  95. TOKEN_TABLE(REGION)
  96. TOKEN_TABLE(TEMPLATE)
  97. TOKEN_TABLE(NAME)
  98. TOKEN_TABLE(ACTIVE)
  99. TOKEN_TABLE(ZOOM)
  100. TOKEN_TABLE(SCALE)
  101. TOKEN_TABLE(BLOCKED)
  102. TOKEN_TABLE(DECORATION)
  103. TOKEN_TABLE(POINT)
  104. TOKEN_TABLE(ALPHA_COLOR)
  105. TOKEN_TABLE(ALPHA)
  106. TOKEN_TABLE(EDITOR_SELECTED_POINT)
  107. TOKEN_TABLE(EDITOR_SELECTED)
  108. TOKEN_TABLE(SCRIPT)
  109. TOKEN_TABLE(CAPTION)
  110. TOKEN_TABLE(PROPERTY)
  111. TOKEN_TABLE(EDITOR_PROPERTY)
  112. TOKEN_TABLE_END
  113. char *params;
  114. int cmd;
  115. BaseParser parser;
  116. if (complete) {
  117. if (parser.getCommand(&buffer, commands, &params) != TOKEN_REGION) {
  118. _gameRef->LOG(0, "'REGION' keyword expected.");
  119. return STATUS_FAILED;
  120. }
  121. buffer = params;
  122. }
  123. for (uint32 i = 0; i < _points.size(); i++) {
  124. delete _points[i];
  125. }
  126. _points.clear();
  127. int ar = 255, ag = 255, ab = 255, alpha = 255;
  128. while ((cmd = parser.getCommand(&buffer, commands, &params)) > 0) {
  129. switch (cmd) {
  130. case TOKEN_TEMPLATE:
  131. if (DID_FAIL(loadFile(params))) {
  132. cmd = PARSERR_GENERIC;
  133. }
  134. break;
  135. case TOKEN_NAME:
  136. setName(params);
  137. break;
  138. case TOKEN_CAPTION:
  139. setCaption(params);
  140. break;
  141. case TOKEN_ACTIVE:
  142. parser.scanStr(params, "%b", &_active);
  143. break;
  144. case TOKEN_BLOCKED:
  145. parser.scanStr(params, "%b", &_blocked);
  146. break;
  147. case TOKEN_DECORATION:
  148. parser.scanStr(params, "%b", &_decoration);
  149. break;
  150. case TOKEN_ZOOM:
  151. case TOKEN_SCALE: {
  152. int j;
  153. parser.scanStr(params, "%d", &j);
  154. _zoom = (float)j;
  155. }
  156. break;
  157. case TOKEN_POINT: {
  158. int x, y;
  159. parser.scanStr(params, "%d,%d", &x, &y);
  160. _points.add(new BasePoint(x, y));
  161. }
  162. break;
  163. case TOKEN_ALPHA_COLOR:
  164. parser.scanStr(params, "%d,%d,%d", &ar, &ag, &ab);
  165. break;
  166. case TOKEN_ALPHA:
  167. parser.scanStr(params, "%d", &alpha);
  168. break;
  169. case TOKEN_EDITOR_SELECTED:
  170. parser.scanStr(params, "%b", &_editorSelected);
  171. break;
  172. case TOKEN_EDITOR_SELECTED_POINT:
  173. parser.scanStr(params, "%d", &_editorSelectedPoint);
  174. break;
  175. case TOKEN_SCRIPT:
  176. addScript(params);
  177. break;
  178. case TOKEN_PROPERTY:
  179. parseProperty(params, false);
  180. break;
  181. case TOKEN_EDITOR_PROPERTY:
  182. parseEditorProperty(params, false);
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. if (cmd == PARSERR_TOKENNOTFOUND) {
  189. _gameRef->LOG(0, "Syntax error in REGION definition");
  190. return STATUS_FAILED;
  191. }
  192. createRegion();
  193. _alpha = BYTETORGBA(ar, ag, ab, alpha);
  194. return STATUS_OK;
  195. }
  196. //////////////////////////////////////////////////////////////////////////
  197. // high level scripting interface
  198. //////////////////////////////////////////////////////////////////////////
  199. bool AdRegion::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) {
  200. /*
  201. //////////////////////////////////////////////////////////////////////////
  202. // SkipTo
  203. //////////////////////////////////////////////////////////////////////////
  204. if (strcmp(name, "SkipTo")==0) {
  205. stack->correctParams(2);
  206. _posX = stack->pop()->getInt();
  207. _posY = stack->pop()->getInt();
  208. stack->pushNULL();
  209. return STATUS_OK;
  210. }
  211. else*/ return BaseRegion::scCallMethod(script, stack, thisStack, name);
  212. }
  213. //////////////////////////////////////////////////////////////////////////
  214. ScValue *AdRegion::scGetProperty(const Common::String &name) {
  215. _scValue->setNULL();
  216. //////////////////////////////////////////////////////////////////////////
  217. // Type
  218. //////////////////////////////////////////////////////////////////////////
  219. if (name == "Type") {
  220. _scValue->setString("ad region");
  221. return _scValue;
  222. }
  223. //////////////////////////////////////////////////////////////////////////
  224. // Name
  225. //////////////////////////////////////////////////////////////////////////
  226. else if (name == "Name") {
  227. _scValue->setString(getName());
  228. return _scValue;
  229. }
  230. //////////////////////////////////////////////////////////////////////////
  231. // Blocked
  232. //////////////////////////////////////////////////////////////////////////
  233. else if (name == "Blocked") {
  234. _scValue->setBool(_blocked);
  235. return _scValue;
  236. }
  237. //////////////////////////////////////////////////////////////////////////
  238. // Decoration
  239. //////////////////////////////////////////////////////////////////////////
  240. else if (name == "Decoration") {
  241. _scValue->setBool(_decoration);
  242. return _scValue;
  243. }
  244. //////////////////////////////////////////////////////////////////////////
  245. // Scale
  246. //////////////////////////////////////////////////////////////////////////
  247. else if (name == "Scale") {
  248. _scValue->setFloat(_zoom);
  249. return _scValue;
  250. }
  251. //////////////////////////////////////////////////////////////////////////
  252. // AlphaColor
  253. //////////////////////////////////////////////////////////////////////////
  254. else if (name == "AlphaColor") {
  255. _scValue->setInt((int)_alpha);
  256. return _scValue;
  257. } else {
  258. return BaseRegion::scGetProperty(name);
  259. }
  260. }
  261. //////////////////////////////////////////////////////////////////////////
  262. bool AdRegion::scSetProperty(const char *name, ScValue *value) {
  263. //////////////////////////////////////////////////////////////////////////
  264. // Name
  265. //////////////////////////////////////////////////////////////////////////
  266. if (strcmp(name, "Name") == 0) {
  267. setName(value->getString());
  268. return STATUS_OK;
  269. }
  270. //////////////////////////////////////////////////////////////////////////
  271. // Blocked
  272. //////////////////////////////////////////////////////////////////////////
  273. else if (strcmp(name, "Blocked") == 0) {
  274. _blocked = value->getBool();
  275. return STATUS_OK;
  276. }
  277. //////////////////////////////////////////////////////////////////////////
  278. // Decoration
  279. //////////////////////////////////////////////////////////////////////////
  280. else if (strcmp(name, "Decoration") == 0) {
  281. _decoration = value->getBool();
  282. return STATUS_OK;
  283. }
  284. //////////////////////////////////////////////////////////////////////////
  285. // Scale
  286. //////////////////////////////////////////////////////////////////////////
  287. else if (strcmp(name, "Scale") == 0) {
  288. _zoom = value->getFloat();
  289. return STATUS_OK;
  290. }
  291. //////////////////////////////////////////////////////////////////////////
  292. // AlphaColor
  293. //////////////////////////////////////////////////////////////////////////
  294. else if (strcmp(name, "AlphaColor") == 0) {
  295. _alpha = (uint32)value->getInt();
  296. return STATUS_OK;
  297. } else {
  298. return BaseRegion::scSetProperty(name, value);
  299. }
  300. }
  301. //////////////////////////////////////////////////////////////////////////
  302. const char *AdRegion::scToString() {
  303. return "[ad region]";
  304. }
  305. //////////////////////////////////////////////////////////////////////////
  306. bool AdRegion::saveAsText(BaseDynamicBuffer *buffer, int indent) {
  307. buffer->putTextIndent(indent, "REGION {\n");
  308. buffer->putTextIndent(indent + 2, "NAME=\"%s\"\n", getName());
  309. buffer->putTextIndent(indent + 2, "CAPTION=\"%s\"\n", getCaption());
  310. buffer->putTextIndent(indent + 2, "BLOCKED=%s\n", _blocked ? "TRUE" : "FALSE");
  311. buffer->putTextIndent(indent + 2, "DECORATION=%s\n", _decoration ? "TRUE" : "FALSE");
  312. buffer->putTextIndent(indent + 2, "ACTIVE=%s\n", _active ? "TRUE" : "FALSE");
  313. buffer->putTextIndent(indent + 2, "SCALE=%d\n", (int)_zoom);
  314. buffer->putTextIndent(indent + 2, "ALPHA_COLOR { %d,%d,%d }\n", RGBCOLGetR(_alpha), RGBCOLGetG(_alpha), RGBCOLGetB(_alpha));
  315. buffer->putTextIndent(indent + 2, "ALPHA = %d\n", RGBCOLGetA(_alpha));
  316. buffer->putTextIndent(indent + 2, "EDITOR_SELECTED=%s\n", _editorSelected ? "TRUE" : "FALSE");
  317. for (uint32 i = 0; i < _scripts.size(); i++) {
  318. buffer->putTextIndent(indent + 2, "SCRIPT=\"%s\"\n", _scripts[i]->_filename);
  319. }
  320. if (_scProp) {
  321. _scProp->saveAsText(buffer, indent + 2);
  322. }
  323. for (uint32 i = 0; i < _points.size(); i++) {
  324. buffer->putTextIndent(indent + 2, "POINT {%d,%d}\n", _points[i]->x, _points[i]->y);
  325. }
  326. BaseClass::saveAsText(buffer, indent + 2);
  327. buffer->putTextIndent(indent, "}\n\n");
  328. return STATUS_OK;
  329. }
  330. //////////////////////////////////////////////////////////////////////////
  331. bool AdRegion::persist(BasePersistenceManager *persistMgr) {
  332. BaseRegion::persist(persistMgr);
  333. persistMgr->transferUint32(TMEMBER(_alpha));
  334. persistMgr->transferBool(TMEMBER(_blocked));
  335. persistMgr->transferBool(TMEMBER(_decoration));
  336. persistMgr->transferFloat(TMEMBER(_zoom));
  337. return STATUS_OK;
  338. }
  339. } // End of namespace Wintermute