Cache text states

This commit is contained in:
rdb 2015-11-11 17:13:30 +01:00
parent a573a11d90
commit eecb1fb3f2
4 changed files with 85 additions and 21 deletions

View File

@ -587,33 +587,16 @@ assemble_text() {
if (placement->_properties != properties) { if (placement->_properties != properties) {
// Get a new set of properties for future glyphs. // Get a new set of properties for future glyphs.
properties = placement->_properties; properties = placement->_properties;
text_state = RenderState::make_empty(); text_state = properties->get_text_state();
shadow_state = RenderState::make_empty();
shadow_xform = LMatrix4::ident_mat(); shadow_xform = LMatrix4::ident_mat();
if (properties->has_text_color()) {
text_state = text_state->add_attrib(ColorAttrib::make_flat(properties->get_text_color()));
if (properties->get_text_color()[3] != 1.0) {
text_state = text_state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha));
}
}
if (properties->has_bin()) {
text_state = text_state->add_attrib(CullBinAttrib::make(properties->get_bin(), properties->get_draw_order() + 2));
}
if (properties->has_shadow()) { if (properties->has_shadow()) {
shadow_state = shadow_state->add_attrib(ColorAttrib::make_flat(properties->get_shadow_color())); shadow_state = properties->get_shadow_state();
if (properties->get_shadow_color()[3] != 1.0) {
shadow_state = shadow_state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha));
}
if (properties->has_bin()) {
shadow_state = shadow_state->add_attrib(CullBinAttrib::make(properties->get_bin(), properties->get_draw_order() + 1));
}
LVector2 offset = properties->get_shadow(); LVector2 offset = properties->get_shadow();
shadow_xform = LMatrix4::translate_mat(offset[0], 0.0f, -offset[1]); shadow_xform = LMatrix4::translate_mat(offset[0], 0.0f, -offset[1]);
} else {
shadow_state.clear();
} }
} }

View File

