draw bm font

This commit is contained in:
Pinchuk Aleksei
2019-07-16 18:14:24 +03:00
parent 1f0e25db91
commit 30331350bd
15 changed files with 509 additions and 25 deletions

View File

@@ -24,7 +24,7 @@
#include "assets/text_asset.hpp"
#include "assets/texture_asset.hpp"
#include "assets/xml_asset.hpp"
#include "assets/bmfont_asset.hpp"
#include "assets/font_asset.hpp"
#include "components/actor.hpp"
#include "components/camera.hpp"
@@ -34,9 +34,11 @@
#include "components/renderer.hpp"
#include "components/scene.hpp"
#include "components/sprite_renderer.hpp"
#include "components/label.hpp"
#include "systems/flipbook_system.hpp"
#include "systems/render_system.hpp"
#include "systems/label_system.hpp"
#include "address.hpp"
#include "asset.hpp"

View File

@@ -36,7 +36,7 @@ namespace e2d
class text_asset;
class texture_asset;
class xml_asset;
class bmfont_asset;
class font_asset;
class actor;
class camera;
@@ -46,9 +46,11 @@ namespace e2d
class renderer;
class scene;
class sprite_renderer;
class label;
class flipbook_system;
class render_system;
class label_system;
template < typename Asset, typename Content >
class content_asset;
@@ -69,5 +71,4 @@ namespace e2d
class sprite;
class starter;
class world;
class bmfont;
}

View File

