aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Graphics')
-rw-r--r--src/libjin/Graphics/Font/je_decoder.cpp93
-rw-r--r--src/libjin/Graphics/Font/je_decoder.h94
-rw-r--r--src/libjin/Graphics/Font/je_font.h100
-rw-r--r--src/libjin/Graphics/Font/je_page.h51
-rw-r--r--src/libjin/Graphics/Font/je_text.cpp154
-rw-r--r--src/libjin/Graphics/Font/je_text.h169
-rw-r--r--src/libjin/Graphics/Font/je_texture_font.cpp318
-rw-r--r--src/libjin/Graphics/Font/je_texture_font.h140
-rw-r--r--src/libjin/Graphics/Font/je_ttf.cpp463
-rw-r--r--src/libjin/Graphics/Font/je_ttf.h288
-rw-r--r--src/libjin/Graphics/Shader/je_jsl_compiler.cpp54
-rw-r--r--src/libjin/Graphics/Shader/je_jsl_compiler.h42
-rw-r--r--src/libjin/Graphics/Shader/je_shader.cpp278
-rw-r--r--src/libjin/Graphics/Shader/je_shader.h200
-rw-r--r--src/libjin/Graphics/animation/je_animation.cpp0
-rw-r--r--src/libjin/Graphics/animation/je_animation.h20
-rw-r--r--src/libjin/Graphics/animation/je_clip.cpp0
-rw-r--r--src/libjin/Graphics/animation/je_clip.h20
-rw-r--r--src/libjin/Graphics/animations/je_animation.cpp14
-rw-r--r--src/libjin/Graphics/animations/je_animation.h64
-rw-r--r--src/libjin/Graphics/animations/je_animator.cpp14
-rw-r--r--src/libjin/Graphics/animations/je_animator.h50
-rw-r--r--src/libjin/Graphics/fonts/je_decoder.cpp96
-rw-r--r--src/libjin/Graphics/fonts/je_decoder.h97
-rw-r--r--src/libjin/Graphics/fonts/je_font.h109
-rw-r--r--src/libjin/Graphics/fonts/je_page.h54
-rw-r--r--src/libjin/Graphics/fonts/je_text.cpp157
-rw-r--r--src/libjin/Graphics/fonts/je_text.h172
-rw-r--r--src/libjin/Graphics/fonts/je_texture_font.cpp322
-rw-r--r--src/libjin/Graphics/fonts/je_texture_font.h144
-rw-r--r--src/libjin/Graphics/fonts/je_ttf.cpp468
-rw-r--r--src/libjin/Graphics/fonts/je_ttf.h292
-rw-r--r--src/libjin/Graphics/je_bitmap.cpp42
-rw-r--r--src/libjin/Graphics/je_bitmap.h21
-rw-r--r--src/libjin/Graphics/je_canvas.cpp15
-rw-r--r--src/libjin/Graphics/je_canvas.h9
-rw-r--r--src/libjin/Graphics/je_color.cpp5
-rw-r--r--src/libjin/Graphics/je_color.h32
-rw-r--r--src/libjin/Graphics/je_gl.cpp80
-rw-r--r--src/libjin/Graphics/je_gl.h46
-rw-r--r--src/libjin/Graphics/je_graphic.cpp96
-rw-r--r--src/libjin/Graphics/je_graphic.h52
-rw-r--r--src/libjin/Graphics/je_graphics.h30
-rw-r--r--src/libjin/Graphics/je_image.cpp3
-rw-r--r--src/libjin/Graphics/je_image.h7
-rw-r--r--src/libjin/Graphics/je_mesh.h12
-rw-r--r--src/libjin/Graphics/je_shapes.cpp32
-rw-r--r--src/libjin/Graphics/je_shapes.h10
-rw-r--r--src/libjin/Graphics/je_sprite.cpp145
-rw-r--r--src/libjin/Graphics/je_sprite.h73
-rw-r--r--src/libjin/Graphics/je_sprite_batch.cpp11
-rw-r--r--src/libjin/Graphics/je_sprite_batch.h9
-rw-r--r--src/libjin/Graphics/je_sprite_sheet.cpp22
-rw-r--r--src/libjin/Graphics/je_sprite_sheet.h33
-rw-r--r--src/libjin/Graphics/je_texture.h11
-rw-r--r--src/libjin/Graphics/je_window.cpp43
-rw-r--r--src/libjin/Graphics/je_window.h14
-rw-r--r--src/libjin/Graphics/particle/je_particle.cpp0
-rw-r--r--src/libjin/Graphics/particle/je_particle.h21
-rw-r--r--src/libjin/Graphics/particle/je_particle_batch.cpp0
-rw-r--r--src/libjin/Graphics/particle/je_particle_batch.h20
-rw-r--r--src/libjin/Graphics/particle/je_particle_emitter.h23
-rw-r--r--src/libjin/Graphics/particle/je_particle_system.cpp1
-rw-r--r--src/libjin/Graphics/particle/je_particle_system.h0
-rw-r--r--src/libjin/Graphics/particles/je_particle.cpp26
-rw-r--r--src/libjin/Graphics/particles/je_particle.h178
-rw-r--r--src/libjin/Graphics/particles/je_particle_emitter.cpp42
-rw-r--r--src/libjin/Graphics/particles/je_particle_emitter.h112
-rw-r--r--src/libjin/Graphics/particles/je_particle_pool.h24
-rw-r--r--src/libjin/Graphics/particles/je_particle_system.cpp (renamed from src/libjin/Graphics/particle/je_particle_emitter.cpp)2
-rw-r--r--src/libjin/Graphics/particles/je_particle_system.h103
-rw-r--r--src/libjin/Graphics/shaders/built-in/je_default.shader.h (renamed from src/libjin/Graphics/Shader/je_default.shader.h)0
-rw-r--r--src/libjin/Graphics/shaders/built-in/je_font.shader.h (renamed from src/libjin/Graphics/Shader/je_font.shader.h)0
-rw-r--r--src/libjin/Graphics/shaders/built-in/je_texture.shader.h (renamed from src/libjin/Graphics/Shader/je_texture.shader.h)0
-rw-r--r--src/libjin/Graphics/shaders/je_base.shader.h (renamed from src/libjin/Graphics/Shader/je_base.shader.h)9
-rw-r--r--src/libjin/Graphics/shaders/je_jsl_compiler.cpp57
-rw-r--r--src/libjin/Graphics/shaders/je_jsl_compiler.h45
-rw-r--r--src/libjin/Graphics/shaders/je_shader.cpp281
-rw-r--r--src/libjin/Graphics/shaders/je_shader.h201
79 files changed, 3799 insertions, 2736 deletions
diff --git a/src/libjin/Graphics/Font/je_decoder.cpp b/src/libjin/Graphics/Font/je_decoder.cpp
deleted file mode 100644
index 01e1990..0000000
--- a/src/libjin/Graphics/Font/je_decoder.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include "je_decoder.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- /* utf8 byte string to unicode codepoint */
- static const char *utf8toCodepoint(const char *p, unsigned *res) {
- return nullptr;
-
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // decoders
- /////////////////////////////////////////////////////////////////////////////
-
- const void* Utf8::decode(const void* data, Codepoint* res) const
- {
- const char* p = (char*)data;
- unsigned x, mask, shift;
- switch (*p & 0xf0) {
- case 0xf0: mask = 0x07; shift = 18; break;
- case 0xe0: mask = 0x0f; shift = 12; break;
- case 0xc0:
- case 0xd0: mask = 0x1f; shift = 6; break;
- default:
- *res = *p;
- return p + 1;
- }
- x = (*p & mask) << shift;
- do {
- if (*(++p) == '\0') {
- *res = x;
- return p;
- }
- shift -= 6;
- x |= (*p & 0x3f) << shift;
- } while (shift);
- *res = x;
- return p + 1;
- }
-
- const void* Utf8::next(const void* data) const
- {
- const char* p = (char*)data;
- unsigned x, mask, shift;
- switch (*p & 0xf0) {
- case 0xf0: mask = 0x07; shift = 18; break;
- case 0xe0: mask = 0x0f; shift = 12; break;
- case 0xc0:
- case 0xd0: mask = 0x1f; shift = 6; break;
- default:
- return p + 1;
- }
- x = (*p & mask) << shift;
- do {
- if (*(++p) == '\0') {
- return p;
- }
- shift -= 6;
- x |= (*p & 0x3f) << shift;
- } while (shift);
- return p + 1;
- }
- /*
- const void* Utf16::decode(const void* data, Codepoint* res) const
- {
- return nullptr;
- }
-
- const void* Utf16::next(const void* data) const
- {
- return nullptr;
- }
- */
- const void* Ascii::decode(const void* data, Codepoint* res) const
- {
- const char* p = (char*)data;
- *res = *p;
- return p + 1;
- }
-
- const void* Ascii::next(const void* data) const
- {
- const char* p = (char*)data;
- return p + 1;
- }
-
- } // namespace Graphics
-} // namespace JinEngine \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_decoder.h b/src/libjin/Graphics/Font/je_decoder.h
deleted file mode 100644
index 36cbda7..0000000
--- a/src/libjin/Graphics/Font/je_decoder.h
+++ /dev/null
@@ -1,94 +0,0 @@
-#ifndef __JE_UTF8_H
-#define __JE_UTF8_H
-
-#include <vector>
-
-#include "je_text.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- /// Text decoder.
- ///
- class Decoder
- {
- public:
-
- ///
- /// Decode a code unit.
- ///
- /// @param data Code units.
- /// @param codepoint Value of code point.
- /// @return Next code unit location.
- ///
- virtual const void* decode(const void* data, Codepoint* codepoint) const = 0 ;
-
- ///
- /// Get next code unit location.
- ///
- /// @param data Code units.
- /// @return Next code unit location.
- ///
- virtual const void* next(const void* data) const = 0;
-
- };
-
- ///
- /// Utf-8 decoder.
- ///
- class Utf8 : public Decoder
- {
- public:
-
- ///
- /// Decode a code unit.
- ///
- /// @param data Code units.
- /// @param codepoint Value of code point.
- /// @return Next code unit location.
- ///
- const void* decode(const void* data, Codepoint* codepoint) const override;
-
- ///
- /// Get next code unit location.
- ///
- /// @param data Code units.
- /// @return Next code unit location.
- ///
- const void* next(const void* data) const override;
-
- };
-
- ///
- /// Ascii decoder.
- ///
- class Ascii : public Decoder
- {
- public:
-
- ///
- /// Decode a code unit.
- ///
- /// @param data Code units.
- /// @param codepoint Value of code point.
- /// @return Next code unit location.
- ///
- const void* decode(const void* data, Codepoint* codepoint) const override;
-
- ///
- /// Get next code unit location.
- ///
- /// @param data Code units.
- /// @return Next code unit location.
- ///
- const void* next(const void* data) const override;
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_font.h b/src/libjin/Graphics/Font/je_font.h
deleted file mode 100644
index 4529902..0000000
--- a/src/libjin/Graphics/Font/je_font.h
+++ /dev/null
@@ -1,100 +0,0 @@
-#ifndef __JE_FONT_H
-#define __JE_FONT_H
-
-#include <vector>
-#include "je_text.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- struct Page;
-
- ///
- /// Base Font class.
- ///
- class Font
- {
- public:
- ///
- /// Font constructor.
- ///
- Font(unsigned fontsize)
- : mFontSize(fontsize)
- {
- }
-
- ///
- /// Font destructor.
- ///
- virtual ~Font() {};
-
- ///
- /// Create page with given text.
- ///
- /// @param text Text to be typesetted.
- /// @param lineheight Line height of text.
- /// @param spacing Spacing between characters. 0 by default.
- /// @return Page if created successfully, otherwise return null.
- ///
- virtual Page* typeset(const Text& text, int lineheight, int spacing = 0) = 0;
-
- ///
- /// Create page with given unicode codepoints.
- ///
- /// @param content Unicode codepoints to be typesetted.
- /// @param lineheight Line height of text.
- /// @param spacing Spacing between characters. 0 by default.
- /// @return Page if created successfully, otherwise return null.
- ///
- virtual Page* typeset(const Content& content, int lineheight, int spacing = 0) = 0;
-
- ///
- /// Render page to given position.
- ///
- /// @param page Page to be rendered.
- /// @param x X value of the position.
- /// @param y Y value of the position.
- ///
- virtual void print(const Page* page, int x, int y) = 0;
-
- ///
- /// Render unicode codepoints to given position.
- ///
- /// @param content Unicode codepoints to be typesetted.
- /// @param x X value of the position.
- /// @param y Y value of the position.
- /// @param lineheight Line height of the content.
- /// @param spacing Spacing between characters.
- ///
- virtual void print(const Content& content, int x, int y, int lineheight, int spacing = 0) = 0;
-
- ///
- /// Render text to given position.
- ///
- /// @param text Text to be rendered.
- /// @param x X value of the position.
- /// @param y Y value of the position.
- /// @param lineheight Line height of the text.
- /// @param spacing Spacing between characters.
- ///
- virtual void print(const Text& text, int x, int y, int lineheight, int spacing = 0) = 0;
-
- ///
- /// Get font size.
- ///
- /// @return Font size.
- ///
- inline unsigned getFontSize() { return mFontSize; };
-
- protected:
-
- unsigned mFontSize;
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // __JE_FONT_H \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_page.h b/src/libjin/Graphics/Font/je_page.h
deleted file mode 100644
index fbc297e..0000000
--- a/src/libjin/Graphics/Font/je_page.h
+++ /dev/null
@@ -1,51 +0,0 @@
-#ifndef __JE_PAGE_H
-#define __JE_PAGE_H
-
-#include "../../math/je_vector2.hpp"
-
-#include "je_font.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- class Font;
-
- ///
- /// Glyphs data to be rendered.
- ///
- struct GlyphVertex
- {
- int x, y; ///< screen coordinates
- float u, v; ///< normalized texture uv
- };
-
- ///
- /// Glyphs info for reducing draw call.
- ///
- struct GlyphArrayDrawInfo
- {
- GLuint texture; ///< atlas
- unsigned int start; ///< glyph vertex indecies
- unsigned int count; ///< glyph vertex count
- };
-
- ///
- /// Page to be rendered.
- ///
- /// A page is a pre-rendered text struct for reducing draw call. Each page
- /// keeps a font pointer which should not be changed.
- ///
- struct Page
- {
- Font* font;
- std::vector<GlyphArrayDrawInfo> glyphinfolist;
- std::vector<GlyphVertex> glyphvertices;
- Math::Vector2<int> size;
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // __JE_PAGE_H \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_text.cpp b/src/libjin/Graphics/Font/je_text.cpp
deleted file mode 100644
index 75dfc7b..0000000
--- a/src/libjin/Graphics/Font/je_text.cpp
+++ /dev/null
@@ -1,154 +0,0 @@
-#include <cstring>
-
-#include "je_text.h"
-#include "je_decoder.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- /////////////////////////////////////////////////////////////////////////////
- // iterator
- /////////////////////////////////////////////////////////////////////////////
-
- Text::Iterator::Iterator(const Iterator& itor)
- : data(itor.data)
- , p(itor.p)
- , encode(itor.encode)
- , length(itor.length)
- {
- switch (encode)
- {
- case Encode::UTF8: decoder = new Utf8(); break;
- case Encode::ASCII: decoder = new Ascii(); break;
- }
- }
-
- Text::Iterator::Iterator(const Encode& _encode, const void* _data, unsigned int _length)
- : data(_data)
- , p(_data)
- , encode(_encode)
- , length(_length)
- {
- switch (encode)
- {
- case Encode::UTF8: decoder = new Utf8(); break;
- case Encode::ASCII: decoder = new Ascii(); break;
- }
- }
-
- Text::Iterator::~Iterator()
- {
- delete decoder;
- }
-
- Codepoint Text::Iterator::get()
- {
- Codepoint codepoint;
- decoder->decode(p, &codepoint);
- return codepoint;
- }
-
- Codepoint Text::Iterator::operator*()
- {
- return get();
- }
- /*
- Text::Iterator Text::Iterator::begin()
- {
- Iterator itor(encode, data, length);
- itor.toBegin();
- return itor;
- }
-
- Text::Iterator Text::Iterator::end()
- {
- Iterator itor(encode, data, length);
- itor.toEnd();
- return itor;
- }
- */
- void Text::Iterator::toBegin()
- {
- p = (const unsigned char*)data;
- }
-
- void Text::Iterator::toEnd()
- {
- p = (const unsigned char*)data + length;
- }
-
- Text::Iterator& Text::Iterator::operator ++()
- {
- p = decoder->next(p);
- return *this;
- }
-
- Text::Iterator Text::Iterator::operator ++(int)
- {
- p = decoder->next(p);
- Iterator itor(encode, data, length);
- itor.p = p;
- return itor;
- }
-
- bool Text::Iterator::operator !=(const Iterator& itor)
- {
- return !(data == itor.data
- && p == itor.p
- && length == itor.length
- && encode == itor.encode);
- }
-
- bool Text::Iterator::operator ==(const Iterator& itor)
- {
- return data == itor.data
- && p == itor.p
- && length == itor.length
- && encode == itor.encode;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // text
- /////////////////////////////////////////////////////////////////////////////
-
- Text::Text(Encode encode, const void* data)
- {
- unsigned length = strlen((const char*)data);
- Iterator end = Iterator(encode, data, length);
- end.toEnd();
- Iterator it = Iterator(encode, data, length);
- for (; it != end; ++it)
- {
- content.push_back(*it);
- }
- }
-
- Text::Text(Encode encode, const void* data, unsigned length)
- {
- Iterator end = Iterator(encode, data, length);
- end.toEnd();
- Iterator it = Iterator(encode, data, length);
- for (; it != end; ++it)
- {
- content.push_back(*it);
- }
- }
-
- Text::~Text()
- {
- }
-
- const Content& Text::getContent() const
- {
- return content;
- }
-
- const Content& Text::operator*() const
- {
- return content;
- }
-
- } // namespace Graphics
-} // namespace JinEngine \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_text.h b/src/libjin/Graphics/Font/je_text.h
deleted file mode 100644
index 7436875..0000000
--- a/src/libjin/Graphics/Font/je_text.h
+++ /dev/null
@@ -1,169 +0,0 @@
-#ifndef __JE_TEXT_H
-#define __JE_TEXT_H
-
-#include <vector>
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- typedef unsigned int Codepoint;
-
- typedef std::vector<Codepoint> Content;
-
- class Text;
-
- class Decoder;
-
- ///
- /// Supported text encoding.
- ///
- enum Encode
- {
- UTF8, ///< utf-8
- ASCII, ///< ASCII
- };
-
- ///
- /// Decoded text. Saved as unicode codepoints.
- ///
- class Text
- {
- public:
- ///
- ///
- ///
- Text(Encode encode, const void* data);
-
- ///
- ///
- ///
- Text(Encode encode, const void* data, unsigned int length);
-
- ///
- ///
- ///
- ~Text();
-
- ///
- ///
- ///
- const Content& getContent() const;
-
- ///
- ///
- ///
- const Content& operator*() const;
-
- private:
- ///
- ///
- ///
- class Iterator
- {
- public:
-
- ///
- ///
- ///
- Iterator(const Iterator& itor);
-
- ///
- ///
- ///
- Iterator(const Encode& encode, const void* data, unsigned int length);
-
- ///
- ///
- ///
- ~Iterator();
-
- ///
- ///
- ///
- Codepoint get();
-
- //Iterator begin();
- //Iterator end();
-
- ///
- ///
- ///
- void toBegin();
-
- ///
- ///
- ///
- void toEnd();
-
- ///
- ///
- ///
- Codepoint operator *();
-
- ///
- ///
- ///
- Iterator& operator ++();
-
- ///
- ///
- ///
- Iterator operator ++(int);
-
- ///
- ///
- ///
- bool operator !=(const Iterator& itor);
-
- ///
- ///
- ///
- bool operator ==(const Iterator& itor);
-
- private:
-
- ///
- ///
- ///
- void operator = (const Iterator&);
-
- ///
- ///
- ///
- const Encode encode;
-
- ///
- ///
- ///
- const Decoder* decoder;
-
- ///
- ///
- ///
- const void* p;
-
- ///
- ///
- ///
- const void* const data;
-
- ///
- ///
- ///
- unsigned int length;
-
- };
-
- ///
- ///
- ///
- Content content;
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_texture_font.cpp b/src/libjin/Graphics/Font/je_texture_font.cpp
deleted file mode 100644
index 9651c1a..0000000
--- a/src/libjin/Graphics/Font/je_texture_font.cpp
+++ /dev/null
@@ -1,318 +0,0 @@
-#include <vector>
-
-#include "../../math/je_vector2.hpp"
-
-#include "../shader/je_shader.h"
-
-#include "je_texture_font.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- using namespace std;
- using namespace Math;
-
- TextureFont * TextureFont::createTextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh)
- {
- TextureFont* tf = new TextureFont(bitmap, codepoints, cellw, cellh);
- return tf;
- }
-
- TextureFont * TextureFont::createTextureFont(const Bitmap* bitmap, const Text& codepoints, int cellw, int cellh)
- {
- TextureFont* tf = new TextureFont(bitmap, *codepoints, cellw, cellh);
- return tf;
- }
-
- 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::createTextureFont(const Bitmap* bitmap, const Text& codepoints, Color mask, int cellh)
- {
- TextureFont* tf = new TextureFont(bitmap, *codepoints, mask, cellh);
- return tf;
- }
-
- TextureFont::~TextureFont()
- {
- }
-
- const TextureFont::TextureGlyph* TextureFont::findGlyph(Codepoint codepoint) const
- {
- auto it = glyphs.find(codepoint);
- if (it != glyphs.end())
- {
- return &it->second;
- }
- else
- return nullptr;
- }
-
- Page* TextureFont::typeset(const Content& text, int lineheight, int spacing)
- {
- Page* page = new Page();
- page->font = this;
- vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
- vector<GlyphVertex>& glyphvertices = page->glyphvertices;
- int texture = -1;
- const TextureGlyph* glyph = nullptr;
- GlyphVertex vertex;
- Vector2<int> p(0, 0);
- int i = 0;
-
- #define glyphvertices_push(_x, _y, _u, _v) \
- vertex.x = _x; vertex.y = _y;\
- vertex.u = _u; vertex.v = _v;\
- glyphvertices.push_back(vertex);\
-
- for (Codepoint c : text)
- {
- // return
- if (c == 0x0D) continue;
- // newline
- if (c == 0x0A)
- {
- p.y += lineheight;
- p.x = 0;
- continue;
- }
- if (c == 0x09)
- {
- // tab = 4*space
- unsigned cw = getCharWidth(0x20);
- p.x += cw * 4;
- continue;
- }
- glyph = findGlyph(c);
- if (glyph == nullptr) continue;
- if (texture != mTexture)
- {
- texture = mTexture;
- GlyphArrayDrawInfo info;
- info.start = i;
- info.count = 0;
- info.texture = texture;
- glyphinfolist.push_back(info);
- }
- glyphinfolist[glyphinfolist.size() - 1].count += 4;
- // normalized
- float w = getWidth(), h = getHeight();
- float nx = glyph->x / w, ny = glyph->y / h;
- float nw = glyph->w / w, nh = glyph->h / h;
- glyphvertices_push(p.x, p.y, nx, ny);
- glyphvertices_push(p.x, p.y + glyph->h, nx, ny + nh);
- glyphvertices_push(p.x + glyph->w, p.y + glyph->h, nx + nw, ny + nh);
- glyphvertices_push(p.x + glyph->w, p.y, nx + nw, ny);
- p.x += glyph->w + spacing;
- i += 4;
- }
- getTextBox(text, &page->size.w, &page->size.h, lineheight, spacing);
- return page;
- }
-
- int TextureFont::getCharWidth(int c)
- {
- auto it = glyphs.find(c);
- if (it != glyphs.end())
- {
- return it->second.w;
- }
- return 0;
- }
-
- int TextureFont::getCharHeight(int c)
- {
- auto it = glyphs.find(c);
- if (it != glyphs.end())
- {
- return it->second.h;
- }
- return 0;
- }
-
- int TextureFont::getTextWidth(const Content& t, int spacing)
- {
- int res = 0;
- int tmp = 0;
- for (Codepoint c : t)
- {
- if (c == 0x0D)
- continue;
- if (c == 0x0A)
- {
- tmp = 0;
- continue;
- }
- if (c == 0x09)
- {
- // tab = 4*space
- unsigned cw = getCharWidth(0x20);
- tmp += cw * 4;
- if (tmp > res) res = tmp;
- continue;
- }
- tmp += getCharWidth(c) + spacing;
- if (tmp > res) res = tmp;
- }
- return res;
- }
-
- int TextureFont::getTextHeight(const Content& t, int lineheight)
- {
- int res = 0;
- bool newline = true;
- for (Codepoint c : t)
- {
- if (c == 0x0A)
- newline = true;
- else if (c == 0x0D);
- else if (newline)
- {
- newline = false;
- res += lineheight;
- }
- }
- return res;
- }
-
- void TextureFont::getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing)
- {
- *w = 0;
- *h = 0;
- int tmp = 0;
- bool newline = true;
- for (Codepoint c : text)
- {
- if (c == 0x0D)
- continue;
- if (c == 0x0A)
- {
- tmp = 0;
- newline = true;
- continue;
- }
- else if (newline)
- {
- newline = false;
- *h += lineheight;
- }
- tmp += getCharWidth(c) + spacing;
- if (tmp > *w)
- *w = tmp;
- }
- }
-
- Page* TextureFont::typeset(const Text& text, int lineheight, int spacing)
- {
- return typeset(*text, lineheight, spacing);
- }
-
- void TextureFont::print(const Page* page, int x, int y)
- {
- Shader* shader = Shader::getCurrentShader();
- const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
- const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
- gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0);
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
- shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
- for (int i = 0; i < glyphinfolist.size(); ++i)
- {
- const GlyphArrayDrawInfo& info = glyphinfolist[i];
- shader->bindVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x);
- shader->bindUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u);
- gl.bindTexture(info.texture);
- gl.drawArrays(GL_QUADS, 0, info.count);
- gl.bindTexture(0);
- }
- }
-
- void TextureFont::print(const Content& text, int x, int y, int lineheight, int spacing)
- {
- Page* page = typeset(text, lineheight, spacing);
- print(page, x, y);
- delete page;
- }
-
- void TextureFont::print(const Text& text, int x, int y, int lineheight, int spacing)
- {
- Page* page = typeset(text, lineheight, spacing);
- print(page, x, y);
- delete page;
- }
-
- TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh)
- : Graphic(bitmap)
- , Font(cellh)
- {
- TextureGlyph glyph;
- Vector2<int> count(bitmap->getWidth() / cellw, bitmap->getHeight() / cellh);
- glyph.w = cellw;
- glyph.h = cellh;
- for (int y = 0; y < count.row; ++y)
- {
- glyph.y = y * cellh;
- for (int x = 0; x < count.colum; ++x)
- {
- glyph.x = x * cellw;
- if (x + y * count.colum >= codepoints.size())
- return;
- glyphs.insert(std::pair<Codepoint, TextureGlyph>(codepoints[x + y * count.colum], glyph));
- }
- }
- }
-
- TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh)
- : Graphic(bitmap)
- , Font(cellh)
- {
- TextureGlyph glyph;
- glyph.h = cellh;
- int w = bitmap->getWidth();
- int h = bitmap->getHeight();
- int i = 0;
- for (int y = 0; y < h; y += cellh)
- {
- glyph.y = y;
- bool newc = false;
- for (int x = 0; x <= w; ++x)
- {
- if (x == w && newc)
- {
- glyph.w = x - glyph.x;
- if (i >= codepoints.size())
- return;
- glyphs.insert(std::pair<Codepoint, TextureGlyph>(codepoints[i], glyph));
- ++i;
- newc = false;
- break;
- }
- Color c = bitmap->getPixels()[x + y * w];
- if (!newc && c != mask)
- {
- glyph.x = x;
- newc = true;
- }
- else if (newc && c == mask)
- {
- glyph.w = x - glyph.x;
- if (i >= codepoints.size())
- return;
- glyphs.insert(std::pair<Codepoint, TextureGlyph>(codepoints[i], glyph));
- if (codepoints[i] == 't')
- {
- int a = 10;
- }
- ++i;
- newc = false;
- }
- }
- }
- }
-
- }
-} \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_texture_font.h b/src/libjin/Graphics/Font/je_texture_font.h
deleted file mode 100644
index 6276350..0000000
--- a/src/libjin/Graphics/Font/je_texture_font.h
+++ /dev/null
@@ -1,140 +0,0 @@
-#ifndef __JE_TEXTURE_FONT_H
-#define __JE_TEXTURE_FONT_H
-
-#include <map>
-#include <vector>
-
-#include "../../math/je_vector4.hpp"
-
-#include "../je_graphic.h"
-#include "../je_bitmap.h"
-
-#include "je_page.h"
-#include "je_font.h"
-#include "je_text.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- ///
- ///
- class TextureFont : public Font
- , public Graphic
- {
- 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
- {
- float 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);
-
- ///
- ///
- ///
- int getCharWidth(int c);
-
- ///
- ///
- ///
- int getCharHeight(int c);
-
- ///
- ///
- ///
- 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);
-
- ///
- ///
- ///
- const TextureGlyph* findGlyph(Codepoint codepoint) const;
-
-
- ///
- ///
- ///
- std::map<Codepoint, TextureGlyph> glyphs;
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_ttf.cpp b/src/libjin/Graphics/Font/je_ttf.cpp
deleted file mode 100644
index a11efb0..0000000
--- a/src/libjin/Graphics/Font/je_ttf.cpp
+++ /dev/null
@@ -1,463 +0,0 @@
-#include "../../core/je_configuration.h"
-#if defined(jin_graphics)
-
-#include <stdio.h>
-
-#include "../../common/je_array.hpp"
-
-#include "../je_gl.h"
-#include "../je_color.h"
-#include "../shader/je_shader.h"
-
-#include "je_ttf.h"
-#include "je_page.h"
-
-#define STB_TRUETYPE_IMPLEMENTATION
-#include "../../3rdparty/stb/stb_truetype.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- /////////////////////////////////////////////////////////////////////////////
- // TTFData
- /////////////////////////////////////////////////////////////////////////////
-
- TTFData* TTFData::createTTFData(const unsigned char* data, unsigned int size)
- {
- TTFData* ttf = nullptr;
- try
- {
- ttf = new TTFData(data, size);
- return ttf;
- }
- catch (...)
- {
- return nullptr;
- }
- }
-
- TTFData::TTFData(const unsigned char* d, unsigned int s)
- {
- raw.size = s;
- raw.data = (unsigned char*)malloc(s);
- memcpy(raw.data, d, s);
- if (!stbtt_InitFont(&info, (const unsigned char*)raw.data, 0))
- {
- delete raw.data;
- throw 0;
- }
- /* push default fontsize */
- pushTTFsize(FONT_SIZE);
- }
-
- TTFData::~TTFData()
- {
- free(raw.data);
- }
-
- TTF* TTFData::createTTF(unsigned fontSize)
- {
- TTF* ttf;
- try
- {
- ttf = new TTF(this, fontSize);
- }
- catch (...)
- {
- return nullptr;
- }
- return ttf;
- }
-
- /*
- * (0, 0)
- * +--------------+ ascent
- * | +--------+ |
- * | | | |
- * | | bitmap | |
- * +--|--------|--+ baseline
- * | +--------+ |
- * +--|-----------+ decent
- * | |
- * leftSideBearing |
- * advanceWidth
- */
- void TTFData::getVMetrics(int* baseline, int* descent)
- {
- float scale = scales.back();
- int ascent;
- stbtt_GetFontVMetrics(&info, &ascent, descent, 0);
- *baseline = (int)(ascent*scale) + 1; // slight adjustment
- *descent = *baseline - (int)(*descent*scale) + 1;
- }
-
- void TTFData::getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing)
- {
- float scale = scales.back();
- int adw, lsb;
- stbtt_GetCodepointHMetrics(&info, codepoint, &adw, &lsb);
- *advanceWidth = (int)(adw*scale);
- *leftSideBearing = (int)(lsb*scale);
- }
-
- void TTFData::pushTTFsize(unsigned int fs)
- {
- float sc = stbtt_ScaleForPixelHeight(&info, fs);
- scales.push_back(sc);
- }
-
- void TTFData::popTTFsize()
- {
- /* always keep default ttf size on the bottom of stack */
- if (scales.size() > 1)
- scales.pop_back();
- }
-
- Channel* TTFData::getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const
- {
- float scale = scales.back();
- Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff);
- return bitmap;
- }
-
- Color* TTFData::getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const
- {
- float scale = scales.back();
- Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff);
- int w = *width, h = *height;
- //int xo = *xoff, yo = *yoff;
- Color* bitmap32 = new Color[w*h];
- for (int y = 0; y < h; ++y)
- {
- for (int x = 0; x < w; ++x)
- {
- bitmap32[x + y * w].set(0xff, 0xff, 0xff, bitmap[x + y * w]);
- }
- }
- free(bitmap);
- return bitmap32;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // TTF
- /////////////////////////////////////////////////////////////////////////////
-
- #include "../shader/je_font.shader.h"
-
- using namespace std;
- using namespace JinEngine::Math;
-
- const int TTF::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 };
- const int TTF::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 };
-
- /* little endian unicode */
- static const char* unicodeLittleEndian(const char* p, unsigned* res)
- {
- }
-
- ///*static*/ TTF* TTF::createTTF(TTFData* fontData, unsigned int fontSzie)
- //{
- // TTF* ttf;
- // try
- // {
- // ttf = new TTF(fontData, fontSzie);
- // }
- // catch (...)
- // {
- // return nullptr;
- // }
- // return ttf;
- //}
-
- TTF::TTF(TTFData* f, unsigned int fontSize)
- : Font(fontSize)
- , cursor(0, 0)
- , ttf(f)
- {
- ttf->pushTTFsize(fontSize);
- ttf->getVMetrics(&baseline, &descent);
- estimateSize();
- ttf->popTTFsize();
- /* create a default texture */
- createAtlas();
- }
-
- /* estimate the size of atlas texture */
- void TTF::estimateSize()
- {
- for (int level = 0; level <= TEXTURE_SIZE_LEVEL_MAX; ++level)
- {
- if (descent * (descent*0.8) * 96 <= TEXTURE_WIDTHS[level] * TEXTURE_HEIGHTS[level])
- {
- textureWidth = TEXTURE_WIDTHS[level];
- textureHeight = TEXTURE_HEIGHTS[level];
- break;
- }
- }
- }
-
- TTF::~TTF()
- {
- }
-
- GLuint TTF::createAtlas()
- {
- GLuint t;
- gl.flushError();
- t = gl.genTexture();
- gl.bindTexture(t);
- gl.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- gl.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- gl.setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- gl.setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- gl.texImage(GL_RGBA8, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE);
- if (glGetError() != GL_NO_ERROR)
- {
- glDeleteTextures(1, &t);
- gl.bindTexture(0);
- return 0;
- }
- atlases.push_back(t);
- gl.bindTexture(0);
- return t;
- }
-
- void TTF::print(const Content& t, int x, int y, int lineheight, int spacing)
- {
- Page* page = typeset(t, lineheight, spacing);
- print(page, x, y);
- delete page;
- }
-
- Page* TTF::typeset(const Content& text, int lineheight, int spacing)
- {
- Page* page = new Page();
- page->font = this;
- vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
- vector<GlyphVertex>& glyphvertices = page->glyphvertices;
- int texture = -1;
- TTFGlyph* glyph = nullptr;
- GlyphVertex vertex;
- Vector2<int> p(0, 0);
- int i = 0;
-
- #define glyphvertices_push(_x, _y, _u, _v) \
- vertex.x = _x; vertex.y = _y;\
- vertex.u = _u; vertex.v = _v;\
- glyphvertices.push_back(vertex);
-
- #define glyphlize(c)\
- do{\
- glyph = &findGlyph(c); \
- if (texture != glyph->atlas) \
- { \
- GlyphArrayDrawInfo info; \
- info.start = i; \
- info.count = 0; \
- info.texture = glyph->atlas; \
- texture = glyph->atlas; \
- glyphinfolist.push_back(info); \
- } \
- glyphinfolist[glyphinfolist.size() - 1].count += 4; \
- TTFGlyph::Bbox& bbox = glyph->bbox; \
- glyphvertices_push(p.x, p.y, bbox.x, bbox.y); \
- glyphvertices_push(p.x, p.y + glyph->height, bbox.x, bbox.y + bbox.h); \
- glyphvertices_push(p.x + glyph->width, p.y + glyph->height, bbox.x + bbox.w, bbox.y + bbox.h); \
- glyphvertices_push(p.x + glyph->width, p.y, bbox.x + bbox.w, bbox.y); \
- }while(0)
-
- for (Codepoint c : text)
- {
- if (c == 0x0D)
- continue;
- if (c == 0x0A)
- {
- /* new line */
- p.y += lineheight;
- p.x = 0;
- continue;
- }
- if (c == 0x09)
- {
- // tab = 4*space
- unsigned cw = getCharWidth(0x20);
- p.x += cw * 4;
- continue;
- }
- glyphlize(c);
- p.x += glyph->width + spacing;
- i += 4;
- }
- getTextBox(text, &page->size.w, &page->size.h, lineheight, spacing);
- return page;
- }
-
- Page* TTF::typeset(const Text& text, int lineheight, int spacing)
- {
- return typeset(*text, lineheight, spacing);
- }
-
- void TTF::print(const Page* page, int x, int y)
- {
- Shader* shader = Shader::getCurrentShader();
- const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
- const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
- gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0);
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
- shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
- for (int i = 0; i < glyphinfolist.size(); ++i)
- {
- const GlyphArrayDrawInfo& info = glyphinfolist[i];
- shader->bindVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x);
- shader->bindUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u);
- gl.bindTexture(info.texture);
- gl.drawArrays(GL_QUADS, 0, info.count);
- gl.bindTexture(0);
- }
- }
-
- void TTF::print(const Text& text, int x, int y, int lineheight, int spacing /* = 0 */)
- {
- print(*text, x, y, lineheight, spacing);
- }
-
- int TTF::getCharWidth(int c)
- {
- int adw, lsb;
- ttf->pushTTFsize(mFontSize);
- ttf->getHMetrics(c, &adw, &lsb);
- ttf->popTTFsize();
- return adw;
- }
-
- int TTF::getCharHeight(int c)
- {
- return descent;
- }
-
- int TTF::getTextWidth(const Content& t, int spacing)
- {
- ttf->pushTTFsize(mFontSize);
- int res = 0;
- int tmp = 0;
- for (Codepoint c : t)
- {
- if (c == 0x0D)
- continue;
- if (c == 0x0A)
- {
- tmp = 0;
- continue;
- }
- tmp += getCharWidth(c) + spacing;
- if (tmp > res)
- res = tmp;
- }
- ttf->popTTFsize();
- return res;
- }
-
- int TTF::getTextHeight(const Content& t, int lineheight)
- {
- ttf->pushTTFsize(mFontSize);
- int res = 0;
- bool newline = true;
- for (Codepoint c : t)
- {
- if (c == 0x0A)
- newline = true;
- else if (c == 0x0D);
- else if (newline)
- {
- newline = false;
- res += lineheight;
- }
- }
- ttf->popTTFsize();
- return res;
- }
-
- void TTF::getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing)
- {
- ttf->pushTTFsize(mFontSize);
- *w = 0;
- *h = 0;
- int tmp = 0;
- bool newline = true;
- for (Codepoint c : text)
- {
- if (c == 0x0D)
- continue;
- if (c == 0x0A)
- {
- tmp = 0;
- newline = true;
- continue;
- }
- else if (newline)
- {
- newline = false;
- *h += lineheight;
- }
- tmp += getCharWidth(c) + spacing;
- if (tmp > *w)
- *w = tmp;
- }
- ttf->popTTFsize();
- }
-
- TTF::TTFGlyph& TTF::bakeGlyph(unsigned int character)
- {
- int w, h, xoff, yoff;
- ttf->pushTTFsize(mFontSize);
- GLuint atlas = atlases.back();
- const Color* bitmap = ttf->getCodepointBitmap(character, &w, &h, &xoff, &yoff);
- int adw, lsb;
- {
- /* bake glyph */
- ttf->getHMetrics(character, &adw, &lsb);
- ttf->popTTFsize();
- if (cursor.x + adw > textureWidth )
- {
- cursor.x = 0;
- cursor.y += descent;
- if (cursor.y + descent * 2 > textureHeight)
- {
- /* create new atlas */
- atlas = createAtlas();
- cursor.y = 0;
- }
- }
- gl.bindTexture(atlas);
- gl.texSubImage(cursor.x + xoff, cursor.y + yoff + baseline, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bitmap);
- gl.bindTexture();
- delete[] bitmap;
- }
- TTFGlyph glyph;
- glyph.atlas = atlas;
- glyph.bbox.x = cursor.x / (float)textureWidth;
- glyph.bbox.y = cursor.y / (float)textureHeight;
- glyph.bbox.w = adw / (float)textureWidth;
- glyph.bbox.h = descent / (float)textureHeight;
- glyph.width = adw;
- glyph.height = descent;
- glyphs.insert(std::pair<unsigned int, TTFGlyph>(character, glyph));
- cursor.x += adw;
- return glyphs[character];
- }
-
- TTF::TTFGlyph& TTF::findGlyph(unsigned int character)
- {
- map<unsigned int, TTFGlyph>::iterator it = glyphs.find(character);
- if (it != glyphs.end())
- return it->second;
- else
- return bakeGlyph(character);
- }
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // defined(jin_graphics) \ No newline at end of file
diff --git a/src/libjin/Graphics/Font/je_ttf.h b/src/libjin/Graphics/Font/je_ttf.h
deleted file mode 100644
index e3d63b2..0000000
--- a/src/libjin/Graphics/Font/je_ttf.h
+++ /dev/null
@@ -1,288 +0,0 @@
-#ifndef __JETTF_H
-#define __JE_TTF_H
-#include "../../core/je_configuration.h"
-#if defined(jin_graphics)
-
-#include <vector>
-#include <map>
-
-#include "../../3rdparty/stb/stb_truetype.h"
-#include "../../math/je_quad.h"
-
-#include "../je_color.h"
-#include "../je_graphic.h"
-
-#include "je_page.h"
-#include "je_font.h"
-#include "je_text.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- class TTF;
-
- //
- // TTFData
- // |- TTF(14px)
- // |- TTF(15px)
- // .
- // .
- // .
- //
- class TTFData
- {
- public:
-
- ///
- ///
- ///
- static TTFData* createTTFData(const unsigned char* data, unsigned int size);
-
- ///
- ///
- ///
- ~TTFData();
-
- ///
- ///
- ///
- TTF* createTTF(unsigned ttfsize);
-
- ///
- ///
- ///
- void pushTTFsize(unsigned ttfsize);
-
- ///
- ///
- ///
- void popTTFsize();
-
- ///
- ///
- ///
- Channel* getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const;
-
- ///
- ///
- ///
- Color* getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const;
-
- ///
- ///
- ///
- void getVMetrics(int* baseline, int* descent);
-
- ///
- ///
- ///
- void getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing);
-
- private:
-
- ///
- ///
- ///
- static const unsigned int FONT_SIZE = 12;
-
- ///
- ///
- ///
- TTFData(const unsigned char* data, unsigned int size);
-
- ///
- ///
- ///
- stbtt_fontinfo info;
-
- ///
- ///
- ///
- struct
- {
- unsigned char* data;
- unsigned int size;
- } raw;
-
- ///
- ///
- ///
- std::vector<float> scales;
-
- };
-
- class TTF : public Font
- {
- public:
- //static TTF* createTTF(TTFData* ttfData, unsigned ttfSzie);
-
- ///
- ///
- ///
- 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 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();
-
- private:
-
- friend class TTFData;
-
- ///
- ///
- ///
- struct TTFGlyph
- {
- GLuint atlas;
- // normalized coordinates
- struct Bbox
- {
- float x, y;
- float w, h;
- } bbox;
- // glyph size in pixel
- unsigned int width, height;
- };
-
- ///
- ///
- ///
- static const int TEXTURE_SIZE_LEVELS_COUNT = 7;
-
- ///
- ///
- ///
- static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1;
-
- ///
- ///
- ///
- static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT];
-
- ///
- ///
- ///
- static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT];
-
- ///
- ///
- ///
- TTF(TTFData* ttf, Codepoint ttfSize);
-
- ///
- ///
- ///
- void estimateSize();
-
- ///
- ///
- ///
- GLuint createAtlas();
-
- ///
- ///
- ///
- TTFGlyph& bakeGlyph(Codepoint character);
-
- ///
- ///
- ///
- TTFGlyph& findGlyph(Codepoint character);
-
- ///
- ///
- ///
- int getCharWidth(int c);
-
- ///
- ///
- ///
- int getCharHeight(int c);
-
- ///
- ///
- ///
- 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<GLuint> atlases;
-
- ///
- ///
- ///
- std::map<Codepoint, TTFGlyph> glyphs;
-
- ///
- ///
- ///
- TTFData* ttf;
-
- ///
- ///
- ///
- int baseline;
-
- ///
- ///
- ///
- int descent;
-
- ///
- ///
- ///
- Math::Vector2<float> cursor;
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // defined(jin_graphics)
-
-#endif // __JE_FONT_H \ No newline at end of file
diff --git a/src/libjin/Graphics/Shader/je_jsl_compiler.cpp b/src/libjin/Graphics/Shader/je_jsl_compiler.cpp
deleted file mode 100644
index 81b14e8..0000000
--- a/src/libjin/Graphics/Shader/je_jsl_compiler.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-#include "../../core/je_configuration.h"
-#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
-
-#include "../../Filesystem/je_buffer.h"
-
-#include "je_jsl_compiler.h"
-
-using namespace std;
-using namespace JinEngine::Filesystem;
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
-#include "je_base.shader.h"
-
- bool JSLCompiler::compile(const string& jsl, string* vertex_shader, string* fragment_shader)
- {
- // parse shader source, need some optimizations
- int loc_VERTEX_SHADER = jsl.find("#VERTEX_SHADER");
- int loc_END_VERTEX_SHADER = jsl.find("#END_VERTEX_SHADER");
- int loc_FRAGMENT_SHADER = jsl.find("#FRAGMENT_SHADER");
- int loc_END_FRAGMENT_SHADER = jsl.find("#END_FRAGMENT_SHADER");
- if (loc_VERTEX_SHADER == string::npos
- || loc_END_VERTEX_SHADER == string::npos
- || loc_FRAGMENT_SHADER == string::npos
- || loc_END_FRAGMENT_SHADER == string::npos
- )
- return false;
- // Load vertex and fragment shader source into buffers.
- {
- // Compile JSL vertex program.
- int start = loc_VERTEX_SHADER + strlen("#VERTEX_SHADER");
- *vertex_shader = jsl.substr(start, loc_END_VERTEX_SHADER - start);
- Buffer vbuffer = Buffer(vertex_shader->length() + BASE_VERTEX_SHADER_SIZE);
- formatVertexShader((char*)&vbuffer, vertex_shader->c_str());
- vertex_shader->assign((char*)&vbuffer);
- }
- {
- // Compile JSL fragment program.
- int start = loc_FRAGMENT_SHADER + strlen("#FRAGMENT_SHADER");
- *fragment_shader = jsl.substr(start, loc_END_FRAGMENT_SHADER - start);
- Buffer fbuffer = Buffer(fragment_shader->length() + BASE_FRAGMENT_SHADER_SIZE);
- formatFragmentShader((char*)&fbuffer, fragment_shader->c_str());
- fragment_shader->assign((char*)&fbuffer);
- }
- return true;
- }
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader) \ No newline at end of file
diff --git a/src/libjin/Graphics/Shader/je_jsl_compiler.h b/src/libjin/Graphics/Shader/je_jsl_compiler.h
deleted file mode 100644
index 29129e1..0000000
--- a/src/libjin/Graphics/Shader/je_jsl_compiler.h
+++ /dev/null
@@ -1,42 +0,0 @@
-#ifndef __JE_JSL_COMPILER_H
-#define __JE_JSL_COMPILER_H
-
-#include "../../core/je_configuration.h"
-#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
-
-#include <string>
-
-#include "../../common/je_singleton.hpp"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- /// Compile JSL into GLSL.
- ///
- class JSLCompiler : public Singleton<JSLCompiler>
- {
- public:
- ///
- /// Compile JSL shader source into GLSL.
- ///
- /// @param jsl JSL shader source.
- /// @param glsl_vertex Output of vertex glsl shader source.
- /// @param glsl_fragment Output of fragment glsl shader source.
- /// @return True if compile successful, otherwise return false.
- ///
- bool compile(const std::string& jsl, std::string* glsl_vertex, std::string* glsl_fragment);
-
- private:
- singleton(JSLCompiler);
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader)
-
-#endif // __JE_JSL_COMPILER_H \ No newline at end of file
diff --git a/src/libjin/Graphics/Shader/je_shader.cpp b/src/libjin/Graphics/Shader/je_shader.cpp
deleted file mode 100644
index b0e7506..0000000
--- a/src/libjin/Graphics/Shader/je_shader.cpp
+++ /dev/null
@@ -1,278 +0,0 @@
-#include "../../core/je_configuration.h"
-#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
-
-#include <iostream>
-
-#include "../../filesystem/je_buffer.h"
-#include "../../utils/je_macros.h"
-
-#include "je_jsl_compiler.h"
-#include "je_shader.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- using namespace std;
- using namespace JinEngine::Filesystem;
-
- //
- // default_texture
- // base_shader
- // SHADER_FORMAT_SIZE
- // formatShader
- //
- #include "je_default.shader.h"
-
- //
- // https://stackoverflow.com/questions/27941496/use-sampler-without-passing-through-value
- // The default value of a sampler variable is 0. From the GLSL 3.30 spec,
- // section "4.3.5 Uniforms":
- //
- // The link time initial value is either the value of the variable's
- // initializer, if present, or 0 if no initializer is present.Sampler
- // types cannot have initializers.
- //
- // Since a value of 0 means that it's sampling from texture unit 0, it will
- // work without ever setting the value as long as you bind your textures to
- // unit 0. This is well defined behavior.
- //
- // Since texture unit 0 is also the default until you call glActiveTexture()
- // with a value other than GL_TEXTURE0, it's very common to always use unit
- // 0 as long as shaders do not need more than one texture.Which means that
- // often times, setting the sampler uniforms is redundant for simple
- // applications.
- //
- // I would still prefer to always set the values.If nothing else, it makes
- // it clear to anybody reading your code that you really mean to sample from
- // texture unit 0, and did not just forget to set the value.
- //
- const int DEFAULT_TEXTURE_UNIT = 0;
-
- /*static*/ Shader* Shader::CurrentShader = nullptr;
-
- Shader* Shader::createShader(const string& program)
- {
- Shader* shader = nullptr;
- try
- {
- shader = new Shader(program);
- }
- catch(...)
- {
- return nullptr;
- }
- return shader;
- }
-
- Shader::Shader(const string& program)
- : mCurrentTextureUnit(DEFAULT_TEXTURE_UNIT)
- {
- if (!compile(program))
- throw 0;
- }
-
- Shader::~Shader()
- {
- if (CurrentShader == this)
- unuse();
- // delete shader program
- glDeleteShader(mPID);
- }
-
- bool Shader::compile(const string& program)
- {
- string vertex_shader, fragment_shader;
- // Compile JSL shader source into GLSL shader source.
- JSLCompiler* compiler = JSLCompiler::get();
- if (!compiler->compile(program, &vertex_shader, &fragment_shader))
- {
- return false;
- }
-#define glsl(SHADER_MODE, SHADER, SRC) \
-do{ \
-const GLchar* src = SRC.c_str(); \
-glShaderSource(SHADER, 1, &src, NULL); \
-glCompileShader(SHADER); \
-GLint success; \
-glGetShaderiv(SHADER, GL_COMPILE_STATUS, &success); \
-if (success == GL_FALSE) \
- return false; \
-}while(0)
- // Compile vertex shader.
- GLuint vid = glCreateShader(GL_VERTEX_SHADER);
- glsl(GL_VERTEX_SHADER, vid, vertex_shader);
- // Compile fragment shader.
- GLuint fid = glCreateShader(GL_FRAGMENT_SHADER);
- glsl(GL_FRAGMENT_SHADER, fid, fragment_shader);
-#undef glsl
- // Create OpenGL shader program.
- mPID = glCreateProgram();
- glAttachShader(mPID, vid);
- glAttachShader(mPID, fid);
- glLinkProgram(mPID);
- GLint success;
- glGetProgramiv(mPID, GL_LINK_STATUS, &success);
- if (success == GL_FALSE)
- return false;
- }
-
- static inline GLint getMaxTextureUnits()
- {
- GLint maxTextureUnits = 0;
- glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
- return maxTextureUnits;
- }
-
- void Shader::use()
- {
- glUseProgram(mPID);
- CurrentShader = this;
- sendInt(SHADER_MAIN_TEXTURE, DEFAULT_TEXTURE_UNIT);
- }
-
- /*static*/ void Shader::unuse()
- {
- glUseProgram(0);
- CurrentShader = nullptr;
- }
-
- GLint Shader::claimTextureUnit(const std::string& name)
- {
- std::map<std::string, GLint>::iterator unit = mTextureUnits.find(name);
- if (unit != mTextureUnits.end())
- return unit->second;
- static GLint MAX_TEXTURE_UNITS = getMaxTextureUnits();
- if (++mCurrentTextureUnit >= MAX_TEXTURE_UNITS)
- return 0;
- mTextureUnits[name] = mCurrentTextureUnit;
- return mCurrentTextureUnit;
- }
-
- #define checkJSL() \
- if (CurrentShader != this) \
- return
-
- void Shader::sendInt(const char* name, int value)
- {
- checkJSL();
- int loc = glGetUniformLocation(mPID, name);
- glUniform1i(loc, value);
- }
-
- void Shader::sendFloat(const char* variable, float number)
- {
- checkJSL();
- int loc = glGetUniformLocation(mPID, variable);
- glUniform1f(loc, number);
- }
-
- //
- // https://www.douban.com/note/627332677/
- // struct TextureUnit
- // {
- // GLuint targetTexture1D;
- // GLuint targetTexture2D;
- // GLuint targetTexture3D;
- // GLuint targetTextureCube;
- // ...
- // };
- //
- // TextureUnit mTextureUnits[GL_MAX_TEXTURE_IMAGE_UNITS]
- // GLuint mCurrentTextureUnit = 0;
- //
- void Shader::sendTexture(const char* variable, const Texture* tex)
- {
- checkJSL();
- GLint location = glGetUniformLocation(mPID, variable);
- if (location == -1)
- return;
- GLint unit = claimTextureUnit(variable);
- if (unit == 0)
- {
- // TODO: 쳣󶨵
- return;
- }
- gl.activeTexUnit(unit);
- glUniform1i(location, unit);
- gl.bindTexture(tex->getTexture());
- gl.activeTexUnit(0);
- }
-
- void Shader::sendCanvas(const char* variable, const Canvas* canvas)
- {
- checkJSL();
- GLint location = glGetUniformLocation(mPID, variable);
- if (location == -1)
- return;
- GLint unit = claimTextureUnit(variable);
- if (unit == 0)
- {
- // TODO: 쳣󶨵
- return;
- }
- glUniform1i(location, unit);
- glActiveTexture(GL_TEXTURE0 + unit);
- gl.bindTexture(canvas->getTexture());
-
- glActiveTexture(GL_TEXTURE0);
- }
-
- void Shader::sendVec2(const char* name, float x, float y)
- {
- checkJSL();
- int loc = glGetUniformLocation(mPID, name);
- glUniform2f(loc, x, y);
- }
-
- void Shader::sendVec3(const char* name, float x, float y, float z)
- {
- checkJSL();
- int loc = glGetUniformLocation(mPID, name);
- glUniform3f(loc, x, y, z);
- }
-
- void Shader::sendVec4(const char* name, float x, float y, float z, float w)
- {
- checkJSL();
- int loc = glGetUniformLocation(mPID, name);
- glUniform4f(loc, x, y, z, w);
- }
-
- void Shader::sendColor(const char* name, const Color* col)
- {
- checkJSL();
- int loc = glGetUniformLocation(mPID, name);
- glUniform4f(loc,
- col->r / 255.f,
- col->g / 255.f,
- col->b / 255.f,
- col->a / 255.f
- );
- }
-
- void Shader::sendMatrix4(const char* name, const Math::Matrix* mat4)
- {
- int loc = glGetUniformLocation(mPID, name);
- glUniformMatrix4fv(loc, 1, GL_FALSE, mat4->getElements());
- }
-
- void Shader::bindVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers)
- {
- GLint loc = glGetAttribLocation(mPID, SHADER_VERTEX_COORDS);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers);
- }
-
- void Shader::bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers)
- {
- GLint loc = glGetAttribLocation(mPID, SHADER_TEXTURE_COORDS);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers);
- }
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader) \ No newline at end of file
diff --git a/src/libjin/Graphics/Shader/je_shader.h b/src/libjin/Graphics/Shader/je_shader.h
deleted file mode 100644
index 928fb0a..0000000
--- a/src/libjin/Graphics/Shader/je_shader.h
+++ /dev/null
@@ -1,200 +0,0 @@
-#ifndef __JE_SHADER_H
-#define __JE_SHADER_H
-
-#include "../../core/je_configuration.h"
-#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
-
-#include <string>
-#include <map>
-
-#include "../../3rdparty/GLee/GLee.h"
-
-#include "../je_color.h"
-#include "../je_texture.h"
-#include "../je_canvas.h"
-
-#include "je_base.shader.h"
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- /// Built in shader program.
- ///
- /// Built in shader program written with custom shading language called JSL (jin
- /// shading language). A JSL program is compiled into glsl, so most glsl built in
- /// functions and structs are available in JSL.
- ///
- class Shader
- {
- public:
- ///
- /// Create shader program from source code.
- ///
- /// @param source The shader source code.
- ///
- static Shader* createShader(const std::string& source);
-
- ///
- /// Get current shader.
- ///
- /// @return Current used shader program.
- ///
- static inline Shader* getCurrentShader() { return CurrentShader; }
-
- ///
- /// Unuse current shader.
- ///
- static void unuse();
-
- ///
- /// Destructor of shader.
- ///
- virtual ~Shader();
-
- ///
- /// Use specific shader.
- ///
- void use();
-
- ///
- /// Send float value to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param number Value of uniform variable to be sent.
- ///
- void sendFloat(const char* name, float number);
-
- ///
- /// Send texture to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param texture Texture to be sent.
- ///
- void sendTexture(const char* name, const Texture* texture);
-
- ///
- /// Send integer value to shader
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param value Value to be sent.
- ///
- void sendInt(const char* name, int value);
-
- ///
- /// Send 2D vector to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param x X value of the vector to be sent.
- /// @param y Y value of the vector to be sent.
- ///
- void sendVec2(const char* name, float x, float y);
-
- ///
- /// Send 3D vector to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param x X value of the vector to be sent.
- /// @param y Y value of the vector to be sent.
- /// @param z Z value of the vector to be sent.
- ///
- void sendVec3(const char* name, float x, float y, float z);
-
- ///
- /// Send 4D vector to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param x X value of the vector to be sent.
- /// @param y Y value of the vector to be sent.
- /// @param z Z value of the vector to be sent.
- /// @param w W value of the vector to be sent.
- ///
- void sendVec4(const char* name, float x, float y, float z, float w);
-
- ///
- /// Send canvas to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param canvas Canvas to be sent.
- ///
- void sendCanvas(const char* name, const Canvas* canvas);
-
- ///
- /// Send color to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param color Color to be sent.
- ///
- void sendColor(const char* name, const Color* color);
-
- ///
- /// Send 4 by 4 matrix to shader.
- ///
- /// @param name Name of the uniform variable to be assigned.
- /// @param mat4 Matrix to be sent.
- ///
- void sendMatrix4(const char* name, const Math::Matrix* mat4);
-
- ///
- /// Set vertices value.
- ///
- /// @param n Number of vertices.
- /// @param type Data type of each component in the array.
- /// @param stride Byte offset between consecutive generic vertex attributes.
- /// @param pointers Pointer to the first component of the first generic vertex
- /// attribute in the array.
- ///
- void bindVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers);
-
- ///
- /// Set texture UV coordinates.
- ///
- /// @param n Number of vertices.
- /// @param type Data type of each component in the array.
- /// @param stride Byte offset between consecutive generic vertex attributes.
- /// @param pointers Pointer to the first component of the first generic vertex
- /// attribute in the array.
- ///
- void bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers);
-
- protected:
- ///
- /// Reference of current used shader.
- ///
- static Shader* CurrentShader;
-
- ///
- /// Get texture unit of the uniform texture. If not, assign one.
- ///
- /// @param name Name of the texture uniform variable.
- /// @return Texture unit which texture variable be assigned.
- ///
- GLint claimTextureUnit(const std::string& name);
-
- ///
- /// Shader constructor.
- ///
- Shader(const std::string& program);
-
- ///
- /// Compile JSL program into GLSL source.
- ///
- /// @param program JSL source code.
- /// @return Return true if compile successed, otherwise return false.
- ///
- bool compile(const std::string& program);
-
- GLuint mPID;
- GLint mCurrentTextureUnit;
- std::map<std::string, GLint> mTextureUnits;
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader)
-
-#endif // __JE_SHADER_H \ No newline at end of file
diff --git a/src/libjin/Graphics/animation/je_animation.cpp b/src/libjin/Graphics/animation/je_animation.cpp
deleted file mode 100644
index e69de29..0000000
--- a/src/libjin/Graphics/animation/je_animation.cpp
+++ /dev/null
diff --git a/src/libjin/Graphics/animation/je_animation.h b/src/libjin/Graphics/animation/je_animation.h
deleted file mode 100644
index f330a0c..0000000
--- a/src/libjin/Graphics/animation/je_animation.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#ifndef __JE_ANIMATION_H
-#define __JE_ANIMATION_H
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- ///
- ///
- class Animation
- {
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/animation/je_clip.cpp b/src/libjin/Graphics/animation/je_clip.cpp
deleted file mode 100644
index e69de29..0000000
--- a/src/libjin/Graphics/animation/je_clip.cpp
+++ /dev/null
diff --git a/src/libjin/Graphics/animation/je_clip.h b/src/libjin/Graphics/animation/je_clip.h
deleted file mode 100644
index d6709dc..0000000
--- a/src/libjin/Graphics/animation/je_clip.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#ifndef __JE_CLIP_H
-#define __JE_CLIP_H
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- /// Animation clip with key.
- ///
- class Clip
- {
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/animations/je_animation.cpp b/src/libjin/Graphics/animations/je_animation.cpp
new file mode 100644
index 0000000..4fe673a
--- /dev/null
+++ b/src/libjin/Graphics/animations/je_animation.cpp
@@ -0,0 +1,14 @@
+#include "je_animation.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Animations
+ {
+
+
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/animations/je_animation.h b/src/libjin/Graphics/animations/je_animation.h
new file mode 100644
index 0000000..9926cf9
--- /dev/null
+++ b/src/libjin/Graphics/animations/je_animation.h
@@ -0,0 +1,64 @@
+#ifndef __JE_ANIMATION_H__
+#define __JE_ANIMATION_H__
+
+#include <vector>
+#include <string>
+
+#include "../je_sprite.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Animations
+ {
+
+ ///
+ /// Animation clip with key.
+ ///
+ class Animation
+ {
+ public:
+ void update(float dt);
+
+ void start();
+ void pause();
+ void stop();
+ void rewind();
+ void setSpeed(float speed);
+
+ ///
+ /// Get current frame index.
+ ///
+ uint getCurrentFrameIndex();
+
+ ///
+ ///
+ ///
+ Sprite* getCurrentFrame();
+
+ ///
+ /// Set current frame index.
+ ///
+ /// @param frame Current frame to play.
+ ///
+ void setCurrentFrame(uint frame);
+
+ private:
+ ///
+ /// Key frames.
+ ///
+ std::vector<Sprite*> mFrames;
+
+ ///
+ /// Animation playing speed.
+ ///
+ float mSpeed;
+
+ };
+
+ } // namespace Animations
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/animations/je_animator.cpp b/src/libjin/Graphics/animations/je_animator.cpp
new file mode 100644
index 0000000..360bd5d
--- /dev/null
+++ b/src/libjin/Graphics/animations/je_animator.cpp
@@ -0,0 +1,14 @@
+#include "je_animator.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Animations
+ {
+
+
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/animations/je_animator.h b/src/libjin/Graphics/animations/je_animator.h
new file mode 100644
index 0000000..6510a7d
--- /dev/null
+++ b/src/libjin/Graphics/animations/je_animator.h
@@ -0,0 +1,50 @@
+#ifndef __JE_ANIMATOR_H__
+#define __JE_ANIMATOR_H__
+
+#include <map>
+#include <vector>
+#include <string>
+
+#include "je_animation.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Animations
+ {
+
+ ///
+ ///
+ ///
+ class Animator
+ {
+ public:
+ void addAnimation(const std::string& key, Animation* clip);
+ bool hasKey(const std::string& key);
+
+ void play();
+ void switchAnimation(const std::string& key);
+
+ ///
+ /// Control clips.
+ ///
+ void stopAnimation();
+ void pauseAnimation();
+ void rewindAnimation();
+ void startAnimation();
+
+ private:
+ ///
+ /// Map a key to clips.
+ ///
+ std::map<std::string, Animation*> mAnimations;
+ Animation* mCurrentAnimation;
+
+ };
+
+ } // namespace Animations
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_decoder.cpp b/src/libjin/Graphics/fonts/je_decoder.cpp
new file mode 100644
index 0000000..02112a0
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_decoder.cpp
@@ -0,0 +1,96 @@
+#include <stdlib.h>
+#include <string.h>
+#include "je_decoder.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ /* utf8 byte string to unicode codepoint */
+ static const char *utf8toCodepoint(const char *p, unsigned *res) {
+ return nullptr;
+
+ }
+
+ /////////////////////////////////////////////////////////////////////////////
+ // decoders
+ /////////////////////////////////////////////////////////////////////////////
+
+ const void* Utf8::decode(const void* data, Codepoint* res) const
+ {
+ const char* p = (char*)data;
+ unsigned x, mask, shift;
+ switch (*p & 0xf0) {
+ case 0xf0: mask = 0x07; shift = 18; break;
+ case 0xe0: mask = 0x0f; shift = 12; break;
+ case 0xc0:
+ case 0xd0: mask = 0x1f; shift = 6; break;
+ default:
+ *res = *p;
+ return p + 1;
+ }
+ x = (*p & mask) << shift;
+ do {
+ if (*(++p) == '\0') {
+ *res = x;
+ return p;
+ }
+ shift -= 6;
+ x |= (*p & 0x3f) << shift;
+ } while (shift);
+ *res = x;
+ return p + 1;
+ }
+
+ const void* Utf8::next(const void* data) const
+ {
+ const char* p = (char*)data;
+ unsigned x, mask, shift;
+ switch (*p & 0xf0) {
+ case 0xf0: mask = 0x07; shift = 18; break;
+ case 0xe0: mask = 0x0f; shift = 12; break;
+ case 0xc0:
+ case 0xd0: mask = 0x1f; shift = 6; break;
+ default:
+ return p + 1;
+ }
+ x = (*p & mask) << shift;
+ do {
+ if (*(++p) == '\0') {
+ return p;
+ }
+ shift -= 6;
+ x |= (*p & 0x3f) << shift;
+ } while (shift);
+ return p + 1;
+ }
+ /*
+ const void* Utf16::decode(const void* data, Codepoint* res) const
+ {
+ return nullptr;
+ }
+
+ const void* Utf16::next(const void* data) const
+ {
+ return nullptr;
+ }
+ */
+ const void* Ascii::decode(const void* data, Codepoint* res) const
+ {
+ const char* p = (char*)data;
+ *res = *p;
+ return p + 1;
+ }
+
+ const void* Ascii::next(const void* data) const
+ {
+ const char* p = (char*)data;
+ return p + 1;
+ }
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_decoder.h b/src/libjin/Graphics/fonts/je_decoder.h
new file mode 100644
index 0000000..840cada
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_decoder.h
@@ -0,0 +1,97 @@
+#ifndef __JE_UTF8_H__
+#define __JE_UTF8_H__
+
+#include <vector>
+
+#include "je_text.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ ///
+ /// Text decoder.
+ ///
+ class Decoder
+ {
+ public:
+
+ ///
+ /// Decode a code unit.
+ ///
+ /// @param data Code units.
+ /// @param codepoint Value of code point.
+ /// @return Next code unit location.
+ ///
+ virtual const void* decode(const void* data, Codepoint* codepoint) const = 0;
+
+ ///
+ /// Get next code unit location.
+ ///
+ /// @param data Code units.
+ /// @return Next code unit location.
+ ///
+ virtual const void* next(const void* data) const = 0;
+
+ };
+
+ ///
+ /// Utf-8 decoder.
+ ///
+ class Utf8 : public Decoder
+ {
+ public:
+
+ ///
+ /// Decode a code unit.
+ ///
+ /// @param data Code units.
+ /// @param codepoint Value of code point.
+ /// @return Next code unit location.
+ ///
+ const void* decode(const void* data, Codepoint* codepoint) const override;
+
+ ///
+ /// Get next code unit location.
+ ///
+ /// @param data Code units.
+ /// @return Next code unit location.
+ ///
+ const void* next(const void* data) const override;
+
+ };
+
+ ///
+ /// Ascii decoder.
+ ///
+ class Ascii : public Decoder
+ {
+ public:
+
+ ///
+ /// Decode a code unit.
+ ///
+ /// @param data Code units.
+ /// @param codepoint Value of code point.
+ /// @return Next code unit location.
+ ///
+ const void* decode(const void* data, Codepoint* codepoint) const override;
+
+ ///
+ /// Get next code unit location.
+ ///
+ /// @param data Code units.
+ /// @return Next code unit location.
+ ///
+ const void* next(const void* data) const override;
+
+ };
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_font.h b/src/libjin/Graphics/fonts/je_font.h
new file mode 100644
index 0000000..9581b9f
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_font.h
@@ -0,0 +1,109 @@
+#ifndef __JE_FONT_H__
+#define __JE_FONT_H__
+
+#include <vector>
+#include "je_text.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ struct Page;
+
+ //
+ // Font
+ // |- TTF
+ // |- TextureFont
+ //
+
+ ///
+ /// Base Font class.
+ ///
+ class Font
+ {
+ public:
+ ///
+ /// Font constructor.
+ ///
+ Font(unsigned fontsize)
+ : mFontSize(fontsize)
+ {
+ }
+
+ ///
+ /// Font destructor.
+ ///
+ virtual ~Font() {};
+
+ ///
+ /// Create page with given text.
+ ///
+ /// @param text Text to be typesetted.
+ /// @param lineheight Line height of text.
+ /// @param spacing Spacing between characters. 0 by default.
+ /// @return Page if created successfully, otherwise return null.
+ ///
+ virtual Page* typeset(const Text& text, int lineheight, int spacing = 0) = 0;
+
+ ///
+ /// Create page with given unicode codepoints.
+ ///
+ /// @param content Unicode codepoints to be typesetted.
+ /// @param lineheight Line height of text.
+ /// @param spacing Spacing between characters. 0 by default.
+ /// @return Page if created successfully, otherwise return null.
+ ///
+ virtual Page* typeset(const Content& content, int lineheight, int spacing = 0) = 0;
+
+ ///
+ /// Render page to given position.
+ ///
+ /// @param page Page to be rendered.
+ /// @param x X value of the position.
+ /// @param y Y value of the position.
+ ///
+ virtual void render(const Page* page, int x, int y) = 0;
+
+ ///
+ /// Render unicode codepoints to given position.
+ ///
+ /// @param content Unicode codepoints to be typesetted.
+ /// @param x X value of the position.
+ /// @param y Y value of the position.
+ /// @param lineheight Line height of the content.
+ /// @param spacing Spacing between characters.
+ ///
+ virtual void render(const Content& content, int x, int y, int lineheight, int spacing = 0) = 0;
+
+ ///
+ /// Render text to given position.
+ ///
+ /// @param text Text to be rendered.
+ /// @param x X value of the position.
+ /// @param y Y value of the position.
+ /// @param lineheight Line height of the text.
+ /// @param spacing Spacing between characters.
+ ///
+ virtual void render(const Text& text, int x, int y, int lineheight, int spacing = 0) = 0;
+
+ ///
+ /// Get font size.
+ ///
+ /// @return Font size.
+ ///
+ inline unsigned getFontSize() { return mFontSize; };
+
+ protected:
+
+ unsigned mFontSize;
+
+ };
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // __JE_FONT_H__
diff --git a/src/libjin/Graphics/fonts/je_page.h b/src/libjin/Graphics/fonts/je_page.h
new file mode 100644
index 0000000..707f53a
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_page.h
@@ -0,0 +1,54 @@
+#ifndef __JE_PAGE_H__
+#define __JE_PAGE_H__
+
+#include "../../math/je_vector2.hpp"
+
+#include "je_font.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ class Font;
+
+ ///
+ /// Glyphs data to be rendered.
+ ///
+ struct GlyphVertex
+ {
+ int x, y; ///< screen coordinates
+ float u, v; ///< normalized texture uv
+ };
+
+ ///
+ /// Glyphs info for reducing draw call.
+ ///
+ struct GlyphArrayDrawInfo
+ {
+ GLuint texture; ///< atlas
+ unsigned int start; ///< glyph vertex indecies
+ unsigned int count; ///< glyph vertex count
+ };
+
+ ///
+ /// Page to be rendered.
+ ///
+ /// A page is a pre-rendered text struct for reducing draw call. Each page
+ /// keeps a font pointer which should not be changed.
+ ///
+ struct Page
+ {
+ Font* font;
+ std::vector<GlyphArrayDrawInfo> glyphinfolist;
+ std::vector<GlyphVertex> glyphvertices;
+ Math::Vector2<int> size;
+ };
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // __JE_PAGE_H__
diff --git a/src/libjin/Graphics/fonts/je_text.cpp b/src/libjin/Graphics/fonts/je_text.cpp
new file mode 100644
index 0000000..80aaa6a
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_text.cpp
@@ -0,0 +1,157 @@
+#include <cstring>
+
+#include "je_text.h"
+#include "je_decoder.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ /////////////////////////////////////////////////////////////////////////////
+ // iterator
+ /////////////////////////////////////////////////////////////////////////////
+
+ Text::Iterator::Iterator(const Iterator& itor)
+ : data(itor.data)
+ , p(itor.p)
+ , encode(itor.encode)
+ , length(itor.length)
+ {
+ switch (encode)
+ {
+ case Encode::UTF8: decoder = new Utf8(); break;
+ case Encode::ASCII: decoder = new Ascii(); break;
+ }
+ }
+
+ Text::Iterator::Iterator(const Encode& _encode, const void* _data, unsigned int _length)
+ : data(_data)
+ , p(_data)
+ , encode(_encode)
+ , length(_length)
+ {
+ switch (encode)
+ {
+ case Encode::UTF8: decoder = new Utf8(); break;
+ case Encode::ASCII: decoder = new Ascii(); break;
+ }
+ }
+
+ Text::Iterator::~Iterator()
+ {
+ delete decoder;
+ }
+
+ Codepoint Text::Iterator::get()
+ {
+ Codepoint codepoint;
+ decoder->decode(p, &codepoint);
+ return codepoint;
+ }
+
+ Codepoint Text::Iterator::operator*()
+ {
+ return get();
+ }
+ /*
+ Text::Iterator Text::Iterator::begin()
+ {
+ Iterator itor(encode, data, length);
+ itor.toBegin();
+ return itor;
+ }
+
+ Text::Iterator Text::Iterator::end()
+ {
+ Iterator itor(encode, data, length);
+ itor.toEnd();
+ return itor;
+ }
+ */
+ void Text::Iterator::toBegin()
+ {
+ p = (const unsigned char*)data;
+ }
+
+ void Text::Iterator::toEnd()
+ {
+ p = (const unsigned char*)data + length;
+ }
+
+ Text::Iterator& Text::Iterator::operator ++()
+ {
+ p = decoder->next(p);
+ return *this;
+ }
+
+ Text::Iterator Text::Iterator::operator ++(int)
+ {
+ p = decoder->next(p);
+ Iterator itor(encode, data, length);
+ itor.p = p;
+ return itor;
+ }
+
+ bool Text::Iterator::operator !=(const Iterator& itor)
+ {
+ return !(data == itor.data
+ && p == itor.p
+ && length == itor.length
+ && encode == itor.encode);
+ }
+
+ bool Text::Iterator::operator ==(const Iterator& itor)
+ {
+ return data == itor.data
+ && p == itor.p
+ && length == itor.length
+ && encode == itor.encode;
+ }
+
+ /////////////////////////////////////////////////////////////////////////////
+ // text
+ /////////////////////////////////////////////////////////////////////////////
+
+ Text::Text(Encode encode, const void* data)
+ {
+ unsigned length = strlen((const char*)data);
+ Iterator end = Iterator(encode, data, length);
+ end.toEnd();
+ Iterator it = Iterator(encode, data, length);
+ for (; it != end; ++it)
+ {
+ content.push_back(*it);
+ }
+ }
+
+ Text::Text(Encode encode, const void* data, unsigned length)
+ {
+ Iterator end = Iterator(encode, data, length);
+ end.toEnd();
+ Iterator it = Iterator(encode, data, length);
+ for (; it != end; ++it)
+ {
+ content.push_back(*it);
+ }
+ }
+
+ Text::~Text()
+ {
+ }
+
+ const Content& Text::getContent() const
+ {
+ return content;
+ }
+
+ const Content& Text::operator*() const
+ {
+ return content;
+ }
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_text.h b/src/libjin/Graphics/fonts/je_text.h
new file mode 100644
index 0000000..6e6f8b0
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_text.h
@@ -0,0 +1,172 @@
+#ifndef __JE_TEXT_H__
+#define __JE_TEXT_H__
+
+#include <vector>
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ typedef unsigned int Codepoint;
+
+ typedef std::vector<Codepoint> Content;
+
+ class Text;
+
+ class Decoder;
+
+ ///
+ /// Supported text encoding.
+ ///
+ enum Encode
+ {
+ UTF8, ///< utf-8
+ ASCII, ///< ASCII
+ };
+
+ ///
+ /// Decoded text. Saved as unicode codepoints.
+ ///
+ class Text
+ {
+ public:
+ ///
+ ///
+ ///
+ Text(Encode encode, const void* data);
+
+ ///
+ ///
+ ///
+ Text(Encode encode, const void* data, unsigned int length);
+
+ ///
+ ///
+ ///
+ ~Text();
+
+ ///
+ ///
+ ///
+ const Content& getContent() const;
+
+ ///
+ ///
+ ///
+ const Content& operator*() const;
+
+ private:
+ ///
+ ///
+ ///
+ class Iterator
+ {
+ public:
+
+ ///
+ ///
+ ///
+ Iterator(const Iterator& itor);
+
+ ///
+ ///
+ ///
+ Iterator(const Encode& encode, const void* data, unsigned int length);
+
+ ///
+ ///
+ ///
+ ~Iterator();
+
+ ///
+ ///
+ ///
+ Codepoint get();
+
+ //Iterator begin();
+ //Iterator end();
+
+ ///
+ ///
+ ///
+ void toBegin();
+
+ ///
+ ///
+ ///
+ void toEnd();
+
+ ///
+ ///
+ ///
+ Codepoint operator *();
+
+ ///
+ ///
+ ///
+ Iterator& operator ++();
+
+ ///
+ ///
+ ///
+ Iterator operator ++(int);
+
+ ///
+ ///
+ ///
+ bool operator !=(const Iterator& itor);
+
+ ///
+ ///
+ ///
+ bool operator ==(const Iterator& itor);
+
+ private:
+
+ ///
+ ///
+ ///
+ void operator = (const Iterator&);
+
+ ///
+ ///
+ ///
+ const Encode encode;
+
+ ///
+ ///
+ ///
+ const Decoder* decoder;
+
+ ///
+ ///
+ ///
+ const void* p;
+
+ ///
+ ///
+ ///
+ const void* const data;
+
+ ///
+ ///
+ ///
+ unsigned int length;
+
+ };
+
+ ///
+ ///
+ ///
+ Content content;
+
+ };
+
+ }
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_texture_font.cpp b/src/libjin/Graphics/fonts/je_texture_font.cpp
new file mode 100644
index 0000000..0d9b882
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_texture_font.cpp
@@ -0,0 +1,322 @@
+#include <vector>
+
+#include "../../math/je_vector2.hpp"
+
+#include "../shaders/je_shader.h"
+
+#include "je_texture_font.h"
+
+using namespace std;
+using namespace JinEngine::Math;
+using namespace JinEngine::Graphics::Shaders;
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ TextureFont * TextureFont::createTextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh)
+ {
+ TextureFont* tf = new TextureFont(bitmap, codepoints, cellw, cellh);
+ return tf;
+ }
+
+ TextureFont * TextureFont::createTextureFont(const Bitmap* bitmap, const Text& codepoints, int cellw, int cellh)
+ {
+ TextureFont* tf = new TextureFont(bitmap, *codepoints, cellw, cellh);
+ return tf;
+ }
+
+ 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::createTextureFont(const Bitmap* bitmap, const Text& codepoints, Color mask, int cellh)
+ {
+ TextureFont* tf = new TextureFont(bitmap, *codepoints, mask, cellh);
+ return tf;
+ }
+
+ TextureFont::~TextureFont()
+ {
+ }
+
+ const TextureFont::TextureGlyph* TextureFont::findGlyph(Codepoint codepoint) const
+ {
+ auto it = glyphs.find(codepoint);
+ if (it != glyphs.end())
+ {
+ return &it->second;
+ }
+ else
+ return nullptr;
+ }
+
+ Page* TextureFont::typeset(const Content& text, int lineheight, int spacing)
+ {
+ Page* page = new Page();
+ page->font = this;
+ vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
+ vector<GlyphVertex>& glyphvertices = page->glyphvertices;
+ int texture = -1;
+ const TextureGlyph* glyph = nullptr;
+ GlyphVertex vertex;
+ Vector2<int> p(0, 0);
+ int i = 0;
+
+#define glyphvertices_push(_x, _y, _u, _v) \
+ vertex.x = _x; vertex.y = _y;\
+ vertex.u = _u; vertex.v = _v;\
+ glyphvertices.push_back(vertex);\
+
+ for (Codepoint c : text)
+ {
+ // return
+ if (c == 0x0D) continue;
+ // newline
+ if (c == 0x0A)
+ {
+ p.y += lineheight;
+ p.x = 0;
+ continue;
+ }
+ if (c == 0x09)
+ {
+ // tab = 4*space
+ unsigned cw = getCharWidth(0x20);
+ p.x += cw * 4;
+ continue;
+ }
+ glyph = findGlyph(c);
+ if (glyph == nullptr) continue;
+ if (texture != getGLTexture())
+ {
+ texture = getGLTexture();
+ GlyphArrayDrawInfo info;
+ info.start = i;
+ info.count = 0;
+ info.texture = texture;
+ glyphinfolist.push_back(info);
+ }
+ glyphinfolist[glyphinfolist.size() - 1].count += 4;
+ // normalized
+ float w = getWidth(), h = getHeight();
+ float nx = glyph->x / w, ny = glyph->y / h;
+ float nw = glyph->w / w, nh = glyph->h / h;
+ glyphvertices_push(p.x, p.y, nx, ny);
+ glyphvertices_push(p.x, p.y + glyph->h, nx, ny + nh);
+ glyphvertices_push(p.x + glyph->w, p.y + glyph->h, nx + nw, ny + nh);
+ glyphvertices_push(p.x + glyph->w, p.y, nx + nw, ny);
+ p.x += glyph->w + spacing;
+ i += 4;
+ }
+ getTextBox(text, &page->size.w, &page->size.h, lineheight, spacing);
+ return page;
+ }
+
+ int TextureFont::getCharWidth(int c)
+ {
+ auto it = glyphs.find(c);
+ if (it != glyphs.end())
+ {
+ return it->second.w;
+ }
+ return 0;
+ }
+
+ int TextureFont::getCharHeight(int c)
+ {
+ auto it = glyphs.find(c);
+ if (it != glyphs.end())
+ {
+ return it->second.h;
+ }
+ return 0;
+ }
+
+ int TextureFont::getTextWidth(const Content& t, int spacing)
+ {
+ int res = 0;
+ int tmp = 0;
+ for (Codepoint c : t)
+ {
+ if (c == 0x0D)
+ continue;
+ if (c == 0x0A)
+ {
+ tmp = 0;
+ continue;
+ }
+ if (c == 0x09)
+ {
+ // tab = 4*space
+ unsigned cw = getCharWidth(0x20);
+ tmp += cw * 4;
+ if (tmp > res) res = tmp;
+ continue;
+ }
+ tmp += getCharWidth(c) + spacing;
+ if (tmp > res) res = tmp;
+ }
+ return res;
+ }
+
+ int TextureFont::getTextHeight(const Content& t, int lineheight)
+ {
+ int res = 0;
+ bool newline = true;
+ for (Codepoint c : t)
+ {
+ if (c == 0x0A)
+ newline = true;
+ else if (c == 0x0D);
+ else if (newline)
+ {
+ newline = false;
+ res += lineheight;
+ }
+ }
+ return res;
+ }
+
+ void TextureFont::getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing)
+ {
+ *w = 0;
+ *h = 0;
+ int tmp = 0;
+ bool newline = true;
+ for (Codepoint c : text)
+ {
+ if (c == 0x0D)
+ continue;
+ if (c == 0x0A)
+ {
+ tmp = 0;
+ newline = true;
+ continue;
+ }
+ else if (newline)
+ {
+ newline = false;
+ *h += lineheight;
+ }
+ tmp += getCharWidth(c) + spacing;
+ if (tmp > *w)
+ *w = tmp;
+ }
+ }
+
+ Page* TextureFont::typeset(const Text& text, int lineheight, int spacing)
+ {
+ return typeset(*text, lineheight, spacing);
+ }
+
+ void TextureFont::render(const Page* page, int x, int y)
+ {
+ Shader* shader = Shader::getCurrentShader();
+ const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
+ const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
+ gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
+ shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
+ for (int i = 0; i < glyphinfolist.size(); ++i)
+ {
+ const GlyphArrayDrawInfo& info = glyphinfolist[i];
+ shader->setVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x);
+ shader->setUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u);
+ gl.bindTexture(info.texture);
+ gl.drawArrays(GL_QUADS, 0, info.count);
+ gl.bindTexture(0);
+ }
+ }
+
+ void TextureFont::render(const Content& text, int x, int y, int lineheight, int spacing)
+ {
+ Page* page = typeset(text, lineheight, spacing);
+ render(page, x, y);
+ delete page;
+ }
+
+ void TextureFont::render(const Text& text, int x, int y, int lineheight, int spacing)
+ {
+ Page* page = typeset(text, lineheight, spacing);
+ render(page, x, y);
+ delete page;
+ }
+
+ TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh)
+ : Graphic(bitmap)
+ , Font(cellh)
+ {
+ TextureGlyph glyph;
+ Vector2<int> count(bitmap->getWidth() / cellw, bitmap->getHeight() / cellh);
+ glyph.w = cellw;
+ glyph.h = cellh;
+ for (int y = 0; y < count.row; ++y)
+ {
+ glyph.y = y * cellh;
+ for (int x = 0; x < count.colum; ++x)
+ {
+ glyph.x = x * cellw;
+ if (x + y * count.colum >= codepoints.size())
+ return;
+ glyphs.insert(std::pair<Codepoint, TextureGlyph>(codepoints[x + y * count.colum], glyph));
+ }
+ }
+ }
+
+ TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh)
+ : Graphic(bitmap)
+ , Font(cellh)
+ {
+ TextureGlyph glyph;
+ glyph.h = cellh;
+ int w = bitmap->getWidth();
+ int h = bitmap->getHeight();
+ int i = 0;
+ for (int y = 0; y < h; y += cellh)
+ {
+ glyph.y = y;
+ bool newc = false;
+ for (int x = 0; x <= w; ++x)
+ {
+ if (x == w && newc)
+ {
+ glyph.w = x - glyph.x;
+ if (i >= codepoints.size())
+ return;
+ glyphs.insert(std::pair<Codepoint, TextureGlyph>(codepoints[i], glyph));
+ ++i;
+ newc = false;
+ break;
+ }
+ Color c = bitmap->getPixels()[x + y * w];
+ if (!newc && c != mask)
+ {
+ glyph.x = x;
+ newc = true;
+ }
+ else if (newc && c == mask)
+ {
+ glyph.w = x - glyph.x;
+ if (i >= codepoints.size())
+ return;
+ glyphs.insert(std::pair<Codepoint, TextureGlyph>(codepoints[i], glyph));
+ if (codepoints[i] == 't')
+ {
+ int a = 10;
+ }
+ ++i;
+ newc = false;
+ }
+ }
+ }
+ }
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_texture_font.h b/src/libjin/Graphics/fonts/je_texture_font.h
new file mode 100644
index 0000000..8a50699
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_texture_font.h
@@ -0,0 +1,144 @@
+#ifndef __JE_TEXTURE_FONT_H__
+#define __JE_TEXTURE_FONT_H__
+
+#include <map>
+#include <vector>
+
+#include "../../math/je_vector4.hpp"
+
+#include "../je_graphic.h"
+#include "../je_bitmap.h"
+
+#include "je_page.h"
+#include "je_font.h"
+#include "je_text.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ ///
+ ///
+ ///
+ class TextureFont
+ : public Font
+ , public Graphic
+ {
+ 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 render(const Page* page, int x, int y) override;
+
+ ///
+ ///
+ ///
+ void render(const Content& text, int x, int y, int linehgiht, int spacing = 0) override;
+
+ ///
+ ///
+ ///
+ void render(const Text& text, int x, int y, int lineheight, int spacing = 0)override;
+
+ private:
+
+ ///
+ ///
+ ///
+ struct TextureGlyph
+ {
+ float 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);
+
+ ///
+ ///
+ ///
+ int getCharWidth(int c);
+
+ ///
+ ///
+ ///
+ int getCharHeight(int c);
+
+ ///
+ ///
+ ///
+ 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);
+
+ ///
+ ///
+ ///
+ const TextureGlyph* findGlyph(Codepoint codepoint) const;
+
+
+ ///
+ ///
+ ///
+ std::map<Codepoint, TextureGlyph> glyphs;
+
+ };
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_ttf.cpp b/src/libjin/Graphics/fonts/je_ttf.cpp
new file mode 100644
index 0000000..9994666
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_ttf.cpp
@@ -0,0 +1,468 @@
+#include "../../core/je_configuration.h"
+#if defined(jin_graphics)
+
+#include <stdio.h>
+
+#include "../../common/je_array.hpp"
+
+#include "../je_gl.h"
+#include "../je_color.h"
+#include "../shaders/je_shader.h"
+
+#include "je_ttf.h"
+#include "je_page.h"
+
+#define STB_TRUETYPE_IMPLEMENTATION
+#include "stb/stb_truetype.h"
+
+using namespace JinEngine::Graphics::Shaders;
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ // TTFData
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ TTFData* TTFData::createTTFData(const unsigned char* data, unsigned int size)
+ {
+ TTFData* ttf = nullptr;
+ try
+ {
+ ttf = new TTFData(data, size);
+ return ttf;
+ }
+ catch (...)
+ {
+ return nullptr;
+ }
+ }
+
+ TTFData::TTFData(const unsigned char* d, unsigned int s)
+ {
+ raw.size = s;
+ raw.data = (unsigned char*)malloc(s);
+ memcpy(raw.data, d, s);
+ if (!stbtt_InitFont(&info, (const unsigned char*)raw.data, 0))
+ {
+ delete raw.data;
+ throw 0;
+ }
+ /* push default fontsize */
+ pushTTFsize(FONT_SIZE);
+ }
+
+ TTFData::~TTFData()
+ {
+ free(raw.data);
+ }
+
+ TTF* TTFData::createTTF(unsigned fontSize)
+ {
+ TTF* ttf;
+ try
+ {
+ ttf = new TTF(this, fontSize);
+ }
+ catch (...)
+ {
+ return nullptr;
+ }
+ return ttf;
+ }
+
+ /*
+ * (0, 0)
+ * +--------------+ ascent
+ * | +--------+ |
+ * | | | |
+ * | | bitmap | |
+ * +--|--------|--+ baseline
+ * | +--------+ |
+ * +--|-----------+ decent
+ * | |
+ * leftSideBearing |
+ * advanceWidth
+ */
+ void TTFData::getVMetrics(int* baseline, int* descent)
+ {
+ float scale = scales.back();
+ int ascent;
+ stbtt_GetFontVMetrics(&info, &ascent, descent, 0);
+ *baseline = (int)(ascent*scale) + 1; // slight adjustment
+ *descent = *baseline - (int)(*descent*scale) + 1;
+ }
+
+ void TTFData::getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing)
+ {
+ float scale = scales.back();
+ int adw, lsb;
+ stbtt_GetCodepointHMetrics(&info, codepoint, &adw, &lsb);
+ *advanceWidth = (int)(adw*scale);
+ *leftSideBearing = (int)(lsb*scale);
+ }
+
+ void TTFData::pushTTFsize(unsigned int fs)
+ {
+ float sc = stbtt_ScaleForPixelHeight(&info, fs);
+ scales.push_back(sc);
+ }
+
+ void TTFData::popTTFsize()
+ {
+ /* always keep default ttf size on the bottom of stack */
+ if (scales.size() > 1)
+ scales.pop_back();
+ }
+
+ Channel* TTFData::getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const
+ {
+ float scale = scales.back();
+ Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff);
+ return bitmap;
+ }
+
+ Color* TTFData::getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const
+ {
+ float scale = scales.back();
+ Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff);
+ int w = *width, h = *height;
+ //int xo = *xoff, yo = *yoff;
+ Color* bitmap32 = new Color[w*h];
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ bitmap32[x + y * w].set(0xff, 0xff, 0xff, bitmap[x + y * w]);
+ }
+ }
+ free(bitmap);
+ return bitmap32;
+ }
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ // TTF
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#include "../shaders/built-in/je_font.shader.h"
+
+ using namespace std;
+ using namespace JinEngine::Math;
+
+ const int TTF::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 };
+ const int TTF::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 };
+
+ /* little endian unicode */
+ static const char* unicodeLittleEndian(const char* p, unsigned* res)
+ {
+ }
+
+ ///*static*/ TTF* TTF::createTTF(TTFData* fontData, unsigned int fontSzie)
+ //{
+ // TTF* ttf;
+ // try
+ // {
+ // ttf = new TTF(fontData, fontSzie);
+ // }
+ // catch (...)
+ // {
+ // return nullptr;
+ // }
+ // return ttf;
+ //}
+
+ TTF::TTF(TTFData* f, unsigned int fontSize)
+ : Font(fontSize)
+ , cursor(0, 0)
+ , ttf(f)
+ {
+ ttf->pushTTFsize(fontSize);
+ ttf->getVMetrics(&baseline, &descent);
+ estimateSize();
+ ttf->popTTFsize();
+ /* create a default texture */
+ createAtlas();
+ }
+
+ /* estimate the size of atlas texture */
+ void TTF::estimateSize()
+ {
+ for (int level = 0; level <= TEXTURE_SIZE_LEVEL_MAX; ++level)
+ {
+ if (descent * (descent*0.8) * 96 <= TEXTURE_WIDTHS[level] * TEXTURE_HEIGHTS[level])
+ {
+ textureWidth = TEXTURE_WIDTHS[level];
+ textureHeight = TEXTURE_HEIGHTS[level];
+ break;
+ }
+ }
+ }
+
+ TTF::~TTF()
+ {
+ }
+
+ GLuint TTF::createAtlas()
+ {
+ GLuint t;
+ gl.flushError();
+ t = gl.genTexture();
+ gl.bindTexture(t);
+ gl.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ gl.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ gl.setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ gl.setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ gl.texImage(GL_RGBA8, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE);
+ if (glGetError() != GL_NO_ERROR)
+ {
+ glDeleteTextures(1, &t);
+ gl.bindTexture(0);
+ return 0;
+ }
+ atlases.push_back(t);
+ gl.bindTexture(0);
+ return t;
+ }
+
+ void TTF::render(const Content& t, int x, int y, int lineheight, int spacing)
+ {
+ Page* page = typeset(t, lineheight, spacing);
+ render(page, x, y);
+ delete page;
+ }
+
+ Page* TTF::typeset(const Content& text, int lineheight, int spacing)
+ {
+ Page* page = new Page();
+ page->font = this;
+ vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
+ vector<GlyphVertex>& glyphvertices = page->glyphvertices;
+ int texture = -1;
+ TTFGlyph* glyph = nullptr;
+ GlyphVertex vertex;
+ Vector2<int> p(0, 0);
+ int i = 0;
+
+#define glyphvertices_push(_x, _y, _u, _v) \
+ vertex.x = _x; vertex.y = _y;\
+ vertex.u = _u; vertex.v = _v;\
+ glyphvertices.push_back(vertex);
+
+#define glyphlize(c)\
+ do{\
+ glyph = &findGlyph(c); \
+ if (texture != glyph->atlas) \
+ { \
+ GlyphArrayDrawInfo info; \
+ info.start = i; \
+ info.count = 0; \
+ info.texture = glyph->atlas; \
+ texture = glyph->atlas; \
+ glyphinfolist.push_back(info); \
+ } \
+ glyphinfolist[glyphinfolist.size() - 1].count += 4; \
+ TTFGlyph::Bbox& bbox = glyph->bbox; \
+ glyphvertices_push(p.x, p.y, bbox.x, bbox.y); \
+ glyphvertices_push(p.x, p.y + glyph->height, bbox.x, bbox.y + bbox.h); \
+ glyphvertices_push(p.x + glyph->width, p.y + glyph->height, bbox.x + bbox.w, bbox.y + bbox.h); \
+ glyphvertices_push(p.x + glyph->width, p.y, bbox.x + bbox.w, bbox.y); \
+ }while(0)
+
+ for (Codepoint c : text)
+ {
+ if (c == 0x0D)
+ continue;
+ if (c == 0x0A)
+ {
+ /* new line */
+ p.y += lineheight;
+ p.x = 0;
+ continue;
+ }
+ if (c == 0x09)
+ {
+ // tab = 4*space
+ unsigned cw = getCharWidth(0x20);
+ p.x += cw * 4;
+ continue;
+ }
+ glyphlize(c);
+ p.x += glyph->width + spacing;
+ i += 4;
+ }
+ getTextBox(text, &page->size.w, &page->size.h, lineheight, spacing);
+ return page;
+ }
+
+ Page* TTF::typeset(const Text& text, int lineheight, int spacing)
+ {
+ return typeset(*text, lineheight, spacing);
+ }
+
+ void TTF::render(const Page* page, int x, int y)
+ {
+ Shader* shader = Shader::getCurrentShader();
+ const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
+ const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
+ gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
+ shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
+ for (int i = 0; i < glyphinfolist.size(); ++i)
+ {
+ const GlyphArrayDrawInfo& info = glyphinfolist[i];
+ shader->setVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x);
+ shader->setUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u);
+ gl.bindTexture(info.texture);
+ gl.drawArrays(GL_QUADS, 0, info.count);
+ gl.bindTexture(0);
+ }
+ }
+
+ void TTF::render(const Text& text, int x, int y, int lineheight, int spacing /* = 0 */)
+ {
+ render(*text, x, y, lineheight, spacing);
+ }
+
+ int TTF::getCharWidth(int c)
+ {
+ int adw, lsb;
+ ttf->pushTTFsize(mFontSize);
+ ttf->getHMetrics(c, &adw, &lsb);
+ ttf->popTTFsize();
+ return adw;
+ }
+
+ int TTF::getCharHeight(int c)
+ {
+ return descent;
+ }
+
+ int TTF::getTextWidth(const Content& t, int spacing)
+ {
+ ttf->pushTTFsize(mFontSize);
+ int res = 0;
+ int tmp = 0;
+ for (Codepoint c : t)
+ {
+ if (c == 0x0D)
+ continue;
+ if (c == 0x0A)
+ {
+ tmp = 0;
+ continue;
+ }
+ tmp += getCharWidth(c) + spacing;
+ if (tmp > res)
+ res = tmp;
+ }
+ ttf->popTTFsize();
+ return res;
+ }
+
+ int TTF::getTextHeight(const Content& t, int lineheight)
+ {
+ ttf->pushTTFsize(mFontSize);
+ int res = 0;
+ bool newline = true;
+ for (Codepoint c : t)
+ {
+ if (c == 0x0A)
+ newline = true;
+ else if (c == 0x0D);
+ else if (newline)
+ {
+ newline = false;
+ res += lineheight;
+ }
+ }
+ ttf->popTTFsize();
+ return res;
+ }
+
+ void TTF::getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing)
+ {
+ ttf->pushTTFsize(mFontSize);
+ *w = 0;
+ *h = 0;
+ int tmp = 0;
+ bool newline = true;
+ for (Codepoint c : text)
+ {
+ if (c == 0x0D)
+ continue;
+ if (c == 0x0A)
+ {
+ tmp = 0;
+ newline = true;
+ continue;
+ }
+ else if (newline)
+ {
+ newline = false;
+ *h += lineheight;
+ }
+ tmp += getCharWidth(c) + spacing;
+ if (tmp > *w)
+ *w = tmp;
+ }
+ ttf->popTTFsize();
+ }
+
+ TTF::TTFGlyph& TTF::bakeGlyph(unsigned int character)
+ {
+ int w, h, xoff, yoff;
+ ttf->pushTTFsize(mFontSize);
+ GLuint atlas = atlases.back();
+ const Color* bitmap = ttf->getCodepointBitmap(character, &w, &h, &xoff, &yoff);
+ int adw, lsb;
+ {
+ /* bake glyph */
+ ttf->getHMetrics(character, &adw, &lsb);
+ ttf->popTTFsize();
+ if (cursor.x + adw > textureWidth)
+ {
+ cursor.x = 0;
+ cursor.y += descent;
+ if (cursor.y + descent * 2 > textureHeight)
+ {
+ /* create new atlas */
+ atlas = createAtlas();
+ cursor.y = 0;
+ }
+ }
+ gl.bindTexture(atlas);
+ gl.texSubImage(cursor.x + xoff, cursor.y + yoff + baseline, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bitmap);
+ gl.bindTexture();
+ delete[] bitmap;
+ }
+ TTFGlyph glyph;
+ glyph.atlas = atlas;
+ glyph.bbox.x = cursor.x / (float)textureWidth;
+ glyph.bbox.y = cursor.y / (float)textureHeight;
+ glyph.bbox.w = adw / (float)textureWidth;
+ glyph.bbox.h = descent / (float)textureHeight;
+ glyph.width = adw;
+ glyph.height = descent;
+ glyphs.insert(std::pair<unsigned int, TTFGlyph>(character, glyph));
+ cursor.x += adw;
+ return glyphs[character];
+ }
+
+ TTF::TTFGlyph& TTF::findGlyph(unsigned int character)
+ {
+ map<unsigned int, TTFGlyph>::iterator it = glyphs.find(character);
+ if (it != glyphs.end())
+ return it->second;
+ else
+ return bakeGlyph(character);
+ }
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // defined(jin_graphics) \ No newline at end of file
diff --git a/src/libjin/Graphics/fonts/je_ttf.h b/src/libjin/Graphics/fonts/je_ttf.h
new file mode 100644
index 0000000..28260f6
--- /dev/null
+++ b/src/libjin/Graphics/fonts/je_ttf.h
@@ -0,0 +1,292 @@
+#ifndef __JE_TTF_H__
+#define __JE_TTF_H__
+#include "../../core/je_configuration.h"
+#if defined(jin_graphics)
+
+#include <vector>
+#include <map>
+
+#include "stb/stb_truetype.h"
+
+#include "../../math/je_quad.h"
+
+#include "../je_color.h"
+#include "../je_graphic.h"
+
+#include "je_page.h"
+#include "je_font.h"
+#include "je_text.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Fonts
+ {
+
+ class TTF;
+
+ //
+ // TTFData
+ // |- TTF(14px)
+ // |- TTF(15px)
+ // .
+ // .
+ // .
+ //
+ class TTFData
+ {
+ public:
+
+ ///
+ ///
+ ///
+ static TTFData* createTTFData(const unsigned char* data, unsigned int size);
+
+ ///
+ ///
+ ///
+ ~TTFData();
+
+ ///
+ ///
+ ///
+ TTF* createTTF(unsigned ttfsize);
+
+ ///
+ ///
+ ///
+ void pushTTFsize(unsigned ttfsize);
+
+ ///
+ ///
+ ///
+ void popTTFsize();
+
+ ///
+ ///
+ ///
+ Channel* getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const;
+
+ ///
+ ///
+ ///
+ Color* getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const;
+
+ ///
+ ///
+ ///
+ void getVMetrics(int* baseline, int* descent);
+
+ ///
+ ///
+ ///
+ void getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing);
+
+ private:
+
+ ///
+ ///
+ ///
+ static const unsigned int FONT_SIZE = 12;
+
+ ///
+ ///
+ ///
+ TTFData(const unsigned char* data, unsigned int size);
+
+ ///
+ ///
+ ///
+ stbtt_fontinfo info;
+
+ ///
+ ///
+ ///
+ struct
+ {
+ unsigned char* data;
+ unsigned int size;
+ } raw;
+
+ ///
+ ///
+ ///
+ std::vector<float> scales;
+
+ };
+
+ class TTF : public Font
+ {
+ public:
+ //static TTF* createTTF(TTFData* ttfData, unsigned ttfSzie);
+
+ ///
+ ///
+ ///
+ Page* typeset(const Text& text, int lineheight, int spacing = 0) override;
+
+ ///
+ ///
+ ///
+ Page* typeset(const Content& text, int lineheight, int spacing = 0) override;
+
+ ///
+ ///
+ ///
+ void render(const Text& text, int x, int y, int lineheight, int spacing = 0) override;
+
+ ///
+ ///
+ ///
+ void render(const Content& text, int x, int y, int lineheight, int spacing = 0) override;
+
+ ///
+ ///
+ ///
+ void render(const Page* page, int x, int y) override;
+
+ ///
+ ///
+ ///
+ ~TTF();
+
+ private:
+
+ friend class TTFData;
+
+ ///
+ ///
+ ///
+ struct TTFGlyph
+ {
+ GLuint atlas;
+ // normalized coordinates
+ struct Bbox
+ {
+ float x, y;
+ float w, h;
+ } bbox;
+ // glyph size in pixel
+ unsigned int width, height;
+ };
+
+ ///
+ ///
+ ///
+ static const int TEXTURE_SIZE_LEVELS_COUNT = 7;
+
+ ///
+ ///
+ ///
+ static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1;
+
+ ///
+ ///
+ ///
+ static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT];
+
+ ///
+ ///
+ ///
+ static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT];
+
+ ///
+ ///
+ ///
+ TTF(TTFData* ttf, Codepoint ttfSize);
+
+ ///
+ ///
+ ///
+ void estimateSize();
+
+ ///
+ ///
+ ///
+ GLuint createAtlas();
+
+ ///
+ ///
+ ///
+ TTFGlyph& bakeGlyph(Codepoint character);
+
+ ///
+ ///
+ ///
+ TTFGlyph& findGlyph(Codepoint character);
+
+ ///
+ ///
+ ///
+ int getCharWidth(int c);
+
+ ///
+ ///
+ ///
+ int getCharHeight(int c);
+
+ ///
+ ///
+ ///
+ 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<GLuint> atlases;
+
+ ///
+ ///
+ ///
+ std::map<Codepoint, TTFGlyph> glyphs;
+
+ ///
+ ///
+ ///
+ TTFData* ttf;
+
+ ///
+ ///
+ ///
+ int baseline;
+
+ ///
+ ///
+ ///
+ int descent;
+
+ ///
+ ///
+ ///
+ Math::Vector2<float> cursor;
+
+ };
+
+ } // namespace Fonts
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // defined(jin_graphics)
+
+#endif // __JE_FONT_H__
diff --git a/src/libjin/Graphics/je_bitmap.cpp b/src/libjin/Graphics/je_bitmap.cpp
index 0747f0b..cdab46d 100644
--- a/src/libjin/Graphics/je_bitmap.cpp
+++ b/src/libjin/Graphics/je_bitmap.cpp
@@ -1,6 +1,7 @@
#define STB_IMAGE_IMPLEMENTATION
-#include "../3rdparty/stb/stb_image.h"
+#include "stb/stb_image.h"
+#include "../common/je_exception.h"
#include "../filesystem/je_asset_database.h"
#include "../math/je_math.h"
@@ -22,7 +23,6 @@ namespace JinEngine
return createBitmap(&buffer, buffer.size());
}
- /* pixelbitmap */
Bitmap* Bitmap::createBitmap(const void* pixel, unsigned width, unsigned height)
{
Bitmap* bitmap = new Bitmap(width, height);
@@ -36,8 +36,11 @@ namespace JinEngine
return nullptr;
int w, h;
void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha);
- if (data == nullptr)
- return nullptr;
+ if (data == nullptr)
+ {
+ throw Exception("Could not create bitmap from image data.");
+ return nullptr;
+ }
Bitmap* bitmap = new Bitmap();
bitmap->pixels = (Color*)data;
bitmap->width = w;
@@ -53,6 +56,20 @@ namespace JinEngine
return bitmap;
}
+ /*static*/ Bitmap* Bitmap::createBitmap(int width, int height, std::function<Color(int, int, int, int)> drawer)
+ {
+ Bitmap* bitmap = new Bitmap(width, height);
+ for (int y = 0; y < height; ++y)
+ {
+ for (int x = 0; x < width; ++x)
+ {
+ Color c = drawer(width, height, x, y);
+ bitmap->setPixel(c, x, y);
+ }
+ }
+ return bitmap;
+ }
+
/*static */ Bitmap* Bitmap::clone(const Bitmap* bitmap)
{
Bitmap* b = new Bitmap();
@@ -74,6 +91,8 @@ namespace JinEngine
width = w;
height = h;
pixels = new Color[w*h];
+ if (pixels == nullptr)
+ throw Exception("No enough memory.");
}
Bitmap::~Bitmap()
@@ -95,6 +114,8 @@ namespace JinEngine
if (pixels != nullptr)
delete[] pixels;
pixels = new Color[w*h];
+ if (pixels == nullptr)
+ throw Exception("Not enough memory.");
size_t s = w * h * sizeof(Color);
memcpy(pixels, p, s);
width = w;
@@ -103,10 +124,12 @@ namespace JinEngine
void Bitmap::setPixel(const Color& c, int x, int y)
{
- if (pixels == nullptr)
- return;
+ if (pixels == nullptr)
+ throw Exception("Bitmap don't have pixel space.");
if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
return;
+ if (x + y * width >= width * height)
+ throw Exception("Pixel <%d, %d> of bitmap is out of range.", x, y);
pixels[x + y * width] = c;
}
@@ -115,7 +138,8 @@ namespace JinEngine
if (pixels != nullptr)
delete[] pixels;
pixels = new Color[w*h];
- size_t s = w * h * sizeof(Color);
+ if (pixels == nullptr)
+ throw Exception("Not enough memory.");
width = w;
height = h;
for (int x = 0; x < w; ++x)
@@ -127,8 +151,10 @@ namespace JinEngine
}
}
- void Bitmap::setPixels(Color* p)
+ void Bitmap::setPixels(Color* p, int count)
{
+ if (count > width * height)
+ throw Exception("Pixels are out of range.");
size_t s = width * height * sizeof(Color);
memcpy(pixels, p, s);
}
diff --git a/src/libjin/Graphics/je_bitmap.h b/src/libjin/Graphics/je_bitmap.h
index ae97d0d..5ab11ca 100644
--- a/src/libjin/Graphics/je_bitmap.h
+++ b/src/libjin/Graphics/je_bitmap.h
@@ -1,9 +1,12 @@
-#ifndef __JE_BITMAP_H
-#define __JE_BITMAP_H
+#ifndef __JE_BITMAP_H__
+#define __JE_BITMAP_H__
#include "../core/je_configuration.h"
#if defined(jin_graphics)
-#include "../3rdparty/GLee/GLee.h"
+#include <functional>
+
+#include "GLee/GLee.h"
+
#include "../common/je_types.h"
#include "../math/je_vector2.hpp"
@@ -17,7 +20,9 @@ namespace JinEngine
///
/// A RGBA32 bitmap.
///
- /// A bitmap keeps pixels and can't draw directly onto screen. To render bitmap, a texture is required.
+ /// A bitmap keeps pixels and can't draw directly onto screen. To render bitmap, a texture is required. A
+ /// texture is a renderable hard ware side structure which could be handled with GPU. For instance, opengl
+ /// create texture and store it in GPU memory for rendering them onto hdc.
///
class Bitmap
{
@@ -59,6 +64,11 @@ namespace JinEngine
///
static Bitmap* createBitmap(int width, int height, Color color = Color::BLACK);
+ ///
+ /// Create bitmap and set bitmap pixels with given drawer.
+ ///
+ static Bitmap* createBitmap(int width, int height, std::function<Color(int, int, int, int)> drawer);
+
///
/// Create bitmap with another one.
///
@@ -119,8 +129,9 @@ namespace JinEngine
/// Set pixels with given color data.
///
/// @param colors New pixels' colors.
+ /// @param count Number of pixels.
///
- void setPixels(Color* colors);
+ void setPixels(Color* colors, int count);
///
/// Get pixel in given position.
diff --git a/src/libjin/Graphics/je_canvas.cpp b/src/libjin/Graphics/je_canvas.cpp
index 7e0858b..7c8e849 100644
--- a/src/libjin/Graphics/je_canvas.cpp
+++ b/src/libjin/Graphics/je_canvas.cpp
@@ -18,10 +18,10 @@ namespace JinEngine
return new Canvas(w, h);
}
- Canvas::Canvas(GLuint n)
- : fbo(n)
- {
- }
+ Canvas::Canvas(GLuint n)
+ : fbo(n)
+ {
+ }
Canvas::Canvas(int w, int h)
: Graphic(w, h)
@@ -33,14 +33,13 @@ namespace JinEngine
fbo = gl.genFrameBuffer();
gl.bindFrameBuffer(fbo);
- // Generate texture save target
- mTexture = gl.genTexture();
- gl.bindTexture(mTexture);
+ GLuint texture = getGLTexture();
+ gl.bindTexture(texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl.texImage(GL_RGBA8, w, h, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
gl.bindTexture(0);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture, 0);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
diff --git a/src/libjin/Graphics/je_canvas.h b/src/libjin/Graphics/je_canvas.h
index f39b0de..3964517 100644
--- a/src/libjin/Graphics/je_canvas.h
+++ b/src/libjin/Graphics/je_canvas.h
@@ -1,5 +1,5 @@
-#ifndef __JE_CANVAS_H
-#define __JE_CANVAS_H
+#ifndef __JE_CANVAS_H__
+#define __JE_CANVAS_H__
#include "../core/je_configuration.h"
#if defined(jin_graphics)
@@ -14,7 +14,8 @@ namespace JinEngine
///
/// A canvas is a rendering target.
///
- class Canvas: public Graphic
+ class Canvas
+ : public Graphic
{
public:
///
@@ -65,4 +66,4 @@ namespace JinEngine
#endif // defined(jin_graphics)
-#endif // __JE_CANVAS_H \ No newline at end of file
+#endif // __JE_CANVAS_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_color.cpp b/src/libjin/Graphics/je_color.cpp
index da48162..c939a1d 100644
--- a/src/libjin/Graphics/je_color.cpp
+++ b/src/libjin/Graphics/je_color.cpp
@@ -13,5 +13,10 @@ namespace JinEngine
const Color Color::MAGENTA = Color(255, 0, 255);
const Color Color::YELLOW = Color(255, 255, 0);
+ const uint32 Color::RMASK = 0x000000ff;
+ const uint32 Color::GMASK = 0x0000ff00;
+ const uint32 Color::BMASK = 0x00ff0000;
+ const uint32 Color::AMASK = 0xff000000;
+
}
} \ No newline at end of file
diff --git a/src/libjin/Graphics/je_color.h b/src/libjin/Graphics/je_color.h
index 8fe7691..06b8f61 100644
--- a/src/libjin/Graphics/je_color.h
+++ b/src/libjin/Graphics/je_color.h
@@ -1,11 +1,13 @@
/**
* Some color operating here.
*/
-#ifndef __JE_COLOR_H
-#define __JE_COLOR_H
+#ifndef __JE_COLOR_H__
+#define __JE_COLOR_H__
#include "../core/je_configuration.h"
#if defined(jin_graphics)
+#include "../math/je_math.h"
+
#include "../common/je_types.h"
#include "../utils/je_endian.h"
@@ -28,6 +30,30 @@ namespace JinEngine
static const Color MAGENTA;
static const Color YELLOW;
+ static const uint32 RMASK;
+ static const uint32 GMASK;
+ static const uint32 BMASK;
+ static const uint32 AMASK;
+
+ ///
+ /// Get lerp color with given factor.
+ ///
+ /// @param start Start color.
+ /// @param end End color.
+ /// @param t Factor of interplation.
+ /// @return Color after interplation.
+ ///
+ static Color lerp(Color start, Color end, float t)
+ {
+ t = Math::clamp<float>(t, 0, 1);
+ Color c;
+ c.r = Math::lerp(start.r, end.r, t);
+ c.g = Math::lerp(start.g, end.g, t);
+ c.b = Math::lerp(start.b, end.b, t);
+ c.a = Math::lerp(start.a, end.a, t);
+ return c;
+ }
+
///
///
///
@@ -90,4 +116,4 @@ namespace JinEngine
#endif // jin_graphics
-#endif // __JE_COLOR_H \ No newline at end of file
+#endif // __JE_COLOR_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_gl.cpp b/src/libjin/Graphics/je_gl.cpp
index 9ef1460..3f19d36 100644
--- a/src/libjin/Graphics/je_gl.cpp
+++ b/src/libjin/Graphics/je_gl.cpp
@@ -1,5 +1,8 @@
#define OGL2D_IMPLEMENT
#include "je_gl.h"
+#include "je_color.h"
+
+using namespace JinEngine::Math;
namespace JinEngine
{
@@ -8,5 +11,82 @@ namespace JinEngine
OpenGL gl;
+ OpenGL::OpenGL()
+ : ogl2d::OpenGL()
+ {
+ mMatrices.push_back(Matrix());
+ calcMatrix();
+ }
+
+ void OpenGL::setColor(Channel r, Channel g, Channel b, Channel a)
+ {
+ setColor(Color(r, g, b, a));
+ }
+
+ void OpenGL::setColor(Color c)
+ {
+ mCurrentColor = c;
+ glColor4f(c.r / 255.f, c.g / 255.f, c.b / 255.f, c.a / 255.f);
+ }
+
+ Color OpenGL::getColor()
+ {
+ return mCurrentColor;
+ }
+
+ void OpenGL::clearMatrix()
+ {
+ mMatrices.clear();
+ mMatrices.push_back(Matrix());
+ mMatrix.setIdentity();
+ }
+
+ void OpenGL::push()
+ {
+ mMatrices.push_back(Matrix());
+ }
+
+ void OpenGL::pop()
+ {
+ if (mMatrices.size() == 1)
+ return;
+ mMatrices.pop_back();
+ calcMatrix();
+ }
+
+ void OpenGL::calcMatrix()
+ {
+ mMatrix.setIdentity();
+ for (Matrix& m : mMatrices)
+ mMatrix *= m;
+ }
+
+ void OpenGL::translate(float x, float y)
+ {
+ if (mMatrices.size() == 1)
+ return;
+ Matrix& m = mMatrices.back();
+ m.translate(x, y);
+ mMatrix.translate(x, y);
+ }
+
+ void OpenGL::scale(float sx, float sy)
+ {
+ if (mMatrices.size() == 1)
+ return;
+ Matrix& m = mMatrices.back();
+ m.scale(sx, sy);
+ mMatrix.scale(sx, sy);
+ }
+
+ void OpenGL::rotate(float r)
+ {
+ if (mMatrices.size() == 1)
+ return;
+ Matrix& m = mMatrices.back();
+ m.rotate(r);
+ mMatrix.rotate(r);
+ }
+
} // namespace Graphics
} // namespace JinEngine
diff --git a/src/libjin/Graphics/je_gl.h b/src/libjin/Graphics/je_gl.h
index cda8bf9..1dddab3 100644
--- a/src/libjin/Graphics/je_gl.h
+++ b/src/libjin/Graphics/je_gl.h
@@ -1,10 +1,14 @@
-#ifndef __JE_OPENGL_H
-#define __JE_OPENGL_H
+#ifndef __JE_OPENGL_H__
+#define __JE_OPENGL_H__
+
+#include <vector>
-#include "../3rdparty/GLee/GLee.h"
-#include "../3rdparty/ogl/OpenGL.h"
#include "../math/je_matrix.h"
+#include "je_color.h"
+#include "GLee/GLee.h"
+#include "ogl/OpenGL.h"
+
namespace JinEngine
{
namespace Graphics
@@ -26,9 +30,35 @@ namespace JinEngine
///
///
///
- OpenGL() : ogl2d::OpenGL()
- {
- }
+ OpenGL();
+
+ void setColor(Channel r, Channel g, Channel b, Channel a);
+
+ void setColor(Color c);
+
+ Color getColor();
+
+ void clearMatrix();
+
+ void push();
+
+ void pop();
+
+ void translate(float x, float y);
+
+ void scale(float sx, float sy);
+
+ void rotate(float r);
+
+ const Math::Matrix& getMatrix() { return mMatrix; };
+
+ private:
+
+ void calcMatrix();
+
+ Color mCurrentColor;
+ std::vector<Math::Matrix> mMatrices;
+ Math::Matrix mMatrix;
};
@@ -37,4 +67,4 @@ namespace JinEngine
} // namespace Graphics
} // namespace JinEngine
-#endif // __JE_OPENGL_H \ No newline at end of file
+#endif // __JE_OPENGL_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_graphic.cpp b/src/libjin/Graphics/je_graphic.cpp
index 831e409..6f3ff58 100644
--- a/src/libjin/Graphics/je_graphic.cpp
+++ b/src/libjin/Graphics/je_graphic.cpp
@@ -5,9 +5,11 @@
#include "../math/je_matrix.h"
-#include "shader/je_shader.h"
+#include "shaders/je_shader.h"
#include "je_graphic.h"
+using namespace JinEngine::Graphics::Shaders;
+
namespace JinEngine
{
namespace Graphics
@@ -17,32 +19,14 @@ namespace JinEngine
: mTexture(0)
, mSize(w, h)
{
- mVertexCoords[0] = 0; mVertexCoords[1] = 0;
- mVertexCoords[2] = 0; mVertexCoords[3] = h;
- mVertexCoords[4] = w; mVertexCoords[5] = h;
- mVertexCoords[6] = w; mVertexCoords[7] = 0;
-
- mTextureCoords[0] = 0; mTextureCoords[1] = 0;
- mTextureCoords[2] = 0; mTextureCoords[3] = 1;
- mTextureCoords[4] = 1; mTextureCoords[5] = 1;
- mTextureCoords[6] = 1; mTextureCoords[7] = 0;
+ mTexture = gl.genTexture();
}
Graphic::Graphic(const Bitmap* bitmap)
: mTexture(0)
{
- uint32 w = mSize.w = bitmap->getWidth();
- uint32 h = mSize.h = bitmap->getHeight();
-
- mVertexCoords[0] = 0; mVertexCoords[1] = 0;
- mVertexCoords[2] = 0; mVertexCoords[3] = h;
- mVertexCoords[4] = w; mVertexCoords[5] = h;
- mVertexCoords[6] = w; mVertexCoords[7] = 0;
-
- mTextureCoords[0] = 0; mTextureCoords[1] = 0;
- mTextureCoords[2] = 0; mTextureCoords[3] = 1;
- mTextureCoords[4] = 1; mTextureCoords[5] = 1;
- mTextureCoords[6] = 1; mTextureCoords[7] = 0;
+ mSize.w = bitmap->getWidth();
+ mSize.h = bitmap->getHeight();
const Color* pixels = bitmap->getPixels();
@@ -50,7 +34,7 @@ namespace JinEngine
gl.bindTexture(mTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- gl.texImage(GL_RGBA8, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+ gl.texImage(GL_RGBA8, mSize.w, mSize.h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
gl.bindTexture(0);
}
@@ -59,49 +43,63 @@ namespace JinEngine
glDeleteTextures(1, &mTexture);
}
- void Graphic::draw(int x, int y, float sx, float sy, float r, float ox, float oy)
+ void Graphic::render(int x, int y, float sx, float sy, float r, float ox, float oy) const
{
gl.ModelMatrix.setTransformation(x, y, r, sx, sy, ox, oy);
+ int w = getWidth(), h = getHeight();
+ static float vertexCoords[8];
+ static float textureCoords[8];
+ // Set vertex coordinates.
+ vertexCoords[0] = 0; vertexCoords[1] = 0;
+ vertexCoords[2] = 0; vertexCoords[3] = h;
+ vertexCoords[4] = w; vertexCoords[5] = h;
+ vertexCoords[6] = w; vertexCoords[7] = 0;
+ // Set texture coordinates.
+ textureCoords[0] = 0; textureCoords[1] = 0;
+ textureCoords[2] = 0; textureCoords[3] = 1;
+ textureCoords[4] = 1; textureCoords[5] = 1;
+ textureCoords[6] = 1; textureCoords[7] = 0;
+ // Set shader.
+ Shader* shader = Shader::getCurrentShader();
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
+ shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
+ shader->setVertexPointer(2, GL_FLOAT, 0, vertexCoords);
+ shader->setUVPointer(2, GL_FLOAT, 0, textureCoords);
- Shader* shader = Shader::getCurrentShader();
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
- shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
- shader->bindVertexPointer(2, GL_FLOAT, 0, mVertexCoords);
- shader->bindUVPointer(2, GL_FLOAT, 0, mTextureCoords);
-
- gl.bindTexture(mTexture);
+ gl.bindTexture(getGLTexture());
gl.drawArrays(GL_QUADS, 0, 4);
gl.bindTexture(0);
}
-
- void Graphic::draw(const Math::Quad& slice, int x, int y, float sx, float sy, float r, float ax, float ay)
+
+ void Graphic::render(const Math::Quad& slice, int x, int y, float sx, float sy, float r, float ax, float ay) const
{
- float vertCoords[8] = {
- 0, 0,
- 0, slice.h,
- slice.w, slice.h,
- slice.w, 0
- };
+ static float vertexCoords[8];
+ static float textureCoords[8];
+
+ // Set vertex coordinates.
+ vertexCoords[0] = 0; vertexCoords[1] = 0;
+ vertexCoords[2] = 0; vertexCoords[3] = slice.h;
+ vertexCoords[4] = slice.w; vertexCoords[5] = slice.h;
+ vertexCoords[6] = slice.w; vertexCoords[7] = 0;
+ // Set texture coordinates.
float slx = slice.x / mSize.w;
float sly = slice.y / mSize.h;
float slw = slice.w / mSize.w;
float slh = slice.h / mSize.h;
- float texCoords[8] = {
- slx, sly,
- slx, sly + slh,
- slx + slw, sly + slh,
- slx + slw, sly
- };
+ textureCoords[0] = slx; textureCoords[1] = sly;
+ textureCoords[2] = slx; textureCoords[3] = sly + slh;
+ textureCoords[4] = slx + slw; textureCoords[5] = sly + slh;
+ textureCoords[6] = slx + slw; textureCoords[7] = sly;
gl.ModelMatrix.setTransformation(x, y, r, sx, sy, ax, ay);
Shader* shader = Shader::getCurrentShader();
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
- shader->bindVertexPointer(2, GL_FLOAT, 0, vertCoords);
- shader->bindUVPointer(2, GL_FLOAT, 0, texCoords);
+ shader->setVertexPointer(2, GL_FLOAT, 0, vertexCoords);
+ shader->setUVPointer(2, GL_FLOAT, 0, textureCoords);
- gl.bindTexture(mTexture);
+ gl.bindTexture(getGLTexture());
gl.drawArrays(GL_QUADS, 0, 4);
gl.bindTexture(0);
}
diff --git a/src/libjin/Graphics/je_graphic.h b/src/libjin/Graphics/je_graphic.h
index fb6b19e..91c8b44 100644
--- a/src/libjin/Graphics/je_graphic.h
+++ b/src/libjin/Graphics/je_graphic.h
@@ -1,5 +1,5 @@
-#ifndef __JE_GRAPHIC_H
-#define __JE_GRAPHIC_H
+#ifndef __JE_GRAPHIC_H__
+#define __JE_GRAPHIC_H__
#include "../core/je_configuration.h"
#if defined(jin_graphics)
@@ -14,21 +14,14 @@ namespace JinEngine
namespace Graphics
{
- //
- // Graphic
- // |-Texture
- // |-Canvas
- //
-
///
- /// Class inherites Graphic doesn't keep any state such as origin, scale and other
- /// properties.
+ /// Class inherites Graphic doesn't keep any state such as origin, scale and other properties.
///
class Graphic
{
public:
///
- ///
+ ///
///
Graphic(int w = 0, int h = 0);
@@ -45,16 +38,6 @@ namespace JinEngine
///
///
///
- void draw(int x, int y, float sx = 1, float sy = 1, float r = 0, float ox = 0, float oy = 0);
-
- ///
- ///
- ///
- void draw(const Math::Quad& slice, int x, int y, float sx = 1, float sy = 1, float r = 0, float ax = 0, float ay = 0);
-
- ///
- ///
- ///
inline int getWidth() const { return mSize.w; }
///
@@ -63,24 +46,33 @@ namespace JinEngine
inline int getHeight() const { return mSize.h; }
///
- ///
+ /// Get opengl texture token.
+ ///
+ /// @return OpenGL texture token.
///
- inline GLuint getTexture() const { return mTexture; }
+ inline GLuint getGLTexture() const { return mTexture; }
///
///
///
void setFilter(GLint min, GLint max);
+ ///
+ /// Render graphic single with given coordinates.
+ ///
+ void render(int x, int y, float sx = 1, float sy = 1, float r = 0, float ox = 0, float oy = 0) const;
+
+ ///
+ /// Render part of graphic single with given coordinates.
+ ///
+ void render(const Math::Quad& slice, int x, int y, float sx = 1, float sy = 1, float r = 0, float ox = 0, float oy = 0) const;
+
protected:
- GLuint mTexture;
+ Math::Vector2<uint> mSize;
private:
- JinEngine::Math::Vector2<uint> mSize;
- // Screen coordinates and uv coordinates.
- float mVertexCoords[8];
- float mTextureCoords[8];
-
+ GLuint mTexture;
+
};
} // namespace Graphics
@@ -88,4 +80,4 @@ namespace JinEngine
#endif // defined(jin_graphics)
-#endif // __JE_GRAPHIC_H \ No newline at end of file
+#endif // __JE_GRAPHIC_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_graphics.h b/src/libjin/Graphics/je_graphics.h
index 2ba003d..979d8f4 100644
--- a/src/libjin/Graphics/je_graphics.h
+++ b/src/libjin/Graphics/je_graphics.h
@@ -1,5 +1,5 @@
-#ifndef __JE_GRAPHICS_H
-#define __JE_GRAPHICS_H
+#ifndef __JE_GRAPHICS_H__
+#define __JE_GRAPHICS_H__
#include "../core/je_configuration.h"
#if defined(jin_graphics)
@@ -10,12 +10,28 @@
#include "je_window.h"
#include "je_bitmap.h"
#include "je_image.h"
+#include "je_sprite.h"
+#include "je_sprite_sheet.h"
-#include "shader/je_shader.h"
+#include "shaders/je_shader.h"
-#include "font/je_ttf.h"
-#include "font/je_text.h"
-#include "font/je_texture_font.h"
+#include "fonts/je_ttf.h"
+#include "fonts/je_text.h"
+#include "fonts/je_texture_font.h"
+
+#include "particles/je_particle_system.h"
+
+//struct Stats
+//{
+// int drawCalls;
+// int drawCallsBatched;
+// int canvasSwitches;
+// int shaderSwitches;
+// int canvases;
+// int images;
+// int fonts;
+// int64 textureMemory;
+//};
#endif // defined(jin_graphics)
-#endif // __JE_GRAPHICS_H \ No newline at end of file
+#endif // __JE_GRAPHICS_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_image.cpp b/src/libjin/Graphics/je_image.cpp
index f800423..6baf16d 100644
--- a/src/libjin/Graphics/je_image.cpp
+++ b/src/libjin/Graphics/je_image.cpp
@@ -1,6 +1,7 @@
-#include "../3rdparty/stb/stb_image.h"
#include "../filesystem/je_asset_database.h"
+#include "stb/stb_image.h"
+
#include "je_image.h"
namespace JinEngine
diff --git a/src/libjin/Graphics/je_image.h b/src/libjin/Graphics/je_image.h
index 04ee4d2..971ac18 100644
--- a/src/libjin/Graphics/je_image.h
+++ b/src/libjin/Graphics/je_image.h
@@ -1,5 +1,5 @@
-#ifndef __JE_IMAGE_H
-#define __JE_IMAGE_H
+#ifndef __JE_IMAGE_H__
+#define __JE_IMAGE_H__
#include "je_bitmap.h"
@@ -13,7 +13,8 @@ namespace JinEngine
///
/// Just like bitmap but only from image file. The pixels data is readonly.
///
- class Image : public Bitmap
+ class Image
+ : public Bitmap
{
public:
///
diff --git a/src/libjin/Graphics/je_mesh.h b/src/libjin/Graphics/je_mesh.h
index ed22d91..e0a38f8 100644
--- a/src/libjin/Graphics/je_mesh.h
+++ b/src/libjin/Graphics/je_mesh.h
@@ -1,5 +1,7 @@
-#ifndef __JE_MESH_H
-#define __JE_MESH_H
+#ifndef __JE_MESH_H__
+#define __JE_MESH_H__
+
+#include "je_graphic.h"
namespace JinEngine
{
@@ -7,14 +9,16 @@ namespace JinEngine
{
///
- ///
+ /// A 2D mesh.
///
class Mesh
{
public:
+ void setGraphic();
+
private:
-
+ const Graphic* mGraphic;
};
diff --git a/src/libjin/Graphics/je_shapes.cpp b/src/libjin/Graphics/je_shapes.cpp
index 3146f31..486c506 100644
--- a/src/libjin/Graphics/je_shapes.cpp
+++ b/src/libjin/Graphics/je_shapes.cpp
@@ -6,9 +6,11 @@
#include "../math/je_matrix.h"
#include "../math/je_constant.h"
-#include "shader/je_shader.h"
+#include "shaders/je_shader.h"
#include "je_shapes.h"
+using namespace JinEngine::Graphics::Shaders;
+
namespace JinEngine
{
namespace Graphics
@@ -21,9 +23,9 @@ namespace JinEngine
float verts[] = { x + 0.5f , y + 0.5f };
Shader* shader = Shader::getCurrentShader();
- shader->bindVertexPointer(2, GL_FLOAT, 0, verts);
+ shader->setVertexPointer(2, GL_FLOAT, 0, verts);
gl.ModelMatrix.setIdentity();
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
glDrawArrays(GL_POINTS, 0, 1);
@@ -32,9 +34,9 @@ namespace JinEngine
void points(int n, GLshort* p)
{
Shader* shader = Shader::getCurrentShader();
- shader->bindVertexPointer(2, GL_SHORT, 0, p);
+ shader->setVertexPointer(2, GL_SHORT, 0, p);
gl.ModelMatrix.setIdentity();
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
glDrawArrays(GL_POINTS, 0, n);
@@ -43,14 +45,14 @@ namespace JinEngine
void line(int x1, int y1, int x2, int y2)
{
float verts[] = {
- x1, y1,
- x2, y2
+ x1 + 0.5f, y1 + 0.5f,
+ x2 + 0.5f, y2 + 0.5f
};
Shader* shader = Shader::getCurrentShader();
- shader->bindVertexPointer(2, GL_FLOAT, 0, verts);
+ shader->setVertexPointer(2, GL_FLOAT, 0, verts);
gl.ModelMatrix.setIdentity();
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
glDrawArrays(GL_LINES, 0, 2);
@@ -83,13 +85,13 @@ namespace JinEngine
void rect(RenderMode mode, int x, int y, int w, int h)
{
- float coords[] = { x, y, x + w, y, x + w, y + h, x, y + h };
+ float coords[] = { x + 0.5f, y + 0.5f, x + w + 0.5f, y + 0.5f, x + w + 0.5f, y + h + 0.5f, x + 0.5f, y + h + 0.5f };
polygon(mode, coords, 4);
}
void triangle(RenderMode mode, int x1, int y1, int x2, int y2, int x3, int y3)
{
- float coords[] = { x1, y1, x2, y2, x3, y3 };
+ float coords[] = { x1 + 0.5f, y1 + 0.5f, x2 + 0.5f, y2 + 0.5f, x3 + 0.5f, y3 + 0.5f };
polygon(mode, coords, 3);
}
@@ -97,9 +99,9 @@ namespace JinEngine
{
Shader* shader = Shader::getCurrentShader();
gl.ModelMatrix.setIdentity();
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
- shader->bindVertexPointer(2, GL_FLOAT, 0, p);
+ shader->setVertexPointer(2, GL_FLOAT, 0, p);
glDrawArrays(GL_LINE_LOOP, 0, count);
}
@@ -114,9 +116,9 @@ namespace JinEngine
{
Shader* shader = Shader::getCurrentShader();
gl.ModelMatrix.setIdentity();
- shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &(gl.getMatrix() * gl.ModelMatrix));
shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
- shader->bindVertexPointer(2, GL_FLOAT, 0, p);
+ shader->setVertexPointer(2, GL_FLOAT, 0, p);
glDrawArrays(GL_POLYGON, 0, count);
}
diff --git a/src/libjin/Graphics/je_shapes.h b/src/libjin/Graphics/je_shapes.h
index 2221526..d62592b 100644
--- a/src/libjin/Graphics/je_shapes.h
+++ b/src/libjin/Graphics/je_shapes.h
@@ -1,5 +1,5 @@
-#ifndef __JE_GEOMETRY_H
-#define __JE_GEOMETRY_H
+#ifndef __JE_GEOMETRY_H__
+#define __JE_GEOMETRY_H__
#include "../core/je_configuration.h"
#if defined(jin_graphics)
@@ -12,11 +12,11 @@ namespace JinEngine
namespace Graphics
{
- typedef enum {
+ enum RenderMode {
NONE = 0,
FILL ,
LINE
- }RenderMode;
+ };
extern void line(int x1, int y1, int x2, int y2);
extern void rect(RenderMode mode, int x, int y, int w, int h);
@@ -31,4 +31,4 @@ namespace JinEngine
#endif // jin_graphics
-#endif // __JE_GEOMETRY_H \ No newline at end of file
+#endif // __JE_GEOMETRY_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_sprite.cpp b/src/libjin/Graphics/je_sprite.cpp
index 3ac976a..810cb0e 100644
--- a/src/libjin/Graphics/je_sprite.cpp
+++ b/src/libjin/Graphics/je_sprite.cpp
@@ -1,11 +1,156 @@
+#include "shaders/je_shader.h"
+
#include "je_sprite.h"
+using namespace JinEngine::Math;
+using namespace JinEngine::Graphics::Shaders;
+
namespace JinEngine
{
namespace Graphics
{
+ Sprite::Sprite()
+ : mShader(nullptr)
+ , mGraphic(nullptr)
+ , mScale(1, 1)
+ , mColor(255, 255, 255, 255)
+ , mIsOriginEnum(false)
+ {
+ }
+
+ Sprite::~Sprite()
+ {
+ }
+
+ void Sprite::setQuad(int x, int y, int w, int h)
+ {
+ mQuad.x = x;
+ mQuad.y = y;
+ mQuad.w = w;
+ mQuad.h = h;
+ if (mIsOriginEnum)
+ setOrigin(mOriginEnum);
+ }
+
+ void Sprite::setRotation(float r)
+ {
+ mRotation = r;
+ }
+
+ void Sprite::setOrigin(Origin origin)
+ {
+ mIsOriginEnum = true;
+ mOriginEnum = origin;
+ int l = 0, r = 0, t = 0, b = 0;
+ Vector2<int> size = getSize();
+ r = size.w;
+ b = size.h;
+ switch (origin)
+ {
+ case TopLeft:
+ mOrigin.x = l;
+ mOrigin.y = t;
+ break;
+ case TopCenter:
+ mOrigin.x = r/2.f;
+ mOrigin.y = t;
+ break;
+ case TopRight:
+ mOrigin.x = r;
+ mOrigin.y = t;
+ break;
+ case MiddleLeft:
+ mOrigin.x = l;
+ mOrigin.y = b/2.f;
+ break;
+ case MiddleCenter:
+ mOrigin.x = r/2.f;
+ mOrigin.y = b/2.f;
+ break;
+ case MiddleRight:
+ mOrigin.x = r;
+ mOrigin.y = b/2.f;
+ break;
+ case BottomLeft:
+ mOrigin.x = l;
+ mOrigin.y = b;
+ break;
+ case BottomCenter:
+ mOrigin.x = r/2.f;
+ mOrigin.y = b;
+ break;
+ case BottomRight:
+ mOrigin.x = r;
+ mOrigin.y = b;
+ break;
+ }
+ }
+
+ void Sprite::setOrigin(int x, int y)
+ {
+ mOrigin.set(x, y);
+ mIsOriginEnum = false;
+ }
+
+ void Sprite::setPosition(float x, float y)
+ {
+ mPosition.set(x, y);
+ }
+
+ void Sprite::setScale(float x, float y)
+ {
+ mScale.set(x, y);
+ }
+
+ void Sprite::setColor(Color color)
+ {
+ mColor = color;
+ }
+
+ void Sprite::setShader(Shader* shader)
+ {
+ mShader = shader;
+ }
+
+ void Sprite::setGraphic(const Graphic* graphic)
+ {
+ mGraphic = graphic;
+ int w = mGraphic->getWidth();
+ int h = mGraphic->getHeight();
+ setQuad(0, 0, w, h);
+ }
+
+
+ void Sprite::move(float x, float y)
+ {
+ mPosition.x += x;
+ mPosition.y += y;
+ }
+
+ void Sprite::rotate(float r)
+ {
+ mRotation += r;
+ }
+
+ void Sprite::scale(float sx, float sy)
+ {
+ mScale.x += sx;
+ mScale.y += sy;
+ }
+ void Sprite::render()
+ {
+ Shader* shader = Shader::getCurrentShader();
+ Color c = gl.getColor();
+ gl.setColor(mColor);
+ if(mShader != nullptr)
+ mShader->use();
+ if(mGraphic != nullptr)
+ mGraphic->render(mQuad, mPosition.x, mPosition.y, mScale.x, mScale.y, mRotation, mOrigin.x, mOrigin.y);
+ shader->use();
+ gl.setColor(c);
+ }
} // namespace Graphics
} // namespace JinEngine \ No newline at end of file
diff --git a/src/libjin/Graphics/je_sprite.h b/src/libjin/Graphics/je_sprite.h
index 3b96162..65e00eb 100644
--- a/src/libjin/Graphics/je_sprite.h
+++ b/src/libjin/Graphics/je_sprite.h
@@ -1,34 +1,85 @@
-#ifndef __JE_SPRITE_H
-#define __JE_SPRITE_H
+#ifndef __JE_SPRITE_H__
+#define __JE_SPRITE_H__
#include "../common/je_types.h"
#include "../math/je_vector2.hpp"
-#include "shader/je_shader.h"
+#include "shaders/je_shader.h"
#include "je_color.h"
namespace JinEngine
{
namespace Graphics
{
+
///
- /// A sprite is unit of rendering, logic and events.
+ /// A sprite is unit of rendering. Animation is based on sprite, but not texture or other graphic stuff.
///
class Sprite
{
public:
- void setOrigin(float x, float y);
- void setPosition(int x, int y);
- void setScale(float x, float y);
+ Sprite();
+ virtual ~Sprite();
+
+ enum Origin
+ {
+ TopLeft,
+ TopCenter,
+ TopRight,
+ MiddleLeft,
+ MiddleCenter,
+ MiddleRight,
+ BottomLeft,
+ BottomCenter,
+ BottomRight
+ };
+
+ void setQuad(int x, int y, int w, int h);
+ void setRotation(float r);
+ void setOrigin(Origin origin);
+ void setOrigin(int x, int y);
+ void setPosition(float x, float y);
+ void setScale(float sx, float sy);
void setColor(Color color);
- void setShader(const Shader* shader);
+ void setShader(Shaders::Shader* shader);
+ void setGraphic(const Graphic* graphic);
+
+ void move(float x, float y);
+ void rotate(float r);
+ void scale(float sx, float sy);
+
+ float getRotation() { return mRotation; }
+ Math::Vector2<int> getSize() { return Math::Vector2<int>(mQuad.w, mQuad.h); }
+ const Math::Quad& getQuad() { return mQuad; }
+ const Math::Vector2<float>& getPosition() { return mPosition; }
+ const Math::Vector2<int>& getOrigin() { return mOrigin; }
+ const Math::Vector2<float>& getScale() { return mScale; }
+ const Color& getColor() { return mColor; }
+ const Graphic* getGraphic() { return mGraphic; }
+ const Shaders::Shader* getShader() { return mShader; }
+
+ ///
+ /// Render callback.
+ ///
+ virtual void render();
private:
- Math::Vector2<int> mPosition;
- Math::Vector2<float> mOrigin;
+ ///
+ /// Origin must be 0~1 float value.
+ ///
+ Math::Vector2<float> mPosition;
+ Math::Vector2<int> mOrigin;
Math::Vector2<float> mScale;
+ float mRotation;
Color mColor;
- const Shader* mShader;
+
+ Math::Quad mQuad;
+
+ bool mIsOriginEnum;
+ Origin mOriginEnum;
+
+ Shaders::Shader* mShader;
+ const Graphic* mGraphic;
};
diff --git a/src/libjin/Graphics/je_sprite_batch.cpp b/src/libjin/Graphics/je_sprite_batch.cpp
index e69de29..f339715 100644
--- a/src/libjin/Graphics/je_sprite_batch.cpp
+++ b/src/libjin/Graphics/je_sprite_batch.cpp
@@ -0,0 +1,11 @@
+#include "je_sprite_batch.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+
+
+
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/je_sprite_batch.h b/src/libjin/Graphics/je_sprite_batch.h
index 85a7951..64f9805 100644
--- a/src/libjin/Graphics/je_sprite_batch.h
+++ b/src/libjin/Graphics/je_sprite_batch.h
@@ -1,5 +1,5 @@
-#ifndef __JE_SPRITE_BATCH_H
-#define __JE_SPRITE_BATCH_H
+#ifndef __JE_GRAPHICS_SPRITE_BATCH_H__
+#define __JE_GRAPHICS_SPRITE_BATCH_H__
namespace JinEngine
{
@@ -8,10 +8,13 @@ namespace JinEngine
class SpriteBatch
{
+ public:
+
+ private:
};
} // namespace Graphics
-} // namespace JinEngine
+} // namespace JinEngine
#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/je_sprite_sheet.cpp b/src/libjin/Graphics/je_sprite_sheet.cpp
new file mode 100644
index 0000000..3a08751
--- /dev/null
+++ b/src/libjin/Graphics/je_sprite_sheet.cpp
@@ -0,0 +1,22 @@
+#include "je_sprite_sheet.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+
+ SpriteSheet::SpriteSheet(const Graphic* graphic)
+ : mGraphic(graphic)
+ {
+ }
+
+ Sprite* SpriteSheet::createSprite(const Math::Quad& quad)
+ {
+ Sprite* spr = new Sprite();
+ spr->setGraphic(mGraphic);
+ spr->setQuad(quad.x, quad.y, quad.w, quad.h);
+ return spr;
+ }
+
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/je_sprite_sheet.h b/src/libjin/Graphics/je_sprite_sheet.h
new file mode 100644
index 0000000..8c56c51
--- /dev/null
+++ b/src/libjin/Graphics/je_sprite_sheet.h
@@ -0,0 +1,33 @@
+#ifndef __JE_SPRITE_SHEET_H__
+#define __JE_SPRITE_SHEET_H__
+
+#include <vector>
+
+#include "../math/je_quad.h"
+
+#include "je_sprite.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+
+ class SpriteSheet
+ {
+ public:
+ ///
+ /// Create a new sprite in sheet.
+ ///
+ Sprite* createSprite(const Math::Quad& quad);
+
+ SpriteSheet(const Graphic* graphic);
+
+ private:
+ const Graphic* const mGraphic;
+
+ };
+
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/je_texture.h b/src/libjin/Graphics/je_texture.h
index ddc8cfc..566ba84 100644
--- a/src/libjin/Graphics/je_texture.h
+++ b/src/libjin/Graphics/je_texture.h
@@ -1,9 +1,9 @@
-#ifndef __JE_TEXTURE_H
-#define __JE_TEXTURE_H
+#ifndef __JE_TEXTURE_H__
+#define __JE_TEXTURE_H__
#include "../core/je_configuration.h"
#if defined(jin_graphics)
-#include "../3rdparty/GLee/GLee.h"
+#include "GLee/GLee.h"
#include "je_color.h"
#include "je_graphic.h"
@@ -17,7 +17,8 @@ namespace JinEngine
///
///
///
- class Texture: public Graphic
+ class Texture
+ : public Graphic
{
public:
///
@@ -53,4 +54,4 @@ namespace JinEngine
#endif // jin_graphics
-#endif // __JE_TEXTURE_H \ No newline at end of file
+#endif // __JE_TEXTURE_H__ \ No newline at end of file
diff --git a/src/libjin/Graphics/je_window.cpp b/src/libjin/Graphics/je_window.cpp
index f0b789e..c14d290 100644
--- a/src/libjin/Graphics/je_window.cpp
+++ b/src/libjin/Graphics/je_window.cpp
@@ -3,15 +3,18 @@
#include <iostream>
+#include "../common/je_exception.h"
#include "../utils/je_utils.h"
#include "../audio/sdl/je_sdl_audio.h"
#include "../utils/je_log.h"
-#include "shader/je_shader.h"
+#include "shaders/je_shader.h"
#include "je_window.h"
#include "je_gl.h"
#include "je_canvas.h"
+using namespace JinEngine::Graphics::Shaders;
+
namespace JinEngine
{
namespace Graphics
@@ -19,9 +22,7 @@ namespace JinEngine
bool Window::initSystem(const SettingBase* s)
{
- #if defined(jin_debug)
- Loghelper::log(Loglevel::LV_INFO, "Init window system");
- #endif
+ jin_log_info("Initialize window system.");
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return false;
@@ -30,8 +31,9 @@ namespace JinEngine
mSize.w = setting->width;
mSize.h = setting->height;
mFps = setting->fps;
- bool vsync = setting->vsync;
- const char* title = setting->title;
+ bool vsync = setting->vsync;
+ const char* title = setting->title;
+ const char* icon = setting->icon;
if (mWnd)
{
@@ -58,34 +60,51 @@ namespace JinEngine
int wx = SDL_WINDOWPOS_UNDEFINED,
wy = SDL_WINDOWPOS_UNDEFINED;
- int flag = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL;
+ int flag = SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL;
if (setting->fullscreen) flag |= SDL_WINDOW_FULLSCREEN;
if (setting->resizable) flag |= SDL_WINDOW_RESIZABLE;
mWnd = SDL_CreateWindow(title, wx, wy, mSize.w, mSize.h, flag);
if (mWnd == NULL)
return false;
+
+ // Set window icon
+ try
+ {
+ Bitmap* bitmap = Bitmap::createBitmap(icon);
+ SDL_Surface *surface;
+ Color* pixels = const_cast<Color*>(bitmap->getPixels());
+ uint w = bitmap->getWidth(), h = bitmap->getHeight();
+ surface = SDL_CreateRGBSurfaceFrom(
+ pixels, w, h, 32, w * 4, Color::RMASK, Color::GMASK, Color::BMASK, Color::AMASK);
+ SDL_SetWindowIcon(mWnd, surface);
+ SDL_FreeSurface(surface);
+ } catch (...) {}
+
ctx = SDL_GL_CreateContext(mWnd);
if (ctx == NULL)
return false;
SDL_GL_SetSwapInterval(vsync ? 1 : 0);
SDL_GL_MakeCurrent(mWnd, ctx);
- // default configuration
+ // Default configuration
gl.setClearColor(0, 0, 0, 0xff);
- gl.pushColor(0xff, 0xff, 0xff, 0xff);
+ glClear(GL_COLOR_BUFFER_BIT);
+ gl.pushColor(0xff, 0xff, 0xff, 0xff);
gl.enable(GL_BLEND);
gl.enable(GL_TEXTURE_2D);
gl.setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- // avoid white screen blink on windows
- swapBuffers();
- // bind to default canvas
+ // Bind to default canvas
Canvas::unbind();
Shader::unuse();
+ // Avoid white blinnk.
+ swapBuffers();
+
return true;
}
void Window::quitSystem()
{
+ jin_log_info("Quit window system.");
// disable opengl
gl.disable(GL_BLEND);
gl.disable(GL_TEXTURE_2D);
diff --git a/src/libjin/Graphics/je_window.h b/src/libjin/Graphics/je_window.h
index 7ca1e5e..831f3e6 100644
--- a/src/libjin/Graphics/je_window.h
+++ b/src/libjin/Graphics/je_window.h
@@ -16,7 +16,8 @@ namespace JinEngine
///
///
///
- class Window : public Subsystem<Window>
+ class Window
+ : public Subsystem<Window>
{
public:
///
@@ -26,6 +27,7 @@ namespace JinEngine
{
public:
const char* title; ///< window title
+ const char* icon; ///< window icon
bool fullscreen; ///< full screen
int width, height; ///< window size
bool vsync; ///< vsync
@@ -58,6 +60,16 @@ namespace JinEngine
///
void swapBuffers();
+ ///
+ ///
+ ///
+ inline void hide() { SDL_HideWindow(mWnd); };
+
+ ///
+ ///
+ ///
+ void show() { SDL_ShowWindow(mWnd); };
+
private:
// declare a singleton
diff --git a/src/libjin/Graphics/particle/je_particle.cpp b/src/libjin/Graphics/particle/je_particle.cpp
deleted file mode 100644
index e69de29..0000000
--- a/src/libjin/Graphics/particle/je_particle.cpp
+++ /dev/null
diff --git a/src/libjin/Graphics/particle/je_particle.h b/src/libjin/Graphics/particle/je_particle.h
deleted file mode 100644
index 2a5c54f..0000000
--- a/src/libjin/Graphics/particle/je_particle.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef __JE_PARTICLE_H
-#define __JE_PARTICLE_H
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- /// Single particle.
- ///
- class Particle
- {
-
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particle/je_particle_batch.cpp b/src/libjin/Graphics/particle/je_particle_batch.cpp
deleted file mode 100644
index e69de29..0000000
--- a/src/libjin/Graphics/particle/je_particle_batch.cpp
+++ /dev/null
diff --git a/src/libjin/Graphics/particle/je_particle_batch.h b/src/libjin/Graphics/particle/je_particle_batch.h
deleted file mode 100644
index 19f4ded..0000000
--- a/src/libjin/Graphics/particle/je_particle_batch.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#ifndef __JE_PARTICLE_BATCH_H
-#define __JE_PARTICLE_BATCH_H
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- ///
- ///
- class ParticleBatch
- {
-
- };
-
- } // namespace Graphics
-} // namespace JinEngine
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particle/je_particle_emitter.h b/src/libjin/Graphics/particle/je_particle_emitter.h
deleted file mode 100644
index e191e36..0000000
--- a/src/libjin/Graphics/particle/je_particle_emitter.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef __JE_PARTICLE_EMMITTER_H
-#define __JE_PARTICLE_EMMITTER_H
-
-namespace JinEngine
-{
- namespace Graphics
- {
-
- ///
- /// Particle emitter
- ///
- class ParticleEmitter
- {
- public:
-
- private:
-
- };
-
- }
-}
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particle/je_particle_system.cpp b/src/libjin/Graphics/particle/je_particle_system.cpp
deleted file mode 100644
index 6f70f09..0000000
--- a/src/libjin/Graphics/particle/je_particle_system.cpp
+++ /dev/null
@@ -1 +0,0 @@
-#pragma once
diff --git a/src/libjin/Graphics/particle/je_particle_system.h b/src/libjin/Graphics/particle/je_particle_system.h
deleted file mode 100644
index e69de29..0000000
--- a/src/libjin/Graphics/particle/je_particle_system.h
+++ /dev/null
diff --git a/src/libjin/Graphics/particles/je_particle.cpp b/src/libjin/Graphics/particles/je_particle.cpp
new file mode 100644
index 0000000..20bbd03
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle.cpp
@@ -0,0 +1,26 @@
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ void Particle::reset()
+ {
+ lifeTime = 0.0f;
+ life = 0.0f;
+ position.set(0, 0);
+ direction = 0.0f;
+ speed.set(0, 0);
+ linearAcceleration.set(0, 0);
+ radialAcceleration = 0.0f;
+ size = 1;
+ sizeBegin = 1;
+ alive = true;
+ }
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle.h b/src/libjin/Graphics/particles/je_particle.h
new file mode 100644
index 0000000..2d112d1
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle.h
@@ -0,0 +1,178 @@
+#ifndef __JE_PARTICLE_H__
+#define __JE_PARTICLE_H__
+
+#include "../../math/je_vector2.hpp"
+#include "../je_color.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ class ParticleEmitter;
+
+ ///
+ ///
+ ///
+ struct LifeTimeDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ float min, max;
+ } random;
+ float life;
+ } life;
+ };
+
+ ///
+ ///
+ ///
+ struct SpeedOverTimeDef
+ {
+ bool enable = false;
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ Math::Vector2<float> startFloor;
+ Math::Vector2<float> startCeil;
+ Math::Vector2<float> endFloor;
+ Math::Vector2<float> endCeil;
+ } random;
+ struct
+ {
+ Math::Vector2<float> start;
+ Math::Vector2<float> end;
+ } speed;
+ } speed;
+ };
+
+ struct SizeOverTimeDef
+ {
+ bool enable = false;
+ bool enableRandom = false;
+ union {
+ struct {
+ float startFloor = 1;
+ float startCeil = 1;
+ float endFloor = 1;
+ float endCeil = 1;
+ } random;
+ struct {
+ float start = 1;
+ float end = 1;
+ } size;
+ } size;
+ };
+
+ struct ColorOverTimeDef
+ {
+ bool enable = false;
+ Color colorStart = Color::WHITE;
+ Color colorEnd = Color::WHITE;
+ };
+
+ struct linearAccelarationDef
+ {
+ Math::Vector2<float> linearAccelaration;
+ };
+
+ struct RadialAccelarationDef
+ {
+ float radialAccelaration;
+ };
+
+ ///
+ ///
+ ///
+ struct ParticleDef
+ {
+ private:
+ friend class ParticleEmitter;
+
+ public:
+ // Basic definitions.
+ LifeTimeDef lifeTimeDef; ///<
+ linearAccelarationDef linearAccelarationDef; ///<
+ RadialAccelarationDef radialAccelarationDef; ///<
+ // Optional definitions.
+ SpeedOverTimeDef speedOverTimeDef; ///<
+ SizeOverTimeDef sizeOverTimeDef; ///<
+ ColorOverTimeDef colorOverTimeDef; ///<
+ };
+
+ ///
+ /// A single particle contains various properties of particle, such as position, accelaration, color and
+ /// other attributes changed over time.
+ ///
+ struct Particle
+ {
+ ///
+ /// Default constructor.
+ ///
+ Particle() {};
+
+ ///
+ /// Reset to default.
+ ///
+ void reset();
+
+ ///
+ /// Whole life time.
+ ///
+ float lifeTime = 0.0f;
+
+ ///
+ /// Current life time.
+ ///
+ float life = 0.0f;
+
+ ///
+ /// Current position.
+ ///
+ Math::Vector2<float> position;
+
+ ///
+ /// Emitte direction.
+ ///
+ float direction = 0;
+
+ Math::Vector2<float> speed;
+ Math::Vector2<float> linearAcceleration;
+ float radialAcceleration = 0;
+
+ ///
+ /// Size over lifetime.
+ ///
+ float size = 1;
+ float sizeBegin = 1;
+ float sizeEnd = 1;
+
+ float rotation = 0;
+ float angle = 0;
+
+ ///
+ /// Color over lifetime.
+ ///
+ Color color = Color::WHITE;
+ Color colorStart = Color::WHITE;
+ Color colorEnd = Color::WHITE;
+
+ ///
+ /// Is particle still alive? Alive is equivalent to NOT available in particle pool.
+ ///
+ bool alive = true;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle_emitter.cpp b/src/libjin/Graphics/particles/je_particle_emitter.cpp
new file mode 100644
index 0000000..edc4bee
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_emitter.cpp
@@ -0,0 +1,42 @@
+#include <time.h>
+
+#include "../../math/je_random.h"
+
+#include "je_particle_emitter.h"
+
+using namespace JinEngine::Math;
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ // Particle emitter
+ static RandomGenerator rnd(time(NULL));
+
+ ParticleEmitter::ParticleEmitter(ParticleSystem& ps)
+ : mParticleSystem(ps)
+ {
+ }
+
+ void ParticleEmitter::update(float dt)
+ {
+ mTime += dt;
+ if (mTime < 1)
+ return;
+
+
+ mTime -= 1;
+ }
+
+ void ParticleEmitter::emit()
+ {
+
+
+ }
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle_emitter.h b/src/libjin/Graphics/particles/je_particle_emitter.h
new file mode 100644
index 0000000..9200532
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_emitter.h
@@ -0,0 +1,112 @@
+#ifndef __JE_PARTICLE_EMITTER_H__
+#define __JE_PARTICLE_EMITTER_H__
+
+#include "../../common/je_temporary.h"
+#include "../../math/je_vector2.hpp"
+
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ struct PositionDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ Math::Vector2<float> min;
+ Math::Vector2<float> max;
+ } random;
+ Math::Vector2<float> position;
+ } position;
+ };
+
+ struct DirectionDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ float min = 0;
+ float max = 0;
+ } random;
+ float direction = 0;
+ } direction;
+ };
+
+ ///
+ /// How many particles emitted per second.
+ ///
+ struct EmitRateDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ float min = 1;
+ float max = 1;
+ } random;
+ float rate = 1;
+ } rate;
+ };
+
+ ///
+ /// Definition of particle emitter.
+ ///
+ struct ParticleEmitterDef : public Temporary
+ {
+ PositionDef positionDef; ///< Emit position(relativily to the particle system center).
+ DirectionDef directionDef; ///< Emit direction.
+ EmitRateDef emitRateDef; ///< Emit rate.
+ };
+
+ class ParticleSystem;
+
+ ///
+ /// Emit a single particle.
+ ///
+ class ParticleEmitter
+ {
+ public:
+ ///
+ ///
+ ///
+ ParticleEmitter(ParticleSystem& ps);
+
+ ///
+ ///
+ ///
+ void update(float dt);
+
+ private:
+ ///
+ ///
+ ///
+ ParticleSystem& mParticleSystem;
+
+ ///
+ /// Emit a particle according to emitter definition and particle definition, particle system should
+ /// assign particle value to the particle in particle pool, but not use this return particle.
+ ///
+ void emit();
+
+ ///
+ ///
+ ///
+ float mTime;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle_pool.h b/src/libjin/Graphics/particles/je_particle_pool.h
new file mode 100644
index 0000000..46cd73a
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_pool.h
@@ -0,0 +1,24 @@
+#ifndef __JE_PARTICLE_BATCH_H__
+#define __JE_PARTICLE_BATCH_H__
+
+#include "../../common/je_pool.hpp"
+
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ ///
+ /// Particle pool for reducing memory fragmentation.
+ ///
+ typedef Pool<Particle> ParticlePool;
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particle/je_particle_emitter.cpp b/src/libjin/Graphics/particles/je_particle_system.cpp
index d57d5ed..68f8f21 100644
--- a/src/libjin/Graphics/particle/je_particle_emitter.cpp
+++ b/src/libjin/Graphics/particles/je_particle_system.cpp
@@ -1,4 +1,4 @@
-#include "je_particle_emitter.h"
+#include "je_particle_system.h"
namespace JinEngine
{
diff --git a/src/libjin/Graphics/particles/je_particle_system.h b/src/libjin/Graphics/particles/je_particle_system.h
new file mode 100644
index 0000000..afa96b2
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_system.h
@@ -0,0 +1,103 @@
+#ifndef __JE_PARTICLE_EMMITTER_H__
+#define __JE_PARTICLE_EMMITTER_H__
+
+#include <vector>
+
+#include "../../common/je_temporary.h"
+#include "../../game/je_gameobject.h"
+
+#include "../je_sprite.h"
+
+#include "je_particle_emitter.h"
+#include "je_particle_pool.h"
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ ///
+ /// Definition of particle system.
+ ///
+ struct ParticleSystemDef : public Temporary
+ {
+ uint maxParticleCount = 1; ///< Max count of particles in pool. 1 by default.
+
+ ParticleEmitterDef emitterDef; ///< Particle emitter definition.
+ ParticleDef particleDef; ///< Particle definition.
+ };
+
+ ///
+ /// Particle emitter, handle all particles it emitts.
+ ///
+ class ParticleSystem : public Game::GameObject
+ {
+ public:
+ ///
+ /// Particle system constructor
+ ///
+ /// @param def Definition of particle system.
+ ///
+ ParticleSystem(const ParticleSystemDef& def);
+
+ ///
+ /// Particle system destructor.
+ ///
+ ~ParticleSystem();
+
+ ///
+ /// Update particle system and all alive particles.
+ ///
+ void update(float sec);
+
+ ///
+ /// Render particle system.
+ ///
+ void render(int x, int y, float sx = 1, float sy = 1, float r = 0, float ax = 0, float ay = 0);
+
+ ///
+ /// Set sprite to render.
+ ///
+ /// @param sprite Sprite to render.
+ ///
+ void setSprite(Sprite* sprite);
+
+ private:
+ // Disable default constructor.
+ ParticleSystem();
+
+ ///
+ /// Particle system definition.
+ ///
+ ParticleSystemDef mDef;
+
+ ///
+ /// Sprite to be drawn.
+ ///
+ Sprite* mSprite;
+
+ ///
+ /// Particle emitter.
+ ///
+ ParticleEmitter mEmitter;
+
+ ///
+ /// Particle pool.
+ ///
+ ParticlePool mParticlePool;
+
+ ///
+ /// Alive particles, that means these particles could join to the life cycle loop.
+ ///
+ std::vector<Particle*> mAliveParticles;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/Shader/je_default.shader.h b/src/libjin/Graphics/shaders/built-in/je_default.shader.h
index 3f57c44..3f57c44 100644
--- a/src/libjin/Graphics/Shader/je_default.shader.h
+++ b/src/libjin/Graphics/shaders/built-in/je_default.shader.h
diff --git a/src/libjin/Graphics/Shader/je_font.shader.h b/src/libjin/Graphics/shaders/built-in/je_font.shader.h
index e04c225..e04c225 100644
--- a/src/libjin/Graphics/Shader/je_font.shader.h
+++ b/src/libjin/Graphics/shaders/built-in/je_font.shader.h
diff --git a/src/libjin/Graphics/Shader/je_texture.shader.h b/src/libjin/Graphics/shaders/built-in/je_texture.shader.h
index d1fc86f..d1fc86f 100644
--- a/src/libjin/Graphics/Shader/je_texture.shader.h
+++ b/src/libjin/Graphics/shaders/built-in/je_texture.shader.h
diff --git a/src/libjin/Graphics/Shader/je_base.shader.h b/src/libjin/Graphics/shaders/je_base.shader.h
index d6f9d7b..88fa872 100644
--- a/src/libjin/Graphics/Shader/je_base.shader.h
+++ b/src/libjin/Graphics/shaders/je_base.shader.h
@@ -1,5 +1,5 @@
-#ifndef __JE_BASE_SHADER_H
-#define __JE_BASE_SHADER_H
+#ifndef __JE_BASE_SHADER_H__
+#define __JE_BASE_SHADER_H__
static const char* base_shared = R"(
#define Number float
@@ -26,8 +26,9 @@ static const char* base_vertex = R"(
#version 130 core
%s
-
+// Projection matrix
uniform mat4 jin_ProjectionMatrix;
+// Model view matrix
uniform mat4 jin_ModelMatrix;
in vec2 jin_VertexCoords;
@@ -85,4 +86,4 @@ static const char* SHADER_MAIN_TEXTURE = "jin_MainTexture";
static const char* SHADER_VERTEX_COORDS = "jin_VertexCoords";
static const char* SHADER_TEXTURE_COORDS = "jin_TextureCoords";
-#endif // __JE_BASE_SHADER_H \ No newline at end of file
+#endif // __JE_BASE_SHADER_H__
diff --git a/src/libjin/Graphics/shaders/je_jsl_compiler.cpp b/src/libjin/Graphics/shaders/je_jsl_compiler.cpp
new file mode 100644
index 0000000..feb88d4
--- /dev/null
+++ b/src/libjin/Graphics/shaders/je_jsl_compiler.cpp
@@ -0,0 +1,57 @@
+#include "../../core/je_configuration.h"
+#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
+
+#include "../../Filesystem/je_buffer.h"
+
+#include "je_jsl_compiler.h"
+
+using namespace std;
+using namespace JinEngine::Filesystem;
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Shaders
+ {
+
+ #include "je_base.shader.h"
+
+ bool JSLCompiler::compile(const string& jsl, string* vertex_shader, string* fragment_shader)
+ {
+ // parse shader source, need some optimizations
+ int loc_VERTEX_SHADER = jsl.find("#VERTEX_SHADER");
+ int loc_END_VERTEX_SHADER = jsl.find("#END_VERTEX_SHADER");
+ int loc_FRAGMENT_SHADER = jsl.find("#FRAGMENT_SHADER");
+ int loc_END_FRAGMENT_SHADER = jsl.find("#END_FRAGMENT_SHADER");
+ if (loc_VERTEX_SHADER == string::npos
+ || loc_END_VERTEX_SHADER == string::npos
+ || loc_FRAGMENT_SHADER == string::npos
+ || loc_END_FRAGMENT_SHADER == string::npos
+ )
+ return false;
+ // Load vertex and fragment shader source into buffers.
+ {
+ // Compile JSL vertex program.
+ int start = loc_VERTEX_SHADER + strlen("#VERTEX_SHADER");
+ *vertex_shader = jsl.substr(start, loc_END_VERTEX_SHADER - start);
+ Buffer vbuffer = Buffer(vertex_shader->length() + BASE_VERTEX_SHADER_SIZE);
+ formatVertexShader((char*)&vbuffer, vertex_shader->c_str());
+ vertex_shader->assign((char*)&vbuffer);
+ }
+ {
+ // Compile JSL fragment program.
+ int start = loc_FRAGMENT_SHADER + strlen("#FRAGMENT_SHADER");
+ *fragment_shader = jsl.substr(start, loc_END_FRAGMENT_SHADER - start);
+ Buffer fbuffer = Buffer(fragment_shader->length() + BASE_FRAGMENT_SHADER_SIZE);
+ formatFragmentShader((char*)&fbuffer, fragment_shader->c_str());
+ fragment_shader->assign((char*)&fbuffer);
+ }
+ return true;
+ }
+
+ } // namespace Shaders
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader) \ No newline at end of file
diff --git a/src/libjin/Graphics/shaders/je_jsl_compiler.h b/src/libjin/Graphics/shaders/je_jsl_compiler.h
new file mode 100644
index 0000000..df1e827
--- /dev/null
+++ b/src/libjin/Graphics/shaders/je_jsl_compiler.h
@@ -0,0 +1,45 @@
+#ifndef __JE_JSL_COMPILER_H__
+#define __JE_JSL_COMPILER_H__
+
+#include "../../core/je_configuration.h"
+#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
+
+#include <string>
+
+#include "../../common/je_singleton.hpp"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Shaders
+ {
+
+ ///
+ /// Compile JSL into GLSL.
+ ///
+ class JSLCompiler : public Singleton<JSLCompiler>
+ {
+ public:
+ ///
+ /// Compile JSL shader source into GLSL.
+ ///
+ /// @param jsl JSL shader source.
+ /// @param glsl_vertex Output of vertex glsl shader source.
+ /// @param glsl_fragment Output of fragment glsl shader source.
+ /// @return True if compile successful, otherwise return false.
+ ///
+ bool compile(const std::string& jsl, std::string* glsl_vertex, std::string* glsl_fragment);
+
+ private:
+ singleton(JSLCompiler);
+
+ };
+
+ } // namespace Shaders
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader)
+
+#endif // __JE_JSL_COMPILER_H__
diff --git a/src/libjin/Graphics/shaders/je_shader.cpp b/src/libjin/Graphics/shaders/je_shader.cpp
new file mode 100644
index 0000000..c1abbf0
--- /dev/null
+++ b/src/libjin/Graphics/shaders/je_shader.cpp
@@ -0,0 +1,281 @@
+#include "../../core/je_configuration.h"
+#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
+
+#include <iostream>
+
+#include "../../filesystem/je_buffer.h"
+#include "../../utils/je_macros.h"
+
+#include "je_jsl_compiler.h"
+#include "je_shader.h"
+
+using namespace std;
+using namespace JinEngine::Filesystem;
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Shaders
+ {
+
+ //
+ // default_texture
+ // base_shader
+ // SHADER_FORMAT_SIZE
+ // formatShader
+ //
+#include "built-in/je_default.shader.h"
+
+//
+// https://stackoverflow.com/questions/27941496/use-sampler-without-passing-through-value
+// The default value of a sampler variable is 0. From the GLSL 3.30 spec,
+// section "4.3.5 Uniforms":
+//
+// The link time initial value is either the value of the variable's
+// initializer, if present, or 0 if no initializer is present.Sampler
+// types cannot have initializers.
+//
+// Since a value of 0 means that it's sampling from texture unit 0, it will
+// work without ever setting the value as long as you bind your textures to
+// unit 0. This is well defined behavior.
+//
+// Since texture unit 0 is also the default until you call glActiveTexture()
+// with a value other than GL_TEXTURE0, it's very common to always use unit
+// 0 as long as shaders do not need more than one texture.Which means that
+// often times, setting the sampler uniforms is redundant for simple
+// applications.
+//
+// I would still prefer to always set the values.If nothing else, it makes
+// it clear to anybody reading your code that you really mean to sample from
+// texture unit 0, and did not just forget to set the value.
+//
+ const int DEFAULT_TEXTURE_UNIT = 0;
+
+ /*static*/ Shader* Shader::CurrentShader = nullptr;
+
+ Shader* Shader::createShader(const string& program)
+ {
+ Shader* shader = nullptr;
+ try
+ {
+ shader = new Shader(program);
+ }
+ catch (...)
+ {
+ return nullptr;
+ }
+ return shader;
+ }
+
+ Shader::Shader(const string& program)
+ : mCurrentTextureUnit(DEFAULT_TEXTURE_UNIT)
+ {
+ if (!compile(program))
+ throw 0;
+ }
+
+ Shader::~Shader()
+ {
+ if (CurrentShader == this)
+ unuse();
+ // delete shader program
+ glDeleteShader(mPID);
+ }
+
+ bool Shader::compile(const string& program)
+ {
+ string vertex_shader, fragment_shader;
+ // Compile JSL shader source into GLSL shader source.
+ JSLCompiler* compiler = JSLCompiler::get();
+ if (!compiler->compile(program, &vertex_shader, &fragment_shader))
+ {
+ return false;
+ }
+#define glsl(SHADER_MODE, SHADER, SRC) \
+do{ \
+const GLchar* src = SRC.c_str(); \
+glShaderSource(SHADER, 1, &src, NULL); \
+glCompileShader(SHADER); \
+GLint success; \
+glGetShaderiv(SHADER, GL_COMPILE_STATUS, &success); \
+if (success == GL_FALSE) \
+ return false; \
+}while(0)
+ // Compile vertex shader.
+ GLuint vid = glCreateShader(GL_VERTEX_SHADER);
+ glsl(GL_VERTEX_SHADER, vid, vertex_shader);
+ // Compile fragment shader.
+ GLuint fid = glCreateShader(GL_FRAGMENT_SHADER);
+ glsl(GL_FRAGMENT_SHADER, fid, fragment_shader);
+#undef glsl
+ // Create OpenGL shader program.
+ mPID = glCreateProgram();
+ glAttachShader(mPID, vid);
+ glAttachShader(mPID, fid);
+ glLinkProgram(mPID);
+ GLint success;
+ glGetProgramiv(mPID, GL_LINK_STATUS, &success);
+ if (success == GL_FALSE)
+ return false;
+ }
+
+ static inline GLint getMaxTextureUnits()
+ {
+ GLint maxTextureUnits = 0;
+ glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
+ return maxTextureUnits;
+ }
+
+ void Shader::use()
+ {
+ glUseProgram(mPID);
+ CurrentShader = this;
+ sendInt(SHADER_MAIN_TEXTURE, DEFAULT_TEXTURE_UNIT);
+ }
+
+ /*static*/ void Shader::unuse()
+ {
+ glUseProgram(0);
+ CurrentShader = nullptr;
+ }
+
+ GLint Shader::claimTextureUnit(const std::string& name)
+ {
+ std::map<std::string, GLint>::iterator unit = mTextureUnits.find(name);
+ if (unit != mTextureUnits.end())
+ return unit->second;
+ static GLint MAX_TEXTURE_UNITS = getMaxTextureUnits();
+ if (++mCurrentTextureUnit >= MAX_TEXTURE_UNITS)
+ return 0;
+ mTextureUnits[name] = mCurrentTextureUnit;
+ return mCurrentTextureUnit;
+ }
+
+#define checkJSL() \
+ if (CurrentShader != this) \
+ return
+
+ void Shader::sendInt(const char* name, int value)
+ {
+ checkJSL();
+ int loc = glGetUniformLocation(mPID, name);
+ glUniform1i(loc, value);
+ }
+
+ void Shader::sendFloat(const char* variable, float number)
+ {
+ checkJSL();
+ int loc = glGetUniformLocation(mPID, variable);
+ glUniform1f(loc, number);
+ }
+
+ //
+ // https://www.douban.com/note/627332677/
+ // struct TextureUnit
+ // {
+ // GLuint targetTexture1D;
+ // GLuint targetTexture2D;
+ // GLuint targetTexture3D;
+ // GLuint targetTextureCube;
+ // ...
+ // };
+ //
+ // TextureUnit mTextureUnits[GL_MAX_TEXTURE_IMAGE_UNITS]
+ // GLuint mCurrentTextureUnit = 0;
+ //
+ void Shader::sendTexture(const char* variable, const Texture* tex)
+ {
+ checkJSL();
+ GLint location = glGetUniformLocation(mPID, variable);
+ if (location == -1)
+ return;
+ GLint unit = claimTextureUnit(variable);
+ if (unit == 0)
+ {
+ // TODO: 쳣󶨵
+ return;
+ }
+ gl.activeTexUnit(unit);
+ glUniform1i(location, unit);
+ gl.bindTexture(tex->getGLTexture());
+ gl.activeTexUnit(0);
+ }
+
+ void Shader::sendCanvas(const char* variable, const Canvas* canvas)
+ {
+ checkJSL();
+ GLint location = glGetUniformLocation(mPID, variable);
+ if (location == -1)
+ return;
+ GLint unit = claimTextureUnit(variable);
+ if (unit == 0)
+ {
+ // TODO: 쳣󶨵
+ return;
+ }
+ glUniform1i(location, unit);
+ glActiveTexture(GL_TEXTURE0 + unit);
+ gl.bindTexture(canvas->getGLTexture());
+
+ glActiveTexture(GL_TEXTURE0);
+ }
+
+ void Shader::sendVec2(const char* name, float x, float y)
+ {
+ checkJSL();
+ int loc = glGetUniformLocation(mPID, name);
+ glUniform2f(loc, x, y);
+ }
+
+ void Shader::sendVec3(const char* name, float x, float y, float z)
+ {
+ checkJSL();
+ int loc = glGetUniformLocation(mPID, name);
+ glUniform3f(loc, x, y, z);
+ }
+
+ void Shader::sendVec4(const char* name, float x, float y, float z, float w)
+ {
+ checkJSL();
+ int loc = glGetUniformLocation(mPID, name);
+ glUniform4f(loc, x, y, z, w);
+ }
+
+ void Shader::sendColor(const char* name, const Color* col)
+ {
+ checkJSL();
+ int loc = glGetUniformLocation(mPID, name);
+ glUniform4f(loc,
+ col->r / 255.f,
+ col->g / 255.f,
+ col->b / 255.f,
+ col->a / 255.f
+ );
+ }
+
+ void Shader::sendMatrix4(const char* name, const Math::Matrix* mat4)
+ {
+ int loc = glGetUniformLocation(mPID, name);
+ glUniformMatrix4fv(loc, 1, GL_FALSE, mat4->getElements());
+ }
+
+ void Shader::setVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers)
+ {
+ GLint loc = glGetAttribLocation(mPID, SHADER_VERTEX_COORDS);
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers);
+ }
+
+ void Shader::setUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers)
+ {
+ GLint loc = glGetAttribLocation(mPID, SHADER_TEXTURE_COORDS);
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(loc, n, type, GL_FALSE, stride, pointers);
+ }
+
+ } // namespace Shaders
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader) \ No newline at end of file
diff --git a/src/libjin/Graphics/shaders/je_shader.h b/src/libjin/Graphics/shaders/je_shader.h
new file mode 100644
index 0000000..2009e79
--- /dev/null
+++ b/src/libjin/Graphics/shaders/je_shader.h
@@ -0,0 +1,201 @@
+#ifndef __JE_SHADER_H__
+#define __JE_SHADER_H__
+
+#include "../../core/je_configuration.h"
+#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
+
+#include <string>
+#include <map>
+
+#include "GLee/GLee.h"
+
+#include "../je_color.h"
+#include "../je_texture.h"
+#include "../je_canvas.h"
+
+#include "je_base.shader.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Shaders
+ {
+
+ ///
+ /// Built in shader program.
+ ///
+ /// Built in shader program written with custom shading language called JSL (jin shading language). A
+ /// JSL program is compiled into glsl, so most glsl built in functions and structs are available in
+ /// JSL.
+ ///
+ class Shader
+ {
+ public:
+ ///
+ /// Create shader program from source code.
+ ///
+ /// @param source The shader source code.
+ ///
+ static Shader* createShader(const std::string& source);
+
+ ///
+ /// Get current shader.
+ ///
+ /// @return Current used shader program.
+ ///
+ static inline Shader* getCurrentShader() { return CurrentShader; }
+
+ ///
+ /// Unuse current shader.
+ ///
+ static void unuse();
+
+ ///
+ /// Destructor of shader.
+ ///
+ virtual ~Shader();
+
+ ///
+ /// Use specific shader.
+ ///
+ void use();
+
+ ///
+ /// Send float value to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param number Value of uniform variable to be sent.
+ ///
+ void sendFloat(const char* name, float number);
+
+ ///
+ /// Send texture to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param texture Texture to be sent.
+ ///
+ void sendTexture(const char* name, const Texture* texture);
+
+ ///
+ /// Send integer value to shader
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param value Value to be sent.
+ ///
+ void sendInt(const char* name, int value);
+
+ ///
+ /// Send 2D vector to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param x X value of the vector to be sent.
+ /// @param y Y value of the vector to be sent.
+ ///
+ void sendVec2(const char* name, float x, float y);
+
+ ///
+ /// Send 3D vector to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param x X value of the vector to be sent.
+ /// @param y Y value of the vector to be sent.
+ /// @param z Z value of the vector to be sent.
+ ///
+ void sendVec3(const char* name, float x, float y, float z);
+
+ ///
+ /// Send 4D vector to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param x X value of the vector to be sent.
+ /// @param y Y value of the vector to be sent.
+ /// @param z Z value of the vector to be sent.
+ /// @param w W value of the vector to be sent.
+ ///
+ void sendVec4(const char* name, float x, float y, float z, float w);
+
+ ///
+ /// Send canvas to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param canvas Canvas to be sent.
+ ///
+ void sendCanvas(const char* name, const Canvas* canvas);
+
+ ///
+ /// Send color to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param color Color to be sent.
+ ///
+ void sendColor(const char* name, const Color* color);
+
+ ///
+ /// Send 4 by 4 matrix to shader.
+ ///
+ /// @param name Name of the uniform variable to be assigned.
+ /// @param mat4 Matrix to be sent.
+ ///
+ void sendMatrix4(const char* name, const Math::Matrix* mat4);
+
+ ///
+ /// Set vertices value.
+ ///
+ /// @param n Number of vertices.
+ /// @param type Data type of each component in the array.
+ /// @param stride Byte offset between consecutive generic vertex attributes.
+ /// @param pointers Pointer to the first component of the first generic vertex attribute in the array.
+ ///
+ void setVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers);
+
+ ///
+ /// Set texture UV coordinates.
+ ///
+ /// @param n Number of vertices.
+ /// @param type Data type of each component in the array.
+ /// @param stride Byte offset between consecutive generic vertex attributes.
+ /// @param pointers Pointer to the first component of the first generic vertex attribute in the array.
+ ///
+ void setUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers);
+
+ protected:
+ ///
+ /// Reference of current used shader.
+ ///
+ static Shader* CurrentShader;
+
+ ///
+ /// Get texture unit of the uniform texture. If not, assign one.
+ ///
+ /// @param name Name of the texture uniform variable.
+ /// @return Texture unit which texture variable be assigned.
+ ///
+ GLint claimTextureUnit(const std::string& name);
+
+ ///
+ /// Shader constructor.
+ ///
+ Shader(const std::string& program);
+
+ ///
+ /// Compile JSL program into GLSL source.
+ ///
+ /// @param program JSL source code.
+ /// @return Return true if compile successed, otherwise return false.
+ ///
+ bool compile(const std::string& program);
+
+ GLuint mPID;
+ GLint mCurrentTextureUnit;
+ std::map<std::string, GLint> mTextureUnits;
+
+ };
+
+ } // namespace Shaders
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader)
+
+#endif // __JE_SHADER_H__ \ No newline at end of file