@ -550,6 +550,7 @@ INLINE void TextProperties::
set_text_color(const LColor &text_color) { set_text_color(const LColor &text_color) {
_text_color = text_color; _text_color = text_color;
_specified |= F_has_text_color; _specified |= F_has_text_color;
_text_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -562,6 +563,7 @@ INLINE void TextProperties::
clear_text_color() { clear_text_color() {
_text_color.set(1.0f, 1.0f, 1.0f, 1.0f); _text_color.set(1.0f, 1.0f, 1.0f, 1.0f);
_specified &= ~F_has_text_color; _specified &= ~F_has_text_color;
_text_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -603,6 +605,7 @@ INLINE void TextProperties::
set_shadow_color(const LColor &shadow_color) { set_shadow_color(const LColor &shadow_color) {
_shadow_color = shadow_color; _shadow_color = shadow_color;
_specified |= F_has_shadow_color; _specified |= F_has_shadow_color;
_shadow_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -614,6 +617,7 @@ INLINE void TextProperties::
clear_shadow_color() { clear_shadow_color() {
_shadow_color.set(0.0f, 0.0f, 0.0f, 1.0f); _shadow_color.set(0.0f, 0.0f, 0.0f, 1.0f);
_specified &= ~F_has_shadow_color; _specified &= ~F_has_shadow_color;
_shadow_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -711,6 +715,8 @@ INLINE void TextProperties::
set_bin(const string &bin) { set_bin(const string &bin) {
_bin = bin; _bin = bin;
_specified |= F_has_bin; _specified |= F_has_bin;
_text_state.clear();
_shadow_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -725,6 +731,8 @@ INLINE void TextProperties::
clear_bin() { clear_bin() {
_bin = string(); _bin = string();
_specified &= ~F_has_bin; _specified &= ~F_has_bin;
_text_state.clear();
_shadow_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -768,6 +776,8 @@ INLINE int TextProperties::
set_draw_order(int draw_order) { set_draw_order(int draw_order) {
_draw_order = draw_order; _draw_order = draw_order;
_specified |= F_has_draw_order; _specified |= F_has_draw_order;
_text_state.clear();
_shadow_state.clear();
return _draw_order + 3; return _draw_order + 3;
} }
@ -780,6 +790,8 @@ INLINE void TextProperties::
clear_draw_order() { clear_draw_order() {
_draw_order = 1; _draw_order = 1;
_specified &= ~F_has_draw_order; _specified &= ~F_has_draw_order;
_text_state.clear();
_shadow_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////

View File

@ -61,6 +61,8 @@ TextProperties() {
TextProperties:: TextProperties::
TextProperties(const TextProperties &copy) { TextProperties(const TextProperties &copy) {
(*this) = copy; (*this) = copy;
_text_state = copy._text_state;
_shadow_state = copy._shadow_state;
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -91,6 +93,9 @@ operator = (const TextProperties &copy) {
_glyph_scale = copy._glyph_scale; _glyph_scale = copy._glyph_scale;
_glyph_shift = copy._glyph_shift; _glyph_shift = copy._glyph_shift;
_text_scale = copy._text_scale; _text_scale = copy._text_scale;
_text_state.clear();
_shadow_state.clear();
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -372,6 +377,62 @@ write(ostream &out, int indent_level) const {
} }
} }
////////////////////////////////////////////////////////////////////
// Function: TextProperties::get_text_state
// Access: Public
// Description: Returns a RenderState object suitable for rendering
// text with these properties.
////////////////////////////////////////////////////////////////////
const RenderState *TextProperties::
get_text_state() const {
if (!_text_state.is_null()) {
return _text_state;
}
CPT(RenderState) state = RenderState::make_empty();
if (has_text_color()) {
state = state->add_attrib(ColorAttrib::make_flat(get_text_color()));
if (get_text_color()[3] != 1.0) {
state = state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha));
}
}
if (has_bin()) {
state = state->add_attrib(CullBinAttrib::make(get_bin(), get_draw_order() + 2));
}
swap(_text_state, state);
return _text_state;
}
////////////////////////////////////////////////////////////////////
// Function: TextProperties::get_shadow_state
// Access: Public
// Description: Returns a RenderState object suitable for rendering
// the shadow of this text with these properties.
////////////////////////////////////////////////////////////////////
const RenderState *TextProperties::
get_shadow_state() const {
if (!_shadow_state.is_null()) {
return _shadow_state;
}
CPT(RenderState) state = RenderState::make_empty();
state = state->add_attrib(ColorAttrib::make_flat(get_shadow_color()));
if (get_shadow_color()[3] != 1.0) {
state = state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha));
}
if (has_bin()) {
state = state->add_attrib(CullBinAttrib::make(get_bin(), get_draw_order() + 1));
}
swap(_shadow_state, state);
return _shadow_state;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: TextProperties::load_default_font // Function: TextProperties::load_default_font
// Access: Private, Static // Access: Private, Static

View File

@ -21,6 +21,7 @@
#include "luse.h" #include "luse.h"
#include "textFont.h" #include "textFont.h"
#include "pointerTo.h" #include "pointerTo.h"
#include "renderState.h"
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Class : TextProperties // Class : TextProperties
@ -169,6 +170,10 @@ PUBLISHED:
void write(ostream &out, int indent_level = 0) const; void write(ostream &out, int indent_level = 0) const;
public:
const RenderState *get_text_state() const;
const RenderState *get_shadow_state() const;
private: private:
static void load_default_font(); static void load_default_font();
@ -216,6 +221,9 @@ private:
PN_stdfloat _glyph_shift; PN_stdfloat _glyph_shift;
PN_stdfloat _text_scale; PN_stdfloat _text_scale;
mutable CPT(RenderState) _text_state;
mutable CPT(RenderState) _shadow_state;
static PT(TextFont) _default_font; static PT(TextFont) _default_font;
static bool _loaded_default_font; static bool _loaded_default_font;