Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include "desktop/assets_manager.hpp"
- #include "desktop/logger.hpp"
- #include "sdl/sdl_wrapper.hpp"
- #include "common/fps_counter.hpp"
- #include "common/quad.hpp"
- #include "common/make_unique.hpp"
- #include "presentation/text_renderer_interface.hpp"
- #include "presentation/opengl/mesh.hpp"
- #include "presentation/opengl/opengl_linker_utils.hpp"
- #include "presentation/opengl/texture_loader.hpp"
- #include "presentation/opengl/programs/chromatic_aberration.hpp"
- #include "presentation/opengl/programs/distance_field.hpp"
- #include "presentation/opengl/programs/sprite.hpp"
- #include "presentation/opengl/utils/batch_buffer.hpp"
- #include "presentation/text/font.hpp"
- #include "presentation/text/glyph_atlas_serialization.hpp"
- #include "presentation/texture_atlas.hpp"
- #include "presentation/utils/box_to_quad.hpp"
- /////////////
- #include "presentation/box.hpp"
- std::vector<presentation::Box> SplitIntoGrid(presentation::Box const &whole, std::vector<float> mix_x, std::vector<float> mix_y) {
- std::vector<presentation::Box> grid;
- auto y_split = presentation::SplitByNthCoordinate(whole, 1, mix_y);
- for (size_t y = 0; y < mix_y.size(); ++y) {
- auto x_split = presentation::SplitByNthCoordinate(y_split[y], 0, mix_x);
- for (auto const &part : x_split) {
- grid.push_back(part);
- }
- }
- return grid;
- }
- std::vector<presentation::Box> SplitNinePatchWithMargin(presentation::Box const &whole, float margin) {
- std::vector<float> mix_x({ margin, whole.Width() - 2.0f * margin, margin});
- std::vector<float> mix_y({ margin, whole.Height() - 2.0f * margin, margin});
- return SplitIntoGrid(whole, mix_x, mix_y);
- }
- struct MenuLayout {
- MenuLayout() :
- whole_(), margins_(), grid_() {
- }
- explicit MenuLayout(presentation::Box const &whole) :
- whole_(whole),
- margins_(SplitNinePatchWithMargin(whole_, 0.3f)),
- grid_(SplitIntoGrid(margins_[4], std::vector<float>(1, 1.0f), std::vector<float>(4, 1.0f))) {
- }
- presentation::Box whole_;
- std::vector<presentation::Box> margins_;
- std::vector<presentation::Box> grid_;
- };
- ////////////////////////////////
- #include "presentation/text.hpp"
- class TextRenderer {
- public:
- TextRenderer(common::AssetsManagerInterface const &assets_manager) :
- // sprite_(assets_manager), font_texture_(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("ExportedFont.png"))), atlas_(presentation::TextureAtlas::GenerateSquareGrid(16)),
- sprite_(assets_manager), font_texture_(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("FONT_DISTANCE_FIELD.png"))), atlas_(presentation::TextureAtlas::GenerateSquareGrid(16)),
- positions_(256), texture_uvs_(256), colors_(256), count_() {
- }
- void TextureUVs(std::string const &text) {
- //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: generate texture UV for string from atlas
- }
- void Add(presentation::Box box, std::string const &text) {
- glm::vec4 color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
- float const font_x_to_y_ratio = 1.0f;
- 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) {
- 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));
- {
- common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(glyph_box, 0.0f));
- common::Quad<glm::vec2> quad_texture(atlas_.GetSubtexture(text[position_in_string]));
- common::Quad<glm::vec4> quad_color(color, color, color, color);
- positions_.Add(quad_position);
- texture_uvs_.Add(quad_texture);
- colors_.Add(quad_color);
- }
- };
- presentation::Text::TextLayout(text, box.Diagonal().x / box.Diagonal().y, font_x_to_y_ratio, character_position);
- }
- void Flush() {
- count_ = positions_.GetNumberOfVertices();
- positions_.FlushBuffer();
- texture_uvs_.FlushBuffer();
- colors_.FlushBuffer();
- }
- void Draw(glm::mat4 const &mvp_matrix) {
- sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp_matrix, font_texture_.Get(), count_);
- }
- private:
- presentation::opengl::programs::Sprite sprite_;
- presentation::opengl::Texture font_texture_;
- presentation::TextureAtlas atlas_;
- presentation::opengl::utils::BatchBuffer<glm::vec3> positions_;
- presentation::opengl::utils::BatchBuffer<glm::vec2> texture_uvs_;
- presentation::opengl::utils::BatchBuffer<glm::vec4> colors_;
- size_t count_;
- };
- ////////////////////////////
- //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: copy paste
- std::unique_ptr<presentation::opengl::TrianglesMesh> BasicMesh() {
- std::vector<glm::vec3> out_positions(presentation::opengl::utils::QUAD_VERTICES);
- std::vector<glm::vec2> out_texture_uvs(presentation::opengl::utils::QUAD_VERTICES);
- std::vector<glm::vec3> out_normals(presentation::opengl::utils::QUAD_VERTICES);
- {
- 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));
- 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));
- glm::vec3 up(0.0f, 0.0f, 1.0f);
- common::Quad<glm::vec3> quad_normal(up, up, up, up);
- presentation::opengl::utils::BatchBuffer<glm::vec3>::QuadToTrianglesVector(quad_position, out_positions, 0);
- presentation::opengl::utils::BatchBuffer<glm::vec2>::QuadToTrianglesVector(quad_texture, out_texture_uvs, 0);
- presentation::opengl::utils::BatchBuffer<glm::vec3>::QuadToTrianglesVector(quad_normal, out_normals, 0);
- }
- return common::make_unique<presentation::opengl::TrianglesMesh>(
- common::make_unique<presentation::opengl::ArrayBuffer<glm::vec3>>(out_positions.data(), out_positions.size(), false),
- common::make_unique<presentation::opengl::ArrayBuffer<glm::vec2>>(out_texture_uvs.data(), out_positions.size(), false),
- common::make_unique<presentation::opengl::ArrayBuffer<glm::vec3>>(out_normals.data(), out_positions.size(), false)
- );
- }
- /////////////////////
- class MenuModel {
- public:
- MenuModel() :
- selected_(0),
- texts_({
- "New game",
- "Options",
- "Credits with super long text attached to it",
- "Exit"
- }) {
- }
- size_t GetSelected() const {
- return selected_;
- }
- void SetSelected(int selected) {
- selected_ = selected;
- }
- size_t GetSize() const {
- return 4;
- }
- void Down() {
- ++selected_;
- if (selected_ == GetSize()) {
- selected_ = 0;
- }
- }
- void Up() {
- if (selected_ == 0) {
- selected_ = GetSize();
- }
- --selected_;
- }
- void Left() {
- }
- void Right() {
- }
- void Back() {
- //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: go to previous menu
- }
- void Action() {
- //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: do what selected control does
- }
- private:
- size_t selected_;
- public:
- std::vector<std::string> texts_; //TODO(piotr.daszkiewicz@gmail.com)[Sep 15, 2015]: take from outside
- };
- #include <boost/optional.hpp>
- class MenuController: public input::GameControllerInterface {
- public:
- MenuController() :
- saved_direction_() {
- }
- void OnAxisMotion(device_id device, axis_id axis, float value) override {
- }
- void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
- saved_direction_ = direction;
- }
- void OnJoyButton(device_id device, button_id button, bool up) override {
- }
- void Control(MenuModel &menu) {
- if (saved_direction_) {
- if (saved_direction_ == HatDirection::DOWN) {
- menu.Down();
- }
- if (saved_direction_ == HatDirection::UP) {
- menu.Up();
- }
- saved_direction_.reset();
- }
- }
- private:
- boost::optional<HatDirection> saved_direction_;
- };
- ////////////////////////
- //////////////////////////////
- struct PlayerStatus: public input::GameControllerInterface {
- enum class WHAT {
- WEAKLY,
- PERMANENT
- };
- static int const JOIN_THRESHOLD = 60;
- PlayerStatus(input::GameControllerInterface::device_id device) :
- state_(WHAT::WEAKLY), device_(device), button_(), ticks_(0) {
- }
- void OnAxisMotion(device_id device, axis_id axis, float value) override {
- }
- void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
- }
- void OnJoyButton(device_id device, button_id button, bool up) override {
- if (state_ == PlayerStatus::WHAT::WEAKLY) {
- if (up) {
- if (button_ == button) {
- button_.reset();
- }
- } if (not button_) {
- button_ = button;
- }
- }
- }
- void Update() {
- if (button_) {
- ++ticks_;
- if (ticks_ > JOIN_THRESHOLD) {
- if (state_ == WHAT::WEAKLY) {
- state_ = WHAT::PERMANENT;
- }
- }
- } else {
- --ticks_;
- }
- }
- bool Unassigned() const {
- return (state_ == WHAT::WEAKLY) and (ticks_ <= 0);
- }
- WHAT state_;
- input::GameControllerInterface::device_id device_;
- boost::optional<input::GameControllerInterface::button_id> button_;
- int ticks_;
- };
- class TestController: public input::GameControllerInterface {
- public:
- TestController() :
- weakly_assigned_(4) {
- }
- typedef std::vector<boost::optional<PlayerStatus>> container_type;
- container_type weakly_assigned_;
- void OnAxisMotion(device_id device, axis_id axis, float value) override {
- }
- void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
- }
- void OnJoyButton(device_id device, button_id button, bool up) override {
- container_type::iterator assigned_it = FindPlayer(device);
- if (assigned_it != weakly_assigned_.end()) {
- (*assigned_it)->OnJoyButton(device, button, up); ///
- } else {
- if (up) {
- } else {
- container_type::iterator free_it = std::find_if(weakly_assigned_.begin(), weakly_assigned_.end(), [&device](container_type::value_type const &element) {
- return element;
- });
- if (free_it != weakly_assigned_.end()) {
- *free_it = PlayerStatus(device);
- (*free_it)->OnJoyButton(device, button, up); ///
- }
- }
- }
- }
- void Update() {
- for (container_type::iterator it = weakly_assigned_.begin(); it != weakly_assigned_.end(); ++it) {
- if ((*it)->Unassigned()) {
- it->reset();
- }
- }
- }
- container_type::iterator FindPlayer(device_id device) {
- container_type::iterator it = std::find_if(weakly_assigned_.begin(), weakly_assigned_.end(), [&device](container_type::value_type const &element) {
- if (element) {
- return element->device_ == device;
- }
- return false;
- });
- return it;
- }
- };
- //////////////////////////////
- struct DRAWER500 {
- DRAWER500(common::AssetsManagerInterface &assets_manager) :
- active_color_(),
- passive_color_(1.0f, 1.0f, 0.0f, 1.0f),
- layout_(),
- sprite_(assets_manager),
- distance_field_program_(assets_manager),
- text_renderer_(assets_manager),
- positions_(2048),
- texture_uvs_(2048),
- colors_(2048),
- count_(),
- frame_texture_(new presentation::opengl::Texture(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("rocket.png")))),
- //test_text_("MBardzo ciekawy text !@#$%^&*()_-=+1234567890"),
- //test_text_("_MB!^)_ -10"),
- //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"),
- 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"),
- glyph_atlas_TODO_(presentation::text::FromString(assets_manager.LoadString("FONT_DISTANCE_FIELD.json"))),
- test_texture_(new presentation::opengl::Texture(presentation::opengl::TextureLoader::TextureFromPng(assets_manager.LoadBuffer("FONT_DISTANCE_FIELD.png")))),
- time_()
- {
- }
- void Setup(float screen_x_to_y_ratio) {
- // 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
- 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
- }
- void Draw(MenuModel const &menu, glm::mat4 const &mvp) {
- float const Z = 0.0f;
- float const margin = 0.05f; //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: OF WHOLE SIZE %
- // float const text_height = 0.08f;
- for (size_t i = 0; i < layout_.grid_.size(); ++i) {
- std::vector<presentation::Box> split = SplitNinePatchWithMargin(layout_.grid_[i], margin);
- 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);
- for (size_t x = 0; x < 3; ++x) {
- for (size_t y = 0; y < 3; ++y) {
- common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(split[3 * y + x], Z));
- float const EVEN_NINE_PATCH = 1.0f / 3.0f;
- float const texture_x_min = EVEN_NINE_PATCH * x;
- float const texture_x_max = EVEN_NINE_PATCH * (x + 1);
- float const texture_y_min = EVEN_NINE_PATCH * y;
- float const texture_y_max = EVEN_NINE_PATCH * (y + 1);
- 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));
- common::Quad<glm::vec4> quad_color(color, color, color, color);
- positions_.Add(quad_position);
- texture_uvs_.Add(quad_texture);
- colors_.Add(quad_color);
- }
- }
- //text plate
- {
- size_t const CENTER_RECTANGLE_INDEX = 4;
- presentation::Box const ¢er_rectangle = split[CENTER_RECTANGLE_INDEX];
- // common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(center_rectangle, Z));
- // 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));
- // glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
- // common::Quad<glm::vec4> quad_color(color, color, color, color);
- // positions_.Add(quad_position);
- // texture_uvs_.Add(quad_texture);
- // colors_.Add(quad_color);
- // char buffer[128];
- // snprintf(buffer, 128, menu);
- // std::string str(buffer);
- text_renderer_.Add(center_rectangle, menu.texts_[i]);
- }
- }
- text_renderer_.Flush();
- count_ = positions_.GetNumberOfVertices();
- positions_.FlushBuffer();
- texture_uvs_.FlushBuffer();
- colors_.FlushBuffer();
- // sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp, frame_texture_->Get(), count_);
- //
- // text_renderer_.Draw(glm::mat4());
- {
- glm::vec4 color(1.0f);
- 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));
- 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));
- common::Quad<glm::vec4> quad_color(color, color, color, color);
- positions_.Add(quad_position);
- texture_uvs_.Add(quad_texture);
- colors_.Add(quad_color);
- count_ = positions_.GetNumberOfVertices();
- positions_.FlushBuffer();
- texture_uvs_.FlushBuffer();
- colors_.FlushBuffer();
- // sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp, test_texture_->Get(), count_);
- }
- auto LAMBDA = [&](presentation::Box const &box, float Z) {//TODO(piotr.daszkiewicz@gmail.com)[Sep 16, 2015]: RENDER SOME TEXT USING GLYPH INFO
- // glm::vec2 const scale(box.Height() / (glyph_atlas_TODO_.font_info_.distance_between_baselines_));
- // glm::vec2 pen_position = box.left_bottom_ + (scale * glyph_atlas_TODO_.font_info_.pen_position_vertical_offset_);
- // for (char const &c : test_text_) {
- // presentation::text::GlyphInformation const &info = glyph_atlas_TODO_.glyphs_info_[c];
- //
- // glm::vec4 color(1.0f);
- //
- // common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(presentation::Move(presentation::ScaleWithOrigin(info.glyph_frame_, scale, glm::vec2()), pen_position), Z));
- // common::Quad<glm::vec2> quad_texture(presentation::utils::BoxToQuad2D(info.uv_));
- // common::Quad<glm::vec4> quad_color(color, color, color, color);
- //
- // pen_position.x += info.advance_x_ * scale.x;
- //
- // positions_.Add(quad_position);
- // texture_uvs_.Add(quad_texture);
- // colors_.Add(quad_color);
- // }
- presentation::EXPERIMENTAL::Text::char_positions_function_type character_position = [&](size_t position_in_string, glm::vec2 const &pen_position, glm::vec2 const &scale) {
- glm::vec2 my_scale(scale * glm::vec2(box.Width(), box.Height()));
- presentation::text::GlyphInformation const &info = glyph_atlas_TODO_.glyphs_info_[test_text_[position_in_string]];
- glm::vec4 color(1.0f);
- common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(presentation::Move(presentation::ScaleWithOrigin(info.glyph_frame_, glm::vec2(my_scale), glm::vec2()),
- presentation::UnitCoordinatesToRectangle(box, pen_position)), Z));
- common::Quad<glm::vec2> quad_texture(presentation::utils::BoxToQuad2D(info.uv_));
- common::Quad<glm::vec4> quad_color(color, color, color, color);
- positions_.Add(quad_position);
- texture_uvs_.Add(quad_texture);
- colors_.Add(quad_color);
- };
- presentation::EXPERIMENTAL::Text::TextLayout(glyph_atlas_TODO_, test_text_, box.Diagonal().x / box.Diagonal().y, character_position);
- count_ = positions_.GetNumberOfVertices();
- positions_.FlushBuffer();
- texture_uvs_.FlushBuffer();
- colors_.FlushBuffer();
- time_ += 1.0f;
- float scale = std::sin(time_ * 0.01f) * 0.05f + 0.06f;
- presentation::Box my_box = presentation::ScaleKeepingCenter(box, glm::vec2(scale));
- 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);
- // 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);
- // TODO_SCALE = glm::translate(TODO_SCALE, glm::vec3(-1.0f, 0.0f, 0.0f));
- // TODO_SCALE = glm::scale(TODO_SCALE, glm::vec3(2.0f / test_text_.size()));
- // TODO_SCALE = glm::scale(TODO_SCALE, glm::vec3(0.1f));
- //sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), TODO_SCALE, test_texture_->Get(), count_);
- float threshold = (std::sin(time_ * 0.01f) + 1.0f) / 2.0f;
- threshold = 0.5f;
- float width = std::sin(time_ * 0.05f) + 1.0f;
- width = 0.05f;
- distance_field_program_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), TODO_SCALE, threshold, width, test_texture_->Get(), count_);
- };
- //LAMBDA(presentation::Box(presentation::Box::position_type(-1.0f, -1.0f), presentation::Box::position_type(1.0f, 1.0f)), 0.0f);
- LAMBDA(layout_.whole_, 0.0f);
- //LAMBDA(presentation::Box(presentation::Box::position_type(-1.0f, -0.1f), presentation::Box::position_type(1.0f, 0.1f)), 0.0f);
- }
- void Draw1000(TestController const &test, glm::mat4 const &mvp) {
- for (size_t i = 0; i < test.weakly_assigned_.size(); ++i) {
- glm::vec4 color(1.0f);
- float UNIT = 1.0f;
- if (test.weakly_assigned_[i]) {
- color = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
- UNIT = static_cast<float>(test.weakly_assigned_[i]->ticks_) / PlayerStatus::JOIN_THRESHOLD;
- }
- float const length = 2.0f / test.weakly_assigned_.size();
- 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));
- common::Quad<glm::vec3> quad_position(presentation::utils::BoxToQuad3D(box, 0.0f));
- 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));
- common::Quad<glm::vec4> quad_color(color, color, color, color);
- positions_.Add(quad_position);
- texture_uvs_.Add(quad_texture);
- colors_.Add(quad_color);
- count_ = positions_.GetNumberOfVertices();
- positions_.FlushBuffer();
- texture_uvs_.FlushBuffer();
- colors_.FlushBuffer();
- sprite_.Render(positions_.GetArrayBuffer(), texture_uvs_.GetArrayBuffer(), colors_.GetArrayBuffer(), mvp, frame_texture_->Get(), count_);
- }
- }
- glm::vec4 active_color_;
- glm::vec4 passive_color_;
- MenuLayout layout_;
- presentation::opengl::programs::Sprite sprite_;
- presentation::opengl::programs::DistanceField distance_field_program_;
- TextRenderer text_renderer_;
- presentation::opengl::utils::BatchBuffer<glm::vec3> positions_;
- presentation::opengl::utils::BatchBuffer<glm::vec2> texture_uvs_;
- presentation::opengl::utils::BatchBuffer<glm::vec4> colors_;
- size_t count_;
- std::unique_ptr<presentation::opengl::Texture> frame_texture_;
- std::string test_text_;
- presentation::text::GlyphAtlas glyph_atlas_TODO_;
- std::unique_ptr<presentation::opengl::Texture> test_texture_;
- float time_;
- };
- class MenuView: public not_sure::ViewInterface {
- public:
- MenuView(common::AssetsManagerInterface &assets_manager, std::unique_ptr<logger::LoggerInterface> logger) :
- fps_counter_(120),
- logger_(std::move(logger)),
- model_matrix_(),
- view_matrix_(),
- projection_matrix_(),
- MVP_matrix_(),
- viewport_(),
- offscreen_viewport_(),
- screen_x_to_y_ratio_(1.0f),
- frame_buffer_(),
- after_effects_mesh_(BasicMesh()),
- after_effects_chromatic_aberration_(assets_manager),
- menu_model_(),
- menu_controller_(),
- menu_drawer_(assets_manager),
- test_controller_() {
- presentation::opengl::CullFaceEnable();
- }
- void SetCamera() {
- // camera_logic_.UpdateCamera(model_, projection_matrix_, view_matrix_);
- MVP_matrix_ = projection_matrix_ * view_matrix_ * model_matrix_;
- //TODO(piotr.daszkiewicz@gmail.com)[Sep 14, 2015]: configure layout
- }
- void OnSurfaceChanged(int width, int height) {
- viewport_ = glm::ivec4(0, 0, width, height);
- float const offscreen_factor = 1.0f;
- offscreen_viewport_ = glm::ivec4(0, 0, width * offscreen_factor, height * offscreen_factor);
- screen_x_to_y_ratio_ = static_cast<float>(width) / height;
- // camera_logic_.SetScreenRatio(screen_x_to_y_ratio_);
- menu_drawer_.Setup(screen_x_to_y_ratio_);
- SetCamera(); // WARNING! Set camera matrixes ASAP because mouse/touch clicks depend on it and may be called before draw.
- // logger::LoggerInterface::rich_stream_type log_stream;
- // 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;
- // logger_->Log(log_stream);
- //
- // projection_matrix_ = glm::frustum(camera_frustum_.left_, camera_frustum_.right_, camera_frustum_.bottom_, camera_frustum_.top_, camera_frustum_.near_, camera_frustum_.far_);
- //
- frame_buffer_.reset(new presentation::opengl::FrameBuffer(offscreen_viewport_.z, offscreen_viewport_.w));
- }
- void OnDrawFrame(long long nano_time) {
- fps_counter_.Frame(nano_time);
- SetCamera();
- frame_buffer_->Bind();
- presentation::opengl::Viewport(offscreen_viewport_.x, offscreen_viewport_.y, offscreen_viewport_.z, offscreen_viewport_.w);
- presentation::opengl::FrameBuffer::Clear();
- // presentation::opengl::DepthTestEnable();
- // presentation::opengl::BlendDisable();
- //
- // presentation::opengl::BlendEnable();
- // presentation::opengl::BlendFunc(presentation::opengl::BlendType::LIGHT);
- //
- // presentation::opengl::DepthWriteDisable();
- // presentation::opengl::DepthTestEnable();
- presentation::opengl::DepthTestDisable();
- // presentation::opengl::DepthWriteEnable();
- presentation::opengl::BlendFunc(presentation::opengl::BlendType::TRANSPARENT);
- presentation::opengl::BlendDisable();
- frame_buffer_->Unbind();
- presentation::opengl::FrameBuffer::Clear();
- presentation::opengl::Viewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w);
- {
- // float const angle = static_cast<float>(nano_time % 5000000000) / 5000000000.0f;
- // after_effects_renderer_.Render(after_effects_mesh_->GetPositions(), after_effects_mesh_->GetTextureUVs(), frame_buffer_->GetTexture(), angle, after_effects_mesh_->GetCount());
- 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());
- // 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());
- }
- presentation::opengl::DepthTestDisable();
- presentation::opengl::BlendEnable();
- // menu_drawer_.Draw(menu_model_, MVP_matrix_);
- menu_drawer_.Draw1000(test_controller_, MVP_matrix_);
- menu_controller_.Control(menu_model_);
- // pad_controller_.Control(model_);
- // model_.Update();
- }
- void OnKeyEvent(input::key_code_type key_code, input::KeyEventType event_type) {
- {
- logger::LoggerInterface::rich_stream_type log_stream;
- log_stream << "Jol key" << key_code << " type " << (int)event_type;
- logger_->Log(log_stream);
- }
- }
- void OnTouchEvent(float x, float y, input::TouchEventType event_type) {
- }
- void OnAxisMotion(device_id device, axis_id axis, float value) override {
- test_controller_.OnAxisMotion(device, axis, value);
- }
- void OnHatMotion(device_id device, hat_id hat, HatDirection direction) override {
- test_controller_.OnHatMotion(device, hat, direction);
- }
- void OnJoyButton(device_id device, button_id button, bool up) override {
- test_controller_.OnJoyButton(device, button, up);
- }
- private:
- common::FpsCounter fps_counter_;
- std::unique_ptr<logger::LoggerInterface> logger_;
- glm::mat4 model_matrix_;
- glm::mat4 view_matrix_;
- glm::mat4 projection_matrix_;
- glm::mat4 MVP_matrix_;
- glm::ivec4 viewport_;
- glm::ivec4 offscreen_viewport_;
- float screen_x_to_y_ratio_;
- std::unique_ptr<presentation::opengl::FrameBuffer> frame_buffer_;
- std::unique_ptr<presentation::opengl::TrianglesMesh> after_effects_mesh_;
- presentation::opengl::programs::ChromaticAberration after_effects_chromatic_aberration_;
- MenuModel menu_model_;
- MenuController menu_controller_;
- DRAWER500 menu_drawer_;
- TestController test_controller_;
- };
- //////////////////////////////////
- class GlueView: public sdl::SdlWrapperInterface {
- public:
- GlueView() :
- view_() {
- }
- void OnSurfaceCreated() {
- presentation::opengl::LibraryStaticInit();
- desktop::AssetsManager am;
- view_.reset(new MenuView(am, std::unique_ptr<logger::LoggerInterface>(new desktop::Logger())));
- }
- void OnSurfaceChanged(int width, int height) {
- view_->OnSurfaceChanged(width, height);
- }
- void OnDrawFrame(long long nano_time) {
- view_->OnDrawFrame(nano_time);
- }
- void OnKeyEvent(input::key_code_type key_code, input::KeyEventType event_type) {
- view_->OnKeyEvent(key_code, event_type);
- }
- void OnTouchEvent(float x, float y, input::TouchEventType event_type) {
- view_->OnTouchEvent(x, y, event_type);
- }
- void OnSurfaceDestroyed() {
- view_.reset();
- }
- void OnAxisMotion(device_id device, axis_id axis, float value) {
- view_->OnAxisMotion(device, axis, value);
- }
- void OnHatMotion(device_id device, hat_id hat, HatDirection direction) {
- view_->OnHatMotion(device, hat, direction);
- }
- void OnJoyButton(device_id device, button_id button, bool up) {
- view_->OnJoyButton(device, button, up);
- }
- std::unique_ptr<not_sure::ViewInterface> view_;
- };
- ////////////////////////////
- int main(int argc, char *argv[]) {
- try {
- GlueView glue_view;
- sdl::main_wrapper(glue_view);
- } catch (std::exception const &exception) {
- std::cout << exception.what() << std::endl;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement