Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.41 KB | None | 0 0
  1.  
  2. #include <memory>
  3.  
  4. #include "desktop/assets_manager.hpp"
  5. #include "desktop/logger.hpp"
  6.  
  7. #include "sdl/sdl_wrapper.hpp"
  8.  
  9. #include "common/fps_counter.hpp"
  10. #include "common/quad.hpp"
  11. #include "common/make_unique.hpp"
  12.  
  13. #include "presentation/text_renderer_interface.hpp"
  14. #include "presentation/opengl/mesh.hpp"
  15. #include "presentation/opengl/opengl_linker_utils.hpp"
  16. #include "presentation/opengl/texture_loader.hpp"
  17. #include "presentation/opengl/programs/chromatic_aberration.hpp"
  18. #include "presentation/opengl/programs/distance_field.hpp"
  19. #include "presentation/opengl/programs/sprite.hpp"
  20. #include "presentation/opengl/utils/batch_buffer.hpp"
  21.  
  22. #include "presentation/text/font.hpp"
  23. #include "presentation/text/glyph_atlas_serialization.hpp"
  24.  
  25. #include "presentation/texture_atlas.hpp"
  26. #include "presentation/utils/box_to_quad.hpp"
  27.  
  28. /////////////
  29. #include "presentation/box.hpp"
  30.  
  31. std::vector<presentation::Box> SplitIntoGrid(presentation::Box const &whole, std::vector<float> mix_x, std::vector<float> mix_y) {
  32. std::vector<presentation::Box> grid;
  33.  
  34. auto y_split = presentation::SplitByNthCoordinate(whole, 1, mix_y);
  35. for (size_t y = 0; y < mix_y.size(); ++y) {
  36. auto x_split = presentation::SplitByNthCoordinate(y_split[y], 0, mix_x);
  37. for (auto const &part : x_split) {
  38. grid.push_back(part);
  39. }
  40. }
  41.  
  42. return grid;
  43. }
  44.  
  45. std::vector<presentation::Box> SplitNinePatchWithMargin(presentation::Box const &whole, float margin) {
  46. std::vector<float> mix_x({ margin, whole.Width() - 2.0f * margin, margin});
  47. std::vector<float> mix_y({ margin, whole.Height() - 2.0f * margin, margin});
  48. return SplitIntoGrid(whole, mix_x, mix_y);
  49. }
  50.  
  51. struct MenuLayout {
  52. MenuLayout() :
  53. whole_(), margins_(), grid_() {
  54. }
  55.  
  56. explicit MenuLayout(presentation::Box const &whole) :
  57. whole_(whole),
  58. margins_(SplitNinePatchWithMargin(whole_, 0.3f)),
  59. grid_(SplitIntoGrid(margins_[4], std::vector<float>(1, 1.0f), std::vector<float>(4, 1.0f))) {
  60. }
  61.  
  62. presentation::Box whole_;
  63. std::vector<presentation::Box> margins_;
  64. std::vector<presentation::Box> grid_;
  65. };
  66. ////////////////////////////////
  67.  
  68. #include "presentation/text.hpp"
  69.  
  70. class TextRenderer {
  71. public:
  72. TextRenderer(common::AssetsManagerInterface const &assets_manager) :
  73. // sprite_(assets_manager), font_texture_(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("ExportedFont.png"))), atlas_(presentation::TextureAtlas::GenerateSquareGrid(16)),
  74. sprite_(assets_manager), font_texture_(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("FONT_DISTANCE_FIELD.png"))), atlas_(presentation::TextureAtlas::GenerateSquareGrid(16)),
  75. positions_(256), texture_uvs_(256), colors_(256), count_() {
  76. }
  77.  
  78. void TextureUVs(std::string const &text) {
  79. //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: generate texture UV for string from atlas
  80. }
  81.  
  82. void Add(presentation::Box box, std::string const &text) {
  83. glm::vec4 color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
  84.  
  85. float const font_x_to_y_ratio = 1.0f;
  86.  
  87. presentation::Text::char_positions_function_type character_position = [&](size_t position_in_string, float unit_left, float unit_bottom, float unit_right, float unit_top) {
  88. presentation::Box glyph_box(box.left_bottom_ + box.Diagonal() * presentation::Box::position_type(unit_left, unit_bottom), box.left_bottom_ + box.Diagonal() * presentation::Box::position_type(unit_right, unit_top));
  89.  
  90. {
  91. common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(glyph_box, 0.0f));
  92. common::Quad<glm::vec2> quad_texture(atlas_.GetSubtexture(text[position_in_string]));
  93. common::Quad<glm::vec4> quad_color(color, color, color, color);
  94.  
  95. positions_.Add(quad_position);
  96. texture_uvs_.Add(quad_texture);
  97. colors_.Add(quad_color);
  98. }
  99. };
  100.  
  101. presentation::Text::TextLayout(text, box.Diagonal().x / box.Diagonal().y, font_x_to_y_ratio, character_position);
  102. }
  103.  
  104. void Flush() {
  105. count_ = positions_.GetNumberOfVertices();
  106. positions_.FlushBuffer();
  107. texture_uvs_.FlushBuffer();
  108. colors_.FlushBuffer();
  109. }
  110.  
  111. void Draw(glm::mat4 const &mvp_matrix) {
  112. sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp_matrix, font_texture_.Get(), count_);
  113. }
  114.  
  115. private:
  116. presentation::opengl::programs::Sprite sprite_;
  117. presentation::opengl::Texture font_texture_;
  118. presentation::TextureAtlas atlas_;
  119.  
  120. presentation::opengl::utils::BatchBuffer<glm::vec3> positions_;
  121. presentation::opengl::utils::BatchBuffer<glm::vec2> texture_uvs_;
  122. presentation::opengl::utils::BatchBuffer<glm::vec4> colors_;
  123. size_t count_;
  124. };
  125.  
  126. ////////////////////////////
  127.  
  128. //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: copy paste
  129. std::unique_ptr<presentation::opengl::TrianglesMesh> BasicMesh() {
  130. std::vector<glm::vec3> out_positions(presentation::opengl::utils::QUAD_VERTICES);
  131. std::vector<glm::vec2> out_texture_uvs(presentation::opengl::utils::QUAD_VERTICES);
  132. std::vector<glm::vec3> out_normals(presentation::opengl::utils::QUAD_VERTICES);
  133.  
  134. {
  135. common::Quad<glm::vec3> quad_position(glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec3(1.0f, -1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 0.0f), glm::vec3(-1.0f, 1.0f, 0.0f));
  136. common::Quad<glm::vec2> quad_texture(glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f));
  137.  
  138. glm::vec3 up(0.0f, 0.0f, 1.0f);
  139. common::Quad<glm::vec3> quad_normal(up, up, up, up);
  140.  
  141. presentation::opengl::utils::BatchBuffer<glm::vec3>::QuadToTrianglesVector(quad_position, out_positions, 0);
  142. presentation::opengl::utils::BatchBuffer<glm::vec2>::QuadToTrianglesVector(quad_texture, out_texture_uvs, 0);
  143. presentation::opengl::utils::BatchBuffer<glm::vec3>::QuadToTrianglesVector(quad_normal, out_normals, 0);
  144. }
  145.  
  146. return common::make_unique<presentation::opengl::TrianglesMesh>(
  147. common::make_unique<presentation::opengl::ArrayBuffer<glm::vec3>>(out_positions.data(), out_positions.size(), false),
  148. common::make_unique<presentation::opengl::ArrayBuffer<glm::vec2>>(out_texture_uvs.data(), out_positions.size(), false),
  149. common::make_unique<presentation::opengl::ArrayBuffer<glm::vec3>>(out_normals.data(), out_positions.size(), false)
  150. );
  151. }
  152.  
  153. /////////////////////
  154.  
  155.  
  156. class MenuModel {
  157. public:
  158. MenuModel() :
  159. selected_(0),
  160. texts_({
  161. "New game",
  162. "Options",
  163. "Credits with super long text attached to it",
  164. "Exit"
  165. }) {
  166. }
  167.  
  168. size_t GetSelected() const {
  169. return selected_;
  170. }
  171.  
  172. void SetSelected(int selected) {
  173. selected_ = selected;
  174. }
  175.  
  176. size_t GetSize() const {
  177. return 4;
  178. }
  179.  
  180. void Down() {
  181. ++selected_;
  182. if (selected_ == GetSize()) {
  183. selected_ = 0;
  184. }
  185. }
  186.  
  187. void Up() {
  188. if (selected_ == 0) {
  189. selected_ = GetSize();
  190. }
  191. --selected_;
  192. }
  193.  
  194. void Left() {
  195. }
  196.  
  197. void Right() {
  198. }
  199.  
  200. void Back() {
  201. //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: go to previous menu
  202. }
  203.  
  204. void Action() {
  205. //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: do what selected control does
  206. }
  207.  
  208. private:
  209. size_t selected_;
  210. public:
  211. std::vector<std::string> texts_; //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: take from outside
  212. };
  213.  
  214. #include <boost/optional.hpp>
  215.  
  216. class MenuController: public input::GameControllerInterface {
  217. public:
  218. MenuController() :
  219. saved_direction_() {
  220. }
  221.  
  222. void OnAxisMotion(device_id device, axis_id axis, float value) override {
  223. }
  224.  
  225. void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
  226. saved_direction_ = direction;
  227. }
  228.  
  229. void OnJoyButton(device_id device, button_id button, bool up) override {
  230.  
  231. }
  232.  
  233. void Control(MenuModel &menu) {
  234. if (saved_direction_) {
  235. if (saved_direction_ == HatDirection::DOWN) {
  236. menu.Down();
  237. }
  238.  
  239. if (saved_direction_ == HatDirection::UP) {
  240. menu.Up();
  241. }
  242.  
  243. saved_direction_.reset();
  244. }
  245. }
  246. private:
  247. boost::optional<HatDirection> saved_direction_;
  248. };
  249.  
  250. ////////////////////////
  251.  
  252. //////////////////////////////
  253.  
  254. struct PlayerStatus: public input::GameControllerInterface {
  255. enum class WHAT {
  256. WEAKLY,
  257. PERMANENT
  258. };
  259.  
  260. static int const JOIN_THRESHOLD = 60;
  261.  
  262. PlayerStatus(input::GameControllerInterface::device_id device) :
  263. state_(WHAT::WEAKLY), device_(device), button_(), ticks_(0) {
  264. }
  265.  
  266. void OnAxisMotion(device_id device, axis_id axis, float value) override {
  267. }
  268.  
  269. void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
  270. }
  271.  
  272. void OnJoyButton(device_id device, button_id button, bool up) override {
  273. if (state_ == PlayerStatus::WHAT::WEAKLY) {
  274. if (up) {
  275. if (button_ == button) {
  276. button_.reset();
  277. }
  278. } if (not button_) {
  279. button_ = button;
  280. }
  281. }
  282. }
  283.  
  284. void Update() {
  285. if (button_) {
  286. ++ticks_;
  287. if (ticks_ > JOIN_THRESHOLD) {
  288. if (state_ == WHAT::WEAKLY) {
  289. state_ = WHAT::PERMANENT;
  290. }
  291. }
  292. } else {
  293. --ticks_;
  294. }
  295. }
  296.  
  297. bool Unassigned() const {
  298. return (state_ == WHAT::WEAKLY) and (ticks_ <= 0);
  299. }
  300.  
  301. WHAT state_;
  302. input::GameControllerInterface::device_id device_;
  303. boost::optional<input::GameControllerInterface::button_id> button_;
  304.  
  305. int ticks_;
  306. };
  307.  
  308. class TestController: public input::GameControllerInterface {
  309. public:
  310. TestController() :
  311. weakly_assigned_(4) {
  312. }
  313.  
  314. typedef std::vector<boost::optional<PlayerStatus>> container_type;
  315. container_type weakly_assigned_;
  316.  
  317. void OnAxisMotion(device_id device, axis_id axis, float value) override {
  318. }
  319.  
  320. void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
  321. }
  322.  
  323. void OnJoyButton(device_id device, button_id button, bool up) override {
  324. container_type::iterator assigned_it = FindPlayer(device);
  325. if (assigned_it != weakly_assigned_.end()) {
  326. (*assigned_it)->OnJoyButton(device, button, up); ///
  327. } else {
  328. if (up) {
  329. } else {
  330. container_type::iterator free_it = std::find_if(weakly_assigned_.begin(), weakly_assigned_.end(), [&device](container_type::value_type const &element) {
  331. return element;
  332. });
  333. if (free_it != weakly_assigned_.end()) {
  334. *free_it = PlayerStatus(device);
  335. (*free_it)->OnJoyButton(device, button, up); ///
  336. }
  337. }
  338. }
  339. }
  340.  
  341. void Update() {
  342. for (container_type::iterator it = weakly_assigned_.begin(); it != weakly_assigned_.end(); ++it) {
  343. if ((*it)->Unassigned()) {
  344. it->reset();
  345. }
  346. }
  347. }
  348.  
  349. container_type::iterator FindPlayer(device_id device) {
  350. container_type::iterator it = std::find_if(weakly_assigned_.begin(), weakly_assigned_.end(), [&device](container_type::value_type const &element) {
  351. if (element) {
  352. return element->device_ == device;
  353. }
  354. return false;
  355. });
  356. return it;
  357. }
  358. };
  359. //////////////////////////////
  360.  
  361. struct DRAWER500 {
  362. DRAWER500(common::AssetsManagerInterface &assets_manager) :
  363. active_color_(),
  364. passive_color_(1.0f, 1.0f, 0.0f, 1.0f),
  365. layout_(),
  366. sprite_(assets_manager),
  367. distance_field_program_(assets_manager),
  368. text_renderer_(assets_manager),
  369. positions_(2048),
  370. texture_uvs_(2048),
  371. colors_(2048),
  372. count_(),
  373. frame_texture_(new presentation::opengl::Texture(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("rocket.png")))),
  374.  
  375. //test_text_("MBardzo ciekawy text !@#$%^&*()_-=+1234567890"),
  376. //test_text_("_MB!^)_ -10"),
  377. //test_text_("MBard zo c iekawy text !@#$%^&*()_-=+123 890 M a o ciekawy text ! %^ &*()_-=+1234567890MBardzo ciekawy text !@#$%^&*()_-=+1234567890 MBardzo ciekawy text !@#$%^&*()_-=+1234567890 MBardzo ciekawy text !@#$%^&*()_-=+1234567890 MBard zo c iekawy text !@#$%^&*()_-=+123 890 M a o ciekawy text ! %^ &*()_-=+1234567890MBardzo ciekawy text !@#$%^&*()_-=+1234567890 MBardzo ciekawy text !@#$%^&*()_-=+1234567890 MBardzociekaw ytext!@#$%^&*()_-=+1234567890"),
  378. test_text_("MBard zo c ieka wy t ext !@# $%^&*()_-=+123 890 M a o c iekawy text ! %^ &*()_-=+123456 78 90MB ardzo ciekawy text !@ #$%^&*()_-=+12 34 567 890 MBar dzo ciekawy text !@ #$%^ &*()_-=+123 45 678 90 MBar dzo ci ekawy text !@ #$%^&*()_-=+12 3456 7890 MBard zo c iekawy text !@ #$%^ &*()_-=+123 890 M a o cie kawy text ! %^ &*( )_- =+1 2345 678 90MB ardzo cie kawy text !@#$ %^&*()_-=+12 3456 7890 MBar dzo cie kawy text !@#$ %^& *()_-=+12 3456 7890 MBard zoci ekaw yte xt!@ #$%^ &*()_-=+1 23456 78 90"),
  379. glyph_atlas_TODO_(presentation::text::FromString(assets_manager.LoadString("FONT_DISTANCE_FIELD.json"))),
  380. test_texture_(new presentation::opengl::Texture(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("FONT_DISTANCE_FIELD.png")))),
  381. time_()
  382. {
  383. }
  384.  
  385. void Setup(float screen_x_to_y_ratio) {
  386. // layout_ = MenuLayout(presentation::Box(presentation::Box::position_type(-1.0f, -1.0f), presentation::Box::position_type(1.0f, 1.0f))); //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: react to screen ratio
  387. layout_ = MenuLayout(presentation::Box(-1.0f, -1.0f, screen_x_to_y_ratio, 1.0f)); //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: react to screen ratio
  388. }
  389.  
  390. void Draw(MenuModel const &menu, glm::mat4 const &mvp) {
  391. float const Z = 0.0f;
  392. float const margin = 0.05f; //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: OF WHOLE SIZE %
  393. // float const text_height = 0.08f;
  394. for (size_t i = 0; i < layout_.grid_.size(); ++i) {
  395. std::vector<presentation::Box> split = SplitNinePatchWithMargin(layout_.grid_[i], margin);
  396. glm::vec4 color = (menu.GetSelected() == i) ? glm::vec4(1.0f, 1.0f, 1.0f, 1.0f) : glm::vec4(1.0f, 0.0f, 1.0f, 1.0f);
  397. for (size_t x = 0; x < 3; ++x) {
  398. for (size_t y = 0; y < 3; ++y) {
  399. common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(split[3 * y + x], Z));
  400. float const EVEN_NINE_PATCH = 1.0f / 3.0f;
  401. float const texture_x_min = EVEN_NINE_PATCH * x;
  402. float const texture_x_max = EVEN_NINE_PATCH * (x + 1);
  403. float const texture_y_min = EVEN_NINE_PATCH * y;
  404. float const texture_y_max = EVEN_NINE_PATCH * (y + 1);
  405. common::Quad<glm::vec2> quad_texture(glm::vec2(texture_x_min, texture_y_min), glm::vec2(texture_x_max, texture_y_min), glm::vec2(texture_x_max, texture_y_max), glm::vec2(texture_x_min, texture_y_max));
  406.  
  407. common::Quad<glm::vec4> quad_color(color, color, color, color);
  408.  
  409. positions_.Add(quad_position);
  410. texture_uvs_.Add(quad_texture);
  411. colors_.Add(quad_color);
  412. }
  413. }
  414.  
  415. //text plate
  416. {
  417. size_t const CENTER_RECTANGLE_INDEX = 4;
  418. presentation::Box const &center_rectangle = split[CENTER_RECTANGLE_INDEX];
  419. // common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(center_rectangle, Z));
  420. // common::Quad<glm::vec2> quad_texture(glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f));
  421. // glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
  422. // common::Quad<glm::vec4> quad_color(color, color, color, color);
  423. // positions_.Add(quad_position);
  424. // texture_uvs_.Add(quad_texture);
  425. // colors_.Add(quad_color);
  426.  
  427. // char buffer[128];
  428. // snprintf(buffer, 128, menu);
  429. // std::string str(buffer);
  430. text_renderer_.Add(center_rectangle, menu.texts_[i]);
  431. }
  432. }
  433. text_renderer_.Flush();
  434.  
  435. count_ = positions_.GetNumberOfVertices();
  436. positions_.FlushBuffer();
  437. texture_uvs_.FlushBuffer();
  438. colors_.FlushBuffer();
  439.  
  440. // sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp, frame_texture_->Get(), count_);
  441. //
  442. // text_renderer_.Draw(glm::mat4());
  443.  
  444. {
  445. glm::vec4 color(1.0f);
  446. common::Quad<glm::vec3> quad_position(glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec3(1.0f, -1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 0.0f), glm::vec3(-1.0f, 1.0f, 0.0f));
  447. common::Quad<glm::vec2> quad_texture(glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f));
  448. common::Quad<glm::vec4> quad_color(color, color, color, color);
  449.  
  450. positions_.Add(quad_position);
  451. texture_uvs_.Add(quad_texture);
  452. colors_.Add(quad_color);
  453.  
  454. count_ = positions_.GetNumberOfVertices();
  455. positions_.FlushBuffer();
  456. texture_uvs_.FlushBuffer();
  457. colors_.FlushBuffer();
  458.  
  459. // sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp, test_texture_->Get(), count_);
  460. }
  461.  
  462. auto LAMBDA = [&](presentation::Box const &box, float Z) {//TODO(piotr.daszkiewicz@gmail.com)[Sep 16, 2015]: RENDER SOME TEXT USING GLYPH INFO
  463. // glm::vec2 const scale(box.Height() / (glyph_atlas_TODO_.font_info_.distance_between_baselines_));
  464. // glm::vec2 pen_position = box.left_bottom_ + (scale * glyph_atlas_TODO_.font_info_.pen_position_vertical_offset_);
  465. // for (char const &c : test_text_) {
  466. // presentation::text::GlyphInformation const &info = glyph_atlas_TODO_.glyphs_info_[c];
  467. //
  468. // glm::vec4 color(1.0f);
  469. //
  470. // common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(presentation::Move(presentation::ScaleWithOrigin(info.glyph_frame_, scale, glm::vec2()), pen_position), Z));
  471. // common::Quad<glm::vec2> quad_texture(presentation::utils::BoxToQuad2D(info.uv_));
  472. // common::Quad<glm::vec4> quad_color(color, color, color, color);
  473. //
  474. // pen_position.x += info.advance_x_ * scale.x;
  475. //
  476. // positions_.Add(quad_position);
  477. // texture_uvs_.Add(quad_texture);
  478. // colors_.Add(quad_color);
  479. // }
  480.  
  481.  
  482. presentation::EXPERIMENTAL::Text::char_positions_function_type character_position = [&](size_t position_in_string, glm::vec2 const &pen_position, glm::vec2 const &scale) {
  483. glm::vec2 my_scale(scale * glm::vec2(box.Width(), box.Height()));
  484.  
  485. presentation::text::GlyphInformation const &info = glyph_atlas_TODO_.glyphs_info_[test_text_[position_in_string]];
  486.  
  487. glm::vec4 color(1.0f);
  488.  
  489. common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(presentation::Move(presentation::ScaleWithOrigin(info.glyph_frame_, glm::vec2(my_scale), glm::vec2()),
  490. presentation::UnitCoordinatesToRectangle(box, pen_position)), Z));
  491. common::Quad<glm::vec2> quad_texture(presentation::utils::BoxToQuad2D(info.uv_));
  492. common::Quad<glm::vec4> quad_color(color, color, color, color);
  493.  
  494. positions_.Add(quad_position);
  495. texture_uvs_.Add(quad_texture);
  496. colors_.Add(quad_color);
  497. };
  498.  
  499. presentation::EXPERIMENTAL::Text::TextLayout(glyph_atlas_TODO_, test_text_, box.Diagonal().x / box.Diagonal().y, character_position);
  500.  
  501. count_ = positions_.GetNumberOfVertices();
  502. positions_.FlushBuffer();
  503. texture_uvs_.FlushBuffer();
  504. colors_.FlushBuffer();
  505.  
  506. time_ += 1.0f;
  507.  
  508. float scale = std::sin(time_ * 0.01f) * 0.05f + 0.06f;
  509.  
  510. presentation::Box my_box = presentation::ScaleKeepingCenter(box, glm::vec2(scale));
  511. glm::mat4 TODO_SCALE = glm::ortho(my_box.left_bottom_.x, my_box.right_top_.x, my_box.left_bottom_.y, my_box.right_top_.y, 1.0f, -1.0f);
  512. // glm::mat4 TODO_SCALE = glm::ortho(box.left_bottom_.x, box.right_top_.x, box.left_bottom_.y, box.right_top_.y, 1.0f, -1.0f);
  513.  
  514. // TODO_SCALE = glm::translate(TODO_SCALE, glm::vec3(-1.0f, 0.0f, 0.0f));
  515. // TODO_SCALE = glm::scale(TODO_SCALE, glm::vec3(2.0f / test_text_.size()));
  516. // TODO_SCALE = glm::scale(TODO_SCALE, glm::vec3(0.1f));
  517.  
  518. //sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), TODO_SCALE, test_texture_->Get(), count_);
  519.  
  520. float threshold = (std::sin(time_ * 0.01f) + 1.0f) / 2.0f;
  521. threshold = 0.5f;
  522. float width = std::sin(time_ * 0.05f) + 1.0f;
  523. width = 0.05f;
  524. distance_field_program_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), TODO_SCALE, threshold, width, test_texture_->Get(), count_);
  525. };
  526. //LAMBDA(presentation::Box(presentation::Box::position_type(-1.0f, -1.0f), presentation::Box::position_type(1.0f, 1.0f)), 0.0f);
  527. LAMBDA(layout_.whole_, 0.0f);
  528. //LAMBDA(presentation::Box(presentation::Box::position_type(-1.0f, -0.1f), presentation::Box::position_type(1.0f, 0.1f)), 0.0f);
  529. }
  530.  
  531. void Draw1000(TestController const &test, glm::mat4 const &mvp) {
  532. for (size_t i = 0; i < test.weakly_assigned_.size(); ++i) {
  533. glm::vec4 color(1.0f);
  534. float UNIT = 1.0f;
  535.  
  536. if (test.weakly_assigned_[i]) {
  537. color = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
  538. UNIT = static_cast<float>(test.weakly_assigned_[i]->ticks_) / PlayerStatus::JOIN_THRESHOLD;
  539. }
  540. float const length = 2.0f / test.weakly_assigned_.size();
  541.  
  542.  
  543. presentation::Box box(presentation::Box::position_type(-1.0f + i * length, -1.0f), presentation::Box::position_type(-1.0f + (i + 1) * length, -1.0f + 2.0f * UNIT));
  544.  
  545. common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(box, 0.0f));
  546. common::Quad<glm::vec2> quad_texture(glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f));
  547. common::Quad<glm::vec4> quad_color(color, color, color, color);
  548.  
  549. positions_.Add(quad_position);
  550. texture_uvs_.Add(quad_texture);
  551. colors_.Add(quad_color);
  552.  
  553. count_ = positions_.GetNumberOfVertices();
  554. positions_.FlushBuffer();
  555. texture_uvs_.FlushBuffer();
  556. colors_.FlushBuffer();
  557.  
  558. sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp, frame_texture_->Get(), count_);
  559. }
  560. }
  561.  
  562. glm::vec4 active_color_;
  563. glm::vec4 passive_color_;
  564. MenuLayout layout_;
  565.  
  566. presentation::opengl::programs::Sprite sprite_;
  567. presentation::opengl::programs::DistanceField distance_field_program_;
  568. TextRenderer text_renderer_;
  569.  
  570. presentation::opengl::utils::BatchBuffer<glm::vec3> positions_;
  571. presentation::opengl::utils::BatchBuffer<glm::vec2> texture_uvs_;
  572. presentation::opengl::utils::BatchBuffer<glm::vec4> colors_;
  573. size_t count_;
  574.  
  575. std::unique_ptr<presentation::opengl::Texture> frame_texture_;
  576.  
  577. std::string test_text_;
  578.  
  579. presentation::text::GlyphAtlas glyph_atlas_TODO_;
  580. std::unique_ptr<presentation::opengl::Texture> test_texture_;
  581. float time_;
  582. };
  583.  
  584. class MenuView: public not_sure::ViewInterface {
  585. public:
  586. MenuView(common::AssetsManagerInterface &assets_manager, std::unique_ptr<logger::LoggerInterface> logger) :
  587. fps_counter_(120),
  588. logger_(std::move(logger)),
  589.  
  590. model_matrix_(),
  591. view_matrix_(),
  592. projection_matrix_(),
  593. MVP_matrix_(),
  594. viewport_(),
  595. offscreen_viewport_(),
  596. screen_x_to_y_ratio_(1.0f),
  597. frame_buffer_(),
  598. after_effects_mesh_(BasicMesh()),
  599. after_effects_chromatic_aberration_(assets_manager),
  600.  
  601. menu_model_(),
  602. menu_controller_(),
  603. menu_drawer_(assets_manager),
  604. test_controller_() {
  605. presentation::opengl::CullFaceEnable();
  606. }
  607.  
  608. void SetCamera() {
  609. // camera_logic_.UpdateCamera(model_, projection_matrix_, view_matrix_);
  610. MVP_matrix_ = projection_matrix_ * view_matrix_ * model_matrix_;
  611. //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: configure layout
  612. }
  613.  
  614. void OnSurfaceChanged(int width, int height) {
  615. viewport_ = glm::ivec4(0, 0, width, height);
  616. float const offscreen_factor = 1.0f;
  617. offscreen_viewport_ = glm::ivec4(0, 0, width * offscreen_factor, height * offscreen_factor);
  618.  
  619. screen_x_to_y_ratio_ = static_cast<float>(width) / height;
  620.  
  621. // camera_logic_.SetScreenRatio(screen_x_to_y_ratio_);
  622.  
  623. menu_drawer_.Setup(screen_x_to_y_ratio_);
  624.  
  625. SetCamera(); // WARNING! Set camera matrixes ASAP because mouse/touch clicks depend on it and may be called before draw.
  626.  
  627. // logger::LoggerInterface::rich_stream_type log_stream;
  628. // log_stream << "Camera" << "eye: " << camera_params_.eye_.x << " " << camera_params_.eye_.y << " " << camera_params_.eye_.z << " look: " << camera_params_.look_.x << " " << camera_params_.look_.y << " " << camera_params_.look_.z;
  629. // logger_->Log(log_stream);
  630. //
  631. // projection_matrix_ = glm::frustum(camera_frustum_.left_, camera_frustum_.right_, camera_frustum_.bottom_, camera_frustum_.top_, camera_frustum_.near_, camera_frustum_.far_);
  632. //
  633. frame_buffer_.reset(new presentation::opengl::FrameBuffer(offscreen_viewport_.z, offscreen_viewport_.w));
  634. }
  635.  
  636. void OnDrawFrame(long long nano_time) {
  637. fps_counter_.Frame(nano_time);
  638. SetCamera();
  639.  
  640. frame_buffer_->Bind();
  641. presentation::opengl::Viewport(offscreen_viewport_.x, offscreen_viewport_.y, offscreen_viewport_.z, offscreen_viewport_.w);
  642. presentation::opengl::FrameBuffer::Clear();
  643.  
  644. // presentation::opengl::DepthTestEnable();
  645. // presentation::opengl::BlendDisable();
  646. //
  647. // presentation::opengl::BlendEnable();
  648. // presentation::opengl::BlendFunc(presentation::opengl::BlendType::LIGHT);
  649. //
  650. // presentation::opengl::DepthWriteDisable();
  651. // presentation::opengl::DepthTestEnable();
  652.  
  653. presentation::opengl::DepthTestDisable();
  654.  
  655. // presentation::opengl::DepthWriteEnable();
  656. presentation::opengl::BlendFunc(presentation::opengl::BlendType::TRANSPARENT);
  657. presentation::opengl::BlendDisable();
  658.  
  659. frame_buffer_->Unbind();
  660. presentation::opengl::FrameBuffer::Clear();
  661. presentation::opengl::Viewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w);
  662.  
  663. {
  664. // float const angle = static_cast<float>(nano_time % 5000000000) / 5000000000.0f;
  665. // after_effects_renderer_.Render(after_effects_mesh_->GetPositions(), after_effects_mesh_->GetTextureUVs(), frame_buffer_->GetTexture(), angle, after_effects_mesh_->GetCount());
  666.  
  667. after_effects_chromatic_aberration_.Render(after_effects_mesh_->GetPositions(), after_effects_mesh_->GetTextureUVs(), frame_buffer_->GetTexture(), glm::vec2(0.0f, 0.0f), after_effects_mesh_->GetCount());
  668. // after_effects_chromatic_aberration_.Render(after_effects_mesh_->GetPositions(), after_effects_mesh_->GetTextureUVs(), frame_buffer_->GetTexture(), glm::vec2(0.0025f, 0.0f), after_effects_mesh_->GetCount());
  669. }
  670.  
  671. presentation::opengl::DepthTestDisable();
  672. presentation::opengl::BlendEnable();
  673.  
  674. // menu_drawer_.Draw(menu_model_, MVP_matrix_);
  675.  
  676. menu_drawer_.Draw1000(test_controller_, MVP_matrix_);
  677.  
  678.  
  679. menu_controller_.Control(menu_model_);
  680. // pad_controller_.Control(model_);
  681.  
  682. // model_.Update();
  683. }
  684.  
  685. void OnKeyEvent(input::key_code_type key_code, input::KeyEventType event_type) {
  686. {
  687. logger::LoggerInterface::rich_stream_type log_stream;
  688. log_stream << "Jol key" << key_code << " type " << (int)event_type;
  689. logger_->Log(log_stream);
  690. }
  691. }
  692.  
  693. void OnTouchEvent(float x, float y, input::TouchEventType event_type) {
  694. }
  695.  
  696. void OnAxisMotion(device_id device, axis_id axis, float value) override {
  697. test_controller_.OnAxisMotion(device, axis, value);
  698. }
  699.  
  700. void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
  701. test_controller_.OnHatMotion(device, hat, direction);
  702. }
  703.  
  704. void OnJoyButton(device_id device, button_id button, bool up) override {
  705. test_controller_.OnJoyButton(device, button, up);
  706. }
  707.  
  708. private:
  709. common::FpsCounter fps_counter_;
  710.  
  711. std::unique_ptr<logger::LoggerInterface> logger_;
  712.  
  713. glm::mat4 model_matrix_;
  714. glm::mat4 view_matrix_;
  715. glm::mat4 projection_matrix_;
  716.  
  717. glm::mat4 MVP_matrix_;
  718.  
  719. glm::ivec4 viewport_;
  720. glm::ivec4 offscreen_viewport_;
  721. float screen_x_to_y_ratio_;
  722.  
  723. std::unique_ptr<presentation::opengl::FrameBuffer> frame_buffer_;
  724.  
  725. std::unique_ptr<presentation::opengl::TrianglesMesh> after_effects_mesh_;
  726. presentation::opengl::programs::ChromaticAberration after_effects_chromatic_aberration_;
  727.  
  728. MenuModel menu_model_;
  729. MenuController menu_controller_;
  730.  
  731. DRAWER500 menu_drawer_;
  732. TestController test_controller_;
  733. };
  734.  
  735.  
  736. //////////////////////////////////
  737.  
  738. class GlueView: public sdl::SdlWrapperInterface {
  739. public:
  740. GlueView() :
  741. view_() {
  742. }
  743.  
  744. void OnSurfaceCreated() {
  745. presentation::opengl::LibraryStaticInit();
  746.  
  747. desktop::AssetsManager am;
  748. view_.reset(new MenuView(am, std::unique_ptr<logger::LoggerInterface>(new desktop::Logger())));
  749. }
  750.  
  751. void OnSurfaceChanged(int width, int height) {
  752. view_->OnSurfaceChanged(width, height);
  753. }
  754.  
  755. void OnDrawFrame(long long nano_time) {
  756. view_->OnDrawFrame(nano_time);
  757. }
  758.  
  759. void OnKeyEvent(input::key_code_type key_code, input::KeyEventType event_type) {
  760. view_->OnKeyEvent(key_code, event_type);
  761. }
  762.  
  763. void OnTouchEvent(float x, float y, input::TouchEventType event_type) {
  764. view_->OnTouchEvent(x, y, event_type);
  765. }
  766.  
  767. void OnSurfaceDestroyed() {
  768. view_.reset();
  769. }
  770.  
  771. void OnAxisMotion(device_id device, axis_id axis, float value) {
  772. view_->OnAxisMotion(device, axis, value);
  773. }
  774.  
  775. void OnHatMotion(device_id device, hat_id hat, HatDirection direction) {
  776. view_->OnHatMotion(device, hat, direction);
  777. }
  778.  
  779. void OnJoyButton(device_id device, button_id button, bool up) {
  780. view_->OnJoyButton(device, button, up);
  781. }
  782.  
  783. std::unique_ptr<not_sure::ViewInterface> view_;
  784. };
  785.  
  786.  
  787. ////////////////////////////
  788.  
  789.  
  790.  
  791. int main(int argc, char *argv[]) {
  792. try {
  793. GlueView glue_view;
  794. sdl::main_wrapper(glue_view);
  795. } catch (std::exception const &exception) {
  796. std::cout << exception.what() << std::endl;
  797. return 1;
  798. }
  799.  
  800. return 0;
  801. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement