Background2D.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <map>
  17. #include <set>
  18. #include <OgreHardwareVertexBuffer.h>
  19. #include <OgreRenderQueueListener.h>
  20. #include <OgreRoot.h>
  21. #include "map/VGearsBackground2DFile.h"
  22. #include "Background2DAnimation.h"
  23. #include "Entity.h"
  24. #include "ScriptManager.h"
  25. /**
  26. * A field background
  27. */
  28. class Background2D : public Ogre::RenderQueueListener{
  29. public:
  30. typedef VGears::Blending Blending;
  31. /**
  32. * How to scroll the background.
  33. */
  34. enum SCROLL_TYPE{
  35. /**
  36. * Don't scroll the background.
  37. */
  38. NONE,
  39. /**
  40. * Linearly scroll.
  41. *
  42. * May seem unnatural in scripted sequences, but it's OK for
  43. * character tracking.
  44. */
  45. LINEAR,
  46. /**
  47. * Smooth (soft in, soft out) scroll.
  48. *
  49. * Best for scripted sequences.
  50. */
  51. SMOOTH
  52. };
  53. /**
  54. * Constructor.
  55. */
  56. Background2D();
  57. /**
  58. * Destructor
  59. */
  60. virtual ~Background2D();
  61. /**
  62. * @todo Understand and document.
  63. * @param[in] event Event.
  64. */
  65. void InputDebug(const VGears::Event& event);
  66. /**
  67. * Hides the background.
  68. *
  69. * Once called, the background will not be rendered until {@see Show()} is called. If the
  70. * background is already hidden, it will do nothing.
  71. */
  72. void Hide();
  73. /**
  74. * Shows the background.
  75. *
  76. * Once called, the background will start rendered until {@see Hide()} is called.
  77. */
  78. void Show();
  79. /**
  80. * Run each frame.
  81. *
  82. * Updates animations and changes in the background.
  83. */
  84. void Update();
  85. /**
  86. * Debug inormation about changes in the background.
  87. */
  88. void UpdateDebug();
  89. /**
  90. * Called on window resize.
  91. *
  92. * Resizes the background, keeping the ratio.
  93. */
  94. void OnResize();
  95. /**
  96. * Removes the background and it's animations.
  97. */
  98. void Clear();
  99. /**
  100. * Flags the background to automatically scroll to an entity position.
  101. *
  102. * @param[in] entity The entity to track.
  103. */
  104. void ScriptAutoScrollToEntity(Entity* entity);
  105. /**
  106. * Retrieves the entity currently being tracked for autoscroll.
  107. *
  108. * @return The entity currently being tracked, or nullptr if the background is not
  109. * currently scrolling to any entity.
  110. */
  111. Entity* GetAutoScrollEntity() const;
  112. /**
  113. * Scrolls the background to the position of the playable character.
  114. *
  115. * Background2D::NONE centers the view instantly and resumes following the character. Any
  116. * other type moves the view over time, and leaves it in place once it arrives.
  117. *
  118. * @param[in] type Scroll type.
  119. * @param[in] seconds Scroll duration.
  120. */
  121. void ScriptScrollToPlayer(const SCROLL_TYPE type, const float seconds);
  122. /**
  123. * Scrolls the background to an entity, and keeps following it once it arrives.
  124. *
  125. * @param[in] entity Entity to scroll to.
  126. * @param[in] type Scroll type.
  127. * @param[in] seconds Scroll duration.
  128. */
  129. void ScriptScrollToEntity(Entity* entity, const SCROLL_TYPE type, const float seconds);
  130. /**
  131. * Updates the destination of the current scroll, if it is moving towards an entity.
  132. */
  133. void UpdateScrollTarget();
  134. /**
  135. * Retrieves the scroll position that centers an entity in the view.
  136. *
  137. * @param[in] entity Entity to center.
  138. * @return Scroll position in game internal screen coordinates.
  139. */
  140. const Ogre::Vector2 GetEntityScroll(const Entity* entity) const;
  141. /**
  142. * Scrolls the background to a position.
  143. *
  144. * @param[in] x X coordinate to scroll to.
  145. * @param[in] y Y coordinate to scroll to.
  146. * @param[in] type Scroll type.
  147. * @param[in] seconds Duration of the scroll.
  148. */
  149. void ScriptScrollToPosition(
  150. const float x, const float y, const SCROLL_TYPE type, const float seconds
  151. );
  152. /**
  153. * Waits for the scroll to complete. If no scroll is active, the script just continues.
  154. *
  155. * @return -1.
  156. */
  157. int ScriptScrollSync();
  158. /**
  159. * Offsets the background to a position.
  160. *
  161. * @param[in] x X coordinate to scroll to.
  162. * @param[in] y Y coordinate to scroll to.
  163. */
  164. void ScriptOffset(const float x, const float y);
  165. /**
  166. * Shows the tiles of a background area that are in a layer.
  167. *
  168. * Tiles can belong to a scripted area, and to the layers of it given by a bit mask: layer
  169. * N is the bit 1 << N. They are only drawn while one of their layers is on. Tiles that
  170. * don't belong to an area are always drawn. Areas start with all their layers off.
  171. *
  172. * @param[in] area Area ID.
  173. * @param[in] layer Layer of the area to turn on, from 0 to 31.
  174. */
  175. void ScriptAreaOn(const int area, const int layer);
  176. /**
  177. * Hides the tiles of a background area that are in a layer.
  178. *
  179. * @param[in] area Area ID.
  180. * @param[in] layer Layer of the area to turn off, from 0 to 31.
  181. */
  182. void ScriptAreaOff(const int area, const int layer);
  183. /**
  184. * Hides all the tiles of a background area, whatever their layer.
  185. *
  186. * @param[in] area Area ID.
  187. */
  188. void ScriptAreaClear(const int area);
  189. /**
  190. * Shakes the view.
  191. *
  192. * The shake is added to the scroll, and doesn't change it. Each axis is a chain of random
  193. * bounces of the given amplitude, one every given number of frames. Changing the
  194. * parameters of a shake that is running takes effect when the current bounce ends.
  195. *
  196. * @param[in] type 0 stops the shake and brings the view back, 1 shakes the X axis, 2 the
  197. * Y axis and 3 both.
  198. * @param[in] x_amplitude Amplitude of the bounces in the X axis, in original screen pixels.
  199. * @param[in] x_frames Frames between bounces in the X axis.
  200. * @param[in] y_amplitude Amplitude of the bounces in the Y axis, in original screen pixels.
  201. * @param[in] y_frames Frames between bounces in the Y axis.
  202. */
  203. /**
  204. * Sets the parallax of a background layer, from the distance the scripts give it.
  205. *
  206. * The layer scrolls at the speed of the background multiplied by the distance divided by
  207. * 4096 (the distance of the layers that don't move with it is 4096), so a layer far away
  208. * moves less than the foreground. That relation comes from the values the field scripts
  209. * use and has not been verified against the original game.
  210. *
  211. * @param[in] layer Layer of the background, from 0 to 3.
  212. * @param[in] distance Parallax distance of the layer.
  213. */
  214. void ScriptSetLayerParallax(const int layer, const int distance);
  215. /**
  216. * Sets the speed a background layer scrolls by itself, on top of the scroll of the
  217. * background.
  218. *
  219. * The speeds are given in 1/256 of a pixel of the original screen for each frame of the
  220. * original game (30 per second). That unit has not been verified against the original game.
  221. *
  222. * @param[in] layer Layer of the background, from 0 to 3.
  223. * @param[in] x Speed on the X axis.
  224. * @param[in] y Speed on the Y axis.
  225. */
  226. void ScriptSetLayerScroll(const int layer, const int x, const int y);
  227. void ScriptShake(
  228. const int type, const int x_amplitude, const int x_frames, const int y_amplitude,
  229. const int y_frames
  230. );
  231. /**
  232. * Stops the current scrolling.
  233. */
  234. void UnsetScroll();
  235. /**
  236. * Retrieves the initial position of the current scroll action.
  237. *
  238. * @return The initial position of the current scroll action.
  239. */
  240. const Ogre::Vector2& GetScrollPositionStart() const;
  241. /**
  242. * Retrieves the final position of the current scroll action.
  243. *
  244. * @return The final position of the current scroll action.
  245. */
  246. const Ogre::Vector2& GetScrollPositionEnd() const;
  247. /**
  248. * Retrieves the type of the current scroll action.
  249. *
  250. * @return The type of the current scroll action.
  251. */
  252. SCROLL_TYPE GetScrollType() const;
  253. /**
  254. * Retrieves the total duration the current scroll action.
  255. *
  256. * @return Duration of the current scroll action.
  257. */
  258. float GetScrollSeconds() const;
  259. /**
  260. * Sets the time taken by the current scroll action.
  261. *
  262. * It represents the time the current scroll action has been going on for.
  263. *
  264. * @param[in] seconds Time taken by the current scroll action.
  265. */
  266. void SetScrollCurrentSeconds(const float seconds);
  267. /**
  268. * Retrieves the time taken by the current scroll action.
  269. *
  270. * It represents the time the current scroll action has been going on for.
  271. *
  272. * @return Time taken by the current scroll action.
  273. */
  274. float GetScrollCurrentSeconds() const;
  275. /**
  276. * Scroll position in screen coordinates.
  277. *
  278. * @param[in] position Position to set the scroll.
  279. */
  280. void SetScreenScroll(const Ogre::Vector2& position);
  281. /**
  282. * Retrieves the scroll position in screen coordinates.
  283. *
  284. * @return Screen coordinates of the scroll.
  285. */
  286. const Ogre::Vector2 GetScreenScroll() const;
  287. /**
  288. * Sets the scroll in game internal screen coordinates.
  289. *
  290. * @param[in] position Position to set the scroll.
  291. */
  292. void SetScroll(const Ogre::Vector2& position);
  293. /**
  294. * Retrieves the scroll position in game internal screen coordinates.
  295. *
  296. * @return Screen coordinates of the scroll.
  297. */
  298. const Ogre::Vector2& GetScroll() const;
  299. /**
  300. * Sets the background image.
  301. *
  302. * @param[in] image Image name.
  303. */
  304. void SetImage( const Ogre::String& image );
  305. /**
  306. * Set the background scrolling range.
  307. *
  308. * The range is applied in game internal screen coordinates.
  309. *
  310. * @param[in] min_x Min scrollabe x coordinate.
  311. * @param[in] min_y Min scrollabe y coordinate.
  312. * @param[in] max_x Max scrollabe x coordinate.
  313. * @param[in] max_y Max scrollabe y coordinate.
  314. */
  315. void SetRange(const int min_x, const int min_y, const int max_x, const int max_y);
  316. /**
  317. * Set the background scrolling range.
  318. *
  319. * The range is applied in game internal screen coordinates.
  320. *
  321. * @param[in] range Range bounds vector.
  322. */
  323. void SetRange(const Ogre::Vector4& range);
  324. /**
  325. * Adds a tile to the background
  326. *
  327. * @param[in] x X coordinate for the tile.
  328. * @param[in] y Y coordinate for the tile.
  329. * @param[in] width Tile width.
  330. * @param[in] height Tile height.
  331. * @param[in] depth Depth of the tile.
  332. * @param[in] u1
  333. * @param[in] v1
  334. * @param[in] u2
  335. * @param[in] v2
  336. * @param[in] blending Blending mode for the tile
  337. * @param[in] area Scripted area the tile belongs to, or 0 if it always has to be drawn.
  338. * @param[in] state Bit mask with the layers of the area in which the tile is drawn.
  339. * @todo Depth is z-index??
  340. * @todo What are v1, v2, u1 and u2?
  341. */
  342. void AddTile(
  343. const int x, const int y, const int width, const int height, const float depth,
  344. const float u1, const float v1, const float u2, const float v2, const Blending blending,
  345. const int area = 0, const int state = 0, const int layer = 0
  346. );
  347. /**
  348. * Adds a tile to the background
  349. *
  350. * @param[in] destination Coordinates for the tile.
  351. * @param[in] width Tile width.
  352. * @param[in] height Tile height.
  353. * @param[in] depth Depth of the tile.
  354. * @param[in] uv
  355. * @param[in] blending Blending mode for the tile
  356. * @todo Depth is z-index??
  357. * @todo What is uv?
  358. */
  359. void AddTile(
  360. const Ogre::Vector2& destination, const int width, const int height,
  361. const float depth, const Ogre::Vector4& uv, const Blending blending
  362. );
  363. /**
  364. * Adds a tile to the background
  365. *
  366. * @param[in] tile The tile to add.
  367. */
  368. void AddTile(const VGears::Tile& tile);
  369. /**
  370. * Converts the depth of a tile to the value that is written to the depth buffer.
  371. *
  372. * The depth is projected with the camera. The tiles that are the farthest (the background
  373. * layers) are at depths so close to each other (as 127.938 and 128) that their projected
  374. * values are closer than what the depth buffer can tell apart, and the order they are
  375. * drawn in decided what was in front. For them the order is kept with a linear mapping
  376. * right behind all the rest of what is drawn.
  377. *
  378. * @param[in] depth Depth of the tile.
  379. * @param[in] projection Projection matrix of the camera, with the depth range of the
  380. * render system.
  381. * @return The value for the depth buffer.
  382. */
  383. static float TileDepthToBuffer(const float depth, const Ogre::Matrix4& projection);
  384. /**
  385. * Updates the UV vector of a tile
  386. *
  387. * @param[in] tile_id ID of the tile to update.
  388. * @param[in] u1
  389. * @param[in] v1
  390. * @param[in] u2
  391. * @param[in] v2
  392. * @todo What are v1, v2, u1 and u2?
  393. */
  394. void UpdateTileUV(
  395. const unsigned int tile_id, const float u1, const float v1, const float u2, const float v2
  396. );
  397. /**
  398. * Adds an animation to the background.
  399. *
  400. * @param[in] animation Animation to add.
  401. */
  402. void AddAnimation(Background2DAnimation* animation);
  403. /**
  404. * Plays an animation.
  405. *
  406. * @param[in] animation The animation to play.
  407. * @param[in] state Animation state.
  408. */
  409. void PlayAnimation(const Ogre::String& animation, const Background2DAnimation::State state);
  410. /**
  411. * Plays an animation in a loop.
  412. *
  413. * The animation is played asynchronously.
  414. *
  415. * @param[in] name Animation name.
  416. */
  417. void ScriptPlayAnimationLooped(const char* name);
  418. /**
  419. * Plays an animation once, then stops.
  420. *
  421. * The animation is played asynchronously.
  422. *
  423. * @param[in] name Animation name.
  424. */
  425. void ScriptPlayAnimationOnce(const char* name);
  426. /**
  427. * Plays an animation once, then stops.
  428. *
  429. * The animation is played synchronously, and the thread is locked
  430. * until it ends.
  431. *
  432. * @param[in] name Animation name.
  433. */
  434. int ScriptAnimationSync(const char* name);
  435. /**
  436. * Ends the render queue.
  437. *
  438. * @param[in] queueGroupId The group id of the queue to end.
  439. * @param[in] invocation
  440. * @param[in] repeatThisInvocation Indicates if the invocation must be repeated.
  441. * @todo Understand and document.
  442. */
  443. void renderQueueEnded(
  444. Ogre::uint8 queueGroupId, const Ogre::String& invocation, bool& repeatThisInvocation
  445. ) override;
  446. /**
  447. * Represents a tile.
  448. */
  449. struct Tile{
  450. /**
  451. * Tile X coordinate.
  452. */
  453. int x;
  454. /**
  455. * Tile Y coordinate.
  456. */
  457. int y;
  458. /**
  459. * Tile width.
  460. */
  461. int width;
  462. /**
  463. * Tile height.
  464. */
  465. int height;
  466. /**
  467. * @todo
  468. */
  469. size_t start_vertex_index;
  470. /**
  471. * Tile blending mode.
  472. */
  473. Blending blending;
  474. /**
  475. * Scripted area the tile belongs to, or 0 if it is always drawn.
  476. */
  477. int area;
  478. /**
  479. * Bit mask with the layers of the area in which the tile is drawn.
  480. */
  481. int state;
  482. /**
  483. * Whether the tile is drawn.
  484. */
  485. bool visible;
  486. };
  487. typedef std::vector<Tile> TileList;
  488. /**
  489. * Checks if any of the layers in a bit mask is on for an area.
  490. *
  491. * @param[in] area Area ID.
  492. * @param[in] layers Bit mask with layers of the area.
  493. * @return True if one of the layers is on.
  494. */
  495. bool IsAreaStateOn(const int area, const int layers) const;
  496. /**
  497. * A chain of random bounces in one axis of the view.
  498. */
  499. struct ShakeAxis{
  500. /**
  501. * Amplitude of the next bounces, in original screen pixels.
  502. */
  503. float amplitude = 0;
  504. /**
  505. * Duration of the next bounces, in seconds.
  506. */
  507. float seconds = 0;
  508. /**
  509. * Whether the axis is moving.
  510. */
  511. bool running = false;
  512. /**
  513. * Whether the axis is going back to the center to stop.
  514. */
  515. bool stopping = false;
  516. /**
  517. * Duration of the movement back to the center, in seconds.
  518. */
  519. float stop_seconds = 0;
  520. /**
  521. * Position at the start of the current bounce.
  522. */
  523. float from = 0;
  524. /**
  525. * Position at the end of the current bounce.
  526. */
  527. float to = 0;
  528. /**
  529. * Duration of the current bounce, in seconds.
  530. */
  531. float duration = 0;
  532. /**
  533. * Time since the start of the current bounce, in seconds.
  534. */
  535. float elapsed = 0;
  536. /**
  537. * Index of the next random factor.
  538. */
  539. size_t step = 0;
  540. /**
  541. * Current position, in original screen pixels.
  542. */
  543. float value = 0;
  544. /**
  545. * Changes the bounces of the axis, and starts it if it is stopped.
  546. */
  547. void Configure(const float new_amplitude, const float new_seconds);
  548. /**
  549. * Makes the axis go back to the center and stop after the current bounce.
  550. */
  551. void Stop(const float seconds_to_center);
  552. /**
  553. * Moves the axis forward in time.
  554. *
  555. * @return True if the position changed.
  556. */
  557. bool Advance(const float delta);
  558. /**
  559. * Starts the next bounce.
  560. */
  561. void StartBounce();
  562. };
  563. /**
  564. * Moves the shake forward in time, and applies it to the view if it changed.
  565. *
  566. * @param[in] delta Seconds since the last frame.
  567. */
  568. void UpdateShake(const float delta);
  569. /**
  570. * Draws or hides the tiles of an area according to its states.
  571. *
  572. * @param[in] area Area ID.
  573. */
  574. void RefreshArea(const int area);
  575. /**
  576. * Writes the position of a tile in its vertex buffer.
  577. *
  578. * A tile that is not visible is written collapsed, so that nothing is rasterised.
  579. *
  580. * @param[in] tile Tile to write.
  581. */
  582. void WriteTilePosition(const Tile& tile);
  583. /**
  584. * Loads a background.
  585. *
  586. * @param[in] background The background to load.
  587. */
  588. virtual void load(const VGears::Background2DFilePtr &background);
  589. protected:
  590. /**
  591. * Loads a tile list to the background.
  592. *
  593. * @param[in] tiles The list of tiles to load.
  594. */
  595. virtual void load(const VGears::Background2DFile::TileList &tiles);
  596. /**
  597. * Loads an animation list to the background.
  598. *
  599. * @param[in] tile_index The index of the tile.
  600. * @param[in] animations List of animations to load.
  601. */
  602. virtual void load( const size_t tile_index, const VGears::AnimationMap &animations);
  603. /**
  604. * Apply the camera position to match the current scroll.
  605. */
  606. virtual void applyScroll();
  607. /**
  608. * Starts a scroll, replacing the current one if there is any.
  609. *
  610. * A scroll without type or duration is applied immediately.
  611. *
  612. * @param[in] position Position to scroll to.
  613. * @param[in] type Scroll type.
  614. * @param[in] seconds Scroll duration.
  615. * @param[in] target_entity Entity the scroll moves towards, or nullptr.
  616. * @param[in] follow_target Keep following target_entity when the scroll ends.
  617. */
  618. void StartScroll(
  619. const Ogre::Vector2& position, const SCROLL_TYPE type, const float seconds,
  620. Entity* target_entity, const bool follow_target
  621. );
  622. /**
  623. * Limits a scroll position to the background range.
  624. *
  625. * @param[in] position Position to limit.
  626. * @return The position, inside the background range.
  627. */
  628. const Ogre::Vector2 ClampScroll(const Ogre::Vector2& position) const;
  629. /**
  630. * Calculates the screen scale.
  631. *
  632. * The scale is calculated as the proportion between the viewport and
  633. * the virtual screen.
  634. */
  635. virtual void calculateScreenScale();
  636. /**
  637. * Sets the virtual screen to world space.
  638. *
  639. * @param[in] pos
  640. * @todo Understand and document.
  641. */
  642. virtual void virtualScreenToWorldSpace(Ogre::Vector2 &pos) const;
  643. /**
  644. * Tile vertex properties.
  645. */
  646. enum{
  647. /**
  648. * Tile vertex count.
  649. *
  650. * @todo Understand and document.
  651. */
  652. TILE_VERTEX_COUNT = 6,
  653. /**
  654. * Tile vertex index size.
  655. */
  656. TILE_VERTEX_INDEX_SIZE = TILE_VERTEX_COUNT + 3
  657. };
  658. private:
  659. /**
  660. * Creates all vertex buffers.
  661. */
  662. void CreateVertexBuffers();
  663. /**
  664. * Destroys all vertex buffers.
  665. */
  666. void DestroyVertexBuffers();
  667. /**
  668. * The scene manager.
  669. */
  670. Ogre::SceneManager* scene_manager_;
  671. /**
  672. * The render system.
  673. */
  674. Ogre::RenderSystem* render_system_;
  675. /**
  676. * The list of tiles in the background.
  677. */
  678. TileList tiles_;
  679. /**
  680. * The layers that are on for each scripted area, as a bit mask.
  681. */
  682. std::map<int, unsigned int> area_states_on_;
  683. /**
  684. * The shake of the view in the X axis.
  685. */
  686. ShakeAxis shake_x_;
  687. /**
  688. * The shake of the view in the Y axis.
  689. */
  690. ShakeAxis shake_y_;
  691. /**
  692. * The shake being applied to the scroll, in virtual screen units.
  693. */
  694. Ogre::Vector2 shake_offset_ = Ogre::Vector2::ZERO;
  695. /**
  696. * Alpha blending render operation.
  697. */
  698. /**
  699. * The number of layers of a background.
  700. */
  701. static const int LAYER_COUNT = 4;
  702. /**
  703. * Part of a vertex buffer that has the tiles of a layer.
  704. */
  705. struct LayerRange{
  706. LayerRange(): first(0), count(0){}
  707. unsigned int first;
  708. unsigned int count;
  709. };
  710. /**
  711. * The tiles of each layer in the buffers of alpha, add and subtract tiles. They are only
  712. * used if the tiles of a layer are together, which is how the installer writes them.
  713. */
  714. LayerRange layer_ranges_[3][LAYER_COUNT];
  715. /**
  716. * The last layer a tile has been added to for each buffer.
  717. */
  718. int last_layer_[3];
  719. /**
  720. * If the tiles of the layers of each buffer are together, one layer after the other.
  721. */
  722. bool layers_contiguous_[3];
  723. /**
  724. * Scroll of each layer relative to the one of the background.
  725. */
  726. float layer_parallax_[LAYER_COUNT][2];
  727. /**
  728. * Speed at which each layer scrolls by itself, in pixels of the virtual screen per second.
  729. */
  730. float layer_scroll_speed_[LAYER_COUNT][2];
  731. /**
  732. * Distance each layer has scrolled by itself, in pixels of the virtual screen.
  733. */
  734. float layer_scroll_offset_[LAYER_COUNT][2];
  735. /**
  736. * Sets the layers to the state they have when a background is loaded.
  737. */
  738. void ResetLayers();
  739. /**
  740. * If any layer is not scrolled as the background is.
  741. */
  742. bool HasLayerScroll() const;
  743. /**
  744. * Draws the tiles of a buffer, each layer scrolled as it has to be.
  745. *
  746. * @param[in] buffer Buffer to draw: 0 for alpha, 1 for add and 2 for subtract.
  747. * @param[in,out] render_op What to draw.
  748. * @param[in] material Material to draw it with.
  749. * @param[in] rs_params Parameters of the render system.
  750. * @param[in] width Width of the viewport.
  751. * @param[in] height Height of the viewport.
  752. */
  753. void RenderBuffer(
  754. const int buffer, Ogre::RenderOperation& render_op, const Ogre::MaterialPtr& material,
  755. Ogre::GpuProgramParametersPtr& rs_params, const float width, const float height
  756. );
  757. Ogre::RenderOperation alpha_render_op_;
  758. /**
  759. * Alpha blending vertex buffer.
  760. */
  761. Ogre::HardwareVertexBufferSharedPtr alpha_vertex_buffer_;
  762. /**
  763. * Alpha blending max vertex count.
  764. */
  765. unsigned int alpha_max_vertex_count_;
  766. /**
  767. * Alpha blending material.
  768. */
  769. Ogre::MaterialPtr alpha_material_;
  770. /**
  771. * Add blending render operation.
  772. */
  773. Ogre::RenderOperation add_render_op_;
  774. /**
  775. * Add blending vertex buffer.
  776. */
  777. Ogre::HardwareVertexBufferSharedPtr add_vertex_buffer_;
  778. /**
  779. * Add blending max vertex count.
  780. */
  781. unsigned int add_max_vertex_count_;
  782. /**
  783. * Add blending material.
  784. */
  785. Ogre::MaterialPtr add_material;
  786. /**
  787. * Substract blending render operation.
  788. */
  789. Ogre::RenderOperation subtract_render_op_;
  790. /**
  791. * Substract blending vertex buffer.
  792. */
  793. Ogre::HardwareVertexBufferSharedPtr subtract_vertex_buffer_;
  794. /**
  795. * Substract blending max vertex count.
  796. */
  797. unsigned int subtract_max_vertex_count_;
  798. /**
  799. * Substract blending material.
  800. */
  801. Ogre::MaterialPtr subtract_material_;
  802. /**
  803. * The entity to keep track of with the scroll.
  804. */
  805. Entity* scroll_entity_;
  806. /**
  807. * Starting position of the current scroll action.
  808. */
  809. Ogre::Vector2 scroll_position_start_;
  810. /**
  811. * Final position of the current scroll action.
  812. */
  813. Ogre::Vector2 scroll_position_end_;
  814. /**
  815. * Type of the current scroll action.
  816. */
  817. SCROLL_TYPE scroll_type_;
  818. /**
  819. * Total duration of the current scroll action, in seconds
  820. */
  821. float scroll_seconds_;
  822. /**
  823. * Secund the currens scroll action has taken so far.
  824. */
  825. float scroll_current_seconds_;
  826. /**
  827. * Entity the current scroll action moves towards, or nullptr.
  828. */
  829. Entity* scroll_target_entity_;
  830. /**
  831. * Whether to keep following the target entity when the current scroll action ends.
  832. */
  833. bool scroll_follow_target_;
  834. /**
  835. * Position of an instant scroll that waits for the end of the frame.
  836. *
  837. * A script that sets a position and scrolls to another one in the same frame must see the
  838. * scroll start where the camera was, not at the first position. See ScriptScrollToPosition.
  839. */
  840. Ogre::Vector2 pending_scroll_;
  841. /**
  842. * Whether there is an instant scroll waiting for the end of the frame.
  843. */
  844. bool has_pending_scroll_ = false;
  845. /**
  846. * @todo Document.
  847. */
  848. std::vector<ScriptId> scroll_sync_;
  849. /**
  850. * The current scroll position, virtual screen size.
  851. */
  852. Ogre::Vector2 position_;
  853. /**
  854. * The current background offset, virtual screen size.
  855. */
  856. Ogre::Vector2 offset_;
  857. /**
  858. * The current scroll position, viewport size.
  859. *
  860. * Includes the offset.
  861. */
  862. Ogre::Vector2 position_real_;
  863. /**
  864. * The scale of the screen
  865. */
  866. Ogre::Real screen_scale_;
  867. /**
  868. * Screen aspect ration.
  869. */
  870. Ogre::Vector2 screen_proportion_;
  871. /**
  872. * The size of the virtual screen.
  873. */
  874. Ogre::Vector2 virtual_screen_size_;
  875. /**
  876. * @todo Document.
  877. */
  878. Ogre::AxisAlignedBox range_;
  879. /**
  880. * State of an animation.
  881. */
  882. struct AnimationPlayed{
  883. /**
  884. * Animation name.
  885. */
  886. Ogre::String name;
  887. /**
  888. * @todo Document
  889. */
  890. std::vector<ScriptId> sync;
  891. /**
  892. * Animation state.
  893. */
  894. Background2DAnimation::State state;
  895. };
  896. /**
  897. * @todo Document.
  898. */
  899. std::vector<AnimationPlayed> animation_played_;
  900. /**
  901. * List of animations.
  902. */
  903. std::vector<Background2DAnimation*> animations_;
  904. };