diff --git a/compile_linux.sh b/compile_linux.sh index 507fc16..b51b97f 100755 --- a/compile_linux.sh +++ b/compile_linux.sh @@ -78,6 +78,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_objects/mesh_instance_2d.cp ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_objects/object_2d.cpp -o sfw/render_objects/object_2d.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_objects/sprite.cpp -o sfw/render_objects/sprite.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_objects/tile_map.cpp -o sfw/render_objects/tile_map.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_objects/text_2d.cpp -o sfw/render_objects/text_2d.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c game_scene.cpp -o game_scene.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c main.cpp -o main.o @@ -106,6 +107,7 @@ ccache g++ -Wall -lm -ldl -lpthread -lX11 -D_REENTRANT -g sfw/core/aabb.o sfw/c sfw/render_objects/object_2d.o \ sfw/render_objects/sprite.o sfw/render_objects/tile_map.o \ sfw/render_objects/camera_2d.o sfw/render_objects/mesh_instance_2d.o \ + sfw/render_objects/text_2d.o \ game_scene.o main.o \ -o game diff --git a/game_scene.cpp b/game_scene.cpp index 7391c4c..9c71f52 100644 --- a/game_scene.cpp +++ b/game_scene.cpp @@ -98,6 +98,8 @@ void GameScene::render() { _font_test_sprite->render(); _font_test_mi->render(); + _text_2d->render(); + //TextRenderer::get_singleton()->font_init(); //TextRenderer::get_singleton()->font_print("test"); } @@ -121,7 +123,7 @@ GameScene::GameScene() { _font_test_sprite = memnew(Sprite); _font_test_mat = memnew(FontMaterial()); - _font_test_mat->texture = _font->get_texture().ptr(); + _font_test_mat->texture = _font->get_texture(); _font_test_sprite->mesh_instance->material = _font_test_mat; _font_test_sprite->width = _font->get_atlas_width(); _font_test_sprite->height = _font->get_atlas_height(); @@ -255,6 +257,13 @@ GameScene::GameScene() { mesh->upload(); */ + + _text_2d = memnew(Text2D); + _text_2d->set_font(_font); + _text_2d->set_text("Test Text2D.\n Newline."); + _text_2d->set_text_color(Color(1, 1, 0)); + _text_2d->update(); + _text_2d->transform.set_origin(Vector2(1200, 250)); } GameScene::~GameScene() { diff --git a/game_scene.h b/game_scene.h index 376cf54..ed7d840 100644 --- a/game_scene.h +++ b/game_scene.h @@ -16,6 +16,7 @@ #include "render_objects/mesh_instance_2d.h" #include "render_objects/mesh_instance_3d.h" #include "render_objects/sprite.h" +#include "render_objects/text_2d.h" #include "render_objects/tile_map.h" class GameScene : public Scene { @@ -55,6 +56,8 @@ public: MeshInstance3D *mi2; ColorMaterial *color_material; + Text2D *_text_2d; + //ColoredMaterial *cmaterial; }; diff --git a/sfw/render_core/font.cpp b/sfw/render_core/font.cpp index fb47086..00b3457 100644 --- a/sfw/render_core/font.cpp +++ b/sfw/render_core/font.cpp @@ -255,18 +255,10 @@ void Font::font_face_from_mem(const void *ttf_data, uint32_t ttf_len, float font //String pngname = "font_debug" + itos(index) + " .png"; //stbi_write_png(pngname.utf8().get_data(), _width, _height, 1, bitmap, 0); - if (!_image.is_valid()) { - _image.instance(); - } - bitmap_data.resize(_width * _height); _image->create(_width, _height, false, Image::FORMAT_L8, bitmap_data); - if (!_texture.is_valid()) { - _texture.instance(); - } - _texture->create_from_image(_image); _texture_offsets.resize(_num_glyphs); @@ -492,6 +484,9 @@ Font::Font() { _descent = 0; _linegap = 0; _linedist = 0; + + _image.instance(); + _texture.instance(); } Font::~Font() { diff --git a/sfw/render_core/font_material.h b/sfw/render_core/font_material.h index 68e3743..52dd1c9 100644 --- a/sfw/render_core/font_material.h +++ b/sfw/render_core/font_material.h @@ -16,7 +16,7 @@ public: set_uniform(projection_matrix_location, RenderState::projection_matrix_2d); set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d); - if (texture) { + if (texture.is_valid()) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); glUniform1i(texture_location, 0); @@ -88,7 +88,6 @@ public: model_view_matrix_location = 0; texture_location = 0; - texture = NULL; } GLint projection_matrix_location; @@ -96,7 +95,7 @@ public: GLint texture_location; - Texture *texture; + Ref texture; }; #endif // TEXT_MATERIAL_H diff --git a/sfw/render_objects/text_2d.cpp b/sfw/render_objects/text_2d.cpp index e04a4f3..2378d20 100644 --- a/sfw/render_objects/text_2d.cpp +++ b/sfw/render_objects/text_2d.cpp @@ -1,37 +1,66 @@ -#include "render_objects/mesh_instance_2d.h" +#include "render_objects/text_2d.h" +#include "render_core/font.h" +#include "render_core/font_material.h" +#include "render_core/mesh.h" #include "render_objects/camera_2d.h" -void MeshInstance2D::render() { - if (!mesh) { +Color Text2D::get_text_color() const { + return _text_color; +} +void Text2D::set_text_color(const Color &p_color) { + _text_color = p_color; +} + +String Text2D::get_text() const { + return _text; +} +void Text2D::set_text(const String &p_text) { + _text = p_text; +} + +Ref Text2D::get_font() const { + return _font; +} +void Text2D::set_font(const Ref &p_font) { + _font = p_font; + + if (_font.is_valid()) { + _material->texture = _font->get_texture(); + } +} + +Vector2 Text2D::get_text_size() { + return _text_size; +} + +void Text2D::update() { + _mesh->clear(); + + if (!_font.is_valid()) { return; } + _text_size = _font->generate_mesh(_text, _mesh, _text_color); + + _mesh->upload(); +} + +void Text2D::render() { Transform2D mat_orig = Camera2D::current_camera->get_model_view_matrix(); Camera2D::current_camera->set_model_view_matrix(mat_orig * transform); - if (material) { - material->bind(); - } - - mesh->render(); - - for (int i = 0; i < children.size(); ++i) { - MeshInstance2D *c = children[i]; - - if (c) { - c->render(); - } - } + _material->bind(); + _mesh->render(); Camera2D::current_camera->set_model_view_matrix(mat_orig); } -MeshInstance2D::MeshInstance2D() { - material = NULL; - mesh = NULL; +Text2D::Text2D() { + _material.instance(); + _mesh.instance(); + _mesh->vertex_dimesions = 2; } -MeshInstance2D::~MeshInstance2D() { - children.clear(); +Text2D::~Text2D() { } diff --git a/sfw/render_objects/text_2d.h b/sfw/render_objects/text_2d.h index 49dcbfa..a693664 100644 --- a/sfw/render_objects/text_2d.h +++ b/sfw/render_objects/text_2d.h @@ -1,26 +1,44 @@ -#ifndef MESH_INSTACE_2D_H -#define MESH_INSTACE_2D_H +#ifndef TEXT_2D_H +#define TEXT_2D_H -#include "core/vector.h" +#include "core/color.h" +#include "core/ustring.h" +#include "core/vector2.h" -#include "render_core/material.h" -#include "render_core/mesh.h" +#include "object/reference.h" -#include "core/transform.h" +#include "object_2d.h" -class MeshInstance2D { +class Font; +class FontMaterial; +class Mesh; + +class Text2D : public Object2D { public: - void render(); + Color get_text_color() const; + void set_text_color(const Color &p_color); - MeshInstance2D(); - ~MeshInstance2D(); + String get_text() const; + void set_text(const String &p_text); - Material *material; - Mesh *mesh; + Ref get_font() const; + void set_font(const Ref &p_font); - Transform2D transform; + Vector2 get_text_size(); - Vector children; + void update(); + void render(); + + Text2D(); + ~Text2D(); + +protected: + Ref _font; + Ref _material; + Ref _mesh; + String _text; + Color _text_color; + Vector2 _text_size; }; #endif // MESH_INSTACE_H