/* client/infowidget.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include #include #include #include #include "auxiliary/functions.h" #include "client/infowidget.h" #include "client/client.h" #include "client/input.h" #include "client/targets.h" #include "render/render.h" #include "core/stats.h" #include "math/mathlib.h" #include "sys/sys.h" #include "ui/paint.h" #include "ui/ui.h" #include "ui/widget.h" namespace client { // helper function void time_to_stream(std::ostringstream &str, float time) { int minutes = (int) floorf(time / 60.0f); int seconds = (int) floorf(time - (float) minutes * 60.0f); str << std::setfill(' ') << std::setw(4) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; } /* -- DevInfoWidget ------------------------------------------------ */ DevInfoWidget::DevInfoWidget(ui::Widget *parent) : ui::Widget(parent) { set_label("devinfo"); set_border(false); set_background(false); } void DevInfoWidget::draw() { std::ostringstream textstream; const core::Entity *target = targets::current(); textstream << "^Nuptime ^B"; time_to_stream(textstream, core::application()->time()); textstream << '\n'; if (core::game()) { textstream << "^Ntime ^B"; time_to_stream(textstream, core::game()->time()); } textstream << '\n'; if (core::localcontrol()) { textstream << '\n'; textstream << "^B" << core::localcontrol()->name() << '\n'; textstream << "^Bentity id ^N" << core::localcontrol()->id() << '\n'; textstream << "^Nthrust ^B" << std::fixed << std::setprecision(2) << core::localcontrol()->thrust() << '\n'; textstream << "^Nspeed ^B" << std::fixed << std::setprecision(2) << core::localcontrol()->speed() << '\n'; textstream << "^Nx,y,z ^B" << std::fixed << std::setprecision(2) << core::localcontrol()->location().x() << ", " << core::localcontrol()->location().y() << ", " << core::localcontrol()->location().z() << '\n'; textstream << "^Nradius ^B" << core::localcontrol()->radius() << '\n'; if (target) { textstream << '\n'; textstream << "^B" << target->name() << '\n'; textstream << "^Bentity id ^N" << target->id() << '\n'; textstream << "^Nx,y,z ^B" << std::fixed << std::setprecision(2) << target->location().x() << ", " << target->location().y() << ", " << target->location().z() << '\n'; textstream << "^Nradius ^B" << target->radius() << '\n'; } } ui::Paint::set_color(palette()->foreground()); ui::Paint::draw_text(global_location(), font(), textstream.str()); } /* -- StatsInfoWidget ---------------------------------------------- */ StatsInfoWidget::StatsInfoWidget(ui::Widget *parent) : ui::Widget(parent) { set_label("stats"); set_border(false); set_background(false); // clear counters for (size_t i = 0; i < fps_counter_size; i++) fps_counter_time[i] = 0.0f; for (size_t i = 0; i < net_counter_size; i++) net_counter_traffic[i] = 0; net_counter_index = 0; fps_counter_index = 0; } void StatsInfoWidget::draw() { // average fps unsigned long fps = 0.0f; const unsigned long now = core::application()->timestamp(); fps_counter_time[fps_counter_index] = now; fps_counter_index = (fps_counter_index + 1) % fps_counter_size; unsigned long min_time = now; for (size_t i = 0; i < fps_counter_size; i++) if (fps_counter_time[i] < min_time) min_time = fps_counter_time[i]; const unsigned long t = now - min_time; if (t > 0) { fps = (fps_counter_size - 1) * 1000 / t; } std::ostringstream textstream; if (core::game()) { textstream << "^Ntime ^B"; time_to_stream(textstream, core::game()->time()); } textstream << std::setfill(' ') << "\n"; textstream << "^Nfps ^B" << std::setw(6) << fps << "\n"; if (core::application()->connected()) { textstream << "^Nfrags ^B" << std::setw(5) << render::Stats::fragments << "\n"; textstream << "^Ntris ^B" << std::setw(5) << render::Stats::tris << "\n"; textstream << "^Nquads ^B" << std::setw(5) << render::Stats::quads << "\n"; if (core::Stats::network_bytes_sent + core::Stats::network_bytes_received) { net_counter_traffic[net_counter_index] = core::Stats::network_bytes_sent + core::Stats::network_bytes_received; net_counter_time[net_counter_index] = core::application()->timestamp(); size_t index_max = net_counter_index; net_counter_index = (net_counter_index + 1) % net_counter_size; size_t index_min = net_counter_index; float d = float (net_counter_time[index_max] - net_counter_time[index_min]) / 1000.0f; if (d > 0) { float traffic = net_counter_traffic[index_max] - net_counter_traffic[index_min]; textstream << "^Nnet ^B" << std::setw(6) << roundf((float) traffic / d) << "\n"; } } } ui::Paint::set_color(palette()->foreground()); ui::Paint::draw_text(global_location(), font(), textstream.str()); } /* -- KeyInfoWidget ------------------------------------------------ */ KeyInfoWidget::KeyInfoWidget(ui::Widget *parent) : Widget(parent) { set_label("keypress"); set_border(false); set_background(false); } void KeyInfoWidget::draw() { Key::Modifier mod = input::modifier(); std::string label; if (mod != Key::None) { if (mod == Key::Shift) label.assign("shift+"); else if (mod == Key::Ctrl) label.assign("ctrl+"); else if (mod == Key::Alt) label.assign("alt+"); } if (input::last_key_pressed()) { label.append(input::last_key_pressed()->name()); } if (label.size()) { ui::Paint::set_color(palette()->highlight()); ui::Paint::draw_label(global_location(), size(), font(), label , ui::AlignCenter); } } /* -- ClockInfoWidget ---------------------------------------------- */ ClockInfoWidget::ClockInfoWidget(ui::Widget *parent) : ui::Widget(parent) { set_label("clock"); set_border(false); set_background(false); clock_mode = ClockOff; } void ClockInfoWidget::draw() { if (mode() == ClockOff) return; int hours, minutes, seconds; sys::get_localtime(hours, minutes, seconds); std::ostringstream clockstr; clockstr << std::setfill(' ') << std::setw(2); if (mode() == Clock12Hours) { std::string suffix("am"); if (hours >= 12) { suffix[0] = 'p'; hours -= 12; } if (hours == 0) { hours += 12; } clockstr << hours << ":" << std::setfill('0') << std::setw(2) << minutes << " " << suffix; } else { clockstr << " " << hours << ":" << std::setfill('0') << std::setw(2) << minutes; } ui::Paint::set_color(palette()->foreground()); ui::Paint::draw_text(global_location(), font(), clockstr.str()); } }