Background2D.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 <OgreHardwareVertexBuffer.h>
  17. #include <OgreRenderQueueListener.h>
  18. #include <OgreRoot.h>
  19. #include "map/VGearsBackground2DFile.h"
  20. #include "Background2DAnimation.h"
  21. #include "Entity.h"
  22. #include "ScriptManager.h"
  23. /**
  24. * A field background
  25. */
  26. class Background2D : public Ogre::RenderQueueListener{
  27. public:
  28. typedef VGears::Blending Blending;
  29. /**
  30. * How to scroll the background.
  31. */
  32. enum SCROLL_TYPE{
  33. /**
  34. * Don't scroll the background.
  35. */
  36. NONE,
  37. /**
  38. * Linearly scroll.
  39. *
  40. * May seem unnatural in scripted sequences, but it's OK for
  41. * character tracking.
  42. */
  43. LINEAR,
  44. /**
  45. * Smooth (soft in, soft out) scroll.
  46. *
  47. * Best for scripted sequences.
  48. */
  49. SMOOTH
  50. };
  51. /**
  52. * Constructor.
  53. */
  54. Background2D();
  55. /**
  56. * Destructor
  57. */
  58. virtual ~Background2D();
  59. /**
  60. * @todo Understand and document.
  61. * @param[in] event Event.
  62. */
  63. void InputDebug(const VGears::Event& event);
  64. /**
  65. * Run each frame.
  66. *
  67. * Updates animations and changes in the background.
  68. */
  69. void Update();
  70. /**
  71. * Debug inormation about changes in the background.
  72. */
  73. void UpdateDebug();
  74. /**
  75. * Called on window resize.
  76. *
  77. * Resizes the background, keeping the ratio.
  78. */
  79. void OnResize();
  80. /**
  81. * Removes the background and it's animations.
  82. */
  83. void Clear();
  84. /**
  85. * Flags the background to automatically scroll to an entity position.
  86. *
  87. * @param[in] entity The entity to track.
  88. */
  89. void ScriptAutoScrollToEntity(Entity* entity);
  90. /**
  91. * Retrieves the entity currently being tracked for autoscroll.
  92. *
  93. * @return The entity currently being tracked, or nullptr if the background is not
  94. * currently scrolling to any entity.
  95. */
  96. Entity* GetAutoScrollEntity() const;
  97. /**
  98. * Scrolls the background to the position of the playable character.
  99. *
  100. * @param[in] type Scroll type.
  101. * @param[in] seconds Scroll duration.
  102. */
  103. void ScriptScrollToPlayer(const SCROLL_TYPE type, const unsigned int seconds);
  104. /**
  105. * Scrolls the background to a position.
  106. *
  107. * @param[in] x X coordinate to scroll to.
  108. * @param[in] y Y coordinate to scroll to.
  109. * @param[in] type Scroll type.
  110. * @param[in] seconds Duration of the scroll.
  111. */
  112. void ScriptScrollToPosition(
  113. const float x, const float y, const SCROLL_TYPE type, const float seconds
  114. );
  115. /**
  116. * Waits for the scroll to complete.
  117. *
  118. * @return -1.
  119. */
  120. int ScriptScrollSync();
  121. /**
  122. * Offsets the background to a position.
  123. *
  124. * @param[in] x X coordinate to scroll to.
  125. * @param[in] y Y coordinate to scroll to.
  126. */
  127. void ScriptOffset(const float x, const float y);
  128. /**
  129. * Stops the current scrolling.
  130. */
  131. void UnsetScroll();
  132. /**
  133. * Retrieves the initial position of the current scroll action.
  134. *
  135. * @return The initial position of the current scroll action.
  136. */
  137. const Ogre::Vector2& GetScrollPositionStart() const;
  138. /**
  139. * Retrieves the final position of the current scroll action.
  140. *
  141. * @return The final position of the current scroll action.
  142. */
  143. const Ogre::Vector2& GetScrollPositionEnd() const;
  144. /**
  145. * Retrieves the type of the current scroll action.
  146. *
  147. * @return The type of the current scroll action.
  148. */
  149. SCROLL_TYPE GetScrollType() const;
  150. /**
  151. * Retrieves the total duration the current scroll action.
  152. *
  153. * @return Duration of the current scroll action.
  154. */
  155. float GetScrollSeconds() const;
  156. /**
  157. * Sets the time taken by the current scroll action.
  158. *
  159. * It represents the time the current scroll action has been going on for.
  160. *
  161. * @param[in] seconds Time taken by the current scroll action.
  162. */
  163. void SetScrollCurrentSeconds(const float seconds);
  164. /**
  165. * Retrieves the time taken by the current scroll action.
  166. *
  167. * It represents the time the current scroll action has been going on for.
  168. *
  169. * @return Time taken by the current scroll action.
  170. */
  171. float GetScrollCurrentSeconds() const;
  172. /**
  173. * Scroll position in screen coordinates.
  174. *
  175. * @param[in] position Position to set the scroll.
  176. */
  177. void SetScreenScroll(const Ogre::Vector2& position);
  178. /**
  179. * Retrieves the scroll position in screen coordinates.
  180. *
  181. * @return Screen coordinates of the scroll.
  182. */
  183. const Ogre::Vector2 GetScreenScroll() const;
  184. /**
  185. * Sets the scroll in game internal screen coordinates.
  186. *
  187. * @param[in] position Position to set the scroll.
  188. */
  189. void SetScroll(const Ogre::Vector2& position);
  190. /**
  191. * Retrieves the scroll position in game internal screen coordinates.
  192. *
  193. * @return Screen coordinates of the scroll.
  194. */
  195. const Ogre::Vector2& GetScroll() const;
  196. /**
  197. * Sets the background image.
  198. *
  199. * @param[in] image Image name.
  200. */
  201. void SetImage( const Ogre::String& image );
  202. /**
  203. * Set the background scrolling range.
  204. *
  205. * The range is applied in game internal screen coordinates.
  206. *
  207. * @param[in] min_x Min scrollabe x coordinate.
  208. * @param[in] min_y Min scrollabe y coordinate.
  209. * @param[in] max_x Max scrollabe x coordinate.
  210. * @param[in] max_y Max scrollabe y coordinate.
  211. */
  212. void SetRange(const int min_x, const int min_y, const int max_x, const int max_y);
  213. /**
  214. * Set the background scrolling range.
  215. *
  216. * The range is applied in game internal screen coordinates.
  217. *
  218. * @param[in] range Range bounds vector.
  219. */
  220. void SetRange(const Ogre::Vector4& range);
  221. /**
  222. * Adds a tile to the background
  223. *
  224. * @param[in] x X coordinate for the tile.
  225. * @param[in] y Y coordinate for the tile.
  226. * @param[in] width Tile width.
  227. * @param[in] height Tile height.
  228. * @param[in] depth Depth of the tile.
  229. * @param[in] u1
  230. * @param[in] v1
  231. * @param[in] u2
  232. * @param[in] v2
  233. * @param[in] blending Blending mode for the tile
  234. * @todo Depth is z-index??
  235. * @todo What are v1, v2, u1 and u2?
  236. */
  237. void AddTile(
  238. const int x, const int y, const int width, const int height, const float depth,
  239. const float u1, const float v1, const float u2, const float v2, const Blending blending
  240. );
  241. /**
  242. * Adds a tile to the background
  243. *
  244. * @param[in] destination Coordinates for the tile.
  245. * @param[in] width Tile width.
  246. * @param[in] height Tile height.
  247. * @param[in] depth Depth of the tile.
  248. * @param[in] uv
  249. * @param[in] blending Blending mode for the tile
  250. * @todo Depth is z-index??
  251. * @todo What is uv?
  252. */
  253. void AddTile(
  254. const Ogre::Vector2& destination, const int width, const int height,
  255. const float depth, const Ogre::Vector4& uv, const Blending blending
  256. );
  257. /**
  258. * Adds a tile to the background
  259. *
  260. * @param[in] tile The tile to add.
  261. */
  262. void AddTile(const VGears::Tile& tile);
  263. /**
  264. * Updates the UV vector of a tile
  265. *
  266. * @param[in] tile_id ID of the tile to update.
  267. * @param[in] u1
  268. * @param[in] v1
  269. * @param[in] u2
  270. * @param[in] v2
  271. * @todo What are v1, v2, u1 and u2?
  272. */
  273. void UpdateTileUV(
  274. const unsigned int tile_id, const float u1, const float v1, const float u2, const float v2
  275. );
  276. /**
  277. * Adds an animation to the background.
  278. *
  279. * @param[in] animation Animation to add.
  280. */
  281. void AddAnimation(Background2DAnimation* animation);
  282. /**
  283. * Plays an animation.
  284. *
  285. * @param[in] animation The animation to play.
  286. * @param[in] state Animation state.
  287. */
  288. void PlayAnimation(const Ogre::String& animation, const Background2DAnimation::State state);
  289. /**
  290. * Plays an animation in a loop.
  291. *
  292. * The animation is played asynchronously.
  293. *
  294. * @param[in] name Animation name.
  295. */
  296. void ScriptPlayAnimationLooped(const char* name);
  297. /**
  298. * Plays an animation once, then stops.
  299. *
  300. * The animation is played asynchronously.
  301. *
  302. * @param[in] name Animation name.
  303. */
  304. void ScriptPlayAnimationOnce(const char* name);
  305. /**
  306. * Plays an animation once, then stops.
  307. *
  308. * The animation is played synchronously, and the thread is locked
  309. * until it ends.
  310. *
  311. * @param[in] name Animation name.
  312. */
  313. int ScriptAnimationSync(const char* name);
  314. /**
  315. * Ends the render queue.
  316. *
  317. * @param[in] queueGroupId The group id of the queue to end.
  318. * @param[in] invocation
  319. * @param[in] repeatThisInvocation Indicates if the invocation must be repeated.
  320. * @todo Understand and document.
  321. */
  322. void renderQueueEnded(
  323. Ogre::uint8 queueGroupId, const Ogre::String& invocation, bool& repeatThisInvocation
  324. ) override;
  325. /**
  326. * Represents a tile.
  327. */
  328. struct Tile{
  329. /**
  330. * Tile X coordinate.
  331. */
  332. int x;
  333. /**
  334. * Tile Y coordinate.
  335. */
  336. int y;
  337. /**
  338. * Tile width.
  339. */
  340. int width;
  341. /**
  342. * Tile height.
  343. */
  344. int height;
  345. /**
  346. * @todo
  347. */
  348. size_t start_vertex_index;
  349. /**
  350. * Tile blending mode.
  351. */
  352. Blending blending;
  353. };
  354. typedef std::vector<Tile> TileList;
  355. /**
  356. * Loads a background.
  357. *
  358. * @param[in] background The background to load.
  359. */
  360. virtual void load(const VGears::Background2DFilePtr &background);
  361. protected:
  362. /**
  363. * Loads a tile list to the background.
  364. *
  365. * @param[in] tiles The list of tiles to load.
  366. */
  367. virtual void load(const VGears::Background2DFile::TileList &tiles);
  368. /**
  369. * Loads an animation list to the background.
  370. *
  371. * @param[in] tile_index The index of the tile.
  372. * @param[in] animations List of animations to load.
  373. */
  374. virtual void load( const size_t tile_index, const VGears::AnimationMap &animations);
  375. /**
  376. * Apply the camera position to match the current scroll.
  377. */
  378. virtual void applyScroll();
  379. /**
  380. * Calculates the screen scale.
  381. *
  382. * The scale is calculated as the proportion between the viewport and
  383. * the virtual screen.
  384. */
  385. virtual void calculateScreenScale();
  386. /**
  387. * Sets the virtual screen to world space.
  388. *
  389. * @param[in] pos
  390. * @todo Understand and document.
  391. */
  392. virtual void virtualScreenToWorldSpace(Ogre::Vector2 &pos) const;
  393. /**
  394. * Tile vertex properties.
  395. */
  396. enum{
  397. /**
  398. * Tile vertex count.
  399. *
  400. * @todo Understand and document.
  401. */
  402. TILE_VERTEX_COUNT = 6,
  403. /**
  404. * Tile vertex index size.
  405. */
  406. TILE_VERTEX_INDEX_SIZE = TILE_VERTEX_COUNT + 3
  407. };
  408. private:
  409. /**
  410. * Creates all vertex buffers.
  411. */
  412. void CreateVertexBuffers();
  413. /**
  414. * Destroys all vertex buffers.
  415. */
  416. void DestroyVertexBuffers();
  417. /**
  418. * The scene manager.
  419. */
  420. Ogre::SceneManager* scene_manager_;
  421. /**
  422. * The render system.
  423. */
  424. Ogre::RenderSystem* render_system_;
  425. /**
  426. * The list of tiles in the background.
  427. */
  428. TileList tiles_;
  429. /**
  430. * Alpha blending render operation.
  431. */
  432. Ogre::RenderOperation alpha_render_op_;
  433. /**
  434. * Alpha blending vertex buffer.
  435. */
  436. Ogre::HardwareVertexBufferSharedPtr alpha_vertex_buffer_;
  437. /**
  438. * Alpha blending max vertex count.
  439. */
  440. unsigned int alpha_max_vertex_count_;
  441. /**
  442. * Alpha blending material.
  443. */
  444. Ogre::MaterialPtr alpha_material_;
  445. /**
  446. * Add blending render operation.
  447. */
  448. Ogre::RenderOperation add_render_op_;
  449. /**
  450. * Add blending vertex buffer.
  451. */
  452. Ogre::HardwareVertexBufferSharedPtr add_vertex_buffer_;
  453. /**
  454. * Add blending max vertex count.
  455. */
  456. unsigned int add_max_vertex_count_;
  457. /**
  458. * Add blending material.
  459. */
  460. Ogre::MaterialPtr add_material;
  461. /**
  462. * Substract blending render operation.
  463. */
  464. Ogre::RenderOperation subtract_render_op_;
  465. /**
  466. * Substract blending vertex buffer.
  467. */
  468. Ogre::HardwareVertexBufferSharedPtr subtract_vertex_buffer_;
  469. /**
  470. * Substract blending max vertex count.
  471. */
  472. unsigned int subtract_max_vertex_count_;
  473. /**
  474. * Substract blending material.
  475. */
  476. Ogre::MaterialPtr subtract_material_;
  477. /**
  478. * The entity to keep track of with the scroll.
  479. */
  480. Entity* scroll_entity_;
  481. /**
  482. * Starting position of the current scroll action.
  483. */
  484. Ogre::Vector2 scroll_position_start_;
  485. /**
  486. * Final position of the current scroll action.
  487. */
  488. Ogre::Vector2 scroll_position_end_;
  489. /**
  490. * Type of the current scroll action.
  491. */
  492. SCROLL_TYPE scroll_type_;
  493. /**
  494. * Total duration of the current scroll action, in seconds
  495. */
  496. float scroll_seconds_;
  497. /**
  498. * Secund the currens scroll action has taken so far.
  499. */
  500. float scroll_current_seconds_;
  501. /**
  502. * @todo Document.
  503. */
  504. std::vector<ScriptId> scroll_sync_;
  505. /**
  506. * The current scroll position, virtual screen size.
  507. */
  508. Ogre::Vector2 position_;
  509. /**
  510. * The current background offset, virtual screen size.
  511. */
  512. Ogre::Vector2 offset_;
  513. /**
  514. * The current scroll position, viewport size.
  515. *
  516. * Includes the offset.
  517. */
  518. Ogre::Vector2 position_real_;
  519. /**
  520. * The scale of the screen
  521. */
  522. Ogre::Real screen_scale_;
  523. /**
  524. * Screen aspect ration.
  525. */
  526. Ogre::Vector2 screen_proportion_;
  527. /**
  528. * The size of the virtual screen.
  529. */
  530. Ogre::Vector2 virtual_screen_size_;
  531. /**
  532. * @todo Document.
  533. */
  534. Ogre::AxisAlignedBox range_;
  535. /**
  536. * State of an animation.
  537. */
  538. struct AnimationPlayed{
  539. /**
  540. * Animation name.
  541. */
  542. Ogre::String name;
  543. /**
  544. * @todo Document
  545. */
  546. std::vector<ScriptId> sync;
  547. /**
  548. * Animation state.
  549. */
  550. Background2DAnimation::State state;
  551. };
  552. /**
  553. * @todo Document.
  554. */
  555. std::vector<AnimationPlayed> animation_played_;
  556. /**
  557. * List of animations.
  558. */
  559. std::vector<Background2DAnimation*> animations_;
  560. };