From 43dd2b51a5060e23e6c07bbd6fa5f6227ed14f1f Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 14 Oct 2018 10:40:07 +0800 Subject: *update --- build/Debug/05Font.exe | Bin 1213440 -> 1213440 bytes build/libjin/libjin.vcxproj | 4 +- build/libjin/libjin.vcxproj.filters | 12 +-- libjin/Graphics/Font/Decoder.cpp | 3 - libjin/Graphics/Font/Decoder.h | 3 - libjin/Graphics/Font/Font.h | 8 +- libjin/Graphics/Font/TTF.cpp | 4 +- libjin/Graphics/Font/TTF.h | 22 +++--- libjin/Graphics/Font/TexFont.cpp | 79 ------------------- libjin/Graphics/Font/TexFont.h | 54 ------------- libjin/Graphics/Font/Text.cpp | 3 - libjin/Graphics/Font/Text.h | 3 - libjin/Graphics/Font/TextureFont.cpp | 65 +++++++++++++++ libjin/Graphics/Font/TextureFont.h | 52 ++++++++++++ test/05Font/main.cpp | 2 +- test/06TextureFont/main.cpp | 149 +++++++++++++++++++++++++++++++++++ 16 files changed, 291 insertions(+), 172 deletions(-) delete mode 100644 libjin/Graphics/Font/TexFont.cpp delete mode 100644 libjin/Graphics/Font/TexFont.h create mode 100644 libjin/Graphics/Font/TextureFont.cpp create mode 100644 libjin/Graphics/Font/TextureFont.h diff --git a/build/Debug/05Font.exe b/build/Debug/05Font.exe index 32d7eaa..25bb6d3 100644 Binary files a/build/Debug/05Font.exe and b/build/Debug/05Font.exe differ diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj index 58b1371..c787847 100644 --- a/build/libjin/libjin.vcxproj +++ b/build/libjin/libjin.vcxproj @@ -37,7 +37,7 @@ - + @@ -94,7 +94,7 @@ - + diff --git a/build/libjin/libjin.vcxproj.filters b/build/libjin/libjin.vcxproj.filters index f194817..3cb01fc 100644 --- a/build/libjin/libjin.vcxproj.filters +++ b/build/libjin/libjin.vcxproj.filters @@ -177,9 +177,6 @@ Graphics - - Graphics\Font - Graphics\Font @@ -192,6 +189,9 @@ Graphics\Font + + Graphics\Font + @@ -388,9 +388,6 @@ Graphics\Font - - Graphics\Font - Graphics\Font @@ -403,6 +400,9 @@ Graphics\Font + + Graphics\Font + diff --git a/libjin/Graphics/Font/Decoder.cpp b/libjin/Graphics/Font/Decoder.cpp index 312e142..b3f2bb2 100644 --- a/libjin/Graphics/Font/Decoder.cpp +++ b/libjin/Graphics/Font/Decoder.cpp @@ -5,8 +5,6 @@ namespace jin { namespace graphics -{ -namespace unicode { /* utf8 byte string to unicode codepoint */ @@ -73,6 +71,5 @@ namespace unicode return nullptr; } -} // unicode } // graphics } // jin \ No newline at end of file diff --git a/libjin/Graphics/Font/Decoder.h b/libjin/Graphics/Font/Decoder.h index dcd2e56..83570e9 100644 --- a/libjin/Graphics/Font/Decoder.h +++ b/libjin/Graphics/Font/Decoder.h @@ -6,8 +6,6 @@ namespace jin { namespace graphics -{ -namespace unicode { typedef unsigned int Codepoint; @@ -51,7 +49,6 @@ namespace unicode const void* next(const void* data) const override; }; -} // unicode } // graphics } // jin diff --git a/libjin/Graphics/Font/Font.h b/libjin/Graphics/Font/Font.h index 1e5f884..424c324 100644 --- a/libjin/Graphics/Font/Font.h +++ b/libjin/Graphics/Font/Font.h @@ -16,12 +16,12 @@ namespace graphics Font() {} virtual ~Font() {}; - virtual Page* typeset(const unicode::Text& text, int lineheight, int spacing = 0) = 0; - virtual Page* typeset(const unicode::Content& text, int lineheight, int spacing = 0) = 0; + virtual Page* typeset(const Text& text, int lineheight, int spacing = 0) = 0; + virtual Page* typeset(const Content& text, int lineheight, int spacing = 0) = 0; virtual void print(const Page* page, int x, int y) = 0; - virtual void print(const unicode::Content& text, int x, int y, int lineheight, int spacing = 0) = 0; - virtual void print(const unicode::Text& text, int x, int y, int lineheight, int spacing = 0) = 0; + virtual void print(const Content& text, int x, int y, int lineheight, int spacing = 0) = 0; + virtual void print(const Text& text, int x, int y, int lineheight, int spacing = 0) = 0; }; diff --git a/libjin/Graphics/Font/TTF.cpp b/libjin/Graphics/Font/TTF.cpp index ea340e4..98e57dd 100644 --- a/libjin/Graphics/Font/TTF.cpp +++ b/libjin/Graphics/Font/TTF.cpp @@ -18,8 +18,6 @@ namespace jin namespace graphics { - using namespace unicode; - /////////////////////////////////////////////////////////////////////////////////////////////// // TTFData /////////////////////////////////////////////////////////////////////////////////////////////// @@ -321,7 +319,7 @@ namespace graphics } } - void TTF::print(const unicode::Text& text, int x, int y, int lineheight, int spacing /* = 0 */) + void TTF::print(const Text& text, int x, int y, int lineheight, int spacing /* = 0 */) { print(*text, x, y, lineheight, spacing); } diff --git a/libjin/Graphics/Font/TTF.h b/libjin/Graphics/Font/TTF.h index 59e8125..4f51138 100644 --- a/libjin/Graphics/Font/TTF.h +++ b/libjin/Graphics/Font/TTF.h @@ -64,11 +64,11 @@ namespace graphics public: static TTF* createTTF(TTFData* ttfData, unsigned int ttfSzie); - Page* typeset(const unicode::Text& text, int lineheight, int spacing = 0) override; - Page* typeset(const unicode::Content& text, int lineheight, int spacing = 0) override; + Page* typeset(const Text& text, int lineheight, int spacing = 0) override; + Page* typeset(const Content& text, int lineheight, int spacing = 0) override; - void print(const unicode::Text& text, int x, int y, int lineheight, int spacing = 0) override; - void print(const unicode::Content& text, int x, int y, int lineheight, int spacing = 0) override; + void print(const Text& text, int x, int y, int lineheight, int spacing = 0) override; + void print(const Content& text, int x, int y, int lineheight, int spacing = 0) override; void print(const Page* page, int x, int y) override; ~TTF(); @@ -92,24 +92,24 @@ namespace graphics static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT]; static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; - TTF(TTFData* ttf, unicode::Codepoint ttfSize); + TTF(TTFData* ttf, Codepoint ttfSize); void estimateSize(); GLuint createAtlas(); - TTFGlyph& bakeGlyph(unicode::Codepoint character); - TTFGlyph& findGlyph(unicode::Codepoint character); + TTFGlyph& bakeGlyph(Codepoint character); + TTFGlyph& findGlyph(Codepoint character); int getCharWidth(int c); int getCharHeight(int c); - int getTextWidth(const unicode::Content& text, int spacing = 0); - int getTextHeight(const unicode::Content& text, int lineheight); - void getTextBox(const unicode::Content& text, int* w, int* h, int lineheight, int spacing = 0); + int getTextWidth(const Content& text, int spacing = 0); + int getTextHeight(const Content& text, int lineheight); + void getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing = 0); int textureWidth; int textureHeight; std::vector atlases; - std::map glyphs; + std::map glyphs; TTFData* ttf; const unsigned int ttfsize; int baseline; diff --git a/libjin/Graphics/Font/TexFont.cpp b/libjin/Graphics/Font/TexFont.cpp deleted file mode 100644 index 990a1af..0000000 --- a/libjin/Graphics/Font/TexFont.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "TexFont.h" - -namespace jin -{ -namespace graphics -{ - - using namespace unicode; - - /* - * +--------------------+ - * | top | - * | l ************* r | - * | e ************* i | - * | f ****glyphs*** g | - * | t ************* h | - * | ************* t | - * | bottom | - * +--------------------+ - */ - - /* create texture font from tilemap */ - TexFont * TexFont::createTexFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh) - { - TexFont* tf = new TexFont(bitmap, codepoints, cellw, cellh); - return tf; - } - - /* create texture font from seperated glyphs */ - TexFont* TexFont::createTexFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh) - { - TexFont* tf = new TexFont(bitmap, codepoints, mask, cellh); - return tf; - } - - TexFont::~TexFont() - { - } - - Page* TexFont::typeset(const Content& text, int lineheight, int spacing) - { - return nullptr; - } - - Page* TexFont::typeset(const Text& text, int lineheight, int spacing) - { - return nullptr; - } - - void TexFont::print(const Page* page, int x, int y) - { - - } - - void TexFont::print(const Content& text, int x, int y, int linehgiht, int spacing) - { - - } - - void TexFont::print(const Text& text, int x, int y, int lineheight, int spacing) - { - - } - - TexFont::TexFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh) - : Drawable(bitmap) - { - int l, r, t, b; - - } - - TexFont::TexFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh) - : Drawable(bitmap) - { - - } - -} -} \ No newline at end of file diff --git a/libjin/Graphics/Font/TexFont.h b/libjin/Graphics/Font/TexFont.h deleted file mode 100644 index c1b2d18..0000000 --- a/libjin/Graphics/Font/TexFont.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef __LIBJIN_TEXTURE_FONT_H -#define __LIBJIN_TEXTURE_FONT_H - -#include -#include - -#include "../../Math/Vector4.hpp" -#include "../Drawable.h" -#include "../Bitmap.h" - -#include "Page.h" -#include "Font.h" -#include "Text.h" - -namespace jin -{ -namespace graphics -{ - - /* Texture font */ - class TexFont : public Font - , public Drawable - { - public: - /* create texture font from tilemap */ - static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Content& codepoints, int cellw, int cellh); - static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Text& text, int cellw, int cellh); - /* create texture font from seperated glyphs */ - static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Content& codepoints, Color mask, int cellh); - static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Text& text, Color mask, int cellh); - - ~TexFont(); - - Page* typeset(const unicode::Text& text, int lineheight, int spacing = 0) override; - Page* typeset(const unicode::Content& text, int lineheight, int spacing = 0) override ; - - void print(const Page* page, int x, int y) override; - void print(const unicode::Content& text, int x, int y, int linehgiht, int spacing = 0) override; - void print(const unicode::Text& text, int x, int y, int lineheight, int spacing = 0)override; - - private: - struct TexGlyph { unsigned short x, y, w, h;}; - - TexFont(const Bitmap* bitmap, const unicode::Content& codepoints, int cellw, int cellh); - TexFont(const Bitmap* bitmap, const unicode::Content& codepoints, Color mask, int cellh); - - std::map glyphs; - - }; - -} -} - -#endif \ No newline at end of file diff --git a/libjin/Graphics/Font/Text.cpp b/libjin/Graphics/Font/Text.cpp index 3ab7f07..b3ba473 100644 --- a/libjin/Graphics/Font/Text.cpp +++ b/libjin/Graphics/Font/Text.cpp @@ -3,8 +3,6 @@ namespace jin { namespace graphics -{ -namespace unicode { ///////////////////////////////////////////////////////////////////////////// @@ -133,6 +131,5 @@ namespace unicode return content; } -} // unicode } // graphics } // jin \ No newline at end of file diff --git a/libjin/Graphics/Font/Text.h b/libjin/Graphics/Font/Text.h index c2cc64b..d092d41 100644 --- a/libjin/Graphics/Font/Text.h +++ b/libjin/Graphics/Font/Text.h @@ -6,8 +6,6 @@ namespace jin { namespace graphics -{ -namespace unicode { class Text; @@ -57,7 +55,6 @@ namespace unicode }; -} // unicode } // graphics } // jin diff --git a/libjin/Graphics/Font/TextureFont.cpp b/libjin/Graphics/Font/TextureFont.cpp new file mode 100644 index 0000000..89600c7 --- /dev/null +++ b/libjin/Graphics/Font/TextureFont.cpp @@ -0,0 +1,65 @@ +#include "TextureFont.h" + +namespace jin +{ +namespace graphics +{ + + /* create texture font from tilemap */ + TextureFont * TextureFont::createTextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh) + { + TextureFont* tf = new TextureFont(bitmap, codepoints, cellw, cellh); + return tf; + } + + /* create texture font from seperated glyphs */ + TextureFont* TextureFont::createTextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh) + { + TextureFont* tf = new TextureFont(bitmap, codepoints, mask, cellh); + return tf; + } + + TextureFont::~TextureFont() + { + } + + Page* TextureFont::typeset(const Content& text, int lineheight, int spacing) + { + + return nullptr; + } + + Page* TextureFont::typeset(const Text& text, int lineheight, int spacing) + { + return nullptr; + } + + void TextureFont::print(const Page* page, int x, int y) + { + + } + + void TextureFont::print(const Content& text, int x, int y, int linehgiht, int spacing) + { + + } + + void TextureFont::print(const Text& text, int x, int y, int lineheight, int spacing) + { + + } + + TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh) + : Drawable(bitmap) + { + int l, r, t, b; + + } + + TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh) + : Drawable(bitmap) + { + } + +} +} \ No newline at end of file diff --git a/libjin/Graphics/Font/TextureFont.h b/libjin/Graphics/Font/TextureFont.h new file mode 100644 index 0000000..243c9d5 --- /dev/null +++ b/libjin/Graphics/Font/TextureFont.h @@ -0,0 +1,52 @@ +#ifndef __LIBJIN_TEXTURE_FONT_H +#define __LIBJIN_TEXTURE_FONT_H + +#include +#include + +#include "../../Math/Vector4.hpp" +#include "../Drawable.h" +#include "../Bitmap.h" + +#include "Page.h" +#include "Font.h" +#include "Text.h" + +namespace jin +{ +namespace graphics +{ + + /* Texture font */ + class TextureFont : public Font + , public Drawable + { + public: + static TextureFont* createTextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh); + static TextureFont* createTextureFont(const Bitmap* bitmap, const Text& text, int cellw, int cellh); + static TextureFont* createTextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh); + static TextureFont* createTextureFont(const Bitmap* bitmap, const Text& text, Color mask, int cellh); + + ~TextureFont(); + + Page* typeset(const Text& text, int lineheight, int spacing = 0) override; + Page* typeset(const Content& text, int lineheight, int spacing = 0) override ; + + void print(const Page* page, int x, int y) override; + void print(const Content& text, int x, int y, int linehgiht, int spacing = 0) override; + void print(const Text& text, int x, int y, int lineheight, int spacing = 0)override; + + private: + struct TextureGlyph { unsigned short x, y, w, h;}; + + TextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh); + TextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh); + + std::map glyphs; + + }; + +} +} + +#endif \ No newline at end of file diff --git a/test/05Font/main.cpp b/test/05Font/main.cpp index 6eebe04..9d44f2f 100644 --- a/test/05Font/main.cpp +++ b/test/05Font/main.cpp @@ -68,7 +68,7 @@ Color frag(Color col, Texture tex, Vertex v) font = TTF::createTTF(data, 15); const char* str = u8R"(源氏物语げんじものがたり)"; int l = strlen(str); - page = font->typeset(unicode::Text(unicode::Encode::UTF8, str, strlen(str)), 15, 0); + page = font->typeset(Text(Encode::UTF8, str, strlen(str)), 15, 0); delete data; //canvas = Canvas::createCanvas(100, 100); //page = font->typeset("こんにちは世界!", 120, 20); diff --git a/test/06TextureFont/main.cpp b/test/06TextureFont/main.cpp index b28b04f..9551604 100644 --- a/test/06TextureFont/main.cpp +++ b/test/06TextureFont/main.cpp @@ -1,3 +1,152 @@ +#include +#include "jin.h" +using namespace jin::core; +using namespace jin::graphics; +using namespace jin::input; +using namespace jin::audio; +using namespace jin::filesystem; +Font* font = nullptr; +Canvas* canvas; +TTFData* data = nullptr; +Shader* shader = nullptr; +Shader* shader2 = nullptr; +Page* page = nullptr; +Texture* tex = nullptr; +float dt; +void onLoad() +{ + const char* font_shader = R"( +#VERTEX_SHADER +Vertex vert(Vertex v) +{ + return v; +} + +#END_VERTEX_SHADER + +#FRAGMENT_SHADER + +Color frag(Color col, Texture tex, Vertex v) +{ + return vec4(col.rgb, texel(tex, v.uv).a); +} + +#END_FRAGMENT_SHADER + )"; + const char* canvas_shader = R"( +#VERTEX_SHADER + +Vertex vert(Vertex v) +{ + return v; +} + +#END_VERTEX_SHADER + +#FRAGMENT_SHADER + +Color frag(Color col, Texture tex, Vertex v) +{ + if(v.uv.x <= 0.002f || v.uv.x >= 0.998f || v.uv.y <= 0.005f || v.uv.y >= 0.995f) + return vec4(1, 1, 1, 1); + else + return texel(tex, v.uv); +} + +#END_FRAGMENT_SHADER +)"; + shader = Shader::createShader(font_shader); + shader2 = Shader::createShader(canvas_shader); + Filesystem* fs = Filesystem::get(); + fs->mount("../Debug"); + Buffer buffer; + fs->read("font.ttf", &buffer); + data = TTFData::createTTFData((const unsigned char*)buffer.data, buffer.size); + font = TTF::createTTF(data, 15); + const char* str = u8R"(Դ爐󤸤Τ)"; + int l = strlen(str); + Text text = Text(Encode::UTF8, str, strlen(str)); + page = font->typeset(text, 15, 0); + delete data; + //canvas = Canvas::createCanvas(100, 100); + //page = font->typeset("ˤ!", 120, 20); + buffer.clear(); + fs->read("img.png", &buffer); + Bitmap* bitmap = Bitmap::createBitmap(buffer.data, buffer.size); + tex = Texture::createTexture(bitmap); + canvas = Canvas::createCanvas(page->size.w, page->size.h); + + Canvas::bind(canvas); + glClear(GL_COLOR_BUFFER_BIT); + glColor4f(1, 1, 1, 1); + if (font != nullptr) + { + //font->print(u8"Hello,\n world!", 10, 10); + //font->print(u8"ڧӧ֧ ާڧ!", 10, 10 + 15 * 1); + shader->use(); + font->print(page, 0, 0); + shader->unuse(); + //font->print(u8"!", 10, 10 + 15*3); + //font->render(page); + } + Canvas::unbind(); +} + +void onEvent(jin::input::Event* e) +{ + static Game* game = Game::get(); + if (e->type == EventType::QUIT) + game->stop(); +} + +void onUpdate(int ms) +{ + dt += ms / (float)1000; +} + +void onDraw() +{ + glClear(GL_COLOR_BUFFER_BIT); + glColor4f(1, 1, 1, 1); + //gl.pushColor(32, 32, 32, 255); + //rect(FILL, 0, 0, 500, 500); + //circle(RenderMode::LINE, 50, 50, 30); + //tex->draw(0, 0, 1, 1, 0); + //tex->draw(20, 50, 1, 1, 0); + //shader->sendFloat("dt", dt); + shader2->use(); + canvas->draw(20 * sin(dt), 10 * cos(dt), 1, 1, 0); + shader2->unuse(); +} + +int main(int argc, char* argv[]) +{ + Game* game = Game::get(); + Game::Setting setting; + setting.loader = onLoad; + setting.eventHandler = onEvent; + setting.updater = onUpdate; + setting.drawer = onDraw; + game->init(&setting); + + Window* wnd = Window::get(); + Window::Setting wndSetting; + wndSetting.width = 600; + wndSetting.height = 512; + wndSetting.title = "Jin v0.1.1"; + wndSetting.fps = 60; + wndSetting.vsync = false; + wndSetting.fullscreen = false; + wndSetting.resizable = false; + wnd->init(&wndSetting); + + game->run(); + + game->quit(); + wnd->quit(); + + return 0; +} \ No newline at end of file -- cgit v1.1-26-g67d0