@@ -9,11 +9,10 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../bmfont.hpp"
namespace e2d
{
class bmfont_asset final : public content_asset<bmfont_asset, bmfont_ptr> {
class font_asset final : public content_asset<font_asset, font_ptr> {
public:
static const char* type_name() noexcept { return "bmfont_asset"; }
static load_async_result load_async(const library& library, str_view address);

View File

@@ -0,0 +1,85 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include "../_high.hpp"
#include "../factory.hpp"
#include "../assets/font_asset.hpp"
namespace e2d
{
class label final {
public:
label() = default;
label(const str_view text);
label& text(const str_view text) noexcept;
const str& text() const noexcept;
label& dirty(bool dirty) noexcept;
bool dirty() const noexcept;
label& font(const font_asset::ptr& font) noexcept;
const font_asset::ptr& font() const noexcept;
private:
bool dirty_ = false;
str text_;
font_asset::ptr font_;
};
template <>
class factory_loader<label> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
label& component,
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const collect_context& ctx) const;
};
}
namespace e2d
{
inline label::label(const str_view text)
: text_(text),
dirty_(false)
{}
inline label& label::text(const str_view text) noexcept {
text_ = text;
dirty_ = true;
return *this;
}
inline const str& label::text() const noexcept {
return text_;
}
inline label& label::dirty(bool dirty) noexcept {
dirty_ = dirty;
return *this;
}
inline bool label::dirty() const noexcept {
return dirty_;
}
inline label& label::font(const font_asset::ptr& font) noexcept {
font_ = font;
dirty_ = true;
return *this;
}
inline const font_asset::ptr& label::font() const noexcept {
return font_;
}
}

View File

@@ -0,0 +1,22 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include "../_high.hpp"
namespace e2d
{
class label_system final : public ecs::system {
public:
label_system();
~label_system() noexcept final;
void process(ecs::registry& owner) override;
private:
class internal_state;
std::unique_ptr<internal_state> state_;
};
}

View File

@@ -30,3 +30,4 @@
#include "time.hpp"
#include "url.hpp"
#include "xml_utils.hpp"
#include "font.hpp"

View File

@@ -25,6 +25,7 @@ namespace e2d
class input_sequence;
class output_sequence;
class url;
class font;
template < typename T >
class module;

View File

@@ -6,15 +6,15 @@
#pragma once
#include "_high.hpp"
#include "_utils.hpp"
namespace e2d
{
class bmfont;
class font;
using bmfont_ptr = std::shared_ptr<bmfont>;
using font_ptr = std::shared_ptr<font>;
class bmfont final {
class font final {
public:
struct char_data {
u32 id{0};
@@ -39,26 +39,32 @@ namespace e2d
};
struct kerning_data {
u32 first{0};
u64 first{0};
u32 second{0};
i32 amount{0};
};
struct common_data {
u32 line_height{0};
u32 base{0};
u32 pages{0};
u32 atlas_width{0};
u32 atlas_height{0};
};
bmfont() = default;
~bmfont() noexcept = default;
font() = default;
~font() noexcept = default;
static bmfont_ptr create(str_view content);
static font_ptr create(str_view content);
char_data data (u32 charId) const noexcept;
common_data common() const noexcept;
i32 kerning (u32 first, u32 second) const noexcept;
private:
info_data info_;
common_data common_;
vector<str> pages_;
vector<char_data> chars_;
vector<kerning_data> kerning_;
flat_map<u64,i32> kerning_;
};
}

View File

@@ -36,3 +36,5 @@ add_e2d_sample(02)
add_e2d_sample(03)
add_e2d_sample(04)
add_e2d_sample(05)
add_e2d_sample(07)

View File

@@ -0,0 +1,21 @@
{
"passes" : [{
"shader" : "model_shader.json",
"state_block" : {
"capabilities_state" : {
"depth_test" : false,
"blending" : true
},
"blending_state" : {
"src_factor" : "src_alpha",
"dst_factor" : "one_minus_src_alpha"
}
}
}],
"property_block" : {
"samplers" : [{
"name" : "u_texture",
"texture" : "boundsTestFont.png"
}]
}
}

View File

@@ -0,0 +1,127 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include "../common.hpp"
using namespace e2d;
namespace
{
struct rotator {
v3f axis;
};
class game_system final : public ecs::system {
public:
void process(ecs::registry& owner) override {
E2D_UNUSED(owner);
const keyboard& k = the<input>().keyboard();
if ( k.is_key_just_released(keyboard_key::f12) ) {
the<dbgui>().toggle_visible(!the<dbgui>().visible());
}
if ( k.is_key_just_released(keyboard_key::escape) ) {
the<window>().set_should_close(true);
}
if ( k.is_key_pressed(keyboard_key::lsuper) && k.is_key_just_released(keyboard_key::enter) ) {
the<window>().toggle_fullscreen(!the<window>().fullscreen());
}
}
};
class camera_system final : public ecs::system {
public:
void process(ecs::registry& owner) override {
owner.for_joined_components<camera>(
[](const ecs::const_entity&, camera& cam){
if ( !cam.target() ) {
cam.viewport(
the<window>().real_size());
cam.projection(math::make_orthogonal_lh_matrix4(
the<window>().real_size().cast_to<f32>(), 0.f, 1000.f));
}
});
}
};
class game final : public starter::application {
public:
bool initialize() final {
return create_scene()
&& create_camera()
&& create_systems();
}
private:
bool create_scene() {
auto model_res = the<library>().load_asset<model_asset>("gnome_model.json");
auto font_mat = the<library>().load_asset<material_asset>("font_material.json");
auto bmfont_res = the<library>().load_asset<font_asset>("boundsTestFont.fnt");
// auto bmfont_res = the<library>().load_asset<font_asset>("bmfont.fnt");
if ( !model_res || !font_mat || !bmfont_res ) {
return false;
}
auto scene_i = the<world>().instantiate();
scene_i->entity_filler()
.component<scene>()
.component<actor>(node::create(scene_i));
node_iptr scene_r = scene_i->get_component<actor>().get().node();
{
auto label_i = the<world>().instantiate();
label l;
l.font(bmfont_res);
l.text("Enduro2d\nis the best\ngame engine\nin the World!");
label_i->entity_filler()
.component<actor>(node::create(label_i, scene_r))
.component<label>(l)
.component<renderer>(renderer()
.materials({font_mat}))
.component<model_renderer>(model_res);
node_iptr label_n = label_i->get_component<actor>().get().node();
label_n->translation(v3f{-300.f,0.f,0.f});
}
return true;
}
bool create_camera() {
auto camera_i = the<world>().instantiate();
camera_i->entity_filler()
.component<camera>(camera()
.background({1.f, 0.4f, 0.f, 1.f}))
.component<actor>(node::create(camera_i));
return true;
}
bool create_systems() {
ecs::registry_filler(the<world>().registry())
.system<game_system>(world::priority_update)
.system<label_system>(world::priority_update)
.system<camera_system>(world::priority_pre_render);
return true;
}
};
}
int e2d_main(int argc, char *argv[]) {
const auto starter_params = starter::parameters(
engine::parameters("sample_07", "enduro2d")
.timer_params(engine::timer_parameters()
.maximal_framerate(100)));
modules::initialize<starter>(argc, argv, starter_params).start<game>();
modules::shutdown<starter>();
return 0;
}

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/assets/bmfont_asset.hpp>
#include <enduro2d/high/assets/font_asset.hpp>
#include <enduro2d/high/assets/text_asset.hpp>
namespace
@@ -20,17 +20,17 @@ namespace
namespace e2d
{
bmfont_asset::load_async_result bmfont_asset::load_async(
font_asset::load_async_result font_asset::load_async(
const library& library, str_view address)
{
return library.load_asset_async<text_asset>(address)
.then([](const text_asset::load_result& text_data){
return the<deferrer>().do_in_main_thread([text_data](){
auto font = bmfont::create(text_data->content());
auto font = font::create(text_data->content());
if ( !font ) {
throw bmfont_asset_loading_exception();
}
return bmfont_asset::create(font);
return font_asset::create(font);
});
});
}

View File

@@ -0,0 +1,52 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/components/label.hpp>
namespace e2d
{
const char* factory_loader<label>::schema_source = R"json({
"type" : "object",
"required" : [],
"additionalProperties" : false,
"properties" : {
"model" : { "$ref": "#/common_definitions/address" }
}
})json";
bool factory_loader<label>::operator()(
label& component,
const fill_context& ctx) const
{
if ( ctx.root.HasMember("label") ) {
// auto model = ctx.dependencies.find_asset<model_asset>(
// path::combine(ctx.parent_address, ctx.root["model"].GetString()));
// if ( !model ) {
// the<debug>().error("label: Dependency 'model' is not found:\n"
// "--> Parent address: %0\n"
// "--> Dependency address: %1",
// ctx.parent_address,
// ctx.root["model"].GetString());
// return false;
// }
// component.label(model);
}
return true;;
}
bool factory_loader<label>::operator()(
asset_dependencies& dependencies,
const collect_context& ctx) const
{
if ( ctx.root.HasMember("model") ) {
// dependencies.add_dependency<model_asset>(
// path::combine(ctx.parent_address, ctx.root["model"].GetString()));
}
return true;
}
}

View File

@@ -0,0 +1,125 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/systems/label_system.hpp>
#include <enduro2d/high/components/label.hpp>
#include <enduro2d/utils/font.hpp>
#include <enduro2d/high/components/model_renderer.hpp>
namespace e2d
{
//
// label_system::internal_state
//
class label_system::internal_state final : private noncopyable {
public:
internal_state() = default;
~internal_state() noexcept = default;
void process(ecs::registry& owner) {
owner.for_joined_components<label, model_renderer>([](
const ecs::const_entity&,
label& l,
model_renderer& mr)
{
if (l.dirty()) {
vector<v3f> vertices;
vector<u32> indices;
vector<v2f> uvs;
vector<v3f> normals;
f32 xoffset{0};
f32 yoffset{0};
str text = l.text();
const auto& f = l.font()->content();
auto common = f->common();
v2f texture_size;
texture_size.x = common.atlas_width;
texture_size.y = common.atlas_height;
vertices.resize(text.size() * 4);
uvs.resize(vertices.size());
indices.resize(text.size() * 6);
f32 x_pos{0};
f32 y_pos{0};
u32 prev_char{0};
for (size_t i = 0; i < text.size(); i++) {
if ( text[i] == '\n' ) {
y_pos -= f->common().line_height;
x_pos = 0;
prev_char = 0;
continue;
}
auto data = f->data(text[i]);
xoffset = 0;
yoffset = data.yoffset;
if ( prev_char != 0 ) {
xoffset = f->kerning(prev_char, data.id);
}
prev_char = data.id;
size_t start_vertices = i * 4;
vertices[start_vertices ] = v3f(x_pos + xoffset,
y_pos + yoffset, 0);
vertices[start_vertices + 1] = v3f(x_pos + xoffset,
y_pos + data.rect.size.y + yoffset, 0);
vertices[start_vertices + 2] = v3f(x_pos + data.rect.size.x + xoffset,
y_pos + data.rect.size.y + yoffset, 0);
vertices[start_vertices + 3] = v3f(x_pos + data.rect.size.x + xoffset,
y_pos + yoffset, 0);
uvs[start_vertices ] = v2f(data.rect.position.x / texture_size.x,
data.rect.position.y / texture_size.y);
uvs[start_vertices + 1] = v2f(data.rect.position.x / texture_size.x,
(data.rect.position.y + data.rect.size.y) / texture_size.y);
uvs[start_vertices + 2] = v2f((data.rect.position.x + data.rect.size.x) / texture_size.x,
(data.rect.position.y + data.rect.size.y) / texture_size.y);
uvs[start_vertices + 3] = v2f((data.rect.position.x + data.rect.size.x) / texture_size.x,
data.rect.position.y / texture_size.y);
size_t start_indices = i * 6;
indices[start_indices] = start_vertices;
indices[start_indices + 1] = start_vertices + 1;
indices[start_indices + 2] = start_vertices + 2;
indices[start_indices + 3] = start_vertices + 2;
indices[start_indices + 4] = start_vertices + 3;
indices[start_indices + 5] = start_vertices;
x_pos += data.xadvance + xoffset;
}
mesh m;
m.set_vertices(std::move(vertices));
m.set_indices(0, std::move(indices));
m.set_uvs(0, std::move(uvs));
model content;
content.set_mesh(mesh_asset::create(m));
content.regenerate_geometry(the<render>());
mr.model()->fill(content);
l.dirty(false);
const model& mod = mr.model()->content();
(const_cast<model*>(&mod))->regenerate_geometry(the<render>());//TODO: const cast
}
});
}
};
//
// label_system
//
label_system::label_system()
: state_(new internal_state()) {}
label_system::~label_system() noexcept = default;
void label_system::process(ecs::registry& owner) {
state_->process(owner);
}
}

View File

@@ -4,13 +4,17 @@
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/bmfont.hpp>
#include <enduro2d/utils/font.hpp>
#include <sstream>
namespace e2d
{
bmfont_ptr bmfont::create (str_view content) {
bmfont_ptr b = std::make_shared<bmfont>();
u64 makeKerningKey (u32 first, u32 second) noexcept {
return (static_cast<u64>(first) << 32) | static_cast<u64>(second);
}
font_ptr font::create (str_view content) {
font_ptr b = std::make_shared<font>();
str s{content.data()};
std::replace(s.begin(), s.end(), '=', ' ');
std::replace(s.begin(), s.end(), ',', ' ');
@@ -46,9 +50,15 @@ namespace e2d
while (!line_stream.eof()) {
if (tag == "lineHeight") {
line_stream >> b->common_.line_height;
} else if (tag == "base") {
line_stream >> b->common_.base;
} else if (tag == "pages") {
line_stream >> b->common_.pages;
b->pages_.resize( b->common_.pages);
} else if (tag == "scaleW") {
line_stream >> b->common_.atlas_width;
} else if (tag == "scaleH") {
line_stream >> b->common_.atlas_height;
}
line_stream >> tag;
}
@@ -93,15 +103,17 @@ namespace e2d
line_stream >> c.rect.position.x;
} else if (tag == "y") {
line_stream >> c.rect.position.y;
} else if (tag == "widht") {
} else if (tag == "width") {
line_stream >> c.rect.size.x;
} else if (tag == "height") {
line_stream >> c.rect.size.y;
c.rect.position.y -= c.rect.size.y;
c.rect.position.y = b->common_.atlas_height - c.rect.position.y - c.rect.size.y;
} else if (tag == "xoffset") {
line_stream >> c.xoffset;
} else if (tag == "yoffset") {
line_stream >> c.yoffset;
c.yoffset = b->common_.base - c.yoffset - c.rect.size.y;
// c.yoffset = -(b->common_.line_height - c.yoffset);
} else if (tag == "xadvance") {
line_stream >> c.xadvance;
} else if (tag == "page") {
@@ -139,9 +151,37 @@ namespace e2d
}
}
b->chars_.insert(b->chars_.begin(), chars.begin(), chars.begin()+chars_counter);
b->kerning_.insert(b->kerning_.begin(), kerning.begin(), kerning.begin()+kerning_counter);
b->chars_.insert(b->chars_.begin(), chars.begin(), chars.begin()+chars_counter);
b->kerning_.reserve(kerning_counter);
u64 key{0};
for ( int i = 0; i < kerning_counter; i++ ) {
b->kerning_.insert_or_assign(makeKerningKey(kerning[i].first,kerning[i].second),
kerning[i].amount);
}
return b;
}
font::char_data font::data (u32 charId) const noexcept {
for ( auto& c: chars_ ) {
if (c.id == charId) {
return c;
}
}
return font::char_data();
}
font::common_data font::common() const noexcept {
return common_;
}
i32 font::kerning (u32 first, u32 second) const noexcept {
auto it = kerning_.find(makeKerningKey(first,second));
if (it != kerning_.end()) {
return it->second;
}
return 0;
}
}