Finally fix the aspect ratio on the blasted FPS meter

This commit is contained in:
rdb 2015-01-07 22:34:43 +01:00
parent af15797a25
commit 6ce891f748
2 changed files with 40 additions and 16 deletions

View File

@ -33,7 +33,10 @@ TypeHandle FrameRateMeter::_type_handle;
// Description:
////////////////////////////////////////////////////////////////////
FrameRateMeter::
FrameRateMeter(const string &name) : TextNode(name) {
FrameRateMeter(const string &name) :
TextNode(name),
_last_aspect_ratio(-1) {
set_cull_callback();
Thread *current_thread = Thread::get_current_thread();
@ -59,7 +62,7 @@ FrameRateMeter(const string &name) : TextNode(name) {
set_align(A_right);
set_transform(LMatrix4::scale_mat(frame_rate_meter_scale) *
LMatrix4::translate_mat(LVector3::rfu(1.0f - frame_rate_meter_side_margins * frame_rate_meter_scale, 0.0f, 1.0f - frame_rate_meter_scale * height)));
LMatrix4::translate_mat(LVector3::rfu(-frame_rate_meter_side_margins * frame_rate_meter_scale, 0.0f, -frame_rate_meter_scale * height)));
set_card_color(0.0f, 0.0f, 0.0f, 0.4);
set_card_as_margin(frame_rate_meter_side_margins, frame_rate_meter_side_margins, 0.1f, 0.0f);
// set_usage_hint(Geom::UH_client);
@ -110,10 +113,11 @@ setup_window(GraphicsOutput *window) {
PT(Lens) lens = new OrthographicLens;
static const PN_stdfloat left = -1.0f;
static const PN_stdfloat right = 1.0f;
static const PN_stdfloat bottom = -1.0f;
static const PN_stdfloat top = 1.0f;
// We choose these values such that we can place the text against (0, 0).
static const PN_stdfloat left = -2.0f;
static const PN_stdfloat right = 0.0f;
static const PN_stdfloat bottom = -2.0f;
static const PN_stdfloat top = 0.0f;
lens->set_film_size(right - left, top - bottom);
lens->set_film_offset((right + left) * 0.5, (top + bottom) * 0.5);
lens->set_near_far(-1000, 1000);
@ -171,6 +175,23 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
// Statistics
PStatTimer timer(_show_fps_pcollector, current_thread);
// This is probably a good time to check if the aspect ratio on
// the window has changed.
int width = _display_region->get_pixel_width();
int height = _display_region->get_pixel_height();
PN_stdfloat aspect_ratio = 1;
if (width != 0 && height != 0) {
aspect_ratio = (PN_stdfloat)height / (PN_stdfloat)width;
}
// Scale the transform by the calculated aspect ratio.
if (aspect_ratio != _last_aspect_ratio) {
_aspect_ratio_transform = TransformState::make_scale(LVecBase3(aspect_ratio, 1, 1));
_last_aspect_ratio = aspect_ratio;
cerr << aspect_ratio << "\n";
}
data._net_transform = data._net_transform->compose(_aspect_ratio_transform);
// Check to see if it's time to update.
double now = _clock_object->get_frame_time(current_thread);
double elapsed = now - _last_update;

View File

@ -77,6 +77,9 @@ private:
string _text_pattern;
ClockObject *_clock_object;
PN_stdfloat _last_aspect_ratio;
CPT(TransformState) _aspect_ratio_transform;
static PStatCollector _show_fps_pcollector;
public: