Background2D.h 16 KB

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